DEADSOFTWARE

update copyrights
[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 (myfreadc(ptr, size, n, f) != n) {
84 ERR_fatal("File reading error\n");
85 }
86 }
88 int8_t myfread8 (FILE *f) {
89 int8_t x;
90 myfread(&x, 1, 1, f);
91 return x;
92 }
94 int16_t myfread16 (FILE *f) {
95 int16_t x;
96 myfread(&x, 2, 1, f);
97 return short2host(x);
98 }
100 int32_t myfread32 (FILE *f) {
101 int32_t x;
102 myfread(&x, 4, 1, f);
103 return int2host(x);
106 void myfwrite (void *ptr, size_t size, size_t n, FILE *f) {
107 assert(fwrite(ptr, size, n, f) == n);
110 void myfwrite8 (int8_t x, FILE *f) {
111 myfwrite(&x, 1, 1, f);
114 void myfwrite16 (int16_t x, FILE *f) {
115 x = short2host(x);
116 myfwrite(&x, 2, 1, f);
119 void myfwrite32 (int32_t x, FILE *f) {
120 x = int2host(x);
121 myfwrite(&x, 4, 1, f);
124 int fexists (char *filename) {
125 FILE *f;
126 if ((f = fopen(filename, "r")))
128 fclose(f);
129 return 1;
131 return 0;