cmdlib.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. #include "cmdlib.h"
  24. #define PATHSEPERATOR '/'
  25. // rad additions
  26. // 11.29.99
  27. PFN_ERR *g_pfnError = NULL;
  28. PFN_PRINTF *g_pfnPrintf = NULL;
  29. PFN_ERR_NUM *g_pfnErrorNum = NULL;
  30. PFN_PRINTF_NUM *g_pfnPrintfNum = NULL;
  31. void Error(const char *pFormat, ...)
  32. {
  33. if (g_pfnError)
  34. {
  35. va_list arg_ptr;
  36. va_start(arg_ptr, pFormat);
  37. g_pfnError(pFormat, arg_ptr);
  38. va_end(arg_ptr);
  39. }
  40. }
  41. void Printf(const char *pFormat, ...)
  42. {
  43. if (g_pfnPrintf)
  44. {
  45. va_list arg_ptr;
  46. va_start(arg_ptr, pFormat);
  47. g_pfnPrintf(pFormat, arg_ptr);
  48. va_end(arg_ptr);
  49. }
  50. }
  51. void ErrorNum(int nErr, const char *pFormat, ...)
  52. {
  53. if (g_pfnErrorNum)
  54. {
  55. va_list arg_ptr;
  56. va_start(arg_ptr, pFormat);
  57. g_pfnErrorNum(nErr, pFormat, arg_ptr);
  58. va_end(arg_ptr);
  59. }
  60. }
  61. void PrintfNum(int nErr, const char *pFormat, ...)
  62. {
  63. if (g_pfnPrintfNum)
  64. {
  65. va_list arg_ptr;
  66. va_start(arg_ptr, pFormat);
  67. g_pfnPrintfNum(nErr, pFormat, arg_ptr);
  68. va_end(arg_ptr);
  69. }
  70. }
  71. void SetErrorHandler(PFN_ERR pe)
  72. {
  73. g_pfnError = pe;
  74. }
  75. void SetPrintfHandler(PFN_PRINTF pe)
  76. {
  77. g_pfnPrintf = pe;
  78. }
  79. void SetErrorHandlerNum(PFN_ERR_NUM pe)
  80. {
  81. g_pfnErrorNum = pe;
  82. }
  83. void SetPrintfHandler(PFN_PRINTF_NUM pe)
  84. {
  85. g_pfnPrintfNum = pe;
  86. }
  87. /*
  88. ================
  89. Q_filelength
  90. ================
  91. */
  92. int Q_filelength (FILE *f)
  93. {
  94. int pos;
  95. int end;
  96. pos = ftell (f);
  97. fseek (f, 0, SEEK_END);
  98. end = ftell (f);
  99. fseek (f, pos, SEEK_SET);
  100. return end;
  101. }
  102. /*
  103. ==============
  104. LoadFile
  105. ==============
  106. */
  107. int LoadFile (const char *filename, void **bufferptr)
  108. {
  109. FILE *f;
  110. int length;
  111. void *buffer;
  112. *bufferptr = NULL;
  113. if ( filename == NULL || strlen(filename) == 0 ) {
  114. return -1;
  115. }
  116. f = fopen( filename, "rb" );
  117. if ( !f ) {
  118. return -1;
  119. }
  120. length = Q_filelength( f );
  121. buffer = Mem_ClearedAlloc( length+1 );
  122. ((char *)buffer)[length] = 0;
  123. if ( (int)fread( buffer, 1, length, f ) != length ) {
  124. Error( "File read failure" );
  125. }
  126. fclose( f );
  127. *bufferptr = buffer;
  128. return length;
  129. }
  130. /*
  131. ==============
  132. DefaultExtension
  133. ==============
  134. */
  135. void DefaultExtension (char *path, char *extension)
  136. {
  137. char *src;
  138. //
  139. // if path doesn't have a .EXT, append extension
  140. // (extension should include the .)
  141. //
  142. src = path + strlen(path) - 1;
  143. while (*src != PATHSEPERATOR && src != path)
  144. {
  145. if (*src == '.')
  146. return; // it has an extension
  147. src--;
  148. }
  149. strcat (path, extension);
  150. }
  151. /*
  152. ==============
  153. DefaultPath
  154. ==============
  155. */
  156. void DefaultPath (char *path, char *basepath)
  157. {
  158. char temp[128];
  159. if (path[0] == PATHSEPERATOR)
  160. return; // absolute path location
  161. strcpy (temp,path);
  162. strcpy (path,basepath);
  163. strcat (path,temp);
  164. }
  165. /*
  166. ==============
  167. StripFilename
  168. ==============
  169. */
  170. void StripFilename (char *path)
  171. {
  172. int length;
  173. length = strlen(path)-1;
  174. while (length > 0 && path[length] != PATHSEPERATOR) {
  175. length--;
  176. }
  177. path[length] = 0;
  178. }
  179. /*
  180. ==============
  181. StripExtension
  182. ==============
  183. */
  184. void StripExtension (char *path)
  185. {
  186. int length;
  187. length = strlen(path)-1;
  188. while (length > 0 && path[length] != '.')
  189. {
  190. length--;
  191. if (path[length] == '/')
  192. return; // no extension
  193. }
  194. if (length) {
  195. path[length] = 0;
  196. }
  197. }