DEADSOFTWARE

b3eb964f476a95f2cfba3e0e71b1995812468999
[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 "misc.h"
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <assert.h>
26 void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext) {
27 const char* end; /* end of processed string */
28 const char* p; /* search pointer */
29 const char* s; /* copy pointer */
31 /* extract drive name */
32 if (path[0] && path[1]==':') {
33 if (drv) {
34 *drv++ = *path++;
35 *drv++ = *path++;
36 *drv = '\0';
37 }
38 } else if (drv)
39 *drv = '\0';
41 /* search for end of string or stream separator */
42 for(end=path; *end && *end!=':'; )
43 end++;
45 /* search for begin of file extension */
46 for(p=end; p>path && *--p!='\\' && *p!='/'; )
47 if (*p == '.') {
48 end = p;
49 break;
50 }
52 if (ext)
53 for(s=end; (*ext=*s++); )
54 ext++;
56 /* search for end of directory name */
57 for(p=end; p>path; )
58 if (*--p=='\\' || *p=='/') {
59 p++;
60 break;
61 }
63 if (name) {
64 for(s=p; s<end; )
65 *name++ = *s++;
67 *name = '\0';
68 }
70 if (dir) {
71 for(s=path; s<p; )
72 *dir++ = *s++;
74 *dir = '\0';
75 }
76 }
78 size_t myfreadc (void *ptr, size_t size, size_t n, FILE *f) {
79 return fread(ptr, size, n, f);
80 }
82 void myfread (void *ptr, size_t size, size_t n, FILE *f) {
83 #if 1
84 myfreadc(ptr, size, n, f);
85 #else
86 if (myfreadc(ptr, size, n, f) != n) {
87 ERR_fatal("File reading error (readed %u, required %u)\n", m, n);
88 }
89 #endif
90 }
92 int8_t myfread8 (FILE *f) {
93 int8_t x;
94 myfread(&x, 1, 1, f);
95 return x;
96 }
98 int16_t myfread16 (FILE *f) {
99 int16_t x;
100 myfread(&x, 2, 1, f);
101 return short2host(x);
104 int32_t myfread32 (FILE *f) {
105 int32_t x;
106 myfread(&x, 4, 1, f);
107 return int2host(x);
110 void myfwrite (void *ptr, size_t size, size_t n, FILE *f) {
111 assert(fwrite(ptr, size, n, f) == n);
114 void myfwrite8 (int8_t x, FILE *f) {
115 myfwrite(&x, 1, 1, f);
118 void myfwrite16 (int16_t x, FILE *f) {
119 x = short2host(x);
120 myfwrite(&x, 2, 1, f);
123 void myfwrite32 (int32_t x, FILE *f) {
124 x = int2host(x);
125 myfwrite(&x, 4, 1, f);
128 int fexists (char *filename) {
129 FILE *f;
130 if ((f = fopen(filename, "r")))
132 fclose(f);
133 return 1;
135 return 0;