Debian Codegraph @

farlng/src/lng.generator.cpp

raw
1 /*
2 Copyright (c) 2003 WARP ItSelf
3 Copyright (c) 2005 WARP ItSelf & Alex Yaroslavsky
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the authors may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "lng.common.h"
29 #include <vector>
30 #include <memory>
31 #include <sys/stat.h>
32 #include <KeyFileHelper.h>
33 #include <utils.h>
34 #define VERSION "v1.5"
35
36 void UnquoteIfNeeded(char *lpStr)
37 {
38 int nLen = strlen(lpStr);
39
40 if (*lpStr != '"' || lpStr[nLen - 1] != '"')
41 return;
42
43 int i = 0;
44
45 for (; i < nLen - 2; i++)
46 lpStr[i] = lpStr[i + 1];
47
48 lpStr[i] = 0;
49 }
50
51 struct LanguageEntry
52 {
53 char *lpLNGFileName;
54 char *lpLNGFileNameTemp;
55
56 char *lpLanguageName;
57 char *lpLanguageDescription;
58
59 int hLNGFile;
60
61 uint32_t dwCRC32;
62 uint32_t dwOldCRC32;
63
64 int cNeedUpdate;
65
66 int nEncoding;
67 };
68
69 bool ReadFromBuffer(char *&lpStart, char *&lpEnd, bool bProcessEscape = true)
70 {
71 bool bInQuotes = false;
72 bool bEscape = false;
73
74 while (true) {
75 while (*lpStart
76 && ((*lpStart == ' ') || (*lpStart == '\n') || (*lpStart == '\r') || (*lpStart == '\t')))
77 lpStart++;
78
79 if (*lpStart == '#') {
80 while (*lpStart != '\r' && *lpStart != '\n')
81 lpStart++;
82 } else
83 break;
84 }
85
86 if (*lpStart) {
87 lpEnd = lpStart;
88
89 do {
90 if (bProcessEscape) {
91 if ((*lpEnd == '"') && !bEscape)
92 bInQuotes = !bInQuotes;
93
94 if (((*lpEnd == ' ') || (*lpEnd == '\t')) && !bInQuotes)
95 break;
96
97 if ((*lpEnd == '\\') && !bEscape && bInQuotes)
98 bEscape = true;
99 else
100 bEscape = false;
101 }
102
103 if ((*lpEnd == '\n') || (*lpEnd == '\r'))
104 break;
105
106 lpEnd++;
107
108 } while (*lpEnd);
109
110 return true;
111 }
112
113 return false;
114 }
115
116 bool ReadFromBufferEx(char *&lpStart, char **lpParam, bool bProcessEscape = true)
117 {
118 char *lpEnd = lpStart;
119
120 if (ReadFromBuffer(lpStart, lpEnd, bProcessEscape)) {
121 *lpParam = (char *)malloc(lpEnd - lpStart + 1);
122 memset(*lpParam, 0, lpEnd - lpStart + 1);
123 memcpy(*lpParam, lpStart, lpEnd - lpStart);
124 lpStart = lpEnd;
125
126 return true;
127 }
128
129 return false;
130 }
131
132 bool ReadComments(char *&lpRealStart, char **lpParam, const char *lpCmtMark, const char *lpCmtReplace)
133 {
134 char *lpStart = lpRealStart;
135 char *lpEnd = lpStart;
136 int dwSize = 1;
137 int nCmtMarkLen = strlen(lpCmtMark);
138 int nCmtReplaceLen = strlen(lpCmtReplace);
139 bool bFirst = true;
140
141 *lpParam = NULL;
142
143 while (true) {
144 if (ReadFromBuffer(lpStart, lpEnd, false)) {
145 if (strncmp(lpStart, lpCmtMark, nCmtMarkLen))
146 break;
147
148 dwSize+= lpEnd - lpStart + 2 + nCmtReplaceLen - nCmtMarkLen;
149
150 *lpParam = (char *)realloc(*lpParam, dwSize);
151
152 if (bFirst) {
153 **lpParam = 0;
154 bFirst = false;
155 }
156
157 strcat(*lpParam, lpCmtReplace);
158 strncat(*lpParam, lpStart + nCmtMarkLen, lpEnd - lpStart - nCmtMarkLen);
159 strcat(*lpParam, "\r\n");
160
161 lpRealStart = lpStart = lpEnd;
162 } else
163 break;
164 }
165
166 return (*lpParam == NULL ? false : true);
167 }
168
169 void SmartWrite(int hFile, const char *lpStr, uint32_t *pCRC32)
170 {
171 size_t dwWritten = WriteAll(hFile, lpStr, strlen(lpStr));
172 *pCRC32 = CRC32(*pCRC32, lpStr, dwWritten);
173 }
174
175 static char *GetTempName(const char *base_name)
176 {
177 char *out = (char *)malloc(strlen(base_name) + 8);
178 sprintf(out, "%s.tmp", base_name);
179 return out;
180 }
181
182 int ReadInteger(char *&lpStart)
183 {
184 char *lpTemp;
185
186 ReadFromBufferEx(lpStart, &lpTemp);
187 int nResult = atol(lpTemp);
188
189 free(lpTemp);
190
191 return nResult;
192 }
193
194 void WriteSignatureIfNeeded(int hFile)
195 {
196 uint32_t dwID = 0xBFBBEF;
197 WriteAll(hFile, &dwID, 3);
198 }
199
200 bool CheckExists(const char *path)
201 {
202 struct stat s = {0};
203 return stat(path, &s) == 0;
204 }
205
206 int main_generator(int argc, char **argv)
207 {
208 printf(".LNG Generator " VERSION "\n");
209 printf("Copyright (C) 2003-2009 WARP ItSelf\n");
210 printf("Copyright (C) 2005 WARP ItSelf & Alex Yaroslavsky\n\n");
211
212 if (argc < 2) {
213 printf("Usage: generator [options] feed_file\n");
214 printf("\nOptions:\n");
215 printf("\t-i filename - optional ini file with update status.\n");
216 printf("\t-ol output_path - language files output path.\n");
217 printf("\t-oh output_path - header file output path.\n");
218 printf("\t-nc - don't write copyright info to generated files.\n");
219 return 0;
220 }
221
222 std::unique_ptr<KeyFileHelper> key_file;
223
224 char *lpLNGOutputPath = NULL;
225 char *lpHOutputPath = NULL;
226
227 bool bWriteCopyright = true;
228
229 if (argc > 2) {
230 for (int i = 1; i < argc - 1; i++) {
231 if (strcmp(argv[i], "-nc") == 0)
232 bWriteCopyright = false;
233 else
234
235 if (strcmp(argv[i], "-i") == 0 && ++i < argc - 1) {
236 key_file.reset(new KeyFileHelper(argv[i]));
237 // lpIniFileName = (char*)malloc (MAX_PATH);
238 // GetFullPathName (argv[i], MAX_PATH, lpIniFileName, NULL);
239 } else
240
241 if (strcmp(argv[i], "-ol") == 0 && ++i < argc - 1) {
242 lpLNGOutputPath = strdup(argv[i]);
243 UnquoteIfNeeded(lpLNGOutputPath);
244 } else
245
246 if (strcmp(argv[i], "-oh") == 0 && ++i < argc - 1) {
247 lpHOutputPath = strdup(argv[i]);
248
249 UnquoteIfNeeded(lpHOutputPath);
250 }
251
252 else {
253 fprintf(stderr, "Bad argument: %s\n", argv[i]);
254 return -1;
255 }
256 }
257 }
258
259 fprintf(stderr, "lpLNGOutputPath=%s\n", lpLNGOutputPath);
260
261 FDScope hFeedFile(OpenInputFile(argv[argc - 1]));
262 if (!hFeedFile.Valid()) {
263 printf("ERROR: Can't open the feed file, exiting.\n");
264 return 0;
265 }
266
267 size_t dwSize = QueryFileSize(hFeedFile);
268
269 if (dwSize >= 3) {
270 uint32_t dwID = 0;
271 ReadAll(hFeedFile, &dwID, 3);
272 if (((dwID & 0x00FFFFFF) == 0xBFBBEF))
273 dwSize-= 3;
274 else
275 lseek(hFeedFile, 0, SEEK_SET);
276 }
277
278 bool bUpdate;
279
280 LanguageEntry *pLangEntries;
281
282 uint32_t dwHeaderCRC32;
283 uint32_t dwHeaderOldCRC32;
284
285 char *lpHPPFileName = NULL;
286 char *lpHPPFileNameTemp = NULL;
287
288 char *lpFullName = (char *)malloc(0x10000);
289
290 char *lpString = (char *)malloc(1024);
291
292 char *pFeedBuffer = (char *)calloc(1, dwSize + 1);
293
294 ReadAll(hFeedFile, pFeedBuffer, dwSize);
295
296 char *lpStart = pFeedBuffer;
297
298 // read h filename
299
300 ReadFromBufferEx(lpStart, &lpHPPFileName);
301 UnquoteIfNeeded(lpHPPFileName);
302
303 // read language count
304 int dwLangs = ReadInteger(lpStart);
305
306 if (dwLangs > 0) {
307 sprintf(lpFullName, "%s/%s", lpHOutputPath ? lpHOutputPath : ".", lpHPPFileName);
308
309 dwHeaderCRC32 = 0;
310 dwHeaderOldCRC32 = (CheckExists(lpFullName) && key_file) ? key_file->GetInt(lpFullName, "CRC32") : 0;
311
312 lpHPPFileNameTemp = GetTempName(lpFullName);
313
314 FDScope hHFile(CreateOutputFile(lpHPPFileNameTemp));
315 if (hHFile.Valid()) {
316 WriteSignatureIfNeeded(hHFile);
317
318 if (bWriteCopyright) {
319 sprintf(lpString,
320 "// This C++ include file was generated by .LNG Generator " VERSION
321 "\r\n// Copyright (C) 2003-2005 WARP ItSelf\r\n// Copyright (C) 2005 WARP ItSelf & "
322 "Alex Yaroslavsky\r\n\n");
323 SmartWrite(hHFile, lpString, &dwHeaderCRC32);
324 }
325
326 pLangEntries = (LanguageEntry *)malloc(dwLangs * sizeof(LanguageEntry));
327
328 // read language names and create .lng files
329
330 for (int i = 0; i < dwLangs; i++) {
331 ReadFromBufferEx(lpStart, &pLangEntries[i].lpLNGFileName);
332
333 ReadFromBufferEx(lpStart, &pLangEntries[i].lpLanguageName);
334 ReadFromBufferEx(lpStart, &pLangEntries[i].lpLanguageDescription);
335
336 UnquoteIfNeeded(pLangEntries[i].lpLanguageName);
337 UnquoteIfNeeded(pLangEntries[i].lpLanguageDescription);
338 UnquoteIfNeeded(pLangEntries[i].lpLNGFileName);
339
340 sprintf(lpFullName, "%s/%s", lpLNGOutputPath ? lpLNGOutputPath : ".",
341 pLangEntries[i].lpLNGFileName);
342
343 pLangEntries[i].cNeedUpdate = 0;
344
345 pLangEntries[i].dwCRC32 = 0;
346 pLangEntries[i].dwOldCRC32 =
347 (CheckExists(lpFullName) && key_file) ? key_file->GetInt(lpFullName, "CRC32") : 0;
348
349 pLangEntries[i].lpLNGFileNameTemp = GetTempName(lpFullName);
350
351 pLangEntries[i].hLNGFile = CreateOutputFile(pLangEntries[i].lpLNGFileNameTemp);
352 if (pLangEntries[i].hLNGFile == -1)
353 printf("WARNING: Can't create the language file \"%s\".\n",
354 pLangEntries[i].lpLNGFileName);
355 else {
356 WriteSignatureIfNeeded(pLangEntries[i].hLNGFile);
357
358 if (bWriteCopyright) {
359 sprintf(lpString,
360 "// This .lng file was generated by .LNG Generator " VERSION
361 "\r\n// Copyright (C) 2003-2005 WARP ItSelf\r\n// Copyright (C) 2005 WARP "
362 "ItSelf & Alex Yaroslavsky\r\n\n");
363 SmartWrite(pLangEntries[i].hLNGFile, lpString, &pLangEntries[i].dwCRC32);
364 }
365
366 sprintf(lpString, ".Language=%s,%s\r\n\n", pLangEntries[i].lpLanguageName,
367 pLangEntries[i].lpLanguageDescription);
368 SmartWrite(pLangEntries[i].hLNGFile, lpString, &pLangEntries[i].dwCRC32);
369 }
370 }
371
372 char *lpHHead;
373 char *lpHTail;
374 char *lpEnum = NULL;
375
376 if (ReadComments(lpStart, &lpHHead, "hhead:", "")) {
377 SmartWrite(hHFile, lpHHead, &dwHeaderCRC32);
378 free(lpHHead);
379 }
380
381 ReadComments(lpStart, &lpHTail, "htail:", "");
382
383 ReadComments(lpStart, &lpEnum, "enum:", "");
384 // sprintf (lpString, "enum %s{\r\n", lpEnum? lpEnum : "");
385 sprintf(lpString, "/* FarLang - start */\r\n");
386 free(lpEnum);
387 SmartWrite(hHFile, lpString, &dwHeaderCRC32);
388
389 // read strings
390
391 bool bRead = true;
392 int nMsgIndex = 0;
393
394 while (bRead) {
395 char *lpMsgID;
396 char *lpLNGString;
397 char *lpHComments;
398
399 if (ReadComments(lpStart, &lpHComments, "h:", "")) {
400 SmartWrite(hHFile, lpHComments, &dwHeaderCRC32);
401 free(lpHComments);
402 }
403
404 ReadComments(lpStart, &lpHComments, "he:", "");
405
406 bRead = ReadFromBufferEx(lpStart, &lpMsgID);
407
408 if (bRead) {
409 char *lpLngComments = NULL;
410 char *lpELngComments = NULL;
411 char *lpSpecificLngComments = NULL;
412
413 // sprintf (lpString, "\t%s,\r\n", lpMsgID);
414 sprintf(lpString, "DECLARE_FARLANGMSG(%s, %d)\r\n", lpMsgID, nMsgIndex);
415 SmartWrite(hHFile, lpString, &dwHeaderCRC32);
416 ++nMsgIndex;
417
418 ReadComments(lpStart, &lpLngComments, "l:", "");
419 ReadComments(lpStart, &lpELngComments, "le:", "");
420
421 for (int i = 0; i < dwLangs; i++) {
422 if (lpLngComments)
423 SmartWrite(pLangEntries[i].hLNGFile, lpLngComments, &pLangEntries[i].dwCRC32);
424
425 if (ReadComments(lpStart, &lpSpecificLngComments, "ls:", "")) {
426 SmartWrite(pLangEntries[i].hLNGFile, lpSpecificLngComments,
427 &pLangEntries[i].dwCRC32);
428 free(lpSpecificLngComments);
429 }
430
431 ReadComments(lpStart, &lpSpecificLngComments, "lse:", "");
432
433 bRead = ReadFromBufferEx(lpStart, &lpLNGString);
434
435 if (bRead) {
436 if (!strncmp(lpLNGString, "upd:", 4)) {
437 size_t length = strlen(lpLNGString);
438 memmove(lpLNGString, lpLNGString + 4, length - 3);
439
440 /*
441 printf (
442 "WARNING: String %s (ID = %s) of %s language needs update!\n",
443 lpLNGString,
444 lpMsgID,
445 pLangEntries[i].lpLanguageName
446 );
447 */
448 SmartWrite(pLangEntries[i].hLNGFile, "// need translation:\r\n",
449 &pLangEntries[i].dwCRC32);
450 pLangEntries[i].cNeedUpdate++;
451 }
452
453 sprintf(lpString, "//[%s]\r\n%s\r\n", lpMsgID, lpLNGString);
454 SmartWrite(pLangEntries[i].hLNGFile, lpString, &pLangEntries[i].dwCRC32);
455 free(lpLNGString);
456 }
457
458 if (lpSpecificLngComments) {
459 SmartWrite(pLangEntries[i].hLNGFile, lpSpecificLngComments,
460 &pLangEntries[i].dwCRC32);
461 free(lpSpecificLngComments);
462 }
463
464 if (lpELngComments)
465 SmartWrite(pLangEntries[i].hLNGFile, lpELngComments, &pLangEntries[i].dwCRC32);
466 }
467
468 free(lpMsgID);
469
470 if (lpLngComments)
471 free(lpLngComments);
472
473 if (lpELngComments)
474 free(lpELngComments);
475 }
476
477 if (lpHComments) {
478 SmartWrite(hHFile, lpHComments, &dwHeaderCRC32);
479 free(lpHComments);
480 }
481 }
482
483 // output needed translations statistics
484 for (int i = 0; i < dwLangs; i++) {
485 if (pLangEntries[i].cNeedUpdate > 0) {
486 printf("INFO: There are %d strings that require review in %s translation!\n\n",
487 pLangEntries[i].cNeedUpdate, pLangEntries[i].lpLanguageName);
488 }
489 }
490
491 // write .h file footer
492
493 lseek(hHFile, -2, SEEK_CUR);
494
495 // sprintf (lpString, "\r\n};\r\n");
496 sprintf(lpString, "\r\n/* FarLang - end */\r\n");
497 SmartWrite(hHFile, lpString, &dwHeaderCRC32);
498
499 if (lpHTail) {
500 SmartWrite(hHFile, lpHTail, &dwHeaderCRC32);
501 free(lpHTail);
502 }
503
504 // play with CRC
505
506 for (int i = 0; i < dwLangs; i++) {
507 close(pLangEntries[i].hLNGFile);
508 pLangEntries[i].hLNGFile = -1;
509
510 sprintf(lpFullName, "%s/%s", lpLNGOutputPath ? lpLNGOutputPath : ".",
511 pLangEntries[i].lpLNGFileName);
512
513 bUpdate = true;
514
515 if (key_file) {
516 if (pLangEntries[i].dwCRC32 == pLangEntries[i].dwOldCRC32) {
517 // printf ("INFO: Language file \"%s\" doesn't need to be updated.\r\n", pLangEntries[i].lpLNGFileName);
518 bUpdate = false;
519 } else {
520 key_file->SetInt(lpFullName, "CRC32", pLangEntries[i].dwCRC32);
521 }
522 }
523
524 if (bUpdate) {
525 if (rename(pLangEntries[i].lpLNGFileNameTemp, lpFullName) == -1) {
526 printf("ERROR: Failed to rename '%s' -> '%s'.\n", pLangEntries[i].lpLNGFileNameTemp,
527 lpFullName);
528 }
529 }
530
531 unlink(pLangEntries[i].lpLNGFileNameTemp);
532
533 free(pLangEntries[i].lpLNGFileNameTemp);
534 free(pLangEntries[i].lpLNGFileName);
535 free(pLangEntries[i].lpLanguageName);
536 free(pLangEntries[i].lpLanguageDescription);
537 }
538
539 free(pLangEntries);
540
541 sprintf(lpFullName, "%s/%s", lpHOutputPath ? lpHOutputPath : ".", lpHPPFileName);
542
543 bUpdate = true;
544
545 if (key_file) {
546 if (dwHeaderCRC32 == dwHeaderOldCRC32) {
547 // printf ("INFO: Header file \"%s\" doesn't need to be updated.\r\n", lpHPPFileName);
548 bUpdate = false;
549 } else {
550 key_file->SetInt(lpFullName, "CRC32", dwHeaderCRC32);
551 }
552 }
553
554 if (bUpdate) {
555 if (rename(lpHPPFileNameTemp, lpFullName) == -1) {
556 printf("ERROR: Failed to rename '%s' -> '%s'.\n", lpHPPFileNameTemp, lpFullName);
557 }
558 }
559 } else
560 printf("ERROR: Can't create the header file, exiting.\n");
561 } else
562 printf("ERROR: Zero languages to process, exiting.\n");
563
564 unlink(lpHPPFileNameTemp);
565
566 free(lpHPPFileNameTemp);
567 free(lpHPPFileName);
568 free(pFeedBuffer);
569 free(lpString);
570
571 free(lpFullName);
572 free(lpHOutputPath);
573 free(lpLNGOutputPath);
574
575 return 0;
576 }