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

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

Generated on Thu Aug 25 12:37:16 2005 for Quake III Arena by  doxygen 1.3.9.1