]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/extra/qe4/parse.c
8258a4ad14ce628e76952a13c13a8643c7cfbb36
[xonotic/netradiant.git] / tools / quake2 / extra / qe4 / parse.c
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools 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 2 Tools 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 Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22
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 GetToken (qboolean crossline)
38 {
39         char    *token_p;
40
41         if (unget)                         // is a token allready waiting?
42                 return true;
43
44 //
45 // skip space
46 //
47 skipspace:
48         while (*script_p <= 32)
49         {
50                 if (!*script_p)
51                 {
52                         if (!crossline)
53                                 Error ("Line %i is incomplete",scriptline);
54                         return false;
55                 }
56                 if (*script_p++ == '\n')
57                 {
58                         if (!crossline)
59                                 Error ("Line %i is incomplete",scriptline);
60                         scriptline++;
61                 }
62         }
63
64         if (script_p[0] == '/' && script_p[1] == '/')   // comment field
65         {
66                 if (!crossline)
67                         Error ("Line %i is incomplete\n",scriptline);
68                 while (*script_p++ != '\n')
69                         if (!*script_p)
70                         {
71                                 if (!crossline)
72                                         Error ("Line %i is incomplete",scriptline);
73                                 return false;
74                         }
75                 goto skipspace;
76         }
77
78 //
79 // copy token
80 //
81         token_p = token;
82
83         if (*script_p == '"')
84         {
85                 script_p++;
86                 while ( *script_p != '"' )
87                 {
88                         if (!*script_p)
89                                 Error ("EOF inside quoted token");
90                         *token_p++ = *script_p++;
91                         if (token_p == &token[MAXTOKEN])
92                                 Error ("Token too large on line %i",scriptline);
93                 }
94                 script_p++;
95         }
96         else while ( *script_p > 32 )
97         {
98                 *token_p++ = *script_p++;
99                 if (token_p == &token[MAXTOKEN])
100                         Error ("Token too large on line %i",scriptline);
101         }
102
103         *token_p = 0;
104
105         return true;
106 }
107
108 void UngetToken (void)
109 {
110         unget = true;
111 }
112
113
114 /*
115 ==============
116 TokenAvailable
117
118 Returns true if there is another token on the line
119 ==============
120 */
121 qboolean TokenAvailable (void)
122 {
123         char    *search_p;
124
125         search_p = script_p;
126
127         while ( *search_p <= 32)
128         {
129                 if (*search_p == '\n')
130                         return false;
131                 if (*search_p == 0)
132                         return false;
133                 search_p++;
134         }
135
136         if (*search_p == ';')
137                 return false;
138
139         return true;
140 }
141