-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.c
350 lines (243 loc) · 5.64 KB
/
tool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Gravit - A gravity simulator
Copyright 2003-2005 Gerald Kaszuba
Gravit is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Gravit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gravit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "gravit.h"
#define shitzta 4
char * va( char *format, ... ) {
va_list argptr;
static char string[shitzta][10000]; // in case va is called by nested functions
static unsigned short index = 0;
char *buf;
buf = string[index];
index++;
if (index == shitzta)
index = 0;
va_start (argptr, format);
vsprintf (buf, format, argptr);
va_end (argptr);
return buf;
}
int gfxPowerOfTwo(int input) {
int value = 1;
while ( value < input )
value <<= 1;
return value;
}
int LoadMemoryDump(char *fileName, unsigned char *d, unsigned int size) {
FILE *fp;
unsigned int i, pos, p, amountToRead;
unsigned int chunkMax = FILE_CHUNK_SIZE;
fp = fopen(fileName, "rb");
if (!fp) {
conAdd(LNORM, "Count not open %s for reading.", fileName);
return 0;
}
i = 0;
pos = 0;
while (pos < size) {
if (size - pos < chunkMax)
amountToRead = size - pos;
else
amountToRead = chunkMax;
p = fread(d, 1, amountToRead, fp);
if (p == 0) {
conAdd(LERR, "Short read on %s", fileName);
fclose(fp);
return 0;
}
d += p;
pos += p;
if (ferror(fp)) {
conAdd(LNORM, "%s", strerror( errno ));
fclose(fp);
return 0;
}
#if 0
if (i) {
conAdd(LLOW, "%3.1f%% (%.0f / %.0f)", 100*(float)pos/(float)size,(float)pos / 1024 / 1024,(float)size / 1024 / 1024);
#ifndef NO_GUI
drawAll();
#endif
i=1000;
}
i--;
#endif
}
fclose(fp);
return 1;
}
int SaveMemoryDump(char *fileName, unsigned char *d, unsigned int total) {
FILE *fp;
unsigned int written, p, write;
fp = fopen(fileName, "wb");
if (!fp) {
conAdd(LNORM, "count not open %s for writing");
return 0;
}
written = 0;
while (written < total) {
write = FILE_CHUNK_SIZE;
if (written + write > total)
write = total - written;
p = fwrite(d, 1, write, fp);
d += p;
written += p;
}
fclose(fp);
conAdd(LLOW, "written %u bytes to %s", written, fileName);
return 1;
}
Uint32 getMS() {
#ifndef NO_GUI
return SDL_GetTicks();
#else
#ifdef WIN32
return GetTickCount();
#else
struct timeb mytime;
ftime(&mytime);
return mytime.time * 1000 + mytime.millitm;
#endif
#endif
}
void setTitle(char *state) {
#ifndef NO_GUI
char *a;
if (state)
a = va("%s - %s", GRAVIT_VERSION, state);
else
a = GRAVIT_VERSION;
SDL_WM_SetCaption(a, a);
#endif
}
int mymkdir(char *path) {
#ifdef WIN32
{
WIN32_FIND_DATA damnwindows;
if (FindFirstFile(path, &damnwindows) == INVALID_HANDLE_VALUE) {
CreateDirectory(path, NULL);
}
}
#else
{
DIR *d;
d = opendir(path);
if (!d) {
mkdir(path, 0755);
} else {
closedir(d);
}
}
#endif
return 1;
}
void freeFileName() {
if (state.fileName) {
free(state.fileName);
state.fileName = 0;
}
}
void setFileName(char *name) {
char buf[255];
strncpy(buf, name, 255);
freeFileName();
state.fileName = malloc(strlen(buf)+1); // +1 for \0
strcpy(state.fileName, buf);
}
char *getRegistryString(char *variable) {
#ifdef WIN32
static char buf[MAX_PATH];
int len = MAX_PATH;
HKEY hkResult;
RegCreateKeyEx(HKEY_CURRENT_USER, REGISTRY_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, NULL);
RegQueryValueEx(hkResult, variable, 0, NULL, buf, &len);
RegCloseKey(hkResult);
return buf;
#endif
return NULL;
}
void setRegistryString(char *variable, char *value) {
#ifdef WIN32
HKEY hkResult;
RegCreateKeyEx(HKEY_CURRENT_USER, REGISTRY_KEY, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkResult, NULL);
RegSetValueEx(hkResult, variable, 0, REG_SZ, value, strlen(value)+1);
RegCloseKey(hkResult);
#endif
}
// return 0 on failure
int myunlink(char *filename) {
#ifdef WIN32
return DeleteFile(filename);
#else
return !unlink(filename);
#endif
}
int fileExists(char *path) {
FILE *fp;
fp = fopen(path, "rb");
if (!fp) return 0;
fclose(fp);
return 1;
}
// finds a file in gravit's search paths
char *findFile(char *file) {
#ifndef WIN32
char *homeDir;
#endif
char *tmp;
// first assume no path, so someone could load "/tmp/script" or "currentdirectory.cfg"
conAdd(LLOW, "Searching %s", file);
if (fileExists(file))
return file;
#ifdef WIN32
// windows users only have one place to look, that is the current directory
return 0;
#endif
#ifndef WIN32
// now look in the users gravit directory (only in UNIX) - /home/user/.gravit/file
homeDir = getenv("HOME");
if (homeDir) {
tmp = va("%s/.gravit/%s", homeDir, file);
conAdd(LLOW, "Searching %s", tmp);
if (fileExists(tmp)) {
return tmp;
}
}
#endif
// finally look in SYSCONFDIR
tmp = va("%s/%s", SYSCONFDIR, file);
conAdd(LLOW, "Searching %s", tmp);
if (fileExists(tmp)) {
return tmp;
}
return 0;
}
// check to see if we can save into $HOME/.gravit/save/ directory or not
int checkHomePath() {
#ifndef WIN32
char *user;
#endif
#ifdef WIN32
return 1;
#else
user = getenv("HOME");
if (!user) {
conAdd(LERR, "$HOME environment variable not set, can not continue.");
return 0;
}
mymkdir(va("%s/.gravit", user));
return 1;
#endif
}