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