home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / util / mbq319 / scriplib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-01  |  3.0 KB  |  188 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. boolean endofscript;
  19. boolean 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. =
  69. = GetToken
  70. =
  71. ==============
  72. */
  73.  
  74. void GetToken (boolean crossline)
  75. {
  76.     char    *token_p;
  77.  
  78.     if (tokenready)                         // is a token allready waiting?
  79.     {
  80.         tokenready = false;
  81.         return;
  82.     }
  83.  
  84.     if (script_p >= scriptend_p)
  85.     {
  86.         if (!crossline)
  87.             Error ("Line %i is incomplete\n",scriptline);
  88.         endofscript = true;
  89.         return;
  90.     }
  91.  
  92. //
  93. // skip space
  94. //
  95. skipspace:
  96.     while (*script_p <= 32)
  97.     {
  98.         if (script_p >= scriptend_p)
  99.         {
  100.             if (!crossline)
  101.                 Error ("Line %i is incomplete\n",scriptline);
  102.             endofscript = true;
  103.             return;
  104.         }
  105.         if (*script_p++ == '\n')
  106.         {
  107.             if (!crossline)
  108.                 Error ("Line %i is incomplete\n",scriptline);
  109.             scriptline++;
  110.         }
  111.     }
  112.  
  113.     if (script_p >= scriptend_p)
  114.     {
  115.         if (!crossline)
  116.             Error ("Line %i is incomplete\n",scriptline);
  117.         endofscript = true;
  118.         return;
  119.     }
  120.  
  121.     if (*script_p == ';' || *script_p == '#')   // semicolon is comment field
  122.     {                                            // also make # a comment field
  123.         if (!crossline)
  124.             Error ("Line %i is incomplete\n",scriptline);
  125.         while (*script_p++ != '\n')
  126.             if (script_p >= scriptend_p)
  127.             {
  128.                 endofscript = true;
  129.                 return;
  130.             }
  131.         goto skipspace;
  132.     }
  133.  
  134. //
  135. // copy token
  136. //
  137.     token_p = token;
  138.  
  139.     while ( *script_p > 32 && *script_p != ';')
  140.     {
  141.         *token_p++ = *script_p++;
  142.         if (script_p == scriptend_p)
  143.             break;
  144.         if (token_p == &token[MAXTOKEN])
  145.             Error ("Token too large on line %i\n",scriptline);
  146.     }
  147.  
  148.     *token_p = 0;
  149. }
  150.  
  151.  
  152. /*
  153. ==============
  154. =
  155. = TokenAvailable
  156. =
  157. = Returns true if there is another token on the line
  158. =
  159. ==============
  160. */
  161.  
  162. boolean TokenAvailable (void)
  163. {
  164.     char    *search_p;
  165.  
  166.     search_p = script_p;
  167.  
  168.     if (search_p >= scriptend_p)
  169.         return false;
  170.  
  171.     while ( *search_p <= 32)
  172.     {
  173.         if (*search_p == '\n')
  174.             return false;
  175.         search_p++;
  176.         if (search_p == scriptend_p)
  177.             return false;
  178.  
  179.     }
  180.  
  181.     if (*search_p == ';')
  182.         return false;
  183.  
  184.     return true;
  185. }
  186.  
  187.  
  188.