DEADSOFTWARE

portability: avoid errors on some compilers
[flatwaifu.git] / src / stubsys / files.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 */
18 #include "glob.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include "files.h"
24 #include "error.h"
26 #include "map.h" // MAP_load
27 #include "save.h" // SAVE_getname
29 #ifdef UNIX
30 # include <sys/stat.h>
31 #endif
33 #include "common/streams.h"
34 #include "common/files.h"
35 #include "common/wadres.h"
36 #include "common/cp866.h"
38 int d_start, d_end;
40 char savname[SAVE_MAX][SAVE_MAXLEN];
41 char savok[SAVE_MAX];
43 static int m_start, m_end;
44 static int s_start, s_end;
46 void F_addwad (const char *fn) {
47 static int i = 0;
48 static FILE_Stream wadh[MAX_WADS];
49 if (i < MAX_WADS) {
50 if (FILE_Open(&wadh[i], fn, "rb")) {
51 if (WADRES_addwad(&wadh[i].base)) {
52 i += 1;
53 } else {
54 ERR_failinit("Invalid WAD %s", fn);
55 }
56 } else {
57 ERR_failinit("Unable to add WAD %s", fn);
58 }
59 } else {
60 ERR_failinit("Too many wads");
61 }
62 }
64 void F_initwads (void) {
65 if (!WADRES_rehash()) {
66 ERR_failinit("F_initwads: failed rehash");
67 }
68 d_start = F_getresid("D_START");
69 d_end = F_getresid("D_END");
70 m_start = F_getresid("M_START");
71 m_end = F_getresid("M_END");
72 s_start = F_getresid("S_START");
73 s_end = F_getresid("S_END");
74 }
76 int F_findres (const char n[8]) {
77 return WADRES_find(n);
78 }
80 int F_getresid (const char n[8]) {
81 int i = F_findres(n);
82 if (i == -1) {
83 ERR_fatal("F_getresid: resource %.8s not found", n);
84 }
85 return i;
86 }
88 void F_getresname (char n[8], int r) {
89 WADRES_getname(r, n);
90 }
92 int F_getsprid (const char n[4], int s, int d, char *dir) {
93 int i = WADRES_findsprite(n, s, d, dir);
94 if (i == -1) {
95 ERR_fatal("F_getsprid: image %.4s%c%c not found", n, s, d);
96 }
97 return i;
98 }
100 int F_getreslen (int r) {
101 return WADRES_getsize(r);
104 /*
105 void F_nextmus (char *s) {
106 int i = F_findres(s);
107 if (i <= m_start || i >= m_end) {
108 i = m_start;
110 for (++i; ; ++i) {
111 if (i >= m_end) {
112 i = m_start + 1;
114 WADRES_getname(i, s);
115 if (cp866_strcasecmp(s, "MENU") == 0 ||
116 cp866_strcasecmp(s, "INTERMUS") == 0 ||
117 cp866_strcasecmp(s, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
118 continue;
120 if (cp866_strncasecmp(s, "DMI", 3) != 0) {
121 break;
126 void F_randmus (char *s) {
127 int i;
128 int n = myrand(10);
129 for (i = 0; i < n; i++) {
130 F_nextmus(s);
133 */
135 void F_loadmap (char n[8]) {
136 int id = F_getresid(n);
137 if (id != -1) {
138 Stream *r = WADRES_getbasereader(id);
139 long offset = WADRES_getoffset(id);
140 stream_setpos(r, offset);
141 if (!MAP_load(r)) {
142 ERR_fatal("Failed to load map");
144 } else {
145 ERR_fatal("Failed to load map: resource %.8s not found", n);
149 static char *getsavfpname (int n, int ro) {
150 static char fn[] = "savgame0.dat";
151 static char p[100];
152 fn[7] = n + '0';
153 #ifdef UNIX
154 char *e = getenv("HOME");
155 strncpy(p, e, 60);
156 strcat(p, "/.flatwaifu");
157 if (!ro) {
158 mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
160 strcat(p, "/");
161 strcat(p, fn);
162 #else
163 strcpy(p, fn);
164 #endif
165 return p;
168 void F_getsavnames (void) {
169 int i;
170 char *p;
171 FILE_Stream rd;
172 for (i = 0; i < SAVE_MAX; ++i) {
173 savok[i] = 0;
174 p = getsavfpname(i, 1);
175 if (FILE_Open(&rd, p, "rb")) {
176 savok[i] = SAVE_getname(&rd.base, savname[i]);
177 FILE_Close(&rd);
179 if (!savok[i]) {
180 memset(savname[i], 0, 24);
181 } else {
182 savname[i][23] = 0;
187 void F_loadgame (int n) {
188 FILE_Stream rd;
189 char *p = getsavfpname(n, 1);
190 if (FILE_Open(&rd, p, "rb")) {
191 SAVE_load(&rd.base);
192 FILE_Close(&rd);
196 void F_savegame (int n, char *s) {
197 FILE_Stream wr;
198 char *p = getsavfpname(n, 0);
199 if (FILE_Open(&wr, p, "wb")) {
200 SAVE_save(&wr.base, s);
201 FILE_Close(&wr);