Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

cmdlib.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 // cmdlib.c
00023 
00024 #include "cmdlib.h"
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 
00028 #ifdef WIN32
00029 #include <direct.h>
00030 #include <windows.h>
00031 #endif
00032 
00033 #ifdef NeXT
00034 #include <libc.h>
00035 #endif
00036 
00037 #define BASEDIRNAME "quake"     // assumed to have a 2 or 3 following
00038 #define PATHSEPERATOR   '/'
00039 
00040 // set these before calling CheckParm
00041 int myargc;
00042 char **myargv;
00043 
00044 char        com_token[1024];
00045 qboolean    com_eof;
00046 
00047 qboolean        archive;
00048 char            archivedir[1024];
00049 
00050 
00051 /*
00052 ===================
00053 ExpandWildcards
00054 
00055 Mimic unix command line expansion
00056 ===================
00057 */
00058 #define MAX_EX_ARGC 1024
00059 int     ex_argc;
00060 char    *ex_argv[MAX_EX_ARGC];
00061 #ifdef _WIN32
00062 #include "io.h"
00063 void ExpandWildcards( int *argc, char ***argv )
00064 {
00065     struct _finddata_t fileinfo;
00066     int     handle;
00067     int     i;
00068     char    filename[1024];
00069     char    filebase[1024];
00070     char    *path;
00071 
00072     ex_argc = 0;
00073     for (i=0 ; i<*argc ; i++)
00074     {
00075         path = (*argv)[i];
00076         if ( path[0] == '-'
00077             || ( !strstr(path, "*") && !strstr(path, "?") ) )
00078         {
00079             ex_argv[ex_argc++] = path;
00080             continue;
00081         }
00082 
00083         handle = _findfirst (path, &fileinfo);
00084         if (handle == -1)
00085             return;
00086 
00087         ExtractFilePath (path, filebase);
00088 
00089         do
00090         {
00091             sprintf (filename, "%s%s", filebase, fileinfo.name);
00092             ex_argv[ex_argc++] = copystring (filename);
00093         } while (_findnext( handle, &fileinfo ) != -1);
00094 
00095         _findclose (handle);
00096     }
00097 
00098     *argc = ex_argc;
00099     *argv = ex_argv;
00100 }
00101 #else
00102 void ExpandWildcards (int *argc, char ***argv)
00103 {
00104 }
00105 #endif
00106 
00107 #ifdef WIN_ERROR
00108 #include <windows.h>
00109 /*
00110 =================
00111 Error
00112 
00113 For abnormal program terminations in windowed apps
00114 =================
00115 */
00116 void Error( const char *error, ... )
00117 {
00118     va_list argptr;
00119     char    text[1024];
00120     char    text2[1024];
00121     int     err;
00122 
00123     err = GetLastError ();
00124 
00125     va_start (argptr,error);
00126     vsprintf (text, error,argptr);
00127     va_end (argptr);
00128 
00129     sprintf (text2, "%s\nGetLastError() = %i", text, err);
00130     MessageBox(NULL, text2, "Error", 0 /* MB_OK */ );
00131 
00132     exit (1);
00133 }
00134 
00135 #else
00136 /*
00137 =================
00138 Error
00139 
00140 For abnormal program terminations in console apps
00141 =================
00142 */
00143 void Error( const char *error, ...)
00144 {
00145     va_list argptr;
00146 
00147     _printf ("\n************ ERROR ************\n");
00148 
00149     va_start (argptr,error);
00150     vprintf (error,argptr);
00151     va_end (argptr);
00152     _printf ("\r\n");
00153 
00154     exit (1);
00155 }
00156 #endif
00157 
00158 // only printf if in verbose mode
00159 qboolean verbose = qfalse;
00160 void qprintf( const char *format, ... ) {
00161     va_list argptr;
00162 
00163     if (!verbose)
00164         return;
00165 
00166     va_start (argptr,format);
00167     vprintf (format,argptr);
00168     va_end (argptr);
00169 
00170 }
00171 
00172 #ifdef WIN32
00173 HWND hwndOut = NULL;
00174 qboolean lookedForServer = qfalse;
00175 UINT wm_BroadcastCommand = -1;
00176 #endif
00177 
00178 void _printf( const char *format, ... ) {
00179     va_list argptr;
00180   char text[4096];
00181   ATOM a;
00182 
00183     va_start (argptr,format);
00184     vsprintf (text, format, argptr);
00185     va_end (argptr);
00186 
00187   printf(text);
00188 
00189 #ifdef WIN32
00190   if (!lookedForServer) {
00191     lookedForServer = qtrue;
00192     hwndOut = FindWindow(NULL, "Q3Map Process Server");
00193     if (hwndOut) {
00194       wm_BroadcastCommand = RegisterWindowMessage( "Q3MPS_BroadcastCommand" );
00195     }
00196   }
00197   if (hwndOut) {
00198     a = GlobalAddAtom(text);
00199     PostMessage(hwndOut, wm_BroadcastCommand, 0, (LPARAM)a);
00200   }
00201 #endif
00202 }
00203 
00204 
00205 /*
00206 
00207 qdir will hold the path up to the quake directory, including the slash
00208 
00209   f:\quake\
00210   /raid/quake/
00211 
00212 gamedir will hold qdir + the game directory (id1, id2, etc)
00213 
00214   */
00215 
00216 char        qdir[1024];
00217 char        gamedir[1024];
00218 char        writedir[1024];
00219 
00220 void SetQdirFromPath( const char *path )
00221 {
00222     char    temp[1024];
00223     const char  *c;
00224   const char *sep;
00225     int     len, count;
00226 
00227     if (!(path[0] == '/' || path[0] == '\\' || path[1] == ':'))
00228     {   // path is partial
00229         Q_getwd (temp);
00230         strcat (temp, path);
00231         path = temp;
00232     }
00233 
00234     // search for "quake2" in path
00235 
00236     len = strlen(BASEDIRNAME);
00237     for (c=path+strlen(path)-1 ; c != path ; c--)
00238     {
00239         int i;
00240 
00241         if (!Q_strncasecmp (c, BASEDIRNAME, len))
00242         {
00243       //
00244             //strncpy (qdir, path, c+len+2-path);
00245       // the +2 assumes a 2 or 3 following quake which is not the
00246       // case with a retail install
00247       // so we need to add up how much to the next separator
00248       sep = c + len;
00249       count = 1;
00250       while (*sep && *sep != '/' && *sep != '\\')
00251       {
00252         sep++;
00253         count++;
00254       }
00255             strncpy (qdir, path, c+len+count-path);
00256             qprintf ("qdir: %s\n", qdir);
00257             for ( i = 0; i < strlen( qdir ); i++ )
00258             {
00259                 if ( qdir[i] == '\\' ) 
00260                     qdir[i] = '/';
00261             }
00262 
00263             c += len+count;
00264             while (*c)
00265             {
00266                 if (*c == '/' || *c == '\\')
00267                 {
00268                     strncpy (gamedir, path, c+1-path);
00269 
00270                     for ( i = 0; i < strlen( gamedir ); i++ )
00271                     {
00272                         if ( gamedir[i] == '\\' ) 
00273                             gamedir[i] = '/';
00274                     }
00275 
00276                     qprintf ("gamedir: %s\n", gamedir);
00277 
00278                     if ( !writedir[0] )
00279                         strcpy( writedir, gamedir );
00280                     else if ( writedir[strlen( writedir )-1] != '/' )
00281                     {
00282                         writedir[strlen( writedir )] = '/';
00283                         writedir[strlen( writedir )+1] = 0;
00284                     }
00285 
00286                     return;
00287                 }
00288                 c++;
00289             }
00290             Error ("No gamedir in %s", path);
00291             return;
00292         }
00293     }
00294     Error ("SetQdirFromPath: no '%s' in %s", BASEDIRNAME, path);
00295 }
00296 
00297 char *ExpandArg (const char *path)
00298 {
00299     static char full[1024];
00300 
00301     if (path[0] != '/' && path[0] != '\\' && path[1] != ':')
00302     {
00303         Q_getwd (full);
00304         strcat (full, path);
00305     }
00306     else
00307         strcpy (full, path);
00308     return full;
00309 }
00310 
00311 char *ExpandPath (const char *path)
00312 {
00313     static char full[1024];
00314     if (!qdir)
00315         Error ("ExpandPath called without qdir set");
00316     if (path[0] == '/' || path[0] == '\\' || path[1] == ':') {
00317         strcpy( full, path );
00318         return full;
00319     }
00320     sprintf (full, "%s%s", qdir, path);
00321     return full;
00322 }
00323 
00324 char *ExpandGamePath (const char *path)
00325 {
00326     static char full[1024];
00327     if (!qdir)
00328         Error ("ExpandGamePath called without qdir set");
00329     if (path[0] == '/' || path[0] == '\\' || path[1] == ':') {
00330         strcpy( full, path );
00331         return full;
00332     }
00333     sprintf (full, "%s%s", gamedir, path);
00334     return full;
00335 }
00336 
00337 char *ExpandPathAndArchive (const char *path)
00338 {
00339     char    *expanded;
00340     char    archivename[1024];
00341 
00342     expanded = ExpandPath (path);
00343 
00344     if (archive)
00345     {
00346         sprintf (archivename, "%s/%s", archivedir, path);
00347         QCopyFile (expanded, archivename);
00348     }
00349     return expanded;
00350 }
00351 
00352 
00353 char *copystring(const char *s)
00354 {
00355     char    *b;
00356     b = malloc(strlen(s)+1);
00357     strcpy (b, s);
00358     return b;
00359 }
00360 
00361 
00362 
00363 /*
00364 ================
00365 I_FloatTime
00366 ================
00367 */
00368 double I_FloatTime (void)
00369 {
00370     time_t  t;
00371     
00372     time (&t);
00373     
00374     return t;
00375 #if 0
00376 // more precise, less portable
00377     struct timeval tp;
00378     struct timezone tzp;
00379     static int      secbase;
00380 
00381     gettimeofday(&tp, &tzp);
00382     
00383     if (!secbase)
00384     {
00385         secbase = tp.tv_sec;
00386         return tp.tv_usec/1000000.0;
00387     }
00388     
00389     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
00390 #endif
00391 }
00392 
00393 void Q_getwd (char *out)
00394 {
00395     int i = 0;
00396 
00397 #ifdef WIN32
00398    _getcwd (out, 256);
00399    strcat (out, "\\");
00400 #else
00401    getwd (out);
00402    strcat (out, "/");
00403 #endif
00404 
00405    while ( out[i] != 0 )
00406    {
00407        if ( out[i] == '\\' )
00408            out[i] = '/';
00409        i++;
00410    }
00411 }
00412 
00413 
00414 void Q_mkdir (const char *path)
00415 {
00416 #ifdef WIN32
00417     if (_mkdir (path) != -1)
00418         return;
00419 #else
00420     if (mkdir (path, 0777) != -1)
00421         return;
00422 #endif
00423     if (errno != EEXIST)
00424         Error ("mkdir %s: %s",path, strerror(errno));
00425 }
00426 
00427 /*
00428 ============
00429 FileTime
00430 
00431 returns -1 if not present
00432 ============
00433 */
00434 int FileTime (const char *path)
00435 {
00436     struct  stat    buf;
00437     
00438     if (stat (path,&buf) == -1)
00439         return -1;
00440     
00441     return buf.st_mtime;
00442 }
00443 
00444 
00445 
00446 /*
00447 ==============
00448 COM_Parse
00449 
00450 Parse a token out of a string
00451 ==============
00452 */
00453 char *COM_Parse (char *data)
00454 {
00455     int     c;
00456     int     len;
00457     
00458     len = 0;
00459     com_token[0] = 0;
00460     
00461     if (!data)
00462         return NULL;
00463         
00464 // skip whitespace
00465 skipwhite:
00466     while ( (c = *data) <= ' ')
00467     {
00468         if (c == 0)
00469         {
00470             com_eof = qtrue;
00471             return NULL;            // end of file;
00472         }
00473         data++;
00474     }
00475     
00476 // skip // comments
00477     if (c=='/' && data[1] == '/')
00478     {
00479         while (*data && *data != '\n')
00480             data++;
00481         goto skipwhite;
00482     }
00483     
00484 
00485 // handle quoted strings specially
00486     if (c == '\"')
00487     {
00488         data++;
00489         do
00490         {
00491             c = *data++;
00492             if (c=='\"')
00493             {
00494                 com_token[len] = 0;
00495                 return data;
00496             }
00497             com_token[len] = c;
00498             len++;
00499         } while (1);
00500     }
00501 
00502 // parse single characters
00503     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
00504     {
00505         com_token[len] = c;
00506         len++;
00507         com_token[len] = 0;
00508         return data+1;
00509     }
00510 
00511 // parse a regular word
00512     do
00513     {
00514         com_token[len] = c;
00515         data++;
00516         len++;
00517         c = *data;
00518     if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
00519             break;
00520     } while (c>32);
00521     
00522     com_token[len] = 0;
00523     return data;
00524 }
00525 
00526 
00527 int Q_strncasecmp (const char *s1, const char *s2, int n)
00528 {
00529     int     c1, c2;
00530     
00531     do
00532     {
00533         c1 = *s1++;
00534         c2 = *s2++;
00535 
00536         if (!n--)
00537             return 0;       // strings are equal until end point
00538         
00539         if (c1 != c2)
00540         {
00541             if (c1 >= 'a' && c1 <= 'z')
00542                 c1 -= ('a' - 'A');
00543             if (c2 >= 'a' && c2 <= 'z')
00544                 c2 -= ('a' - 'A');
00545             if (c1 != c2)
00546                 return -1;      // strings not equal
00547         }
00548     } while (c1);
00549     
00550     return 0;       // strings are equal
00551 }
00552 
00553 int Q_stricmp (const char *s1, const char *s2)
00554 {
00555     return Q_strncasecmp (s1, s2, 99999);
00556 }
00557 
00558 
00559 char *strupr (char *start)
00560 {
00561     char    *in;
00562     in = start;
00563     while (*in)
00564     {
00565         *in = toupper(*in);
00566         in++;
00567     }
00568     return start;
00569 }
00570 
00571 char *strlower (char *start)
00572 {
00573     char    *in;
00574     in = start;
00575     while (*in)
00576     {
00577         *in = tolower(*in); 
00578         in++;
00579     }
00580     return start;
00581 }
00582 
00583 
00584 /*
00585 =============================================================================
00586 
00587                         MISC FUNCTIONS
00588 
00589 =============================================================================
00590 */
00591 
00592 
00593 /*
00594 =================
00595 CheckParm
00596 
00597 Checks for the given parameter in the program's command line arguments
00598 Returns the argument number (1 to argc-1) or 0 if not present
00599 =================
00600 */
00601 int CheckParm (const char *check)
00602 {
00603     int             i;
00604 
00605     for (i = 1;i<myargc;i++)
00606     {
00607         if ( !Q_stricmp(check, myargv[i]) )
00608             return i;
00609     }
00610 
00611     return 0;
00612 }
00613 
00614 
00615 
00616 /*
00617 ================
00618 Q_filelength
00619 ================
00620 */
00621 int Q_filelength (FILE *f)
00622 {
00623     int     pos;
00624     int     end;
00625 
00626     pos = ftell (f);
00627     fseek (f, 0, SEEK_END);
00628     end = ftell (f);
00629     fseek (f, pos, SEEK_SET);
00630 
00631     return end;
00632 }
00633 
00634 
00635 FILE *SafeOpenWrite (const char *filename)
00636 {
00637     FILE    *f;
00638 
00639     f = fopen(filename, "wb");
00640 
00641     if (!f)
00642         Error ("Error opening %s: %s",filename,strerror(errno));
00643 
00644     return f;
00645 }
00646 
00647 FILE *SafeOpenRead (const char *filename)
00648 {
00649     FILE    *f;
00650 
00651     f = fopen(filename, "rb");
00652 
00653     if (!f)
00654         Error ("Error opening %s: %s",filename,strerror(errno));
00655 
00656     return f;
00657 }
00658 
00659 
00660 void SafeRead (FILE *f, void *buffer, int count)
00661 {
00662     if ( fread (buffer, 1, count, f) != (size_t)count)
00663         Error ("File read failure");
00664 }
00665 
00666 
00667 void SafeWrite (FILE *f, const void *buffer, int count)
00668 {
00669     if (fwrite (buffer, 1, count, f) != (size_t)count)
00670         Error ("File write failure");
00671 }
00672 
00673 
00674 /*
00675 ==============
00676 FileExists
00677 ==============
00678 */
00679 qboolean    FileExists (const char *filename)
00680 {
00681     FILE    *f;
00682 
00683     f = fopen (filename, "r");
00684     if (!f)
00685         return qfalse;
00686     fclose (f);
00687     return qtrue;
00688 }
00689 
00690 /*
00691 ==============
00692 LoadFile
00693 ==============
00694 */
00695 int    LoadFile( const char *filename, void **bufferptr )
00696 {
00697     FILE    *f;
00698     int    length;
00699     void    *buffer;
00700 
00701     f = SafeOpenRead (filename);
00702     length = Q_filelength (f);
00703     buffer = malloc (length+1);
00704     ((char *)buffer)[length] = 0;
00705     SafeRead (f, buffer, length);
00706     fclose (f);
00707 
00708     *bufferptr = buffer;
00709     return length;
00710 }
00711 
00712 
00713 /*
00714 ==============
00715 LoadFileBlock
00716 -
00717 rounds up memory allocation to 4K boundry
00718 -
00719 ==============
00720 */
00721 int    LoadFileBlock( const char *filename, void **bufferptr )
00722 {
00723     FILE    *f;
00724     int    length, nBlock, nAllocSize;
00725     void    *buffer;
00726 
00727     f = SafeOpenRead (filename);
00728     length = Q_filelength (f);
00729   nAllocSize = length;
00730   nBlock = nAllocSize % MEM_BLOCKSIZE;
00731   if ( nBlock > 0) {
00732     nAllocSize += MEM_BLOCKSIZE - nBlock;
00733   }
00734     buffer = malloc (nAllocSize+1);
00735   memset(buffer, 0, nAllocSize+1);
00736     SafeRead (f, buffer, length);
00737     fclose (f);
00738 
00739     *bufferptr = buffer;
00740     return length;
00741 }
00742 
00743 
00744 /*
00745 ==============
00746 TryLoadFile
00747 
00748 Allows failure
00749 ==============
00750 */
00751 int    TryLoadFile (const char *filename, void **bufferptr)
00752 {
00753     FILE    *f;
00754     int    length;
00755     void    *buffer;
00756 
00757     *bufferptr = NULL;
00758 
00759     f = fopen (filename, "rb");
00760     if (!f)
00761         return -1;
00762     length = Q_filelength (f);
00763     buffer = malloc (length+1);
00764     ((char *)buffer)[length] = 0;
00765     SafeRead (f, buffer, length);
00766     fclose (f);
00767 
00768     *bufferptr = buffer;
00769     return length;
00770 }
00771 
00772 
00773 /*
00774 ==============
00775 SaveFile
00776 ==============
00777 */
00778 void    SaveFile (const char *filename, const void *buffer, int count)
00779 {
00780     FILE    *f;
00781 
00782     f = SafeOpenWrite (filename);
00783     SafeWrite (f, buffer, count);
00784     fclose (f);
00785 }
00786 
00787 
00788 
00789 void DefaultExtension (char *path, const char *extension)
00790 {
00791     char    *src;
00792 //
00793 // if path doesnt have a .EXT, append extension
00794 // (extension should include the .)
00795 //
00796     src = path + strlen(path) - 1;
00797 
00798     while (*src != '/' && *src != '\\' && src != path)
00799     {
00800         if (*src == '.')
00801             return;                 // it has an extension
00802         src--;
00803     }
00804 
00805     strcat (path, extension);
00806 }
00807 
00808 
00809 void DefaultPath (char *path, const char *basepath)
00810 {
00811     char    temp[128];
00812 
00813     if (path[0] == PATHSEPERATOR)
00814         return;                   // absolute path location
00815     strcpy (temp,path);
00816     strcpy (path,basepath);
00817     strcat (path,temp);
00818 }
00819 
00820 
00821 void    StripFilename (char *path)
00822 {
00823     int             length;
00824 
00825     length = strlen(path)-1;
00826     while (length > 0 && path[length] != PATHSEPERATOR)
00827         length--;
00828     path[length] = 0;
00829 }
00830 
00831 void    StripExtension (char *path)
00832 {
00833     int             length;
00834 
00835     length = strlen(path)-1;
00836     while (length > 0 && path[length] != '.')
00837     {
00838         length--;
00839         if (path[length] == '/')
00840             return;     // no extension
00841     }
00842     if (length)
00843         path[length] = 0;
00844 }
00845 
00846 
00847 /*
00848 ====================
00849 Extract file parts
00850 ====================
00851 */
00852 // FIXME: should include the slash, otherwise
00853 // backing to an empty path will be wrong when appending a slash
00854 void ExtractFilePath (const char *path, char *dest)
00855 {
00856     const char    *src;
00857 
00858     src = path + strlen(path) - 1;
00859 
00860 //
00861 // back up until a \ or the start
00862 //
00863     while (src != path && *(src-1) != '\\' && *(src-1) != '/')
00864         src--;
00865 
00866     memcpy (dest, path, src-path);
00867     dest[src-path] = 0;
00868 }
00869 
00870 void ExtractFileBase (const char *path, char *dest)
00871 {
00872     const char    *src;
00873 
00874     src = path + strlen(path) - 1;
00875 
00876 //
00877 // back up until a \ or the start
00878 //
00879     while (src != path && *(src-1) != PATHSEPERATOR)
00880         src--;
00881 
00882     while (*src && *src != '.')
00883     {
00884         *dest++ = *src++;
00885     }
00886     *dest = 0;
00887 }
00888 
00889 void ExtractFileExtension (const char *path, char *dest)
00890 {
00891     const char    *src;
00892 
00893     src = path + strlen(path) - 1;
00894 
00895 //
00896 // back up until a . or the start
00897 //
00898     while (src != path && *(src-1) != '.')
00899         src--;
00900     if (src == path)
00901     {
00902         *dest = 0;  // no extension
00903         return;
00904     }
00905 
00906     strcpy (dest,src);
00907 }
00908 
00909 
00910 /*
00911 ==============
00912 ParseNum / ParseHex
00913 ==============
00914 */
00915 int ParseHex (const char *hex)
00916 {
00917     const char    *str;
00918     int    num;
00919 
00920     num = 0;
00921     str = hex;
00922 
00923     while (*str)
00924     {
00925         num <<= 4;
00926         if (*str >= '0' && *str <= '9')
00927             num += *str-'0';
00928         else if (*str >= 'a' && *str <= 'f')
00929             num += 10 + *str-'a';
00930         else if (*str >= 'A' && *str <= 'F')
00931             num += 10 + *str-'A';
00932         else
00933             Error ("Bad hex number: %s",hex);
00934         str++;
00935     }
00936 
00937     return num;
00938 }
00939 
00940 
00941 int ParseNum (const char *str)
00942 {
00943     if (str[0] == '$')
00944         return ParseHex (str+1);
00945     if (str[0] == '0' && str[1] == 'x')
00946         return ParseHex (str+2);
00947     return atol (str);
00948 }
00949 
00950 
00951 
00952 /*
00953 ============================================================================
00954 
00955                     BYTE ORDER FUNCTIONS
00956 
00957 ============================================================================
00958 */
00959 
00960 #ifdef _SGI_SOURCE
00961 #define __BIG_ENDIAN__
00962 #endif
00963 
00964 #ifdef __BIG_ENDIAN__
00965 
00966 short   LittleShort (short l)
00967 {
00968     byte    b1,b2;
00969 
00970     b1 = l&255;
00971     b2 = (l>>8)&255;
00972 
00973     return (b1<<8) + b2;
00974 }
00975 
00976 short   BigShort (short l)
00977 {
00978     return l;
00979 }
00980 
00981 
00982 int    LittleLong (int l)
00983 {
00984     byte    b1,b2,b3,b4;
00985 
00986     b1 = l&255;
00987     b2 = (l>>8)&255;
00988     b3 = (l>>16)&255;
00989     b4 = (l>>24)&255;
00990 
00991     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
00992 }
00993 
00994 int    BigLong (int l)
00995 {
00996     return l;
00997 }
00998 
00999 
01000 float   LittleFloat (float l)
01001 {
01002     union {byte b[4]; float f;} in, out;
01003     
01004     in.f = l;
01005     out.b[0] = in.b[3];
01006     out.b[1] = in.b[2];
01007     out.b[2] = in.b[1];
01008     out.b[3] = in.b[0];
01009     
01010     return out.f;
01011 }
01012 
01013 float   BigFloat (float l)
01014 {
01015     return l;
01016 }
01017 
01018 
01019 #else
01020 
01021 
01022 short   BigShort (short l)
01023 {
01024     byte    b1,b2;
01025 
01026     b1 = l&255;
01027     b2 = (l>>8)&255;
01028 
01029     return (b1<<8) + b2;
01030 }
01031 
01032 short   LittleShort (short l)
01033 {
01034     return l;
01035 }
01036 
01037 
01038 int    BigLong (int l)
01039 {
01040     byte    b1,b2,b3,b4;
01041 
01042     b1 = l&255;
01043     b2 = (l>>8)&255;
01044     b3 = (l>>16)&255;
01045     b4 = (l>>24)&255;
01046 
01047     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
01048 }
01049 
01050 int    LittleLong (int l)
01051 {
01052     return l;
01053 }
01054 
01055 float   BigFloat (float l)
01056 {
01057     union {byte b[4]; float f;} in, out;
01058     
01059     in.f = l;
01060     out.b[0] = in.b[3];
01061     out.b[1] = in.b[2];
01062     out.b[2] = in.b[1];
01063     out.b[3] = in.b[0];
01064     
01065     return out.f;
01066 }
01067 
01068 float   LittleFloat (float l)
01069 {
01070     return l;
01071 }
01072 
01073 
01074 #endif
01075 
01076 
01077 //=======================================================
01078 
01079 
01080 // FIXME: byte swap?
01081 
01082 // this is a 16 bit, non-reflected CRC using the polynomial 0x1021
01083 // and the initial and final xor values shown below...  in other words, the
01084 // CCITT standard CRC used by XMODEM
01085 
01086 #define CRC_INIT_VALUE  0xffff
01087 #define CRC_XOR_VALUE   0x0000
01088 
01089 static unsigned short crctable[256] =
01090 {
01091     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
01092     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
01093     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
01094     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
01095     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
01096     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
01097     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
01098     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
01099     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
01100     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
01101     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
01102     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
01103     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
01104     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
01105     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
01106     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
01107     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
01108     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
01109     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
01110     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
01111     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
01112     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
01113     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
01114     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
01115     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
01116     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
01117     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
01118     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
01119     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
01120     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
01121     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
01122     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
01123 };
01124 
01125 void CRC_Init(unsigned short *crcvalue)
01126 {
01127     *crcvalue = CRC_INIT_VALUE;
01128 }
01129 
01130 void CRC_ProcessByte(unsigned short *crcvalue, byte data)
01131 {
01132     *crcvalue = (*crcvalue << 8) ^ crctable[(*crcvalue >> 8) ^ data];
01133 }
01134 
01135 unsigned short CRC_Value(unsigned short crcvalue)
01136 {
01137     return crcvalue ^ CRC_XOR_VALUE;
01138 }
01139 //=============================================================================
01140 
01141 /*
01142 ============
01143 CreatePath
01144 ============
01145 */
01146 void    CreatePath (const char *path)
01147 {
01148     const char  *ofs;
01149     char        c;
01150     char        dir[1024];
01151 
01152 #ifdef _WIN32
01153     int     olddrive = -1;
01154 
01155     if ( path[1] == ':' )
01156     {
01157         olddrive = _getdrive();
01158         _chdrive( toupper( path[0] ) - 'A' + 1 );
01159     }
01160 #endif
01161 
01162     if (path[1] == ':')
01163         path += 2;
01164 
01165     for (ofs = path+1 ; *ofs ; ofs++)
01166     {
01167         c = *ofs;
01168         if (c == '/' || c == '\\')
01169         {   // create the directory
01170             memcpy( dir, path, ofs - path );
01171             dir[ ofs - path ] = 0;
01172             Q_mkdir( dir );
01173         }
01174     }
01175 
01176 #ifdef _WIN32
01177     if ( olddrive != -1 )
01178     {
01179         _chdrive( olddrive );
01180     }
01181 #endif
01182 }
01183 
01184 
01185 /*
01186 ============
01187 QCopyFile
01188 
01189   Used to archive source files
01190 ============
01191 */
01192 void QCopyFile (const char *from, const char *to)
01193 {
01194     void    *buffer;
01195     int     length;
01196 
01197     length = LoadFile (from, &buffer);
01198     CreatePath (to);
01199     SaveFile (to, buffer, length);
01200     free (buffer);
01201 }

Generated on Thu Aug 25 12:38:07 2005 for Quake III Arena by  doxygen 1.3.9.1