home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / common / scriplib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  2.9 KB  |  186 lines

  1. // scriplib.c
  2.  
  3. #include "cmdlib.h"
  4. #include "scriplib.h"
  5.  
  6. /*
  7. =============================================================================
  8.  
  9.                         PARSING STUFF
  10.  
  11. =============================================================================
  12. */
  13.  
  14. char    token[MAXTOKEN];
  15. char    *scriptbuffer,*script_p,*scriptend_p;
  16. int             grabbed;
  17. int             scriptline;
  18. qboolean endofscript;
  19. qboolean tokenready;                     // only true if UnGetToken was just called
  20.  
  21. /*
  22. ==============
  23. =
  24. = LoadScriptFile
  25. =
  26. ==============
  27. */
  28.  
  29. void LoadScriptFile (char *filename)
  30. {
  31.     int            size;
  32.  
  33.     size = LoadFile (filename, (void **)&scriptbuffer);
  34.  
  35.     script_p = scriptbuffer;
  36.     scriptend_p = script_p + size;
  37.     scriptline = 1;
  38.     endofscript = false;
  39.     tokenready = false;
  40. }
  41.  
  42.  
  43. /*
  44. ==============
  45. =
  46. = UnGetToken
  47. =
  48. = Signals that the current token was not used, and should be reported
  49. = for the next GetToken.  Note that
  50.  
  51. GetToken (true);
  52. UnGetToken ();
  53. GetToken (false);
  54.  
  55. = could cross a line boundary.
  56. =
  57. ==============
  58. */
  59.  
  60. void UnGetToken (void)
  61. {
  62.     tokenready = true;
  63. }
  64.  
  65.  
  66. /*
  67. ==============
  68. GetToken
  69. ==============
  70. */
  71. qboolean GetToken (qboolean crossline)
  72. {
  73.     char    *token_p;
  74.  
  75.     if (tokenready)                         // is a token allready waiting?
  76.     {
  77.         tokenready = false;
  78.         return true;
  79.     }
  80.  
  81.     if (script_p >= scriptend_p)
  82.     {
  83.         if (!crossline)
  84.             Error ("Line %i is incomplete\n",scriptline);
  85.         endofscript = true;
  86.         return false;
  87.     }
  88.  
  89. //
  90. // skip space
  91. //
  92. skipspace:
  93.     while (*script_p <= 32)
  94.     {
  95.         if (script_p >= scriptend_p)
  96.         {
  97.             if (!crossline)
  98.                 Error ("Line %i is incomplete\n",scriptline);
  99.             endofscript = true;
  100.             return true;
  101.         }
  102.         if (*script_p++ == '\n')
  103.         {
  104.             if (!crossline)
  105.                 Error ("Line %i is incomplete\n",scriptline);
  106.             scriptline++;
  107.         }
  108.     }
  109.  
  110.     if (script_p >= scriptend_p)
  111.     {
  112.         if (!crossline)
  113.             Error ("Line %i is incomplete\n",scriptline);
  114.         endofscript = true;
  115.         return true;
  116.     }
  117.  
  118.     if (*script_p == ';' || *script_p == '#')   // semicolon is comment field
  119.     {                                            // also make # a comment field
  120.         if (!crossline)
  121.             Error ("Line %i is incomplete\n",scriptline);
  122.         while (*script_p++ != '\n')
  123.             if (script_p >= scriptend_p)
  124.             {
  125.                 endofscript = true;
  126.                 return false;
  127.             }
  128.         goto skipspace;
  129.     }
  130.  
  131. //
  132. // copy token
  133. //
  134.     token_p = token;
  135.  
  136.     while ( *script_p > 32 && *script_p != ';')
  137.     {
  138.         *token_p++ = *script_p++;
  139.         if (script_p == scriptend_p)
  140.             break;
  141.         if (token_p == &token[MAXTOKEN])
  142.             Error ("Token too large on line %i\n",scriptline);
  143.     }
  144.  
  145.     *token_p = 0;
  146.     return true;
  147. }
  148.  
  149.  
  150. /*
  151. ==============
  152. =
  153. = TokenAvailable
  154. =
  155. = Returns true if there is another token on the line
  156. =
  157. ==============
  158. */
  159.  
  160. qboolean TokenAvailable (void)
  161. {
  162.     char    *search_p;
  163.  
  164.     search_p = script_p;
  165.  
  166.     if (search_p >= scriptend_p)
  167.         return false;
  168.  
  169.     while ( *search_p <= 32)
  170.     {
  171.         if (*search_p == '\n')
  172.             return false;
  173.         search_p++;
  174.         if (search_p == scriptend_p)
  175.             return false;
  176.  
  177.     }
  178.  
  179.     if (*search_p == ';')
  180.         return false;
  181.  
  182.     return true;
  183. }
  184.  
  185.  
  186.