misc.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "qedefs.h"
  2. char token[MAXTOKEN];
  3. boolean unget;
  4. char *script_p;
  5. int scriptline;
  6. void StartTokenParsing (char *data)
  7. {
  8. scriptline = 1;
  9. script_p = data;
  10. unget = false;
  11. }
  12. boolean GetToken (boolean crossline)
  13. {
  14. char *token_p;
  15. if (unget) // is a token allready waiting?
  16. return true;
  17. //
  18. // skip space
  19. //
  20. skipspace:
  21. while (*script_p <= 32)
  22. {
  23. if (!*script_p)
  24. {
  25. if (!crossline)
  26. Error ("Line %i is incomplete",scriptline);
  27. return false;
  28. }
  29. if (*script_p++ == '\n')
  30. {
  31. if (!crossline)
  32. Error ("Line %i is incomplete",scriptline);
  33. scriptline++;
  34. }
  35. }
  36. if (script_p[0] == '/' && script_p[1] == '/') // comment field
  37. {
  38. if (!crossline)
  39. Error ("Line %i is incomplete\n",scriptline);
  40. while (*script_p++ != '\n')
  41. if (!*script_p)
  42. {
  43. if (!crossline)
  44. Error ("Line %i is incomplete",scriptline);
  45. return false;
  46. }
  47. goto skipspace;
  48. }
  49. //
  50. // copy token
  51. //
  52. token_p = token;
  53. if (*script_p == '"')
  54. {
  55. script_p++;
  56. while ( *script_p != '"' )
  57. {
  58. if (!*script_p)
  59. Error ("EOF inside quoted token");
  60. *token_p++ = *script_p++;
  61. if (token_p == &token[MAXTOKEN])
  62. Error ("Token too large on line %i",scriptline);
  63. }
  64. script_p++;
  65. }
  66. else while ( *script_p > 32 )
  67. {
  68. *token_p++ = *script_p++;
  69. if (token_p == &token[MAXTOKEN])
  70. Error ("Token too large on line %i",scriptline);
  71. }
  72. *token_p = 0;
  73. return true;
  74. }
  75. void UngetToken ()
  76. {
  77. unget = true;
  78. }
  79. void qprintf (char *fmt, ...) // prints text to cmd_out_i
  80. {
  81. va_list argptr;
  82. static char string[1024];
  83. va_start (argptr, fmt);
  84. vsprintf (string, fmt,argptr);
  85. va_end (argptr);
  86. [g_cmd_out_i setStringValue: string];
  87. NXPing ();
  88. return;
  89. }
  90. /*
  91. =================
  92. Error
  93. For abnormal program terminations
  94. =================
  95. */
  96. BOOL in_error;
  97. void Error (char *error, ...)
  98. {
  99. va_list argptr;
  100. static char string[1024];
  101. if (in_error)
  102. [NXApp terminate: NULL];
  103. in_error = YES;
  104. va_start (argptr,error);
  105. vsprintf (string,error,argptr);
  106. va_end (argptr);
  107. strcat (string, "\nmap saved to "FN_CRASHSAVE);
  108. [map_i writeMapFile: FN_CRASHSAVE useRegion: NO];
  109. NXRunAlertPanel ("Error",string,NULL,NULL,NULL);
  110. [NXApp terminate: NULL];
  111. }
  112. void CleanupName (char *in, char *out)
  113. {
  114. int i;
  115. for (i=0 ; i< 16 ; i++ )
  116. {
  117. if (!in[i])
  118. break;
  119. out[i] = toupper(in[i]);
  120. }
  121. for ( ; i< 16 ; i++ )
  122. out[i] = 0;
  123. }
  124. void PrintRect (NXRect *r)
  125. {
  126. printf ("(%4.0f, %4.0f) + (%4.0f, %4.0f) = (%4.0f,%4.0f)\n"
  127. ,r->origin.x,r->origin.y,
  128. r->size.width, r->size.height, r->origin.x+r->size.width,
  129. r->origin.y+r->size.height);
  130. }
  131. /*
  132. ============
  133. FileTime
  134. returns -1 if not present
  135. ============
  136. */
  137. int FileTime (char *path)
  138. {
  139. struct stat buf;
  140. if (stat (path,&buf) == -1)
  141. return -1;
  142. return buf.st_mtime;
  143. }
  144. /*
  145. ============
  146. CreatePath
  147. ============
  148. */
  149. void CreatePath (char *path)
  150. {
  151. char *ofs;
  152. for (ofs = path+1 ; *ofs ; ofs++)
  153. {
  154. if (*ofs == '/')
  155. { // create the directory
  156. *ofs = 0;
  157. mkdir (path,0777);
  158. *ofs = '/';
  159. }
  160. }
  161. }
  162. int I_FileOpenRead (char *path, int *handle)
  163. {
  164. int h;
  165. struct stat fileinfo;
  166. h = open (path, O_RDONLY, 0666);
  167. *handle = h;
  168. if (h == -1)
  169. return -1;
  170. if (fstat (h,&fileinfo) == -1)
  171. Error ("Error fstating %s", path);
  172. return fileinfo.st_size;
  173. }
  174. int I_FileOpenWrite (char *path)
  175. {
  176. int handle;
  177. umask (0);
  178. handle = open(path,O_RDWR | O_CREAT | O_TRUNC
  179. , 0666);
  180. if (handle == -1)
  181. Error ("Error opening %s: %s", path,strerror(errno));
  182. return handle;
  183. }
  184. /*
  185. ============
  186. Sys_UpdateFile
  187. Copies a more recent net file to the local drive
  188. ============
  189. */
  190. void Sys_UpdateFile (char *path, char *netpath)
  191. {
  192. int ltime, ntime;
  193. int in, out, size;
  194. char *buf;
  195. ltime = FileTime (path);
  196. ntime = FileTime (netpath);
  197. if (ntime <= ltime)
  198. return; // up to date
  199. // copy the file
  200. printf ("UpdateFile: copying %s to %s...\n", netpath, path);
  201. size = I_FileOpenRead (netpath, &in);
  202. buf = malloc (size);
  203. if (read (in, buf, size) != size)
  204. Error ("UpdateFile: couldn't read all of %s", netpath);
  205. close (in);
  206. CreatePath (path);
  207. out = I_FileOpenWrite (path);
  208. write (out, buf, size);
  209. close (out);
  210. }