Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
5.6 KiB

  1. #include "toollib.h"
  2. char g_tl_token[MAXTOKEN];
  3. char* g_tl_scriptbuff;
  4. char* g_tl_scriptptr;
  5. char* g_tl_scriptendptr;
  6. int g_tl_scriptsize;
  7. int g_tl_scriptline;
  8. bool g_tl_endofscript;
  9. bool g_tl_tokenready;
  10. int g_tl_oldscriptline;
  11. char* g_tl_oldscriptptr;
  12. /*****************************************************************************
  13. TL_FreeScriptFile
  14. *****************************************************************************/
  15. void TL_FreeScriptFile(void)
  16. {
  17. if (g_tl_scriptbuff)
  18. {
  19. TL_Free(g_tl_scriptbuff);
  20. g_tl_scriptbuff = NULL;
  21. }
  22. }
  23. /*****************************************************************************
  24. TL_LoadScriptFile
  25. *****************************************************************************/
  26. void TL_LoadScriptFile(const char* filename)
  27. {
  28. g_tl_scriptsize = TL_LoadFile(filename,(void **)&g_tl_scriptbuff);
  29. TL_ResetParser();
  30. }
  31. /*****************************************************************************
  32. TL_SetScriptData
  33. *****************************************************************************/
  34. void TL_SetScriptData(char* data, int length)
  35. {
  36. g_tl_scriptbuff = data;
  37. g_tl_scriptsize = length;
  38. TL_ResetParser();
  39. }
  40. /*****************************************************************************
  41. TL_UnGetToken
  42. *****************************************************************************/
  43. void TL_UnGetToken(void)
  44. {
  45. g_tl_tokenready = true;
  46. }
  47. /*****************************************************************************
  48. TL_GetToken
  49. *****************************************************************************/
  50. char* TL_GetToken(bool crossline)
  51. {
  52. char* tokenptr;
  53. if (g_tl_tokenready)
  54. {
  55. g_tl_tokenready = false;
  56. return (g_tl_token);
  57. }
  58. g_tl_token[0] = '\0';
  59. if (g_tl_scriptptr >= g_tl_scriptendptr)
  60. {
  61. if (!crossline)
  62. TL_Error("TL_GetToken: Line %d is incomplete",g_tl_scriptline);
  63. g_tl_endofscript = true;
  64. return (NULL);
  65. }
  66. skipspace:
  67. while (*g_tl_scriptptr <= ' ')
  68. {
  69. if (g_tl_scriptptr >= g_tl_scriptendptr)
  70. {
  71. if (!crossline)
  72. TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline);
  73. g_tl_endofscript = true;
  74. return (NULL);
  75. }
  76. if (*g_tl_scriptptr++ == '\n')
  77. {
  78. if (!crossline)
  79. TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline);
  80. g_tl_scriptline++;
  81. }
  82. }
  83. if (g_tl_scriptptr >= g_tl_scriptendptr)
  84. {
  85. if (!crossline)
  86. TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline);
  87. g_tl_endofscript = true;
  88. return (NULL);
  89. }
  90. // skip commented line
  91. if ((g_tl_scriptptr[0] == ';') || (g_tl_scriptptr[0] == '/' && g_tl_scriptptr[1] == '/'))
  92. {
  93. if (!crossline)
  94. TL_Error("GetToken: Line %i is incomplete",g_tl_scriptline);
  95. while (*g_tl_scriptptr++ != '\n')
  96. {
  97. if (g_tl_scriptptr >= g_tl_scriptendptr)
  98. {
  99. g_tl_endofscript = true;
  100. return (NULL);
  101. }
  102. }
  103. g_tl_scriptline++;
  104. goto skipspace;
  105. }
  106. tokenptr = g_tl_token;
  107. if (g_tl_scriptptr[0] == '\"' && g_tl_scriptptr[1])
  108. {
  109. // copy quoted token
  110. do
  111. {
  112. *tokenptr++ = *g_tl_scriptptr++;
  113. if (g_tl_scriptptr == g_tl_scriptendptr)
  114. break;
  115. if (tokenptr == &g_tl_token[MAXTOKEN])
  116. TL_Error("GetToken: Token too large on line %i",g_tl_scriptline);
  117. }
  118. while (*g_tl_scriptptr >= ' ' && *g_tl_scriptptr != '\"');
  119. if (g_tl_scriptptr[0] == '\"')
  120. *tokenptr++ = *g_tl_scriptptr++;
  121. }
  122. else
  123. {
  124. // copy token
  125. while (*g_tl_scriptptr > ' ' && *g_tl_scriptptr != ';' && *g_tl_scriptptr != '\"')
  126. {
  127. *tokenptr++ = *g_tl_scriptptr++;
  128. if (g_tl_scriptptr == g_tl_scriptendptr)
  129. break;
  130. if (tokenptr == &g_tl_token[MAXTOKEN])
  131. TL_Error("GetToken: Token too large on line %i",g_tl_scriptline);
  132. }
  133. }
  134. *tokenptr = '\0';
  135. return (g_tl_token);
  136. }
  137. /*****************************************************************************
  138. TL_SkipRestOfLine
  139. *****************************************************************************/
  140. void TL_SkipRestOfLine(void)
  141. {
  142. while (*g_tl_scriptptr++ != '\n')
  143. {
  144. if (g_tl_scriptptr >= g_tl_scriptendptr)
  145. {
  146. break;
  147. }
  148. }
  149. g_tl_scriptline++;
  150. }
  151. /*****************************************************************************
  152. TL_TokenAvailable
  153. Returns (TRUE) if token available on line.
  154. *****************************************************************************/
  155. bool TL_TokenAvailable (void)
  156. {
  157. char* ptr;
  158. ptr = g_tl_scriptptr;
  159. while (*ptr <= ' ')
  160. {
  161. if (ptr >= g_tl_scriptendptr)
  162. {
  163. g_tl_endofscript = true;
  164. return (false);
  165. }
  166. if (*ptr++ == '\n')
  167. return (false);
  168. }
  169. return (true);
  170. }
  171. /*****************************************************************************
  172. TL_EndOfScript
  173. Returns (TRUE) at end of script
  174. *****************************************************************************/
  175. bool TL_EndOfScript(void)
  176. {
  177. if (g_tl_scriptptr >= g_tl_scriptendptr)
  178. {
  179. g_tl_endofscript = true;
  180. return (true);
  181. }
  182. return (false);
  183. }
  184. /*****************************************************************************
  185. TL_ResetParser
  186. *****************************************************************************/
  187. void TL_ResetParser(void)
  188. {
  189. g_tl_scriptptr = g_tl_scriptbuff;
  190. g_tl_scriptendptr = g_tl_scriptptr + g_tl_scriptsize;
  191. g_tl_scriptline = 1;
  192. g_tl_endofscript = false;
  193. g_tl_tokenready = false;
  194. }
  195. /*****************************************************************************
  196. TL_SaveParser
  197. *****************************************************************************/
  198. void TL_SaveParser(void)
  199. {
  200. g_tl_oldscriptline = g_tl_scriptline;
  201. g_tl_oldscriptptr = g_tl_scriptptr;
  202. }
  203. /*****************************************************************************
  204. TL_RestoreParser
  205. *****************************************************************************/
  206. void TL_RestoreParser(void)
  207. {
  208. g_tl_scriptline = g_tl_oldscriptline;
  209. g_tl_scriptptr = g_tl_oldscriptptr;
  210. }