DEADSOFTWARE

files: move wad reader to separete implementation
[flatwaifu.git] / src / my.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
19 #include "glob.h"
20 #include "error.h"
21 #include "common/endianness.h"
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <assert.h>
27 void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext) {
28 const char* end; /* end of processed string */
29 const char* p; /* search pointer */
30 const char* s; /* copy pointer */
32 /* extract drive name */
33 if (path[0] && path[1]==':') {
34 if (drv) {
35 *drv++ = *path++;
36 *drv++ = *path++;
37 *drv = '\0';
38 }
39 } else if (drv)
40 *drv = '\0';
42 /* search for end of string or stream separator */
43 for(end=path; *end && *end!=':'; )
44 end++;
46 /* search for begin of file extension */
47 for(p=end; p>path && *--p!='\\' && *p!='/'; )
48 if (*p == '.') {
49 end = p;
50 break;
51 }
53 if (ext)
54 for(s=end; (*ext=*s++); )
55 ext++;
57 /* search for end of directory name */
58 for(p=end; p>path; )
59 if (*--p=='\\' || *p=='/') {
60 p++;
61 break;
62 }
64 if (name) {
65 for(s=p; s<end; )
66 *name++ = *s++;
68 *name = '\0';
69 }
71 if (dir) {
72 for(s=path; s<p; )
73 *dir++ = *s++;
75 *dir = '\0';
76 }
77 }
79 size_t myfreadc (void *ptr, size_t size, size_t n, FILE *f) {
80 return fread(ptr, size, n, f);
81 }
83 void myfread (void *ptr, size_t size, size_t n, FILE *f) {
84 #if 1
85 myfreadc(ptr, size, n, f);
86 #else
87 if (myfreadc(ptr, size, n, f) != n) {
88 ERR_fatal("File reading error (readed %u, required %u)\n", m, n);
89 }
90 #endif
91 }
93 int8_t myfread8 (FILE *f) {
94 int8_t x;
95 myfread(&x, 1, 1, f);
96 return x;
97 }
99 int16_t myfread16 (FILE *f) {
100 int16_t x;
101 myfread(&x, 2, 1, f);
102 return short2host(x);
105 int32_t myfread32 (FILE *f) {
106 int32_t x;
107 myfread(&x, 4, 1, f);
108 return int2host(x);
111 void myfwrite (void *ptr, size_t size, size_t n, FILE *f) {
112 assert(fwrite(ptr, size, n, f) == n);
115 void myfwrite8 (int8_t x, FILE *f) {
116 myfwrite(&x, 1, 1, f);
119 void myfwrite16 (int16_t x, FILE *f) {
120 x = short2host(x);
121 myfwrite(&x, 2, 1, f);
124 void myfwrite32 (int32_t x, FILE *f) {
125 x = int2host(x);
126 myfwrite(&x, 4, 1, f);
129 int fexists (char *filename) {
130 FILE *f;
131 if ((f = fopen(filename, "r")))
133 fclose(f);
134 return 1;
136 return 0;