cmdlib.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //
  2. // start of shared cmdlib stuff
  3. //
  4. #include "cmdlib.h"
  5. #include "windows.h"
  6. #define PATHSEPERATOR '/'
  7. // rad additions
  8. // 11.29.99
  9. PFN_ERR *g_pfnError = NULL;
  10. PFN_PRINTF *g_pfnPrintf = NULL;
  11. PFN_ERR_NUM *g_pfnErrorNum = NULL;
  12. PFN_PRINTF_NUM *g_pfnPrintfNum = NULL;
  13. void Error(const char *pFormat, ...)
  14. {
  15. if (g_pfnError)
  16. {
  17. va_list arg_ptr;
  18. va_start(arg_ptr, pFormat);
  19. g_pfnError(pFormat, arg_ptr);
  20. va_end(arg_ptr);
  21. }
  22. }
  23. void Printf(const char *pFormat, ...)
  24. {
  25. if (g_pfnPrintf)
  26. {
  27. va_list arg_ptr;
  28. va_start(arg_ptr, pFormat);
  29. g_pfnPrintf(pFormat, arg_ptr);
  30. va_end(arg_ptr);
  31. }
  32. }
  33. void ErrorNum(int nErr, const char *pFormat, ...)
  34. {
  35. if (g_pfnErrorNum)
  36. {
  37. va_list arg_ptr;
  38. va_start(arg_ptr, pFormat);
  39. g_pfnErrorNum(nErr, pFormat, arg_ptr);
  40. va_end(arg_ptr);
  41. }
  42. }
  43. void PrintfNum(int nErr, const char *pFormat, ...)
  44. {
  45. if (g_pfnPrintfNum)
  46. {
  47. va_list arg_ptr;
  48. va_start(arg_ptr, pFormat);
  49. g_pfnPrintfNum(nErr, pFormat, arg_ptr);
  50. va_end(arg_ptr);
  51. }
  52. }
  53. void SetErrorHandler(PFN_ERR pe)
  54. {
  55. g_pfnError = pe;
  56. }
  57. void SetPrintfHandler(PFN_PRINTF pe)
  58. {
  59. g_pfnPrintf = pe;
  60. }
  61. void SetErrorHandlerNum(PFN_ERR_NUM pe)
  62. {
  63. g_pfnErrorNum = pe;
  64. }
  65. void SetPrintfHandler(PFN_PRINTF_NUM pe)
  66. {
  67. g_pfnPrintfNum = pe;
  68. }
  69. // rad end
  70. #define MEM_BLOCKSIZE 4096
  71. void* qblockmalloc(size_t nSize)
  72. {
  73. void *b;
  74. // round up to threshold
  75. int nAllocSize = nSize % MEM_BLOCKSIZE;
  76. if ( nAllocSize > 0)
  77. {
  78. nSize += MEM_BLOCKSIZE - nAllocSize;
  79. }
  80. b = malloc(nSize + 1);
  81. memset (b, 0, nSize);
  82. return b;
  83. }
  84. void* qmalloc (size_t nSize)
  85. {
  86. void *b;
  87. b = malloc(nSize + 1);
  88. memset (b, 0, nSize);
  89. return b;
  90. }
  91. /*
  92. ================
  93. Q_filelength
  94. ================
  95. */
  96. int Q_filelength (FILE *f)
  97. {
  98. int pos;
  99. int end;
  100. pos = ftell (f);
  101. fseek (f, 0, SEEK_END);
  102. end = ftell (f);
  103. fseek (f, pos, SEEK_SET);
  104. return end;
  105. }
  106. // FIXME: need error handler
  107. FILE *SafeOpenWrite (const char *filename)
  108. {
  109. FILE *f;
  110. f = fopen(filename, "wb");
  111. if (!f)
  112. {
  113. Error ("Error opening %s: %s",filename,strerror(errno));
  114. }
  115. return f;
  116. }
  117. FILE *SafeOpenRead (const char *filename)
  118. {
  119. FILE *f;
  120. f = fopen(filename, "rb");
  121. if (!f)
  122. {
  123. Error ("Error opening %s: %s",filename,strerror(errno));
  124. }
  125. return f;
  126. }
  127. void SafeRead (FILE *f, void *buffer, int count)
  128. {
  129. if ( (int)fread (buffer, 1, count, f) != count)
  130. Error ("File read failure");
  131. }
  132. void SafeWrite (FILE *f, const void *buffer, int count)
  133. {
  134. if ( (int)fwrite (buffer, 1, count, f) != count)
  135. Error ("File read failure");
  136. }
  137. /*
  138. ==============
  139. LoadFile
  140. ==============
  141. */
  142. int LoadFile (const char *filename, void **bufferptr)
  143. {
  144. FILE *f;
  145. int length;
  146. void *buffer;
  147. *bufferptr = NULL;
  148. if (filename == NULL || strlen(filename) == 0)
  149. {
  150. return -1;
  151. }
  152. f = fopen (filename, "rb");
  153. if (!f)
  154. {
  155. return -1;
  156. }
  157. length = Q_filelength (f);
  158. buffer = qblockmalloc (length+1);
  159. ((char *)buffer)[length] = 0;
  160. SafeRead (f, buffer, length);
  161. fclose (f);
  162. *bufferptr = buffer;
  163. return length;
  164. }
  165. /*
  166. ==============
  167. LoadFileNoCrash
  168. returns -1 length if not present
  169. ==============
  170. */
  171. int LoadFileNoCrash (const char *filename, void **bufferptr)
  172. {
  173. FILE *f;
  174. int length;
  175. void *buffer;
  176. f = fopen (filename, "rb");
  177. if (!f)
  178. return -1;
  179. length = Q_filelength (f);
  180. buffer = qmalloc (length+1);
  181. ((char *)buffer)[length] = 0;
  182. SafeRead (f, buffer, length);
  183. fclose (f);
  184. *bufferptr = buffer;
  185. return length;
  186. }
  187. /*
  188. ==============
  189. SaveFile
  190. ==============
  191. */
  192. void SaveFile (const char *filename, void *buffer, int count)
  193. {
  194. FILE *f;
  195. f = SafeOpenWrite (filename);
  196. SafeWrite (f, buffer, count);
  197. fclose (f);
  198. }
  199. void DefaultExtension (char *path, char *extension)
  200. {
  201. char *src;
  202. //
  203. // if path doesn't have a .EXT, append extension
  204. // (extension should include the .)
  205. //
  206. src = path + strlen(path) - 1;
  207. while (*src != PATHSEPERATOR && src != path)
  208. {
  209. if (*src == '.')
  210. return; // it has an extension
  211. src--;
  212. }
  213. strcat (path, extension);
  214. }
  215. void DefaultPath (char *path, char *basepath)
  216. {
  217. char temp[128];
  218. if (path[0] == PATHSEPERATOR)
  219. return; // absolute path location
  220. strcpy (temp,path);
  221. strcpy (path,basepath);
  222. strcat (path,temp);
  223. }
  224. void StripFilename (char *path)
  225. {
  226. int length;
  227. length = strlen(path)-1;
  228. while (length > 0 && path[length] != PATHSEPERATOR)
  229. length--;
  230. path[length] = 0;
  231. }
  232. void StripExtension (char *path)
  233. {
  234. int length;
  235. length = strlen(path)-1;
  236. while (length > 0 && path[length] != '.')
  237. {
  238. length--;
  239. if (path[length] == '/')
  240. return; // no extension
  241. }
  242. if (length)
  243. path[length] = 0;
  244. }
  245. /*
  246. ====================
  247. Extract file parts
  248. ====================
  249. */
  250. void ExtractFilePath (const char *path, char *dest)
  251. {
  252. const char *src;
  253. src = path + strlen(path) - 1;
  254. //
  255. // back up until a \ or the start
  256. //
  257. while (src != path && *(src-1) != PATHSEPERATOR)
  258. src--;
  259. memcpy (dest, path, src-path);
  260. dest[src-path] = 0;
  261. }
  262. void ExtractFileName (const char *path, char *dest)
  263. {
  264. const char *src;
  265. src = path + strlen(path) - 1;
  266. //
  267. // back up until a \ or the start
  268. //
  269. while (src != path && *(src-1) != '/'
  270. && *(src-1) != '\\' )
  271. src--;
  272. while (*src)
  273. {
  274. *dest++ = *src++;
  275. }
  276. *dest = 0;
  277. }
  278. void ExtractFileBase (const char *path, char *dest)
  279. {
  280. const char *src;
  281. src = path + strlen(path) - 1;
  282. //
  283. // back up until a \ or the start
  284. //
  285. while (src != path && *(src-1) != '/'
  286. && *(src-1) != '\\' )
  287. src--;
  288. while (*src && *src != '.')
  289. {
  290. *dest++ = *src++;
  291. }
  292. *dest = 0;
  293. }
  294. void ExtractFileExtension (const char *path, char *dest)
  295. {
  296. const char *src;
  297. src = path + strlen(path) - 1;
  298. //
  299. // back up until a . or the start
  300. //
  301. while (src != path && *(src-1) != '.')
  302. src--;
  303. if (src == path)
  304. {
  305. *dest = 0; // no extension
  306. return;
  307. }
  308. strcpy (dest,src);
  309. }
  310. void ConvertDOSToUnixName( char *dst, const char *src )
  311. {
  312. while ( *src )
  313. {
  314. if ( *src == '\\' )
  315. *dst = '/';
  316. else
  317. *dst = *src;
  318. dst++; src++;
  319. }
  320. *dst = 0;
  321. }
  322. char* StrDup(char* pStr)
  323. {
  324. if (pStr)
  325. {
  326. return strcpy(new char[strlen(pStr)+1], pStr);
  327. }
  328. return NULL;
  329. }
  330. char* StrDup(const char* pStr)
  331. {
  332. if (pStr)
  333. {
  334. return strcpy(new char[strlen(pStr)+1], pStr);
  335. }
  336. return NULL;
  337. }
  338. /*
  339. ============================================================================
  340. BYTE ORDER FUNCTIONS
  341. ============================================================================
  342. */
  343. #ifdef _SGI_SOURCE
  344. #define __BIG_ENDIAN__
  345. #endif
  346. #ifdef __BIG_ENDIAN__
  347. short LittleShort (short l)
  348. {
  349. byte b1,b2;
  350. b1 = l&255;
  351. b2 = (l>>8)&255;
  352. return (b1<<8) + b2;
  353. }
  354. short BigShort (short l)
  355. {
  356. return l;
  357. }
  358. int LittleLong (int l)
  359. {
  360. byte b1,b2,b3,b4;
  361. b1 = l&255;
  362. b2 = (l>>8)&255;
  363. b3 = (l>>16)&255;
  364. b4 = (l>>24)&255;
  365. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  366. }
  367. int BigLong (int l)
  368. {
  369. return l;
  370. }
  371. float LittleFloat (float l)
  372. {
  373. union {byte b[4]; float f;} in, out;
  374. in.f = l;
  375. out.b[0] = in.b[3];
  376. out.b[1] = in.b[2];
  377. out.b[2] = in.b[1];
  378. out.b[3] = in.b[0];
  379. return out.f;
  380. }
  381. float BigFloat (float l)
  382. {
  383. return l;
  384. }
  385. #else
  386. short BigShort (short l)
  387. {
  388. byte b1,b2;
  389. b1 = l&255;
  390. b2 = (l>>8)&255;
  391. return (b1<<8) + b2;
  392. }
  393. short LittleShort (short l)
  394. {
  395. return l;
  396. }
  397. int BigLong (int l)
  398. {
  399. byte b1,b2,b3,b4;
  400. b1 = l&255;
  401. b2 = (l>>8)&255;
  402. b3 = (l>>16)&255;
  403. b4 = (l>>24)&255;
  404. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  405. }
  406. int LittleLong (int l)
  407. {
  408. return l;
  409. }
  410. float BigFloat (float l)
  411. {
  412. union {byte b[4]; float f;} in, out;
  413. in.f = l;
  414. out.b[0] = in.b[3];
  415. out.b[1] = in.b[2];
  416. out.b[2] = in.b[1];
  417. out.b[3] = in.b[0];
  418. return out.f;
  419. }
  420. float LittleFloat (float l)
  421. {
  422. return l;
  423. }
  424. #endif