DEADSOFTWARE

bfae25f516d150ccbf2edd38e3af7eaea89ac493
[flatwaifu.git] / src / my.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include <SDL.h>
24 #include "glob.h"
25 #include "error.h"
26 #include "misc.h"
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <assert.h>
31 void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext)
32 {
33 const char* end; /* end of processed string */
34 const char* p; /* search pointer */
35 const char* s; /* copy pointer */
37 /* extract drive name */
38 if (path[0] && path[1]==':') {
39 if (drv) {
40 *drv++ = *path++;
41 *drv++ = *path++;
42 *drv = '\0';
43 }
44 } else if (drv)
45 *drv = '\0';
47 /* search for end of string or stream separator */
48 for(end=path; *end && *end!=':'; )
49 end++;
51 /* search for begin of file extension */
52 for(p=end; p>path && *--p!='\\' && *p!='/'; )
53 if (*p == '.') {
54 end = p;
55 break;
56 }
58 if (ext)
59 for(s=end; (*ext=*s++); )
60 ext++;
62 /* search for end of directory name */
63 for(p=end; p>path; )
64 if (*--p=='\\' || *p=='/') {
65 p++;
66 break;
67 }
69 if (name) {
70 for(s=p; s<end; )
71 *name++ = *s++;
73 *name = '\0';
74 }
76 if (dir) {
77 for(s=path; s<p; )
78 *dir++ = *s++;
80 *dir = '\0';
81 }
82 }
84 size_t myfreadc (void *ptr, size_t size, size_t n, FILE *f) {
85 return fread(ptr, size, n, f);
86 }
88 void myfread (void *ptr, size_t size, size_t n, FILE *f) {
89 if (myfreadc(ptr, size, n, f) != n) {
90 ERR_fatal("File reading error\n");
91 }
92 }
94 int8_t myfread8 (FILE *f) {
95 int8_t x;
96 myfread(&x, 1, 1, f);
97 return x;
98 }
100 int16_t myfread16 (FILE *f) {
101 int16_t x;
102 myfread(&x, 2, 1, f);
103 return short2host(x);
106 int32_t myfread32 (FILE *f) {
107 int32_t x;
108 myfread(&x, 4, 1, f);
109 return int2host(x);
112 void myfwrite (void *ptr, size_t size, size_t n, FILE *f) {
113 assert(fwrite(ptr, size, n, f) == n);
116 void myfwrite8 (int8_t x, FILE *f) {
117 myfwrite(&x, 1, 1, f);
120 void myfwrite16 (int16_t x, FILE *f) {
121 x = short2host(x);
122 myfwrite(&x, 2, 1, f);
125 void myfwrite32 (int32_t x, FILE *f) {
126 x = int2host(x);
127 myfwrite(&x, 4, 1, f);
130 void myrandomize(void)
132 srand(SDL_GetTicks());
135 int fexists(char * filename)
137 FILE *f;
138 if ((f = fopen(filename, "r")))
140 fclose(f);
141 return 1;
143 return 0;