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 ===========================================================================
27 #define PATHSEPERATOR '/'
37 double I_FloatTime (void)
45 // more precise, less portable
50 gettimeofday(&tp, &tzp);
55 return tp.tv_usec/1000000.0;
58 return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
67 Parse a token out of a string
70 char *COM_Parse (char *data)
83 while ( (c = *data) <= ' ')
88 return NULL; // end of file;
94 if (c=='/' && data[1] == '/')
96 while (*data && *data != '\n')
102 // handle quoted strings specially
119 // parse single characters
120 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
128 // parse a regular word
135 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
144 int Q_strncasecmp (char *s1, char *s2, int n)
154 return 0; // strings are equal until end point
158 if (c1 >= 'a' && c1 <= 'z')
160 if (c2 >= 'a' && c2 <= 'z')
163 return -1; // strings not equal
166 return 0; // strings are equal
172 int Q_strcasecmp (char *s1, char *s2)
174 return Q_strncasecmp (s1, s2, 99999);
180 =============================================================================
184 =============================================================================
189 char *argv[MAX_NUM_ARGVS];
196 void ParseCommandLine (char *lpCmdLine)
199 argv[0] = "programname";
201 while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
203 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
208 argv[argc] = lpCmdLine;
211 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
230 Checks for the given parameter in the program's command line arguments
231 Returns the argument number (1 to argc-1) or 0 if not present
234 int CheckParm (char *check)
238 for (i = 1;i<argc;i++)
240 if ( !Q_strcasecmp(check, argv[i]) )
254 int Q_filelength (FILE *f)
260 fseek (f, 0, SEEK_END);
262 fseek (f, pos, SEEK_SET);
268 FILE *SafeOpenWrite (char *filename)
272 f = fopen(filename, "wb");
275 Error ("Error opening %s: %s",filename,strerror(errno));
280 FILE *SafeOpenRead (char *filename)
284 f = fopen(filename, "rb");
287 Error ("Error opening %s: %s",filename,strerror(errno));
293 void SafeRead (FILE *f, void *buffer, int count)
295 if ( (int)fread (buffer, 1, count, f) != count)
296 Error ("File read failure");
300 void SafeWrite (FILE *f, void *buffer, int count)
302 if ( (int)fwrite (buffer, 1, count, f) != count)
303 Error ("File read failure");
313 int LoadFile (char *filename, void **bufferptr)
318 extern void *qmalloc( size_t size );
320 f = fopen (filename, "rb");
326 length = Q_filelength (f);
327 buffer = qmalloc (length+1);
328 ((char *)buffer)[length] = 0;
329 SafeRead (f, buffer, length);
341 returns -1 length if not present
344 int LoadFileNoCrash (char *filename, void **bufferptr)
350 f = fopen (filename, "rb");
353 length = Q_filelength (f);
354 buffer = qmalloc (length+1);
355 ((char *)buffer)[length] = 0;
356 SafeRead (f, buffer, length);
369 void SaveFile (char *filename, void *buffer, int count)
373 f = SafeOpenWrite (filename);
374 SafeWrite (f, buffer, count);
380 void DefaultExtension (char *path, char *extension)
384 // if path doesn't have a .EXT, append extension
385 // (extension should include the .)
387 src = path + strlen(path) - 1;
389 while (*src != PATHSEPERATOR && src != path)
392 return; // it has an extension
396 strcat (path, extension);
400 void DefaultPath (char *path, char *basepath)
404 if (path[0] == PATHSEPERATOR)
405 return; // absolute path location
407 strcpy (path,basepath);
412 void StripFilename (char *path)
416 length = strlen(path)-1;
417 while (length > 0 && path[length] != PATHSEPERATOR)
422 void StripExtension (char *path)
426 length = strlen(path)-1;
427 while (length > 0 && path[length] != '.')
430 if (path[length] == '/')
431 return; // no extension
443 void ExtractFilePath (char *path, char *dest)
447 src = path + strlen(path) - 1;
450 // back up until a \ or the start
452 while (src != path && *(src-1) != PATHSEPERATOR)
455 memcpy (dest, path, src-path);
459 void ExtractFileName (char *path, char *dest)
463 src = path + strlen(path) - 1;
466 // back up until a \ or the start
468 while (src != path && *(src-1) != '/'
469 && *(src-1) != '\\' )
479 void ExtractFileBase (char *path, char *dest)
483 src = path + strlen(path) - 1;
486 // back up until a \ or the start
488 while (src != path && *(src-1) != '/'
489 && *(src-1) != '\\' )
492 while (*src && *src != '.')
499 void ExtractFileExtension (char *path, char *dest)
503 src = path + strlen(path) - 1;
506 // back up until a . or the start
508 while (src != path && *(src-1) != '.')
512 *dest = 0; // no extension
525 int ParseHex (char *hex)
536 if (*str >= '0' && *str <= '9')
538 else if (*str >= 'a' && *str <= 'f')
539 num += 10 + *str-'a';
540 else if (*str >= 'A' && *str <= 'F')
541 num += 10 + *str-'A';
543 Error ("Bad hex number: %s",hex);
551 int ParseNum (char *str)
554 return ParseHex (str+1);
555 if (str[0] == '0' && str[1] == 'x')
556 return ParseHex (str+2);
563 ============================================================================
567 ============================================================================
571 #define __BIG_ENDIAN__
574 #ifdef __BIG_ENDIAN__
576 short LittleShort (short l)
586 short BigShort (short l)
592 int LittleLong (int l)
601 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
610 float LittleFloat (float l)
612 union {byte b[4]; float f;} in, out;
623 float BigFloat (float l)
632 short BigShort (short l)
642 short LittleShort (short l)
657 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
660 int LittleLong (int l)
665 float BigFloat (float l)
667 union {byte b[4]; float f;} in, out;
678 float LittleFloat (float l)