1 // cmdlib.c
2 
3 #include "cmdlib.h"
4 #ifdef WIN32
5 #else
6 #include <sys/time.h>
7 #endif
8 
9 #define PATHSEPERATOR   '/'
10 
11 // set these before calling CheckParm
12 int myargc;
13 char **myargv;
14 
15 char	com_token[1024];
16 int		com_eof;
17 
18 /*
19 ================
20 I_FloatTime
21 ================
22 */
I_FloatTime(void)23 double I_FloatTime (void)
24 {
25 #ifdef WIN32
26 	return 0;
27 #else
28 	struct timeval tp;
29 	struct timezone tzp;
30 	static int		secbase;
31 
32 	gettimeofday(&tp, &tzp);
33 
34 	if (!secbase)
35 	{
36 		secbase = tp.tv_sec;
37 		return tp.tv_usec/1000000.0;
38 	}
39 
40 	return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
41 #endif
42 }
43 
44 
45 /*
46 ==============
47 COM_Parse
48 
49 Parse a token out of a string
50 ==============
51 */
COM_Parse(char * data)52 char *COM_Parse (char *data)
53 {
54 	int		c;
55 	int		len;
56 
57 	len = 0;
58 	com_token[0] = 0;
59 
60 	if (!data)
61 		return NULL;
62 
63 // skip whitespace
64 skipwhite:
65 	while ( (c = *data) <= ' ')
66 	{
67 		if (c == 0)
68 		{
69 			com_eof = true;
70 			return NULL;			// end of file;
71 		}
72 		data++;
73 	}
74 
75 // skip // comments
76 	if (c=='/' && data[1] == '/')
77 	{
78 		while (*data && *data != '\n')
79 			data++;
80 		goto skipwhite;
81 	}
82 
83 
84 // handle quoted strings specially
85 	if (c == '\"')
86 	{
87 		data++;
88 		do
89 		{
90 			c = *data++;
91 			if (c=='\"')
92 			{
93 				com_token[len] = 0;
94 				return data;
95 			}
96 			com_token[len] = c;
97 			len++;
98 		} while (1);
99 	}
100 
101 // parse single characters
102 	if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
103 	{
104 		com_token[len] = c;
105 		len++;
106 		com_token[len] = 0;
107 		return data+1;
108 	}
109 
110 // parse a regular word
111 	do
112 	{
113 		com_token[len] = c;
114 		data++;
115 		len++;
116 		c = *data;
117 	if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
118 			break;
119 	} while (c>32);
120 
121 	com_token[len] = 0;
122 	return data;
123 }
124 
125 
126 
127 
128 /*
129 ================
130 filelength
131 ================
132 */
133 #ifndef WIN32
filelength(int handle)134 int filelength (int handle)
135 {
136 	struct stat	fileinfo;
137 
138 	if (fstat (handle,&fileinfo) == -1)
139 	{
140 		Error ("Error fstating");
141 	}
142 
143 	return fileinfo.st_size;
144 }
145 
tell(int handle)146 int tell (int handle)
147 {
148 	return lseek (handle, 0, SEEK_CUR);
149 }
150 #endif
151 
strupr(char * start)152 char *strupr (char *start)
153 {
154 	char	*in;
155 	in = start;
156 	while (*in)
157 	{
158 		*in = toupper(*in);
159 		in++;
160 	}
161 	return start;
162 }
163 
strlower(char * start)164 char *strlower (char *start)
165 {
166 	char	*in;
167 	in = start;
168 	while (*in)
169 	{
170 		*in = tolower(*in);
171 		in++;
172 	}
173 	return start;
174 }
175 
176 
177 /*
178 =============================================================================
179 
180 						MISC FUNCTIONS
181 
182 =============================================================================
183 */
184 
185 /*
186 =================
187 Error
188 
189 For abnormal program terminations
190 =================
191 */
Error(char * error,...)192 void Error (char *error, ...)
193 {
194 	va_list argptr;
195 
196 	printf ("\n************ ERROR ************\n");
197 
198 	va_start (argptr,error);
199 	vprintf (error,argptr);
200 	va_end (argptr);
201 	printf ("\n");
202 	exit (1);
203 }
204 
205 
206 /*
207 =================
208 CheckParm
209 
210 Checks for the given parameter in the program's command line arguments
211 Returns the argument number (1 to argc-1) or 0 if not present
212 =================
213 */
CheckParm(char * check)214 int CheckParm (char *check)
215 {
216 	int             i;
217 
218 	for (i = 1;i<myargc;i++)
219 	{
220 #ifdef WIN32
221 		if ( !_stricmp(check, myargv[i]) )
222 			return i;
223 #else
224 		if ( !strcasecmp(check, myargv[i]) )
225 			return i;
226 #endif
227 	}
228 
229 	return 0;
230 }
231 
232 
233 #ifndef O_BINARY
234 #define O_BINARY 0
235 #endif
236 
SafeOpenWrite(char * filename)237 int SafeOpenWrite (char *filename)
238 {
239 	int     handle;
240 
241 	umask (0);
242 
243 	handle = open(filename,O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
244 	, 0666);
245 
246 	if (handle == -1)
247 		Error ("Error opening %s: %s",filename,strerror(errno));
248 
249 	return handle;
250 }
251 
SafeOpenRead(char * filename)252 int SafeOpenRead (char *filename)
253 {
254 	int     handle;
255 
256 	handle = open(filename,O_RDONLY | O_BINARY);
257 
258 	if (handle == -1)
259 		Error ("Error opening %s: %s",filename,strerror(errno));
260 
261 	return handle;
262 }
263 
264 
SafeRead(int handle,void * buffer,long count)265 void SafeRead (int handle, void *buffer, long count)
266 {
267 	if (read (handle,buffer,count) != count)
268 		Error ("File read failure");
269 }
270 
271 
SafeWrite(int handle,void * buffer,long count)272 void SafeWrite (int handle, void *buffer, long count)
273 {
274 	if (write (handle,buffer,count) != count)
275 		Error ("File write failure");
276 }
277 
278 
SafeMalloc(long size)279 void *SafeMalloc (long size)
280 {
281 	void *ptr;
282 
283 	ptr = malloc (size);
284 
285 	if (!ptr)
286 		Error ("Malloc failure for %lu bytes",size);
287 
288 	return ptr;
289 }
290 
291 
292 /*
293 ==============
294 LoadFile
295 ==============
296 */
LoadFile(char * filename,void ** bufferptr)297 long    LoadFile (char *filename, void **bufferptr)
298 {
299 	int             handle;
300 	long    length;
301 	void    *buffer;
302 
303 	handle = SafeOpenRead (filename);
304 	length = filelength (handle);
305 	buffer = SafeMalloc (length+1);
306 	((byte *)buffer)[length] = 0;
307 	SafeRead (handle, buffer, length);
308 	close (handle);
309 
310 	*bufferptr = buffer;
311 	return length;
312 }
313 
314 
315 /*
316 ==============
317 SaveFile
318 ==============
319 */
SaveFile(char * filename,void * buffer,long count)320 void    SaveFile (char *filename, void *buffer, long count)
321 {
322 	int             handle;
323 
324 	handle = SafeOpenWrite (filename);
325 	SafeWrite (handle, buffer, count);
326 	close (handle);
327 }
328 
329 
330 
DefaultExtension(char * path,char * extension)331 void DefaultExtension (char *path, char *extension)
332 {
333 	char    *src;
334 //
335 // if path doesn't have a .EXT, append extension
336 // (extension should include the .)
337 //
338 	src = path + strlen(path) - 1;
339 
340 	while (*src != PATHSEPERATOR && src != path)
341 	{
342 		if (*src == '.')
343 			return;                 // it has an extension
344 		src--;
345 	}
346 
347 	strcat (path, extension);
348 }
349 
350 
DefaultPath(char * path,char * basepath)351 void DefaultPath (char *path, char *basepath)
352 {
353 	char    temp[128];
354 
355 	if (path[0] == PATHSEPERATOR)
356 		return;                   // absolute path location
357 	strcpy (temp,path);
358 	strcpy (path,basepath);
359 	strcat (path,temp);
360 }
361 
362 
StripFilename(char * path)363 void    StripFilename (char *path)
364 {
365 	int             length;
366 
367 	length = strlen(path)-1;
368 	while (length > 0 && path[length] != PATHSEPERATOR)
369 		length--;
370 	path[length] = 0;
371 }
372 
StripExtension(char * path)373 void    StripExtension (char *path)
374 {
375 	int             length;
376 
377 	length = strlen(path)-1;
378 	while (length > 0 && path[length] != '.')
379 	{
380 		length--;
381 		if (path[length] == '/')
382 			return;		// no extension
383 	}
384 	if (length)
385 		path[length] = 0;
386 }
387 
388 
389 /*
390 ====================
391 Extract file parts
392 ====================
393 */
ExtractFilePath(char * path,char * dest)394 void ExtractFilePath (char *path, char *dest)
395 {
396 	char    *src;
397 
398 	src = path + strlen(path) - 1;
399 
400 //
401 // back up until a \ or the start
402 //
403 	while (src != path && *(src-1) != PATHSEPERATOR)
404 		src--;
405 
406 	memcpy (dest, path, src-path);
407 	dest[src-path] = 0;
408 }
409 
ExtractFileBase(char * path,char * dest)410 void ExtractFileBase (char *path, char *dest)
411 {
412 	char    *src;
413 
414 	src = path + strlen(path) - 1;
415 
416 //
417 // back up until a \ or the start
418 //
419 	while (src != path && *(src-1) != PATHSEPERATOR)
420 		src--;
421 
422 	while (*src && *src != '.')
423 	{
424 		*dest++ = *src++;
425 	}
426 	*dest = 0;
427 }
428 
ExtractFileExtension(char * path,char * dest)429 void ExtractFileExtension (char *path, char *dest)
430 {
431 	char    *src;
432 
433 	src = path + strlen(path) - 1;
434 
435 //
436 // back up until a . or the start
437 //
438 	while (src != path && *(src-1) != '.')
439 		src--;
440 	if (src == path)
441 	{
442 		*dest = 0;	// no extension
443 		return;
444 	}
445 
446 	strcpy (dest,src);
447 }
448 
449 
450 /*
451 ==============
452 ParseNum / ParseHex
453 ==============
454 */
ParseHex(char * hex)455 long ParseHex (char *hex)
456 {
457 	char    *str;
458 	long    num;
459 
460 	num = 0;
461 	str = hex;
462 
463 	while (*str)
464 	{
465 		num <<= 4;
466 		if (*str >= '0' && *str <= '9')
467 			num += *str-'0';
468 		else if (*str >= 'a' && *str <= 'f')
469 			num += 10 + *str-'a';
470 		else if (*str >= 'A' && *str <= 'F')
471 			num += 10 + *str-'A';
472 		else
473 			Error ("Bad hex number: %s",hex);
474 		str++;
475 	}
476 
477 	return num;
478 }
479 
480 
ParseNum(char * str)481 long ParseNum (char *str)
482 {
483 	if (str[0] == '$')
484 		return ParseHex (str+1);
485 	if (str[0] == '0' && str[1] == 'x')
486 		return ParseHex (str+2);
487 	return atol (str);
488 }
489 
490 
491 
492 /*
493 ============================================================================
494 
495 					BYTE ORDER FUNCTIONS
496 
497 ============================================================================
498 */
499 
500 #ifndef INLINE
501 
502 #ifdef __BIG_ENDIAN__
503 
LittleShort(short l)504 short   LittleShort (short l)
505 {
506 	byte    b1,b2;
507 
508 	b1 = l&255;
509 	b2 = (l>>8)&255;
510 
511 	return (b1<<8) + b2;
512 }
513 
BigShort(short l)514 short   BigShort (short l)
515 {
516 	return l;
517 }
518 
LittleLong(long l)519 long    LittleLong (long l)
520 {
521 	byte    b1,b2,b3,b4;
522 
523 	b1 = l&255;
524 	b2 = (l>>8)&255;
525 	b3 = (l>>16)&255;
526 	b4 = (l>>24)&255;
527 
528 	return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
529 }
530 
BigLong(long l)531 long    BigLong (long l)
532 {
533 	return l;
534 }
535 
536 
LittleFloat(float l)537 float	LittleFloat (float l)
538 {
539 	union {byte b[4]; float f;} in, out;
540 
541 	in.f = l;
542 	out.b[0] = in.b[3];
543 	out.b[1] = in.b[2];
544 	out.b[2] = in.b[1];
545 	out.b[3] = in.b[0];
546 
547 	return out.f;
548 }
549 
BigFloat(float l)550 float	BigFloat (float l)
551 {
552 	return l;
553 }
554 
555 
556 #else
557 
558 
BigShort(short l)559 short   BigShort (short l)
560 {
561 	byte    b1,b2;
562 
563 	b1 = l&255;
564 	b2 = (l>>8)&255;
565 
566 	return (b1<<8) + b2;
567 }
568 
LittleShort(short l)569 short   LittleShort (short l)
570 {
571 	return l;
572 }
573 
574 
BigLong(long l)575 long    BigLong (long l)
576 {
577 	byte    b1,b2,b3,b4;
578 
579 	b1 = l&255;
580 	b2 = (l>>8)&255;
581 	b3 = (l>>16)&255;
582 	b4 = (l>>24)&255;
583 
584 	return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
585 }
586 
LittleLong(long l)587 long    LittleLong (long l)
588 {
589 	return l;
590 }
591 
BigFloat(float l)592 float	BigFloat (float l)
593 {
594 	union {byte b[4]; float f;} in, out;
595 
596 	in.f = l;
597 	out.b[0] = in.b[3];
598 	out.b[1] = in.b[2];
599 	out.b[2] = in.b[1];
600 	out.b[3] = in.b[0];
601 
602 	return out.f;
603 }
604 
LittleFloat(float l)605 float	LittleFloat (float l)
606 {
607 	return l;
608 }
609 
610 #endif
611 
612 #endif
613