2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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 Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
26 #include "globaldefs.h"
28 #define PATHSEPERATOR '/'
38 double I_FloatTime (void)
46 // more precise, less portable
51 gettimeofday(&tp, &tzp);
56 return tp.tv_usec/1000000.0;
59 return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
68 Parse a token out of a string
71 char *COM_Parse (char *data)
84 while ( (c = *data) <= ' ')
89 return NULL; // end of file;
95 if (c=='/' && data[1] == '/')
97 while (*data && *data != '\n')
103 // handle quoted strings specially
120 // parse single characters
121 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
129 // parse a regular word
136 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
145 int Q_strncasecmp (char *s1, char *s2, int n)
155 return 0; // strings are equal until end point
159 if (c1 >= 'a' && c1 <= 'z')
161 if (c2 >= 'a' && c2 <= 'z')
164 return -1; // strings not equal
167 return 0; // strings are equal
173 int Q_strcasecmp (char *s1, char *s2)
175 return Q_strncasecmp (s1, s2, 99999);
181 =============================================================================
185 =============================================================================
190 char *argv[MAX_NUM_ARGVS];
197 void ParseCommandLine (char *lpCmdLine)
200 argv[0] = "programname";
202 while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
204 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
209 argv[argc] = lpCmdLine;
212 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
231 Checks for the given parameter in the program's command line arguments
232 Returns the argument number (1 to argc-1) or 0 if not present
235 int CheckParm (char *check)
239 for (i = 1;i<argc;i++)
241 if ( !Q_strcasecmp(check, argv[i]) )
255 int Q_filelength (FILE *f)
261 fseek (f, 0, SEEK_END);
263 fseek (f, pos, SEEK_SET);
269 FILE *SafeOpenWrite (char *filename)
273 f = fopen(filename, "wb");
276 Error ("Error opening %s: %s",filename,strerror(errno));
281 FILE *SafeOpenRead (char *filename)
285 f = fopen(filename, "rb");
288 Error ("Error opening %s: %s",filename,strerror(errno));
294 void SafeRead (FILE *f, void *buffer, int count)
296 if ( (int)fread (buffer, 1, count, f) != count)
297 Error ("File read failure");
301 void SafeWrite (FILE *f, void *buffer, int count)
303 if ( (int)fwrite (buffer, 1, count, f) != count)
304 Error ("File read failure");
314 int LoadFile (char *filename, void **bufferptr)
319 extern void *qmalloc( size_t size );
321 f = fopen (filename, "rb");
327 length = Q_filelength (f);
328 buffer = qmalloc (length+1);
329 ((char *)buffer)[length] = 0;
330 SafeRead (f, buffer, length);
342 returns -1 length if not present
345 int LoadFileNoCrash (char *filename, void **bufferptr)
351 f = fopen (filename, "rb");
354 length = Q_filelength (f);
355 buffer = qmalloc (length+1);
356 ((char *)buffer)[length] = 0;
357 SafeRead (f, buffer, length);
370 void SaveFile (char *filename, void *buffer, int count)
374 f = SafeOpenWrite (filename);
375 SafeWrite (f, buffer, count);
381 void DefaultExtension (char *path, char *extension)
385 // if path doesn't have a .EXT, append extension
386 // (extension should include the .)
388 src = path + strlen(path) - 1;
390 while (*src != PATHSEPERATOR && src != path)
393 return; // it has an extension
397 strcat (path, extension);
401 void DefaultPath (char *path, char *basepath)
405 if (path[0] == PATHSEPERATOR)
406 return; // absolute path location
408 strcpy (path,basepath);
413 void StripFilename (char *path)
417 length = strlen(path)-1;
418 while (length > 0 && path[length] != PATHSEPERATOR)
423 void StripExtension (char *path)
427 length = strlen(path)-1;
428 while (length > 0 && path[length] != '.')
431 if (path[length] == '/')
432 return; // no extension
444 void ExtractFilePath (char *path, char *dest)
448 src = path + strlen(path) - 1;
451 // back up until a \ or the start
453 while (src != path && *(src-1) != PATHSEPERATOR)
456 memcpy (dest, path, src-path);
460 void ExtractFileName (char *path, char *dest)
464 src = path + strlen(path) - 1;
467 // back up until a \ or the start
469 while (src != path && *(src-1) != '/'
470 && *(src-1) != '\\' )
480 void ExtractFileBase (char *path, char *dest)
484 src = path + strlen(path) - 1;
487 // back up until a \ or the start
489 while (src != path && *(src-1) != '/'
490 && *(src-1) != '\\' )
493 while (*src && *src != '.')
500 void ExtractFileExtension (char *path, char *dest)
504 src = path + strlen(path) - 1;
507 // back up until a . or the start
509 while (src != path && *(src-1) != '.')
513 *dest = 0; // no extension
526 int ParseHex (char *hex)
537 if (*str >= '0' && *str <= '9')
539 else if (*str >= 'a' && *str <= 'f')
540 num += 10 + *str-'a';
541 else if (*str >= 'A' && *str <= 'F')
542 num += 10 + *str-'A';
544 Error ("Bad hex number: %s",hex);
552 int ParseNum (char *str)
555 return ParseHex (str+1);
556 if (str[0] == '0' && str[1] == 'x')
557 return ParseHex (str+2);
564 ============================================================================
568 ============================================================================
571 #if GDEF_ARCH_ENDIAN_BIG
573 short LittleShort (short l)
583 short BigShort (short l)
589 int LittleLong (int l)
598 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
607 float LittleFloat (float l)
609 union {byte b[4]; float f;} in, out;
620 float BigFloat (float l)
629 short BigShort (short l)
639 short LittleShort (short l)
654 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
657 int LittleLong (int l)
662 float BigFloat (float l)
664 union {byte b[4]; float f;} in, out;
675 float LittleFloat (float l)