13 void stat(const char *fmt
, ...);
15 #if __BYTE_ORDER == __LITTLE_ENDIAN
16 uint16_t fgeti(FILE *fp
)
19 fread(&value
, 2, 1, fp
);
23 uint32_t fgetl(FILE *fp
)
26 fread(&value
, 4, 1, fp
);
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
);
40 uint16_t fgeti(FILE *fp
)
48 uint32_t fgetl(FILE *fp
)
55 return (d
<<24)|(c
<<16)|(b
<<8)|(a
);
58 void fputi(uint16_t word
, FILE *fp
)
64 void fputl(uint32_t word
, FILE *fp
)
68 fputc(word
>> 16, fp
);
69 fputc(word
>> 24, fp
);
71 #endif // __BYTE_ORDER == __LITTLE_ENDIAN
75 double fgetfloat(FILE *fp
)
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];
88 void fputfloat(double q
, FILE *fp
)
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
);
102 // read a string from a file until a null is encountered
103 void freadstring(FILE *fp
, char *buf
, int max
)
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
);
127 // write a string to a file-- does NOT null-terminate it
128 void fputstringnonull(const char *buf
, FILE *fp
)
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
)
140 int stringlength
= strlen(str
);
142 for(i
=0;i
<stringlength
;i
++)
144 if (fgetc(fp
) != str
[i
]) result
= 0;
150 // read data from a file until ',' or CR
151 void fgetcsv(FILE *fp
, char *str
, int maxlen
)
157 for(i
=j
=0;i
<maxlen
;i
++)
161 if (ch
==13 || ch
==',' || ch
=='}' || ch
==-1)
175 // read a number from a CSV'd list in a file
176 int fgeticsv(FILE *fp
)
179 fgetcsv(fp
, buffer
, sizeof(buffer
));
183 double fgetfcsv(FILE *fp
)
186 fgetcsv(fp
, buffer
, sizeof(buffer
));
191 // read data from a file until CR
192 void fgetline(FILE *fp
, char *str
, int maxlen
)
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;
206 int filesize(FILE *fp
)
211 fseek(fp
, 0, SEEK_END
);
213 fseek(fp
, cp
, SEEK_SET
);
218 bool file_exists(const char *fname
)
222 fp
= fileopen(fname
, "rb");
228 char *stprintf(const char *fmt
, ...)
231 char *str
= GetStaticStr();
234 vsnprintf(str
, 255, fmt
, ar
);
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
)
253 staterr("random(): warning: max < min [%d, %d]", min
, max
);
261 if (range
>= RAND_MAX
)
263 staterr("random(): range > RAND_MAX", min
, max
);
267 val
= getrand() % (range
+ 1);
273 seed
= (seed
* 0x343FD) + 0x269EC3;
277 void seedrand(uint32_t newseed
)
283 void c------------------------------() {}
287 bool strbegin(const char *bigstr
, const char *smallstr
)
291 for(i
=0;smallstr
[i
];i
++)
292 if (bigstr
[i
] != smallstr
[i
]) return false;
297 bool strcasebegin(const char *bigstr
, const char *smallstr
)
301 for(i
=0;smallstr
[i
];i
++)
302 if (toupper(bigstr
[i
]) != toupper(smallstr
[i
])) return false;
308 // returns how many strings are in a null-terminated array of C strings
309 int count_string_list(const char *list
[])
312 for(i
=0;list
[i
];i
++) ;
317 char *GetStaticStr(void)
319 static int counter
= 0;
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
);
336 if (maxlen
>= 2) memcpy(dst
, src
, maxlen
- 2);
337 if (maxlen
>= 1) dst
[maxlen
- 1] = 0;
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)
359 // read a boolean value (a single bit) from a file
360 char fbooleanread(FILE *fp
)
364 if (boolmask_r
== 256)
366 boolbyte
= fgetc(fp
);
370 value
= (boolbyte
& boolmask_r
) ? 1:0;
375 void fbooleanwrite(char bit
, FILE *fp
)
377 if (boolmask_w
== 256)
386 boolbyte
|= boolmask_w
;
392 void fbooleanflush(FILE *fp
)