parse.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qe3.h"
  19. char token[MAXTOKEN];
  20. qboolean unget;
  21. char *script_p;
  22. int scriptline;
  23. void StartTokenParsing (char *data)
  24. {
  25. scriptline = 1;
  26. script_p = data;
  27. unget = false;
  28. }
  29. qboolean GetToken (qboolean crossline)
  30. {
  31. char *token_p;
  32. if (unget) // is a token allready waiting?
  33. return true;
  34. //
  35. // skip space
  36. //
  37. skipspace:
  38. while (*script_p <= 32)
  39. {
  40. if (!*script_p)
  41. {
  42. if (!crossline)
  43. Error ("Line %i is incomplete",scriptline);
  44. return false;
  45. }
  46. if (*script_p++ == '\n')
  47. {
  48. if (!crossline)
  49. Error ("Line %i is incomplete",scriptline);
  50. scriptline++;
  51. }
  52. }
  53. if (script_p[0] == '/' && script_p[1] == '/') // comment field
  54. {
  55. if (!crossline)
  56. Error ("Line %i is incomplete\n",scriptline);
  57. while (*script_p++ != '\n')
  58. if (!*script_p)
  59. {
  60. if (!crossline)
  61. Error ("Line %i is incomplete",scriptline);
  62. return false;
  63. }
  64. goto skipspace;
  65. }
  66. //
  67. // copy token
  68. //
  69. token_p = token;
  70. if (*script_p == '"')
  71. {
  72. script_p++;
  73. while ( *script_p != '"' )
  74. {
  75. if (!*script_p)
  76. Error ("EOF inside quoted token");
  77. *token_p++ = *script_p++;
  78. if (token_p == &token[MAXTOKEN])
  79. Error ("Token too large on line %i",scriptline);
  80. }
  81. script_p++;
  82. }
  83. else while ( *script_p > 32 )
  84. {
  85. *token_p++ = *script_p++;
  86. if (token_p == &token[MAXTOKEN])
  87. Error ("Token too large on line %i",scriptline);
  88. }
  89. *token_p = 0;
  90. return true;
  91. }
  92. void UngetToken (void)
  93. {
  94. unget = true;
  95. }
  96. /*
  97. ==============
  98. TokenAvailable
  99. Returns true if there is another token on the line
  100. ==============
  101. */
  102. qboolean TokenAvailable (void)
  103. {
  104. char *search_p;
  105. search_p = script_p;
  106. while ( *search_p <= 32)
  107. {
  108. if (*search_p == '\n')
  109. return false;
  110. if (*search_p == 0)
  111. return false;
  112. search_p++;
  113. }
  114. if (*search_p == ';')
  115. return false;
  116. return true;
  117. }