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