aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/client/LayoutParser.java
blob: 61e2defacc1ed5049527f9219a6411c3a5a28d90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * LayoutParser.java
 * Copyright (C) 2003
 */
/*
 Copyright (C) 1997-2001 Id Software, Inc.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 */
package jake2.client;

import jake2.Defines;
import jake2.qcommon.Com;

final class LayoutParser {
    int tokenPos;

    int tokenLength;

    int index;

    int length;

    String data;

    LayoutParser() {
	init(null);
    }

    public void init(String layout) {
	tokenPos = 0;
	tokenLength = 0;
	index = 0;
	data = (layout != null) ? layout : "";
	length = (layout != null) ? layout.length() : 0;
    }

    public boolean hasNext() {
	return !isEof();
    }

    public void next() {
	if (data == null) {
	    tokenLength = 0;
	    return;
	}

	while (true) {
	    // skip whitespace
	    skipwhites();
	    if (isEof()) {
		tokenLength = 0;
		return;
	    }

	    // skip // comments
	    if (getchar() == '/') {
		if (nextchar() == '/') {
		    skiptoeol();
		    // goto skip whitespace
		    continue;
		} else {
		    prevchar();
		    break;
		}
	    } else
		break;
	}

	int c;
	int len = 0;
	// handle quoted strings specially
	if (getchar() == '\"') {
	    nextchar();
	    tokenPos = index;
	    while (true) {
		c = getchar();
		nextchar();
		if (c == '\"' || c == 0) {
		    tokenLength = len;
		    return;
		}
		if (len < Defines.MAX_TOKEN_CHARS) {
		    ++len;
		}
	    }
	}

	// parse a regular word
	c = getchar();
	tokenPos = index;
	do {
	    if (len < Defines.MAX_TOKEN_CHARS) {
		++len;
	    }
	    c = nextchar();
	} while (c > 32);

	if (len == Defines.MAX_TOKEN_CHARS) {
	    Com.Printf("Token exceeded " + Defines.MAX_TOKEN_CHARS
		    + " chars, discarded.\n");
	    len = 0;
	}

	tokenLength = len;
	return;
    }

    public boolean tokenEquals(String other) {
	if (tokenLength != other.length())
	    return false;
	return data.regionMatches(tokenPos, other, 0, tokenLength);
    }

    public int tokenAsInt() {
	if (tokenLength == 0)
	    return 0;
	return atoi();
    }

    public String token() {
	if (tokenLength == 0)
	    return "";
	return data.substring(tokenPos, tokenPos + tokenLength);
    }

    private int atoi() {
	int result = 0;
	boolean negative = false;
	int i = 0, max = tokenLength;
	String s = data;
	int limit;
	int multmin;
	int digit;

	if (max > 0) {
	    if (s.charAt(tokenPos) == '-') {
		negative = true;
		limit = Integer.MIN_VALUE;
		i++;
	    } else {
		limit = -Integer.MAX_VALUE;
	    }
	    multmin = limit / 10;
	    if (i < max) {
		digit = Character.digit(s.charAt(tokenPos + i++), 10);
		if (digit < 0) {
		    return 0; // wrong format
		} else {
		    result = -digit;
		}
	    }
	    while (i < max) {
		// Accumulating negatively avoids surprises near MAX_VALUE
		digit = Character.digit(s.charAt(tokenPos + i++), 10);
		if (digit < 0) {
		    return 0; // wrong format
		}
		if (result < multmin) {
		    return 0; // wrong format
		}
		result *= 10;
		if (result < limit + digit) {
		    return 0; // wrong format
		}
		result -= digit;
	    }
	} else {
	    return 0; // wrong format
	}
	if (negative) {
	    if (i > 1) {
		return result;
	    } else { /* Only got "-" */
		return 0; // wrong format
	    }
	} else {
	    return -result;
	}
    }

    private char getchar() {
	if (index < length) {
	    return data.charAt(index);
	}
	return 0;
    }

    private char nextchar() {
	++index;
	if (index < length) {
	    return data.charAt(index);
	}
	return 0;
    }

    private char prevchar() {
	if (index > 0) {
	    --index;
	    return data.charAt(index);
	}
	return 0;
    }

    private boolean isEof() {
	return index >= length;
    }

    private char skipwhites() {
	char c = 0;
	while (index < length && ((c = data.charAt(index)) <= ' ') && c != 0)
	    ++index;
	return c;
    }

    private char skiptoeol() {
	char c = 0;
	while (index < length && (c = data.charAt(index)) != '\n' && c != 0)
	    ++index;
	return c;
    }
}