NXEngine v1.0.0.6
[NXEngine.git] / common / misc.cpp
blob3e572bda9b0623585ab376edfab6a5a3b0d24d51
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <math.h>
8 #include <ctype.h>
10 #include "basics.h"
11 #include "misc.fdh"
13 void stat(const char *fmt, ...);
15 #if __BYTE_ORDER == __LITTLE_ENDIAN
16 uint16_t fgeti(FILE *fp)
18 uint16_t value;
19 fread(&value, 2, 1, fp);
20 return value;
23 uint32_t fgetl(FILE *fp)
25 uint32_t value;
26 fread(&value, 4, 1, fp);
27 return value;
30 void fputi(uint16_t word, FILE *fp)
32 fwrite(&word, 2, 1, fp);
35 void fputl(uint32_t word, FILE *fp)
37 fwrite(&word, 4, 1, fp);
39 #else
40 uint16_t fgeti(FILE *fp)
42 uint16_t a, b;
43 a = fgetc(fp);
44 b = fgetc(fp);
45 return (b << 8) | a;
48 uint32_t fgetl(FILE *fp)
50 uint32_t a, b, c, d;
51 a = fgetc(fp);
52 b = fgetc(fp);
53 c = fgetc(fp);
54 d = fgetc(fp);
55 return (d<<24)|(c<<16)|(b<<8)|(a);
58 void fputi(uint16_t word, FILE *fp)
60 fputc(word, fp);
61 fputc(word >> 8, fp);
64 void fputl(uint32_t word, FILE *fp)
66 fputc(word, fp);
67 fputc(word >> 8, fp);
68 fputc(word >> 16, fp);
69 fputc(word >> 24, fp);
71 #endif // __BYTE_ORDER == __LITTLE_ENDIAN
75 double fgetfloat(FILE *fp)
77 char buf[16];
78 double *float_ptr;
79 int i;
81 for(i=0;i<4;i++) fgetc(fp);
82 for(i=0;i<8;i++) buf[i] = fgetc(fp);
84 float_ptr = (double *)&buf[0];
85 return *float_ptr;
88 void fputfloat(double q, FILE *fp)
90 char *float_ptr;
91 int i;
93 float_ptr = (char *)&q;
95 for(i=0;i<4;i++) fputc(0, fp);
96 for(i=0;i<8;i++) fputc(float_ptr[i], fp);
98 return;
102 // read a string from a file until a null is encountered
103 void freadstring(FILE *fp, char *buf, int max)
105 int i;
107 --max;
108 for(i=0;i<max;i++)
110 buf[i] = fgetc(fp);
111 if (!buf[i])
113 return;
117 buf[i] = 0;
120 // write a string to a file and null-terminate it
121 void fputstring(const char *buf, FILE *fp)
123 if (buf[0]) fprintf(fp, "%s", buf);
124 fputc(0, fp);
127 // write a string to a file-- does NOT null-terminate it
128 void fputstringnonull(const char *buf, FILE *fp)
130 if (buf[0])
131 fprintf(fp, "%s", buf);
135 // reads strlen(str) bytes from file fp, and returns true if they match "str"
136 bool fverifystring(FILE *fp, const char *str)
138 int i;
139 char result = 1;
140 int stringlength = strlen(str);
142 for(i=0;i<stringlength;i++)
144 if (fgetc(fp) != str[i]) result = 0;
147 return result;
150 // read data from a file until ',' or CR
151 void fgetcsv(FILE *fp, char *str, int maxlen)
153 int i, j;
154 char ch;
156 maxlen--;
157 for(i=j=0;i<maxlen;i++)
159 ch = fgetc(fp);
161 if (ch==13 || ch==',' || ch=='}' || ch==-1)
163 break;
166 if (ch != 10)
168 str[j++] = ch;
172 str[j] = 0;
175 // read a number from a CSV'd list in a file
176 int fgeticsv(FILE *fp)
178 char buffer[80];
179 fgetcsv(fp, buffer, sizeof(buffer));
180 return atoi(buffer);
183 double fgetfcsv(FILE *fp)
185 char buffer[80];
186 fgetcsv(fp, buffer, sizeof(buffer));
187 return atof(buffer);
191 // read data from a file until CR
192 void fgetline(FILE *fp, char *str, int maxlen)
194 int k;
195 str[0] = 0;
196 fgets(str, maxlen - 1, fp);
198 // trim the CRLF that fgets appends
199 for(k=strlen(str)-1;k>=0;k--)
201 if (str[k] != 13 && str[k] != 10) break;
202 str[k] = 0;
206 int filesize(FILE *fp)
208 int cp, sz;
210 cp = ftell(fp);
211 fseek(fp, 0, SEEK_END);
212 sz = ftell(fp);
213 fseek(fp, cp, SEEK_SET);
215 return sz;
218 bool file_exists(const char *fname)
220 FILE *fp;
222 fp = fileopen(fname, "rb");
223 if (!fp) return 0;
224 fclose(fp);
225 return 1;
228 char *stprintf(const char *fmt, ...)
230 va_list ar;
231 char *str = GetStaticStr();
233 va_start(ar, fmt);
234 vsnprintf(str, 255, fmt, ar);
235 va_end(ar);
237 return str;
241 void c------------------------------() {}
244 static uint32_t seed = 0;
246 // return a random number between min and max inclusive
247 int random(int min, int max)
249 int range, val;
251 if (max < min)
253 staterr("random(): warning: max < min [%d, %d]", min, max);
254 min ^= max;
255 max ^= min;
256 min ^= max;
259 range = (max - min);
261 if (range >= RAND_MAX)
263 staterr("random(): range > RAND_MAX", min, max);
264 return 0;
267 val = getrand() % (range + 1);
268 return val + min;
271 uint32_t getrand()
273 seed = (seed * 0x343FD) + 0x269EC3;
274 return seed;
277 void seedrand(uint32_t newseed)
279 seed = newseed;
283 void c------------------------------() {}
287 bool strbegin(const char *bigstr, const char *smallstr)
289 int i;
291 for(i=0;smallstr[i];i++)
292 if (bigstr[i] != smallstr[i]) return false;
294 return true;
297 bool strcasebegin(const char *bigstr, const char *smallstr)
299 int i;
301 for(i=0;smallstr[i];i++)
302 if (toupper(bigstr[i]) != toupper(smallstr[i])) return false;
304 return true;
308 // returns how many strings are in a null-terminated array of C strings
309 int count_string_list(const char *list[])
311 int i;
312 for(i=0;list[i];i++) ;
313 return i;
317 char *GetStaticStr(void)
319 static int counter = 0;
320 static struct
322 char str[1024];
323 } bufs[24];
325 if (++counter >= 24) counter = 0;
326 return bufs[counter].str;
329 // a strncpy that works as you might expect
330 void maxcpy(char *dst, const char *src, int maxlen)
332 int len = strlen(src);
334 if (len >= maxlen)
336 if (maxlen >= 2) memcpy(dst, src, maxlen - 2);
337 if (maxlen >= 1) dst[maxlen - 1] = 0;
339 else
341 memcpy(dst, src, len + 1);
346 void c------------------------------() {}
349 static int boolbyte, boolmask_r, boolmask_w;
351 // prepare for a boolean read operation
352 void fresetboolean(void)
354 boolmask_r = 256;
355 boolmask_w = 1;
356 boolbyte = 0;
359 // read a boolean value (a single bit) from a file
360 char fbooleanread(FILE *fp)
362 char value;
364 if (boolmask_r == 256)
366 boolbyte = fgetc(fp);
367 boolmask_r = 1;
370 value = (boolbyte & boolmask_r) ? 1:0;
371 boolmask_r <<= 1;
372 return value;
375 void fbooleanwrite(char bit, FILE *fp)
377 if (boolmask_w == 256)
379 fputc(boolbyte, fp);
380 boolmask_w = 1;
381 boolbyte = 0;
384 if (bit)
386 boolbyte |= boolmask_w;
389 boolmask_w <<= 1;
392 void fbooleanflush(FILE *fp)
394 fputc(boolbyte, fp);
395 boolmask_w = 1;