author | tron |
Mon, 25 Apr 2005 13:52:59 +0000 | |
changeset 1728 | ea7210a31593 |
parent 1721 | e2887b445ac0 |
child 2052 | 56c0c2882ae8 |
permissions | -rw-r--r-- |
1721
e2887b445ac0
(svn r2225) -Fix: split the endian-detection to target/host. Now cross-compiling
truelight
parents:
1376
diff
changeset
|
1 |
#define STRGEN |
e2887b445ac0
(svn r2225) -Fix: split the endian-detection to target/host. Now cross-compiling
truelight
parents:
1376
diff
changeset
|
2 |
|
0 | 3 |
#include "../stdafx.h" |
4 |
#include <stdio.h> |
|
5 |
#include <string.h> |
|
6 |
#include <stdlib.h> |
|
7 |
#include <stdarg.h> |
|
8 |
||
9 |
#if !defined(WIN32) || defined(__CYGWIN__) |
|
10 |
#include <unistd.h> |
|
11 |
#endif |
|
12 |
||
181
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
13 |
#ifdef __MORPHOS__ |
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
14 |
#ifdef stderr |
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
15 |
#undef stderr |
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
16 |
#endif |
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
17 |
#define stderr stdout |
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
18 |
#endif // __MORPHOS__ |
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
19 |
|
0 | 20 |
/* Compiles a list of strings into a compiled string list */ |
21 |
||
22 |
#define lengthof(x) (sizeof(x)/sizeof(x[0])) |
|
23 |
||
24 |
typedef void (*ParseCmdProc)(char *buf, int value); |
|
25 |
||
26 |
typedef struct { |
|
27 |
uint32 ident; |
|
28 |
uint32 version; // 32-bits of auto generated version info which is basically a hash of strings.h |
|
29 |
char name[32]; // the international name of this language |
|
30 |
char own_name[32]; // the localized name of this language |
|
1376
d4e1dd8eb7e5
(svn r1880) [Codechange] Added isocodes to langfiles and support code to strgen (Lauri Nurmi)
miham
parents:
1097
diff
changeset
|
31 |
char isocode[16]; // the ISO code for the language (not country code) |
0 | 32 |
uint16 offsets[32]; // the offsets |
33 |
} LanguagePackHeader; |
|
34 |
||
35 |
typedef struct CmdStruct { |
|
36 |
const char *cmd; |
|
37 |
ParseCmdProc proc; |
|
38 |
long value; |
|
39 |
} CmdStruct; |
|
40 |
||
41 |
typedef struct LineName { |
|
42 |
struct LineName *hash_next; |
|
43 |
int value; |
|
44 |
char str[1]; |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
45 |
} LineName; |
0 | 46 |
|
47 |
int _cur_line; |
|
48 |
bool _warnings; |
|
49 |
||
50 |
uint32 _hash; |
|
1376
d4e1dd8eb7e5
(svn r1880) [Codechange] Added isocodes to langfiles and support code to strgen (Lauri Nurmi)
miham
parents:
1097
diff
changeset
|
51 |
char _lang_name[32], _lang_ownname[32], _lang_isocode[16]; |
0 | 52 |
|
53 |
#define HASH_SIZE 1023 |
|
54 |
LineName *_hash_head[HASH_SIZE]; |
|
55 |
unsigned int hash_str(const char *s) { |
|
56 |
unsigned int hash = 0; |
|
57 |
for(;*s;s++) |
|
58 |
hash = ((hash << 3) | (hash >> 29)) ^ *s; |
|
59 |
return hash % HASH_SIZE; |
|
60 |
} |
|
61 |
void hash_add(const char *s, int value) { |
|
62 |
unsigned int len = strlen(s); |
|
63 |
LineName *ln = (LineName*)malloc(sizeof(LineName) + len); |
|
64 |
unsigned int hash = hash_str(s); |
|
65 |
ln->hash_next = _hash_head[hash]; |
|
66 |
_hash_head[hash] = ln; |
|
67 |
ln->value = value; |
|
68 |
ln->str[len] = 0; |
|
69 |
memcpy(ln->str, s, len); |
|
70 |
} |
|
71 |
||
72 |
int hash_find(const char *s) { |
|
73 |
LineName *ln = _hash_head[hash_str(s)]; |
|
74 |
while (ln != NULL) { |
|
75 |
if (!strcmp(ln->str, s)) { |
|
76 |
return ln->value; |
|
77 |
} |
|
78 |
ln = ln->hash_next; |
|
79 |
} |
|
80 |
return -1; |
|
81 |
} |
|
82 |
||
83 |
void warning(const char *s, ...) { |
|
84 |
char buf[1024]; |
|
85 |
va_list va; |
|
86 |
va_start(va, s); |
|
87 |
vsprintf(buf, s, va); |
|
88 |
va_end(va); |
|
89 |
fprintf(stderr, "%d: ERROR: %s\n", _cur_line, buf); |
|
90 |
_warnings = true; |
|
91 |
} |
|
92 |
||
93 |
void NORETURN error(const char *s, ...) { |
|
94 |
char buf[1024]; |
|
95 |
va_list va; |
|
96 |
va_start(va, s); |
|
97 |
vsprintf(buf, s, va); |
|
98 |
va_end(va); |
|
99 |
fprintf(stderr, "%d: FATAL: %s\n", _cur_line, buf); |
|
100 |
exit(1); |
|
101 |
} |
|
102 |
||
103 |
void ttd_strlcpy(char *dst, const char *src, size_t len) |
|
104 |
{ |
|
105 |
assert(len > 0); |
|
106 |
while (--len && *src) |
|
107 |
*dst++=*src++; |
|
108 |
*dst = 0; |
|
109 |
} |
|
110 |
||
111 |
||
112 |
// first byte tells if it's english.lng or a custom language file |
|
113 |
// second part is the name of the string |
|
114 |
// third part is the actual string contents |
|
115 |
char *allstr[65536]; |
|
116 |
||
117 |
byte _put_buf[4096]; |
|
118 |
int _put_pos; |
|
119 |
int _next_string_id; |
|
120 |
||
121 |
void put_byte(byte c) { |
|
122 |
if (_put_pos == lengthof(_put_buf)) |
|
123 |
error("Put buffer too small"); |
|
124 |
_put_buf[_put_pos++] = c; |
|
125 |
} |
|
126 |
||
127 |
void emit_buf(int ent) { |
|
128 |
char *s; |
|
129 |
||
130 |
if (ent < 0 || ent >= 0x10000) { |
|
131 |
warning("Invalid string ID %d\n", ent); |
|
132 |
return; |
|
133 |
} |
|
134 |
||
135 |
if (allstr[ent] != 0) { |
|
136 |
warning("Duplicate string ID %d\n", ent); |
|
137 |
return; |
|
138 |
} |
|
139 |
||
140 |
// Allocate the string, and put the uint16 before with the length. |
|
141 |
s = (char*)malloc(sizeof(uint16) + _put_pos); |
|
142 |
*((uint16*)s) = _put_pos; |
|
143 |
memcpy(s + sizeof(uint16), _put_buf, _put_pos); |
|
144 |
||
145 |
allstr[ent] = s; |
|
146 |
||
147 |
_put_pos = 0; |
|
148 |
} |
|
149 |
||
150 |
void EmitSingleByte(char *buf, int value) { |
|
151 |
if (*buf != 0) |
|
152 |
warning("Ignoring trailing letters in command"); |
|
153 |
put_byte((byte)value); |
|
154 |
} |
|
155 |
||
156 |
void EmitEscapedByte(char *buf, int value) { |
|
157 |
if (*buf != 0) |
|
158 |
warning("Ignoring trailing letters in command"); |
|
159 |
put_byte((byte)0x85); |
|
160 |
put_byte((byte)value); |
|
161 |
} |
|
162 |
||
163 |
||
164 |
void EmitStringInl(char *buf, int value) { |
|
165 |
int id; |
|
166 |
||
167 |
if (*buf>='0' && *buf <= '9') { |
|
168 |
id = strtol(buf, NULL, 0); |
|
169 |
if (id < 0 || id>=0x10000) { |
|
170 |
warning("Invalid inline num %s\n", buf); |
|
171 |
return; |
|
172 |
} |
|
173 |
} else { |
|
174 |
id = hash_find(buf); |
|
175 |
if (id == -1) { |
|
176 |
warning("Invalid inline string '%s'", buf); |
|
177 |
return; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
put_byte(0x81); |
|
182 |
put_byte((byte)(id & 0xFF)); |
|
183 |
put_byte((byte)(id >> 8)); |
|
184 |
} |
|
185 |
||
186 |
void EmitSetX(char *buf, int value) { |
|
187 |
char *err; |
|
188 |
int x = strtol(buf, &err, 0); |
|
189 |
if (*err != 0) |
|
190 |
error("SetX param invalid"); |
|
191 |
put_byte(1); |
|
192 |
put_byte((byte)x); |
|
193 |
} |
|
194 |
||
195 |
void EmitSetXY(char *buf, int value) { |
|
196 |
char *err; |
|
197 |
int x = strtol(buf, &err, 0), y; |
|
198 |
if (*err != 0) error("SetXY param invalid"); |
|
199 |
y = strtol(err+1, &err, 0); |
|
200 |
if (*err != 0) error("SetXY param invalid"); |
|
201 |
||
202 |
put_byte(0x1F); |
|
203 |
put_byte((byte)x); |
|
204 |
put_byte((byte)y); |
|
205 |
} |
|
206 |
||
207 |
static const CmdStruct _cmd_structs[] = { |
|
208 |
// Update position |
|
209 |
{"SETX", EmitSetX, 1}, |
|
210 |
{"SETXY", EmitSetXY, 2}, |
|
211 |
||
212 |
// Font size |
|
213 |
{"TINYFONT", EmitSingleByte, 8}, |
|
214 |
{"BIGFONT", EmitSingleByte, 9}, |
|
215 |
||
216 |
// New line |
|
217 |
{"", EmitSingleByte, 10}, |
|
218 |
||
219 |
// Colors |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
220 |
{"BLUE", EmitSingleByte, 15}, |
0 | 221 |
{"SILVER", EmitSingleByte, 16}, |
222 |
{"GOLD", EmitSingleByte, 17}, |
|
223 |
{"RED", EmitSingleByte, 18}, |
|
224 |
{"PURPLE", EmitSingleByte, 19}, |
|
225 |
{"LTBROWN", EmitSingleByte, 20}, |
|
226 |
{"ORANGE", EmitSingleByte, 21}, |
|
227 |
{"GREEN", EmitSingleByte, 22}, |
|
228 |
{"YELLOW", EmitSingleByte, 23}, |
|
229 |
{"DKGREEN", EmitSingleByte, 24}, |
|
230 |
{"CREAM", EmitSingleByte, 25}, |
|
231 |
{"BROWN", EmitSingleByte, 26}, |
|
232 |
{"WHITE", EmitSingleByte, 27}, |
|
233 |
{"LTBLUE", EmitSingleByte, 28}, |
|
234 |
{"GRAY", EmitSingleByte, 29}, |
|
235 |
{"DKBLUE", EmitSingleByte, 30}, |
|
236 |
{"BLACK", EmitSingleByte, 31}, |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
237 |
|
0 | 238 |
// 0x7B=123 is the LAST special character we may use. |
239 |
||
240 |
// Numbers |
|
241 |
{"COMMA32", EmitSingleByte, 0x7B}, |
|
242 |
{"COMMA16", EmitSingleByte, 0x7C}, |
|
243 |
{"COMMA8", EmitSingleByte, 0x7D}, |
|
244 |
{"NUMU16", EmitSingleByte, 0x7E}, |
|
245 |
||
246 |
{"CURRENCY", EmitSingleByte, 0x7F}, |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
247 |
|
236
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
248 |
// 0x85 |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
249 |
{"CURRCOMPACT", EmitEscapedByte, 0}, // compact currency (32 bits) |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
250 |
{"INT32", EmitEscapedByte, 1}, // signed 32 bit integer |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
251 |
{"REV", EmitEscapedByte, 2}, // openttd revision string |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
252 |
{"SHORTCARGO", EmitEscapedByte, 3}, // short cargo description, only ### tons, or ### litres |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
253 |
{"CURRCOMPACT64", EmitEscapedByte, 4}, // compact currency 64 bits |
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
254 |
|
0 | 255 |
{"STRINL", EmitStringInl, 0x81}, |
256 |
||
257 |
{"DATE_LONG", EmitSingleByte, 0x82}, |
|
258 |
{"DATE_SHORT", EmitSingleByte, 0x83}, |
|
259 |
||
260 |
{"VELOCITY", EmitSingleByte, 0x84}, |
|
261 |
{"SKIP16", EmitSingleByte, 0x85}, |
|
262 |
{"SKIP", EmitSingleByte, 0x86}, |
|
263 |
{"VOLUME", EmitSingleByte, 0x87}, |
|
264 |
||
265 |
{"STRING", EmitSingleByte, 0x88}, |
|
266 |
||
267 |
{"CARGO", EmitSingleByte, 0x99}, |
|
268 |
{"STATION", EmitSingleByte, 0x9A}, |
|
269 |
{"TOWN", EmitSingleByte, 0x9B}, |
|
270 |
{"CURRENCY64", EmitSingleByte, 0x9C}, |
|
395
788a9bba0889
(svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents:
250
diff
changeset
|
271 |
{"WAYPOINT", EmitSingleByte, 0x9D}, // waypoint name |
1097
196431cb50fe
(svn r1598) Feature: Message history now is stickyable and resizeable
dominik
parents:
520
diff
changeset
|
272 |
{"DATE_TINY", EmitSingleByte, 0x9E}, |
0 | 273 |
// 0x9E=158 is the LAST special character we may use. |
274 |
||
275 |
{"UPARROW", EmitSingleByte, 0xA0}, |
|
276 |
{"POUNDSIGN", EmitSingleByte, 0xA3}, |
|
277 |
{"YENSIGN", EmitSingleByte, 0xA5}, |
|
278 |
{"COPYRIGHT", EmitSingleByte, 0xA9}, |
|
279 |
{"DOWNARROW", EmitSingleByte, 0xAA}, |
|
280 |
{"CHECKMARK", EmitSingleByte, 0xAC}, |
|
281 |
{"CROSS", EmitSingleByte, 0xAD}, |
|
282 |
{"RIGHTARROW", EmitSingleByte, 0xAF}, |
|
283 |
||
520
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
284 |
{"TRAIN", EmitSingleByte, 0xb4}, |
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
285 |
{"LORRY", EmitSingleByte, 0xb5}, |
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
286 |
{"BUS", EmitSingleByte, 0xb6}, |
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
287 |
{"PLANE", EmitSingleByte, 0xb7}, |
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
288 |
{"SHIP", EmitSingleByte, 0xb8}, |
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
289 |
|
0 | 290 |
{"SMALLUPARROW", EmitSingleByte, 0xBC}, |
291 |
{"SMALLDOWNARROW", EmitSingleByte, 0xBD}, |
|
292 |
{"THREE_FOURTH", EmitSingleByte, 0xBE}, |
|
293 |
}; |
|
294 |
||
295 |
const CmdStruct *find_cmd(const char *s, int len) { |
|
296 |
int i; |
|
297 |
const CmdStruct *cs = _cmd_structs; |
|
298 |
for(i=0; i!=lengthof(_cmd_structs); i++,cs++) { |
|
299 |
if (!strncmp(cs->cmd, s, len) && cs->cmd[len] == 0) |
|
300 |
return cs; |
|
301 |
} |
|
302 |
return NULL; |
|
303 |
} |
|
304 |
||
305 |
||
306 |
// returns 0 on EOL |
|
307 |
// returns 1-255 on literal byte |
|
308 |
// else returns command struct |
|
309 |
const CmdStruct *parse_command_string(char **str, char *param, const char *errortext) |
|
310 |
{ |
|
311 |
char *s = *str, *start; |
|
312 |
byte c = *s++; |
|
313 |
const CmdStruct *cmd; |
|
314 |
||
315 |
if (c != '{') { |
|
316 |
*str = s; |
|
317 |
return (CmdStruct*) (int)c; |
|
318 |
} |
|
319 |
||
320 |
// parse command name |
|
321 |
start = s; |
|
322 |
for(;;) { |
|
323 |
c = *s++; |
|
324 |
if (c == '}' || c == ' ') |
|
325 |
break; |
|
326 |
if (c == 0) { |
|
327 |
if (errortext) warning("Missing } from command '%s' in '%s'", start, errortext); |
|
328 |
return NULL; |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
cmd = find_cmd(start, s - start - 1); |
|
333 |
if (cmd == NULL) { |
|
334 |
if (errortext) warning("Undefined command '%.*s' in '%s'", s - start - 1, start, errortext); |
|
335 |
return NULL; |
|
336 |
} |
|
337 |
||
338 |
if (c == ' ') { |
|
339 |
// copy params |
|
340 |
start = s; |
|
341 |
for(;;) { |
|
342 |
c = *s++; |
|
343 |
if (c == '}') break; |
|
344 |
if (c == 0) { |
|
345 |
if (errortext) warning("Missing } from command '%s' in '%s'", start, errortext); |
|
346 |
return NULL; |
|
347 |
} |
|
348 |
*param++ = c; |
|
349 |
} |
|
350 |
||
351 |
} |
|
352 |
*param = 0; |
|
353 |
||
354 |
*str = s; |
|
355 |
||
356 |
return cmd; |
|
357 |
} |
|
358 |
||
359 |
||
360 |
void handle_pragma(char *str) |
|
361 |
{ |
|
362 |
if (!memcmp(str, "id ", 3)) { |
|
363 |
_next_string_id = strtoul(str + 3, NULL, 0); |
|
364 |
} else if (!memcmp(str, "name ", 5)) { |
|
365 |
ttd_strlcpy(_lang_name, str + 5, sizeof(_lang_name)); |
|
366 |
} else if (!memcmp(str, "ownname ", 8)) { |
|
367 |
ttd_strlcpy(_lang_ownname, str + 8, sizeof(_lang_ownname)); |
|
1376
d4e1dd8eb7e5
(svn r1880) [Codechange] Added isocodes to langfiles and support code to strgen (Lauri Nurmi)
miham
parents:
1097
diff
changeset
|
368 |
} else if (!memcmp(str, "isocode ", 8)) { |
d4e1dd8eb7e5
(svn r1880) [Codechange] Added isocodes to langfiles and support code to strgen (Lauri Nurmi)
miham
parents:
1097
diff
changeset
|
369 |
ttd_strlcpy(_lang_isocode, str + 8, sizeof(_lang_isocode)); |
0 | 370 |
} else { |
371 |
error("unknown pragma '%s'", str); |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
bool check_commands_match(char *a, char *b) |
|
376 |
{ |
|
377 |
const CmdStruct *ar, *br; |
|
378 |
char param[100]; |
|
379 |
||
380 |
do { |
|
381 |
// read until next command from a. |
|
382 |
do { |
|
383 |
ar = parse_command_string(&a, param, NULL); |
|
384 |
} while (ar != NULL && (unsigned long)ar <= 255); |
|
385 |
||
386 |
// read until next command from b. |
|
387 |
do { |
|
388 |
br = parse_command_string(&b, param, NULL); |
|
389 |
} while (br != NULL && (unsigned long)br <= 255); |
|
390 |
||
391 |
// make sure they are identical |
|
392 |
if (ar != br) return false; |
|
393 |
} while (ar); |
|
394 |
||
395 |
return true; |
|
396 |
} |
|
397 |
||
398 |
void handle_string(char *str, bool master) { |
|
399 |
char *s,*t,*r; |
|
400 |
int ent; |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
401 |
|
0 | 402 |
if (*str == '#') { |
403 |
if (str[1] == '#' && str[2] != '#') |
|
404 |
handle_pragma(str + 2); |
|
405 |
return; |
|
406 |
} |
|
407 |
||
408 |
// Ignore comments & blank lines |
|
409 |
if (*str == ';' || *str == ' ' || *str == 0) |
|
410 |
return; |
|
411 |
||
412 |
s = strchr(str, ':'); |
|
413 |
if (s == NULL) { |
|
414 |
warning("Line has no ':' delimiter"); |
|
415 |
return; |
|
416 |
} |
|
417 |
||
418 |
// Trim spaces |
|
419 |
for(t=s;t > str && (t[-1]==' ' || t[-1]=='\t'); t--); |
|
420 |
*t = 0; |
|
421 |
||
422 |
ent = hash_find(str); |
|
423 |
||
424 |
s++; // skip : |
|
425 |
||
426 |
// allocate string entry |
|
427 |
r = malloc(strlen(str) + strlen(s) + 3); |
|
428 |
*r = master; |
|
429 |
strcpy(r + 1, str); |
|
430 |
strcpy(r + 2 + strlen(str), s); |
|
431 |
||
432 |
if (master) { |
|
433 |
if (ent != -1) { |
|
434 |
warning("String name '%s' is used multiple times", str); |
|
435 |
return; |
|
436 |
} |
|
437 |
||
438 |
ent = _next_string_id++; |
|
439 |
if (allstr[ent]) { |
|
440 |
warning("String ID 0x%X for '%s' already in use by '%s'", ent, str, allstr[ent] + 1); |
|
441 |
return; |
|
442 |
} |
|
443 |
allstr[ent] = r; |
|
444 |
||
445 |
// add to hash table |
|
446 |
hash_add(str, ent); |
|
447 |
} else { |
|
448 |
if (ent == -1) { |
|
449 |
warning("String name '%s' does not exist in master file", str); |
|
450 |
_warnings = 0; // non-fatal |
|
451 |
return; |
|
452 |
} |
|
453 |
||
454 |
if (!allstr[ent][0]) { |
|
455 |
warning("String name '%s' is used multiple times", str); |
|
456 |
_warnings = 0; // non-fatal |
|
457 |
return; |
|
458 |
} |
|
459 |
||
460 |
// check that the commands match |
|
461 |
if (!check_commands_match(s, allstr[ent] + 2 + strlen(allstr[ent] + 1))) { |
|
462 |
fprintf(stderr, "Warning: String name '%s' does not match the layout of the master string\n", str); |
|
463 |
_warnings = 0; // non-fatal |
|
464 |
return; |
|
465 |
} |
|
466 |
||
467 |
if (s[0] == ':' && s[1] == 0) { |
|
468 |
allstr[ent][0] = 0; // use string from master file legitiately |
|
469 |
free(r); |
|
470 |
} else { |
|
471 |
free(allstr[ent]); |
|
472 |
allstr[ent] = r; |
|
473 |
} |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
uint32 my_hash_str(uint32 hash, const char *s) |
|
478 |
{ |
|
479 |
for(;*s;s++) { |
|
480 |
hash = ((hash << 3) | (hash >> 29)) ^ *s; |
|
481 |
if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1; |
|
482 |
} |
|
483 |
return hash; |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
484 |
|
0 | 485 |
} |
486 |
||
487 |
void parse_file(const char *file, bool english) { |
|
488 |
char buf[2048]; |
|
489 |
int i; |
|
490 |
FILE *in; |
|
491 |
||
492 |
in = fopen(file, "r"); |
|
493 |
if (in == NULL) { error("Cannot open file '%s'", file); } |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
494 |
|
0 | 495 |
_cur_line = 1; |
496 |
while (fgets(buf, sizeof(buf),in) != NULL) { |
|
497 |
i = strlen(buf); |
|
498 |
while (i>0 && (buf[i-1]=='\r' || buf[i-1]=='\n' || buf[i-1] == ' ')) i--; |
|
499 |
buf[i] = 0; |
|
500 |
||
501 |
handle_string(buf, english); |
|
502 |
_cur_line++; |
|
503 |
} |
|
504 |
||
505 |
fclose(in); |
|
506 |
||
507 |
// make a hash of the file to get a unique "version number" |
|
508 |
if (english) { |
|
509 |
uint32 hash = 0; |
|
510 |
char *s; |
|
511 |
const CmdStruct *cs; |
|
512 |
for(i = 0; i!=65536; i++) { |
|
513 |
if ((s=allstr[i]) != NULL) { |
|
514 |
hash ^= i * 0x717239; |
|
515 |
if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1; |
|
516 |
hash = my_hash_str(hash, s + 1); |
|
517 |
||
518 |
s = s + 2 + strlen(s + 1); |
|
519 |
while ((cs = parse_command_string(&s, buf, NULL)) != 0) { |
|
520 |
if ( (uint)cs >= 256) { |
|
521 |
hash ^= (cs - _cmd_structs) * 0x1234567; |
|
522 |
if (hash & 1) hash = (hash>>1) ^ 0xF00BAA4; else hash >>= 1; |
|
523 |
} |
|
524 |
} |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
525 |
} |
0 | 526 |
} |
527 |
_hash = hash; |
|
528 |
} |
|
529 |
} |
|
530 |
||
531 |
int count_inuse(int grp) { |
|
532 |
int i; |
|
533 |
||
534 |
for(i=0x800; --i >= 0;) { |
|
535 |
if (allstr[(grp<<11)+i] != NULL) |
|
536 |
break; |
|
537 |
} |
|
538 |
return i + 1; |
|
539 |
} |
|
540 |
||
541 |
void check_all_used() { |
|
542 |
int i; |
|
543 |
LineName *ln; |
|
544 |
int num_warn = 10; |
|
545 |
||
546 |
for (i=0; i!=HASH_SIZE; i++) { |
|
547 |
for(ln = _hash_head[i]; ln!=NULL; ln = ln->hash_next) { |
|
548 |
if (allstr[ln->value] == 0) { |
|
549 |
if (++num_warn < 50) { |
|
550 |
warning("String %s has no definition. Using NULL value", ln->str); |
|
551 |
} |
|
552 |
_put_pos = 0; |
|
553 |
emit_buf(ln->value); |
|
554 |
} |
|
555 |
} |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
void write_length(FILE *f, uint length) |
|
560 |
{ |
|
561 |
if (length < 0xC0) { |
|
562 |
fputc(length, f); |
|
563 |
} else if (length < 0x4000) { |
|
564 |
fputc((length >> 8) | 0xC0, f); |
|
565 |
fputc(length & 0xFF, f); |
|
566 |
} else { |
|
567 |
error("string too long"); |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
void gen_output(FILE *f) { |
|
572 |
uint16 in_use[32]; |
|
573 |
uint16 in_use_file[32]; |
|
574 |
int i,j; |
|
575 |
int tot_str = 0; |
|
576 |
||
577 |
check_all_used(); |
|
578 |
||
579 |
for(i=0; i!=32; i++) { |
|
580 |
int n = count_inuse(i); |
|
581 |
in_use[i] = n; |
|
582 |
in_use_file[i] = TO_LE16(n); |
|
583 |
tot_str += n; |
|
584 |
} |
|
585 |
||
586 |
fwrite(in_use_file, 32*sizeof(uint16), 1, f); |
|
587 |
||
588 |
for(i=0; i!=32; i++) { |
|
589 |
for(j=0; j!=in_use[i]; j++) { |
|
590 |
char *s = allstr[(i<<11)+j]; |
|
591 |
if (s == NULL) error("Internal error, s==NULL"); |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
592 |
|
0 | 593 |
write_length(f, *(uint16*)s); |
594 |
fwrite(s + sizeof(uint16), *(uint16*)s , 1, f); |
|
595 |
tot_str--; |
|
596 |
} |
|
597 |
} |
|
598 |
||
599 |
fputc(0, f); // write trailing nul character. |
|
600 |
||
601 |
if (tot_str != 0) { |
|
602 |
error("Internal error, tot_str != 0"); |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
bool compare_files(const char *n1, const char *n2) |
|
607 |
{ |
|
608 |
FILE *f1, *f2; |
|
609 |
char b1[4096]; |
|
610 |
char b2[4096]; |
|
611 |
size_t l1, l2; |
|
612 |
||
613 |
f2 = fopen(n2, "rb"); |
|
614 |
if (f2 == NULL) return false; |
|
615 |
||
616 |
f1 = fopen(n1, "rb"); |
|
617 |
if (f1 == NULL) error("can't open %s", n1); |
|
618 |
||
619 |
do { |
|
620 |
l1 = fread(b1, 1, sizeof(b1), f1); |
|
621 |
l2 = fread(b2, 1, sizeof(b2), f2); |
|
622 |
||
623 |
if (l1 != l2 || memcmp(b1, b2, l1)) { |
|
624 |
fclose(f2); |
|
625 |
fclose(f1); |
|
626 |
return false; |
|
627 |
} |
|
628 |
} while (l1); |
|
629 |
||
630 |
fclose(f2); |
|
631 |
fclose(f1); |
|
632 |
return true; |
|
633 |
} |
|
634 |
||
635 |
void write_strings_h(const char *filename) |
|
636 |
{ |
|
637 |
FILE *out; |
|
638 |
int i; |
|
639 |
int next = -1; |
|
640 |
int lastgrp; |
|
641 |
||
642 |
out = fopen("tmp.xxx", "w"); |
|
643 |
if (out == NULL) { error("can't open tmp.xxx"); } |
|
644 |
||
645 |
fprintf(out, "enum {"); |
|
646 |
||
647 |
lastgrp = 0; |
|
648 |
||
649 |
for(i = 0; i!=65536; i++) { |
|
650 |
if (allstr[i]) { |
|
651 |
if (lastgrp != (i >> 11)) { |
|
652 |
lastgrp = (i >> 11); |
|
653 |
fprintf(out, "};\n\nenum {"); |
|
654 |
} |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
655 |
|
0 | 656 |
fprintf(out, next == i ? "%s,\n" : "\n%s = 0x%X,\n", allstr[i] + 1, i); |
657 |
next = i + 1; |
|
658 |
} |
|
659 |
} |
|
660 |
||
661 |
fprintf(out, "};\n"); |
|
662 |
||
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
663 |
fprintf(out, |
0 | 664 |
"\nenum {\n" |
181
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
665 |
"\tLANGUAGE_PACK_IDENT = 0x474E414C, // Big Endian value for 'LANG' (LE is 0x 4C 41 4E 47)\n" |
0 | 666 |
"\tLANGUAGE_PACK_VERSION = 0x%X,\n" |
667 |
"};\n", (unsigned int)_hash); |
|
668 |
||
669 |
||
670 |
fclose(out); |
|
671 |
||
672 |
if (compare_files("tmp.xxx", filename)) { |
|
673 |
// files are equal. tmp.xxx is not needed |
|
674 |
unlink("tmp.xxx"); |
|
675 |
} else { |
|
676 |
// else rename tmp.xxx into filename |
|
677 |
#if defined(WIN32) |
|
678 |
unlink(filename); |
|
679 |
#endif |
|
680 |
if (rename("tmp.xxx", filename) == -1) error("rename() failed"); |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
void write_langfile(const char *filename, int show_todo) |
|
685 |
{ |
|
686 |
FILE *f; |
|
687 |
int in_use[32]; |
|
688 |
LanguagePackHeader hdr; |
|
689 |
int i,j; |
|
690 |
const CmdStruct *cs; |
|
691 |
char param[128]; |
|
692 |
||
693 |
f = fopen(filename, "wb"); |
|
694 |
if (f == NULL) error("can't open %s", filename); |
|
695 |
||
696 |
memset(&hdr, 0, sizeof(hdr)); |
|
697 |
for(i=0; i!=32; i++) { |
|
698 |
int n = count_inuse(i); |
|
699 |
in_use[i] = n; |
|
700 |
hdr.offsets[i] = TO_LE16(n); |
|
701 |
} |
|
702 |
||
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
703 |
// see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...) |
181
0b95567f2d0f
(svn r182) -Fix: [1024380] strgen diff (Warning fix + MorphOS fix). Removes the longest standing warning on compilation of strgen (tokai)
darkvater
parents:
0
diff
changeset
|
704 |
hdr.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG' |
0 | 705 |
hdr.version = TO_LE32(_hash); |
706 |
strcpy(hdr.name, _lang_name); |
|
707 |
strcpy(hdr.own_name, _lang_ownname); |
|
1376
d4e1dd8eb7e5
(svn r1880) [Codechange] Added isocodes to langfiles and support code to strgen (Lauri Nurmi)
miham
parents:
1097
diff
changeset
|
708 |
strcpy(hdr.isocode, _lang_isocode); |
0 | 709 |
|
710 |
fwrite(&hdr, sizeof(hdr), 1, f); |
|
711 |
||
712 |
for(i=0; i!=32; i++) { |
|
713 |
for(j=0; j!=in_use[i]; j++) { |
|
714 |
char *s = allstr[(i<<11)+j], *str; |
|
715 |
||
716 |
if (s == NULL) { |
|
717 |
write_length(f, 0); |
|
718 |
} else { |
|
719 |
// move to string |
|
720 |
str = s + 2 + strlen(s + 1); |
|
721 |
||
722 |
if (show_todo && s[0]) { |
|
723 |
if (show_todo == 2) { |
|
724 |
fprintf(stderr, "Warning:%s: String '%s' is untranslated\n", filename, s + 1); |
|
725 |
} else { |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
726 |
const char *s = "<TODO> "; |
0 | 727 |
while(*s) put_byte(*s++); |
728 |
} |
|
729 |
} |
|
730 |
||
731 |
for(;;) { |
|
732 |
cs = parse_command_string(&str, param, s[0] ? "english.lng" : filename); |
|
733 |
if (cs == NULL) break; |
|
734 |
if ( (unsigned long) cs <= 255) { |
|
735 |
put_byte( (byte) (int)cs); |
|
736 |
} else { |
|
737 |
cs->proc(param, cs->value); |
|
738 |
} |
|
739 |
} |
|
740 |
||
741 |
write_length(f, _put_pos); |
|
742 |
fwrite(_put_buf, 1, _put_pos, f); |
|
743 |
_put_pos = 0; |
|
744 |
} |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
fputc(0, f); |
|
749 |
||
750 |
fclose(f); |
|
751 |
} |
|
752 |
||
250
733e18ca6e93
(svn r251) -Project ready for 0.3.4 release. Updated installer, readme and fileinformation
darkvater
parents:
236
diff
changeset
|
753 |
int CDECL main(int argc, char* argv[]) |
0 | 754 |
{ |
755 |
char *r; |
|
756 |
char buf[256]; |
|
757 |
int show_todo = 0; |
|
758 |
||
759 |
if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) { |
|
520
27396ea4b00b
(svn r847) Add the station type icons to strgen: {TRAIN}, {LORRY}, {BUS}, {PLANE} and {SHIP}
tron
parents:
451
diff
changeset
|
760 |
puts("$Revision$"); |
0 | 761 |
return 0; |
762 |
} |
|
763 |
||
764 |
if (argc > 1 && !strcmp(argv[1], "-t")) { |
|
765 |
show_todo = 1; |
|
766 |
argc--, argv++; |
|
767 |
} |
|
768 |
||
769 |
if (argc > 1 && !strcmp(argv[1], "-w")) { |
|
770 |
show_todo = 2; |
|
771 |
argc--, argv++; |
|
772 |
} |
|
773 |
||
774 |
||
775 |
if (argc == 1) { |
|
776 |
// parse master file |
|
777 |
parse_file("lang/english.txt", true); |
|
778 |
if (_warnings) return 1; |
|
779 |
||
780 |
// write english.lng and strings.h |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
781 |
|
0 | 782 |
write_langfile("lang/english.lng", 0); |
783 |
write_strings_h("table/strings.h"); |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
181
diff
changeset
|
784 |
|
0 | 785 |
} else if (argc == 2) { |
786 |
parse_file("lang/english.txt", true); |
|
787 |
parse_file(argv[1], false); |
|
788 |
if (_warnings) return 1; |
|
789 |
strcpy(buf, argv[1]); |
|
790 |
r = strrchr(buf, '.'); |
|
791 |
if (!r || strcmp(r, ".txt")) r = strchr(buf, 0); |
|
792 |
strcpy(r, ".lng"); |
|
793 |
write_langfile(buf, show_todo); |
|
794 |
} else { |
|
795 |
fprintf(stderr, "invalid arguments\n"); |
|
796 |
} |
|
797 |
||
798 |
return 0; |
|
799 |
} |
|
800 |