home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / source / misc.m < prev    next >
Encoding:
Text File  |  1996-08-08  |  4.1 KB  |  275 lines

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