PARSE.CPP 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena 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 III Arena 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 Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "stdafx.h"
  19. #include "qe3.h"
  20. char token[MAXTOKEN];
  21. qboolean unget;
  22. char *script_p;
  23. int scriptline;
  24. void StartTokenParsing (char *data)
  25. {
  26. scriptline = 1;
  27. script_p = data;
  28. unget = false;
  29. }
  30. qboolean WINAPI GetToken (qboolean crossline)
  31. {
  32. char *token_p;
  33. if (unget) // is a token allready waiting?
  34. {
  35. unget = false;
  36. return true;
  37. }
  38. //
  39. // skip space
  40. //
  41. skipspace:
  42. while (*script_p <= 32)
  43. {
  44. if (!*script_p)
  45. {
  46. if (!crossline)
  47. Sys_Printf("Warning: Line %i is incomplete [01]\n",scriptline);
  48. return false;
  49. }
  50. if (*script_p++ == '\n')
  51. {
  52. if (!crossline)
  53. Sys_Printf("Warning: Line %i is incomplete [02]\n",scriptline);
  54. scriptline++;
  55. }
  56. }
  57. if (script_p[0] == '/' && script_p[1] == '/') // comment field
  58. {
  59. if (!crossline)
  60. Sys_Printf("Warning: Line %i is incomplete [03]\n",scriptline);
  61. while (*script_p++ != '\n')
  62. if (!*script_p)
  63. {
  64. if (!crossline)
  65. Sys_Printf("Warning: Line %i is incomplete [04]\n",scriptline);
  66. return false;
  67. }
  68. goto skipspace;
  69. }
  70. //
  71. // copy token
  72. //
  73. token_p = token;
  74. if (*script_p == '"')
  75. {
  76. script_p++;
  77. //if (*script_p == '"') // handle double quotes i suspect they are put in by other editors cccasionally
  78. // script_p++;
  79. while ( *script_p != '"' )
  80. {
  81. if (!*script_p)
  82. Error ("EOF inside quoted token");
  83. *token_p++ = *script_p++;
  84. if (token_p == &token[MAXTOKEN])
  85. Error ("Token too large on line %i",scriptline);
  86. }
  87. script_p++;
  88. //if (*script_p == '"') // handle double quotes i suspect they are put in by other editors cccasionally
  89. // script_p++;
  90. }
  91. else while ( *script_p > 32 )
  92. {
  93. *token_p++ = *script_p++;
  94. if (token_p == &token[MAXTOKEN])
  95. Error ("Token too large on line %i",scriptline);
  96. }
  97. *token_p = 0;
  98. return true;
  99. }
  100. void WINAPI UngetToken (void)
  101. {
  102. unget = true;
  103. }
  104. /*
  105. ==============
  106. TokenAvailable
  107. Returns true if there is another token on the line
  108. ==============
  109. */
  110. qboolean TokenAvailable (void)
  111. {
  112. char *search_p;
  113. search_p = script_p;
  114. while ( *search_p <= 32)
  115. {
  116. if (*search_p == '\n')
  117. return false;
  118. if (*search_p == 0)
  119. return false;
  120. search_p++;
  121. }
  122. if (*search_p == ';')
  123. return false;
  124. return true;
  125. }