home *** CD-ROM | disk | FTP | other *** search
- // cmdlib.c
-
- #define __LITTLE_ENDIAN__
-
- #include "cmdlib.h"
- #include "sys\stat.h"
- #include <fcntl.h>
-
- #define PATHSEPERATOR '/'
-
- char com_token[1024];
- int com_eof;
-
- /*
- ==============
- COM_Parse
-
- Parse a token out of a string
- ==============
- */
- char *COM_Parse (char *data)
- {
- int c;
- int len;
-
- len = 0;
- com_token[0] = 0;
-
- if (!data)
- return NULL;
-
- // skip whitespace
- skipwhite:
- while ( (c = *data) <= ' ')
- {
- if (c == 0)
- {
- com_eof = true;
- return NULL; // end of file;
- }
- data++;
- }
-
- // skip // comments
- if (c=='/' && data[1] == '/')
- {
- while (*data && *data != '\n')
- data++;
- goto skipwhite;
- }
-
-
- // handle quoted strings specially
- if (c == '\"')
- {
- data++;
- do
- {
- c = *data++;
- if (c=='\"')
- {
- com_token[len] = 0;
- return data;
- }
- com_token[len] = c;
- len++;
- } while (1);
- }
-
- // parse single characters
- if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
- {
- com_token[len] = c;
- len++;
- com_token[len] = 0;
- return data+1;
- }
-
- // parse a regular word
- do
- {
- com_token[len] = c;
- data++;
- len++;
- c = *data;
- if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
- break;
- } while (c>32);
-
- com_token[len] = 0;
- return data;
- }
-
-
- /*
- ================
- =
- = filelength
- =
- ================
- */
-
- int filelength (int handle)
- {
- struct stat fileinfo;
-
- if (fstat (handle,&fileinfo) == -1)
- {
- fprintf (stderr,"Error fstating");
- exit (1);
- }
-
- return fileinfo.st_size;
- }
-
- int tell (int handle)
- {
- return lseek (handle, 0, 0);
- }
-
- char *strupr (char *start)
- {
- char *in;
- in = start;
- while (*in)
- {
- *in = toupper(*in);
- in++;
- }
- return start;
- }
-
- char *strlower (char *start)
- {
- char *in;
- in = start;
- while (*in)
- {
- *in = tolower(*in);
- in++;
- }
- return start;
- }
-
- #ifdef NeXT
- char *getcwd (char *path, int length)
- {
- return getwd(path);
- }
- #endif
-
- /* globals for command line args */
- extern int NXArgc;
- extern char **NXArgv;
- #define myargc NXArgc
- #define myargv NXArgv
-
- #ifndef NeXT
- int NXArgc;
- char **NXArgv;
- #endif
-
-
- /*
- =============================================================================
-
- MISC FUNCTIONS
-
- =============================================================================
- */
-
- /*
- =================
- =
- = Error
- =
- = For abnormal program terminations
- =
- =================
- */
-
- void Error (char *error, ...)
- {
- va_list argptr;
-
- printf ("\n\nError: ");
- va_start (argptr,error);
- vprintf (error,argptr);
- va_end (argptr);
- printf ("\n");
- exit (1);
- }
-
-
- /*
- =================
- =
- = CheckParm
- =
- = Checks for the given parameter in the program's command line arguments
- =
- = Returns the argument number (1 to argc-1) or 0 if not present
- =
- =================
- */
-
- int CheckParm (char *check)
- {
- int i;
-
- for (i = 1;i<myargc;i++)
- {
- if ( !stricmp(check, myargv[i]) )
- return i;
- }
-
- return 0;
- }
-
-
-
-
- int SafeOpenWrite (char *filename)
- {
- int handle;
-
- umask (0);
-
- handle = open(filename,O_RDWR | O_CREAT | O_TRUNC
- , 0666);
-
- if (handle == -1)
- Error ("Error opening %s: %s",filename,strerror(errno));
-
- return handle;
- }
-
- int SafeOpenRead (char *filename)
- {
- int handle;
-
- handle = open(filename,O_RDONLY);
-
- if (handle == -1)
- Error ("Error opening %s: %s",filename,strerror(errno));
-
- return handle;
- }
-
-
- void SafeRead (int handle, void *buffer, int count)
- {
- int iocount;
-
- iocount = read (handle,buffer,count);
- if (iocount != count)
- Error ("File read failure");
- }
-
-
- void SafeWrite (int handle, void *buffer, int count)
- {
- int iocount;
-
- iocount = write (handle,buffer,count);
- if (iocount != count)
- Error ("File write failure");
- }
-
-
- void *SafeMalloc (int size)
- {
- void *ptr;
-
- ptr = malloc (size);
-
- if (!ptr)
- Error ("Malloc failure for %lu bytes",size);
-
- return ptr;
- }
-
-
- /*
- ==============
- =
- = LoadFile
- =
- ==============
- */
-
- int LoadFile (char *filename, void **bufferptr)
- {
- int handle;
- int length;
- void *buffer;
-
- handle = SafeOpenRead (filename);
- length = filelength (handle);
- buffer = SafeMalloc (length+1);
- SafeRead (handle, buffer, length);
- close (handle);
- ((byte *)buffer)[length] = 0;
-
- *bufferptr = buffer;
- return length;
- }
-
-
- /*
- ==============
- =
- = SaveFile
- =
- ==============
- */
-
- void SaveFile (char *filename, void *buffer, int count)
- {
- int handle;
-
- handle = SafeOpenWrite (filename);
- SafeWrite (handle, buffer, count);
- close (handle);
- }
-
-
-
- void DefaultExtension (char *path, char *extension)
- {
- char *src;
- //
- // if path doesn't have a .EXT, append extension
- // (extension should include the .)
- //
- src = path + strlen(path) - 1;
-
- while (*src != PATHSEPERATOR && src != path)
- {
- if (*src == '.')
- return; // it has an extension
- src--;
- }
-
- strcat (path, extension);
- }
-
-
- void DefaultPath (char *path, char *basepath)
- {
- char temp[128];
-
- if (path[0] == PATHSEPERATOR)
- return; // absolute path location
- strcpy (temp,path);
- strcpy (path,basepath);
- strcat (path,temp);
- }
-
-
- void StripFilename (char *path)
- {
- int length;
-
- length = strlen(path)-1;
- while (length > 0 && path[length] != PATHSEPERATOR)
- length--;
- path[length] = 0;
- }
-
- void StripExtension (char *path)
- {
- int length;
-
- // FIXME: this fucks up on a file like ../gfz/aztec
- length = strlen(path)-1;
- while (length > 0 && path[length] != '.')
- length--;
- if (length)
- path[length] = 0;
- }
-
-
- /*
- ====================
- =
- = Extract file parts
- =
- ====================
- */
-
- void ExtractFilePath (char *path, char *dest)
- {
- char *src;
-
- src = path + strlen(path) - 1;
-
- //
- // back up until a \ or the start
- //
- while (src != path && *(src-1) != PATHSEPERATOR)
- src--;
-
- memcpy (dest, path, src-path);
- dest[src-path] = 0;
- }
-
- void ExtractFileBase (char *path, char *dest)
- {
- char *src;
-
- src = path + strlen(path) - 1;
-
- //
- // back up until a \ or the start
- //
- while (src != path && *(src-1) != PATHSEPERATOR)
- src--;
-
- while (*src && *src != '.')
- {
- *dest++ = *src++;
- }
- *dest = 0;
- }
-
- void ExtractFileExtension (char *path, char *dest)
- {
- char *src;
-
- src = path + strlen(path) - 1;
-
- //
- // back up until a . or the start
- //
- while (src != path && *(src-1) != '.')
- src--;
- if (src == path)
- {
- *dest = 0; // no extension
- return;
- }
-
- strcpy (dest,src);
- }
-
-
- /*
- ==============
- =
- = ParseNum / ParseHex
- =
- ==============
- */
-
- int ParseHex (char *hex)
- {
- char *str;
- int num;
-
- num = 0;
- str = hex;
-
- while (*str)
- {
- num <<= 4;
- if (*str >= '0' && *str <= '9')
- num += *str-'0';
- else if (*str >= 'a' && *str <= 'f')
- num += 10 + *str-'a';
- else if (*str >= 'A' && *str <= 'F')
- num += 10 + *str-'A';
- else
- Error ("Bad hex number: %s",hex);
- str++;
- }
-
- return num;
- }
-
-
- int ParseNum (char *str)
- {
- if (str[0] == '$')
- return ParseHex (str+1);
- if (str[0] == '0' && str[1] == 'x')
- return ParseHex (str+2);
- return atol (str);
- }
-
-
- int GetKey (void)
- {
- return getchar ();
- }
-
-
- /*
- ============================================================================
-
- BYTE ORDER FUNCTIONS
-
- ============================================================================
- */
-
- #ifdef __alpha
- #define __LITTLE_ENDIAN__
- #endif
-
-
- #ifdef __BIG_ENDIAN__
-
- short LittleShort (short l)
- {
- byte b1,b2;
-
- b1 = l&255;
- b2 = (l>>8)&255;
-
- return (b1<<8) + b2;
- }
-
- short BigShort (short l)
- {
- return l;
- }
-
-
- int LittleLong (int l)
- {
- byte b1,b2,b3,b4;
-
- b1 = l&255;
- b2 = (l>>8)&255;
- b3 = (l>>16)&255;
- b4 = (l>>24)&255;
-
- return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
- }
-
- int BigLong (int l)
- {
- printf("HEY!! THIS SHOULDN'T BE CALLED!!!\n");
- return l;
- }
-
-
- float LittleFloat (float l)
- {
- union {byte b[4]; float f;} in, out;
-
- in.f = l;
- out.b[0] = in.b[3];
- out.b[1] = in.b[2];
- out.b[2] = in.b[1];
- out.b[3] = in.b[0];
-
- return out.f;
- }
-
- float BigFloat (float l)
- {
- return l;
- }
-
- #endif
-
- #ifdef __LITTLE_ENDIAN__
-
- short BigShort (short l)
- {
- byte b1,b2;
-
- b1 = l&255;
- b2 = (l>>8)&255;
-
- return (b1<<8) + b2;
- }
-
- short LittleShort (short l)
- {
- return l;
- }
-
- unsigned long BigLong(unsigned long val) {return ((val & 0x000000FF) << 24) | ((val & 0xFF00) << 8) | ((val & 0x00FF0000) >> 8) | (val >> 24);}
-
- /* long BigLong(long a) {
- long f, g, r;
-
- printf("---> BigLong() was called with a value of: %lu\n", a);
- f = a >> 16;
- g = a & 0x0000FFFF;
- r = (g << 16) + f;
- printf("---> BigLong() returned: %lu\n", r);
- return r;
- } */
-
- /*
- long BigLong (int l)
- {
- byte b1,b2,b3,b4;
- long duh;
-
- b1 = l&255;
- b2 = (l>>8)&255;
- b3 = (l>>16)&255;
- b4 = (l>>24)&255;
- duh = (long) (((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4);
- return duh;
- }
- */
-
- int LittleLong (int l)
- {
- return l;
- }
-
- float BigFloat (float l)
- {
- union {byte b[4]; float f;} in, out;
-
- in.f = l;
- out.b[0] = in.b[3];
- out.b[1] = in.b[2];
- out.b[2] = in.b[1];
- out.b[3] = in.b[0];
-
- return out.f;
- }
-
- float LittleFloat (float l)
- {
- return l;
- }
-
-
-
- #endif
-
-