cmdlib.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // cmdlib.c
  2. #include "cmdlib.h"
  3. #define PATHSEPERATOR '/'
  4. /*
  5. ================
  6. I_FloatTime
  7. ================
  8. */
  9. double I_FloatTime (void)
  10. {
  11. struct timeval tp;
  12. struct timezone tzp;
  13. static int secbase;
  14. gettimeofday(&tp, &tzp);
  15. if (!secbase)
  16. {
  17. secbase = tp.tv_sec;
  18. return tp.tv_usec/1000000.0;
  19. }
  20. return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  21. }
  22. char com_token[1024];
  23. boolean com_eof;
  24. /*
  25. ==============
  26. COM_Parse
  27. Parse a token out of a string
  28. ==============
  29. */
  30. char *COM_Parse (char *data)
  31. {
  32. int c;
  33. int len;
  34. com_eof = false;
  35. len = 0;
  36. com_token[0] = 0;
  37. if (!data)
  38. return NULL;
  39. // skip whitespace
  40. skipwhite:
  41. while ( (c = *data) <= ' ')
  42. {
  43. if (c == 0)
  44. {
  45. com_eof = true;
  46. return NULL; // end of file;
  47. }
  48. data++;
  49. }
  50. // skip // comments
  51. if (c=='/' && data[1] == '/')
  52. {
  53. while (*data && *data != '\n')
  54. data++;
  55. goto skipwhite;
  56. }
  57. // handle quoted strings specially
  58. if (c == '\"')
  59. {
  60. data++;
  61. do
  62. {
  63. c = *data++;
  64. if (c=='\"')
  65. {
  66. com_token[len] = 0;
  67. return data;
  68. }
  69. com_token[len] = c;
  70. len++;
  71. } while (1);
  72. }
  73. // parse single characters
  74. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  75. {
  76. com_token[len] = c;
  77. len++;
  78. com_token[len] = 0;
  79. return data+1;
  80. }
  81. // parse a regular word
  82. do
  83. {
  84. com_token[len] = c;
  85. data++;
  86. len++;
  87. c = *data;
  88. if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
  89. break;
  90. } while (c>32);
  91. com_token[len] = 0;
  92. return data;
  93. }
  94. /*
  95. ================
  96. =
  97. = filelength
  98. =
  99. ================
  100. */
  101. int filelength (int handle)
  102. {
  103. struct stat fileinfo;
  104. if (fstat (handle,&fileinfo) == -1)
  105. {
  106. fprintf (stderr,"Error fstating");
  107. exit (1);
  108. }
  109. return fileinfo.st_size;
  110. }
  111. int tell (int handle)
  112. {
  113. return lseek (handle, 0, L_INCR);
  114. }
  115. char *strupr (char *start)
  116. {
  117. char *in;
  118. in = start;
  119. while (*in)
  120. {
  121. *in = toupper(*in);
  122. in++;
  123. }
  124. return start;
  125. }
  126. char *strlower (char *start)
  127. {
  128. char *in;
  129. in = start;
  130. while (*in)
  131. {
  132. *in = tolower(*in);
  133. in++;
  134. }
  135. return start;
  136. }
  137. char *getcwd (char *path, int length)
  138. {
  139. return getwd(path);
  140. }
  141. /* globals for command line args */
  142. extern int NXArgc;
  143. extern char **NXArgv;
  144. #define myargc NXArgc
  145. #define myargv NXArgv
  146. /*
  147. =============================================================================
  148. MISC FUNCTIONS
  149. =============================================================================
  150. */
  151. /*
  152. =================
  153. =
  154. = CheckParm
  155. =
  156. = Checks for the given parameter in the program's command line arguments
  157. =
  158. = Returns the argument number (1 to argc-1) or 0 if not present
  159. =
  160. =================
  161. */
  162. int CheckParm (char *check)
  163. {
  164. int i;
  165. for (i = 1;i<myargc;i++)
  166. {
  167. if ( !stricmp(check, myargv[i]) )
  168. return i;
  169. }
  170. return 0;
  171. }
  172. int SafeOpenWrite (char *filename)
  173. {
  174. int handle;
  175. umask (0);
  176. handle = open(filename,O_RDWR | O_CREAT | O_TRUNC
  177. , 0666);
  178. if (handle == -1)
  179. Error ("Error opening %s: %s",filename,strerror(errno));
  180. return handle;
  181. }
  182. int SafeOpenRead (char *filename)
  183. {
  184. int handle;
  185. handle = open(filename,O_RDONLY);
  186. if (handle == -1)
  187. Error ("Error opening %s: %s",filename,strerror(errno));
  188. return handle;
  189. }
  190. void SafeRead (int handle, void *buffer, long count)
  191. {
  192. int iocount;
  193. iocount = read (handle,buffer,count);
  194. if (iocount != count)
  195. Error ("File read failure");
  196. }
  197. void SafeWrite (int handle, void *buffer, long count)
  198. {
  199. int iocount;
  200. iocount = write (handle,buffer,count);
  201. if (iocount != count)
  202. Error ("File write failure");
  203. }
  204. void *SafeMalloc (long size)
  205. {
  206. void *ptr;
  207. ptr = malloc (size);
  208. if (!ptr)
  209. Error ("Malloc failure for %lu bytes",size);
  210. return ptr;
  211. }
  212. /*
  213. ==============
  214. =
  215. = LoadFile
  216. =
  217. = appends a 0 byte
  218. ==============
  219. */
  220. long LoadFile (char *filename, void **bufferptr)
  221. {
  222. int handle;
  223. long length;
  224. void *buffer;
  225. handle = SafeOpenRead (filename);
  226. length = filelength (handle);
  227. buffer = SafeMalloc (length+1);
  228. ((char *)buffer)[length] = 0;
  229. SafeRead (handle, buffer, length);
  230. close (handle);
  231. *bufferptr = buffer;
  232. return length;
  233. }
  234. /*
  235. ==============
  236. =
  237. = SaveFile
  238. =
  239. ==============
  240. */
  241. void SaveFile (char *filename, void *buffer, long count)
  242. {
  243. int handle;
  244. handle = SafeOpenWrite (filename);
  245. SafeWrite (handle, buffer, count);
  246. close (handle);
  247. }
  248. void DefaultExtension (char *path, char *extension)
  249. {
  250. char *src;
  251. //
  252. // if path doesn't have a .EXT, append extension
  253. // (extension should include the .)
  254. //
  255. src = path + strlen(path) - 1;
  256. while (*src != PATHSEPERATOR && src != path)
  257. {
  258. if (*src == '.')
  259. return; // it has an extension
  260. src--;
  261. }
  262. strcat (path, extension);
  263. }
  264. void DefaultPath (char *path, char *basepath)
  265. {
  266. char temp[128];
  267. if (path[0] == PATHSEPERATOR)
  268. return; // absolute path location
  269. strcpy (temp,path);
  270. strcpy (path,basepath);
  271. strcat (path,temp);
  272. }
  273. void StripFilename (char *path)
  274. {
  275. int length;
  276. length = strlen(path)-1;
  277. while (length > 0 && path[length] != PATHSEPERATOR)
  278. length--;
  279. path[length] = 0;
  280. }
  281. void StripExtension (char *path)
  282. {
  283. int length;
  284. length = strlen(path)-1;
  285. while (length > 0 && path[length] != '.')
  286. length--;
  287. if (length)
  288. path[length] = 0;
  289. }
  290. /*
  291. ====================
  292. =
  293. = Extract file parts
  294. =
  295. ====================
  296. */
  297. void ExtractFilePath (char *path, char *dest)
  298. {
  299. char *src;
  300. src = path + strlen(path) - 1;
  301. //
  302. // back up until a \ or the start
  303. //
  304. while (src != path && *(src-1) != PATHSEPERATOR)
  305. src--;
  306. memcpy (dest, path, src-path);
  307. dest[src-path] = 0;
  308. }
  309. void ExtractFileBase (char *path, char *dest)
  310. {
  311. char *src;
  312. src = path + strlen(path) - 1;
  313. //
  314. // back up until a \ or the start
  315. //
  316. while (src != path && *(src-1) != PATHSEPERATOR)
  317. src--;
  318. while (*src && *src != '.')
  319. {
  320. *dest++ = *src++;
  321. }
  322. *dest = 0;
  323. }
  324. void ExtractFileExtension (char *path, char *dest)
  325. {
  326. char *src;
  327. src = path + strlen(path) - 1;
  328. //
  329. // back up until a . or the start
  330. //
  331. while (src != path && *(src-1) != '.')
  332. src--;
  333. if (src == path)
  334. {
  335. *dest = 0; // no extension
  336. return;
  337. }
  338. strcpy (dest,src);
  339. }
  340. /*
  341. ==============
  342. =
  343. = ParseNum / ParseHex
  344. =
  345. ==============
  346. */
  347. long ParseHex (char *hex)
  348. {
  349. char *str;
  350. long num;
  351. num = 0;
  352. str = hex;
  353. while (*str)
  354. {
  355. num <<= 4;
  356. if (*str >= '0' && *str <= '9')
  357. num += *str-'0';
  358. else if (*str >= 'a' && *str <= 'f')
  359. num += 10 + *str-'a';
  360. else if (*str >= 'A' && *str <= 'F')
  361. num += 10 + *str-'A';
  362. else
  363. Error ("Bad hex number: %s",hex);
  364. str++;
  365. }
  366. return num;
  367. }
  368. long ParseNum (char *str)
  369. {
  370. if (str[0] == '$')
  371. return ParseHex (str+1);
  372. if (str[0] == '0' && str[1] == 'x')
  373. return ParseHex (str+2);
  374. return atol (str);
  375. }
  376. int GetKey (void)
  377. {
  378. return getchar ();
  379. }
  380. /*
  381. ============================================================================
  382. BYTE ORDER FUNCTIONS
  383. ============================================================================
  384. */
  385. #ifdef __BIG_ENDIAN__
  386. short LittleShort (short l)
  387. {
  388. byte b1,b2;
  389. b1 = l&255;
  390. b2 = (l>>8)&255;
  391. return (b1<<8) + b2;
  392. }
  393. short BigShort (short l)
  394. {
  395. return l;
  396. }
  397. long LittleLong (long 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 ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  405. }
  406. long BigLong (long l)
  407. {
  408. return l;
  409. }
  410. float LittleFloat (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 BigFloat (float l)
  421. {
  422. return l;
  423. }
  424. #else
  425. short BigShort (short l)
  426. {
  427. byte b1,b2;
  428. b1 = l&255;
  429. b2 = (l>>8)&255;
  430. return (b1<<8) + b2;
  431. }
  432. short LittleShort (short l)
  433. {
  434. return l;
  435. }
  436. long BigLong (long l)
  437. {
  438. byte b1,b2,b3,b4;
  439. b1 = l&255;
  440. b2 = (l>>8)&255;
  441. b3 = (l>>16)&255;
  442. b4 = (l>>24)&255;
  443. return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
  444. }
  445. long LittleLong (long l)
  446. {
  447. return l;
  448. }
  449. float BigFloat (float l)
  450. {
  451. union {byte b[4]; float f;} in, out;
  452. in.f = l;
  453. out.b[0] = in.b[3];
  454. out.b[1] = in.b[2];
  455. out.b[2] = in.b[1];
  456. out.b[3] = in.b[0];
  457. return out.f;
  458. }
  459. float LittleFloat (float l)
  460. {
  461. return l;
  462. }
  463. #endif