cmdlib.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #ifdef SYSTEM_WIN32
  2. #pragma warning(disable:4996)
  3. #endif
  4. #ifdef SYSTEM_WIN32
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. #endif
  8. #include "cmdlib.h"
  9. #include "messages.h"
  10. #include "hlassert.h"
  11. #include "blockmem.h"
  12. #include "log.h"
  13. #ifdef SYSTEM_POSIX
  14. #ifdef HAVE_SYS_STAT_H
  15. #include <sys/stat.h>
  16. #endif
  17. #ifdef HAVE_FCNTL_H
  18. #include <fcntl.h>
  19. #endif
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #endif
  24. #define PATHSEPARATOR(c) ((c) == '\\' || (c) == '/')
  25. /*
  26. * ================
  27. * I_FloatTime
  28. * ================
  29. */
  30. double I_FloatTime()
  31. {
  32. #ifdef SYSTEM_WIN32
  33. FILETIME ftime;
  34. double rval;
  35. GetSystemTimeAsFileTime(&ftime);
  36. rval = ftime.dwLowDateTime;
  37. rval += ((__int64)ftime.dwHighDateTime) << 32;
  38. return (rval / 10000000.0);
  39. #endif
  40. #ifdef SYSTEM_POSIX
  41. struct timeval tp;
  42. struct timezone tzp;
  43. static int secbase;
  44. gettimeofday(&tp, &tzp);
  45. if (!secbase)
  46. {
  47. secbase = tp.tv_sec;
  48. return tp.tv_usec / 1000000.0;
  49. }
  50. return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
  51. #endif
  52. }
  53. #ifdef SYSTEM_POSIX
  54. char* strupr(char* string)
  55. {
  56. int i;
  57. int len = strlen(string);
  58. for (i = 0; i < len; i++)
  59. {
  60. string[i] = toupper(string[i]);
  61. }
  62. return string;
  63. }
  64. char* strlwr(char* string)
  65. {
  66. int i;
  67. int len = strlen(string);
  68. for (i = 0; i < len; i++)
  69. {
  70. string[i] = tolower(string[i]);
  71. }
  72. return string;
  73. }
  74. #endif
  75. // Case Insensitive substring matching
  76. const char* stristr(const char* const string, const char* const substring)
  77. {
  78. char* string_copy;
  79. char* substring_copy;
  80. const char* match;
  81. string_copy = _strdup(string);
  82. _strlwr(string_copy);
  83. substring_copy = _strdup(substring);
  84. _strlwr(substring_copy);
  85. match = strstr(string_copy, substring_copy);
  86. if (match)
  87. {
  88. match = (string + (match - string_copy));
  89. }
  90. free(string_copy);
  91. free(substring_copy);
  92. return match;
  93. }
  94. /*--------------------------------------------------------------------
  95. // New implementation of FlipSlashes, DefaultExtension, StripFilename,
  96. // StripExtension, ExtractFilePath, ExtractFile, ExtractFileBase, etc.
  97. ----------------------------------------------------------------------*/
  98. #ifdef ZHLT_NEW_FILE_FUNCTIONS
  99. //Since all of these functions operate around either the extension
  100. //or the directory path, centralize getting both numbers here so we
  101. //can just reference them everywhere else. Use strrchr to give a
  102. //speed boost while we're at it.
  103. inline void getFilePositions(char* path, int* extension_position, int* directory_position)
  104. {
  105. char* ptr = strrchr(path,'.');
  106. if(ptr == 0)
  107. { *extension_position = -1; }
  108. else
  109. { *extension_position = ptr - path; }
  110. ptr = max(strrchr(path,'/'),strrchr(path,'\\'));
  111. if(ptr == 0)
  112. { *directory_position = -1; }
  113. else
  114. {
  115. *directory_position = ptr - path;
  116. if(*directory_position > *extension_position)
  117. { *extension_position = -1; }
  118. //cover the case where we were passed a directory - get 2nd-to-last slash
  119. if(*directory_position == strlen(path) - 1)
  120. {
  121. do
  122. {
  123. --(*directory_position);
  124. }
  125. while(*directory_position > -1 && path[*directory_position] != '/' && path[*directory_position] != '\\');
  126. }
  127. }
  128. }
  129. char* FlipSlashes(char* string)
  130. {
  131. char* ptr = string;
  132. if(SYSTEM_SLASH_CHAR == '\\')
  133. {
  134. while(ptr = strchr(ptr,'/'))
  135. { *ptr = SYSTEM_SLASH_CHAR; }
  136. }
  137. else
  138. {
  139. while(ptr = strchr(ptr,'\\'))
  140. { *ptr = SYSTEM_SLASH_CHAR; }
  141. }
  142. return string;
  143. }
  144. void DefaultExtension(char* path, const char* extension)
  145. {
  146. int extension_pos, directory_pos;
  147. getFilePositions(path,&extension_pos,&directory_pos);
  148. if(extension_pos == -1)
  149. { strcat(path,extension); }
  150. }
  151. void StripFilename(char* path)
  152. {
  153. int extension_pos, directory_pos;
  154. getFilePositions(path,&extension_pos,&directory_pos);
  155. if(directory_pos == -1)
  156. { path[0] = 0; }
  157. else
  158. { path[directory_pos] = 0; }
  159. }
  160. void StripExtension(char* path)
  161. {
  162. int extension_pos, directory_pos;
  163. getFilePositions(path,&extension_pos,&directory_pos);
  164. if(extension_pos != -1)
  165. { path[extension_pos] = 0; }
  166. }
  167. void ExtractFilePath(char* const path, char* dest)
  168. {
  169. int extension_pos, directory_pos;
  170. getFilePositions(path,&extension_pos,&directory_pos);
  171. if(directory_pos != -1)
  172. {
  173. memcpy(dest,path,directory_pos+1); //include directory slash
  174. dest[directory_pos+1] = 0;
  175. }
  176. else
  177. { dest[0] = 0; }
  178. }
  179. void ExtractFile(char* const path, char* dest)
  180. {
  181. int extension_pos, directory_pos;
  182. getFilePositions(path,&extension_pos,&directory_pos);
  183. int length = (int)strlen(path);
  184. if(directory_pos == -1) { directory_pos = 0; }
  185. else { length -= directory_pos + 1; }
  186. memcpy(dest,path+directory_pos+1,length); //exclude directory slash
  187. dest[length] = 0;
  188. }
  189. void ExtractFileBase(char* const path, char* dest)
  190. {
  191. int extension_pos, directory_pos;
  192. getFilePositions(path,&extension_pos,&directory_pos);
  193. int length = extension_pos == -1 ? (int)strlen(path) : extension_pos;
  194. if(directory_pos == -1) { directory_pos = 0; }
  195. else { length -= directory_pos + 1; }
  196. memcpy(dest,path+directory_pos+1,length); //exclude directory slash
  197. dest[length] = 0;
  198. }
  199. void ExtractFileExtension(char* const path, char* dest)
  200. {
  201. int extension_pos, directory_pos;
  202. getFilePositions(path,&extension_pos,&directory_pos);
  203. if(extension_pos != -1)
  204. {
  205. int length = (int)strlen(path) - extension_pos;
  206. memcpy(dest,path+extension_pos,length); //include extension '.'
  207. dest[length] = 0;
  208. }
  209. else
  210. { dest[0] = 0; }
  211. }
  212. //-------------------------------------------------------------------
  213. #else //old cmdlib functions
  214. char* FlipSlashes(char* string)
  215. {
  216. while (*string)
  217. {
  218. if (PATHSEPARATOR(*string))
  219. {
  220. *string = SYSTEM_SLASH_CHAR;
  221. }
  222. string++;
  223. }
  224. return string;
  225. }
  226. void DefaultExtension(char* path, const char* extension)
  227. {
  228. char* src;
  229. //
  230. // if path doesn't have a .EXT, append extension
  231. // (extension should include the .)
  232. //
  233. src = path + strlen(path) - 1;
  234. while (!PATHSEPARATOR(*src) && src != path)
  235. {
  236. if (*src == '.')
  237. return; // it has an extension
  238. src--;
  239. }
  240. strcat(path, extension);
  241. }
  242. void StripFilename(char* path)
  243. {
  244. int length;
  245. length = strlen(path) - 1;
  246. while (length > 0 && !PATHSEPARATOR(path[length]))
  247. length--;
  248. path[length] = 0;
  249. }
  250. void StripExtension(char* path)
  251. {
  252. int length;
  253. length = strlen(path) - 1;
  254. while (length > 0 && path[length] != '.')
  255. {
  256. length--;
  257. if (PATHSEPARATOR(path[length]))
  258. return; // no extension
  259. }
  260. if (length)
  261. path[length] = 0;
  262. }
  263. /*
  264. * ====================
  265. * Extract file parts
  266. * ====================
  267. */
  268. void ExtractFilePath(const char* const path, char* dest)
  269. {
  270. hlassert (path != dest);
  271. const char* src;
  272. src = path + strlen(path) - 1;
  273. //
  274. // back up until a \ or the start
  275. //
  276. while (src != path && !PATHSEPARATOR(*(src - 1)))
  277. src--;
  278. memcpy(dest, path, src - path);
  279. dest[src - path] = 0;
  280. }
  281. void ExtractFile(const char* const path, char* dest)
  282. {
  283. hlassert (path != dest);
  284. const char* src;
  285. src = path + strlen(path) - 1;
  286. while (src != path && !PATHSEPARATOR(*(src - 1)))
  287. src--;
  288. while (*src)
  289. {
  290. *dest++ = *src++;
  291. }
  292. *dest = 0;
  293. }
  294. void ExtractFileBase(const char* const path, char* dest)
  295. {
  296. hlassert (path != dest);
  297. const char* src;
  298. src = path + strlen(path) - 1;
  299. //
  300. // back up until a \ or the start
  301. //
  302. while (src != path && !PATHSEPARATOR(*(src - 1)))
  303. src--;
  304. while (*src && *src != '.')
  305. {
  306. *dest++ = *src++;
  307. }
  308. *dest = 0;
  309. }
  310. void ExtractFileExtension(const char* const path, char* dest)
  311. {
  312. hlassert (path != dest);
  313. const char* src;
  314. src = path + strlen(path) - 1;
  315. //
  316. // back up until a . or the start
  317. //
  318. while (src != path && *(src - 1) != '.')
  319. src--;
  320. if (src == path)
  321. {
  322. *dest = 0; // no extension
  323. return;
  324. }
  325. strcpy_s(dest, src);
  326. }
  327. #endif
  328. /*
  329. * ============================================================================
  330. *
  331. * BYTE ORDER FUNCTIONS
  332. *
  333. * ============================================================================
  334. */
  335. #ifdef WORDS_BIGENDIAN
  336. short LittleShort(const short l)
  337. {
  338. byte b1, b2;
  339. b1 = l & 255;
  340. b2 = (l >> 8) & 255;
  341. return (b1 << 8) + b2;
  342. }
  343. short BigShort(const short l)
  344. {
  345. return l;
  346. }
  347. int LittleLong(const int l)
  348. {
  349. byte b1, b2, b3, b4;
  350. b1 = l & 255;
  351. b2 = (l >> 8) & 255;
  352. b3 = (l >> 16) & 255;
  353. b4 = (l >> 24) & 255;
  354. return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  355. }
  356. int BigLong(const int l)
  357. {
  358. return l;
  359. }
  360. float LittleFloat(const float l)
  361. {
  362. union
  363. {
  364. byte b[4];
  365. float f;
  366. }
  367. in , out;
  368. in.f = l;
  369. out.b[0] = in.b[3];
  370. out.b[1] = in.b[2];
  371. out.b[2] = in.b[1];
  372. out.b[3] = in.b[0];
  373. return out.f;
  374. }
  375. float BigFloat(const float l)
  376. {
  377. return l;
  378. }
  379. #else // Little endian (Intel, etc)
  380. short BigShort(const short l)
  381. {
  382. byte b1, b2;
  383. b1 = (byte) (l & 255);
  384. b2 = (byte) ((l >> 8) & 255);
  385. return (short)((b1 << 8) + b2);
  386. }
  387. short LittleShort(const short l)
  388. {
  389. return l;
  390. }
  391. int BigLong(const int l)
  392. {
  393. byte b1, b2, b3, b4;
  394. b1 = (byte) (l & 255);
  395. b2 = (byte) ((l >> 8) & 255);
  396. b3 = (byte) ((l >> 16) & 255);
  397. b4 = (byte) ((l >> 24) & 255);
  398. return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  399. }
  400. int LittleLong(const int l)
  401. {
  402. return l;
  403. }
  404. float BigFloat(const float l)
  405. {
  406. union
  407. {
  408. byte b[4];
  409. float f;
  410. }
  411. in , out;
  412. in.f = l;
  413. out.b[0] = in.b[3];
  414. out.b[1] = in.b[2];
  415. out.b[2] = in.b[1];
  416. out.b[3] = in.b[0];
  417. return out.f;
  418. }
  419. float LittleFloat(const float l)
  420. {
  421. return l;
  422. }
  423. #endif
  424. //=============================================================================
  425. bool CDECL safe_snprintf(char* const dest, const size_t count, const char* const args, ...)
  426. {
  427. size_t amt;
  428. va_list argptr;
  429. hlassert(count > 0);
  430. va_start(argptr, args);
  431. amt = vsnprintf(dest, count, args, argptr);
  432. va_end(argptr);
  433. // truncated (bad!, snprintf doesn't null terminate the string when this happens)
  434. if (amt == count)
  435. {
  436. dest[count - 1] = 0;
  437. return false;
  438. }
  439. return true;
  440. }
  441. bool safe_strncpy(char* const dest, const char* const src, const size_t count)
  442. {
  443. return safe_snprintf(dest, count, "%s", src);
  444. }
  445. bool safe_strncat(char* const dest, const char* const src, const size_t count)
  446. {
  447. if (count)
  448. {
  449. strncat(dest, src, count);
  450. dest[count - 1] = 0; // Ensure it is null terminated
  451. return true;
  452. }
  453. else
  454. {
  455. Warning("safe_strncat passed empty count");
  456. return false;
  457. }
  458. }
  459. bool TerminatedString(const char* buffer, const int size)
  460. {
  461. int x;
  462. for (x = 0; x < size; x++, buffer++)
  463. {
  464. if ((*buffer) == 0)
  465. {
  466. return true;
  467. }
  468. }
  469. return false;
  470. }