home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / readmap / token.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-29  |  2.1 KB  |  94 lines

  1. /*-------------------------------------------------------------------------
  2. File    : token.c
  3. Author  : John Carmack (id Software) and Cameron Newham.
  4. Version : 1.0
  5. Date    : 96/08/29
  6.  
  7. Description
  8. -----------
  9. Parses an array of char looking for tokens.
  10.  
  11. Comments
  12. --------
  13. This is almost verbatim from Carmack's code.
  14. -------------------------------------------------------------------------*/
  15.  
  16. #include "readmap.h"
  17. #include "io.h"
  18.  
  19.  
  20. /*--------------------------------------
  21. Gets a Token
  22. --------------------------------------*/
  23. int GetToken (int crossline, char *token, int *scriptline, char *dat, int *loc)
  24. {
  25.  
  26.   char *token_p;  /* a pointer into the token data */
  27.  
  28.   /* skip whitespace and ctrls */
  29. skipspace:
  30.   while (dat[*loc] <= 32)
  31.   {
  32.     if (dat[*loc] == '\0')
  33.     {
  34.       if (!crossline)
  35.         Error (*scriptline, "Line is incomplete.\n");
  36.       return FALSE;  /* End of data when error occured */
  37.     }
  38.     if (dat[(*loc)++] == '\n')
  39.     {
  40.       if (!crossline)
  41.         Error (*scriptline, "Line is incomplete.\n");
  42.         (*scriptline)++;  /* Try next line */
  43.     }
  44.   }
  45.  
  46.   if (dat[*loc] == '/' && dat[(*loc)+1] == '/')  /* check comment field */
  47.   {
  48.     if (!crossline)
  49.       Error (*scriptline, "Line is incomplete.\n");
  50.     while (dat[(*loc)++] != '\n')
  51.     {
  52.       if (dat[*loc] == '\0')
  53.       {
  54.         if (!crossline)
  55.           Error (*scriptline, "Line is incomplete.\n");
  56.         return FALSE;
  57.       }
  58.     }
  59.     goto skipspace;
  60.   }
  61.  
  62.   /* copy the token */
  63.   token_p = token;
  64.  
  65.   if (dat[*loc] == '"')
  66.   {
  67.     (*loc)++;
  68.     while (dat[*loc] != '"')
  69.     {
  70.       /* loop until we have a closing " */
  71.       if (dat[*loc] == '\0')
  72.         Error (*scriptline, "EOF is inside the quoted token\n");
  73.       *token_p = dat[(*loc)++];
  74.       token_p++;
  75.       if (token_p == &token[MAXTOKEN])
  76.         Error (*scriptline, "Token is too large\n");
  77.     }
  78.     (*loc)++;
  79.   }
  80.   else while (dat[*loc] > 32)
  81.   {
  82.     /* keep reading in the token */
  83.     *token_p = dat[(*loc)++];
  84.     token_p++;
  85.     if (token_p == &token[MAXTOKEN])
  86.       Error (*scriptline, "Token is too large\n");
  87.   }
  88.  
  89.   *token_p = 0; /* null terminate the token string */
  90.  
  91.   return (TRUE);
  92. }
  93.  
  94.