| 1 |
/* |
| 2 |
Copyright (c) 2005 WARP ItSelf & Alex Yaroslavsky |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without |
| 6 |
modification, are permitted provided that the following conditions |
| 7 |
are met: |
| 8 |
1. Redistributions of source code must retain the above copyright |
| 9 |
notice, this list of conditions and the following disclaimer. |
| 10 |
2. Redistributions in binary form must reproduce the above copyright |
| 11 |
notice, this list of conditions and the following disclaimer in the |
| 12 |
documentation and/or other materials provided with the distribution. |
| 13 |
3. The name of the authors may not be used to endorse or promote products |
| 14 |
derived from this software without specific prior written permission. |
| 15 |
|
| 16 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 |
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
// BUG: /* comments don't work! |
| 28 |
|
| 29 |
#include "lng.common.h" |
| 30 |
|
| 31 |
#define VERSION "v1.0 rc3" |
| 32 |
|
| 33 |
void FixQuotes(char *lpStr) |
| 34 |
{ |
| 35 |
char *lpStart = lpStr; |
| 36 |
char *lpEnd = lpStr + strlen(lpStr) - 1; |
| 37 |
char *lpTmp = (char *)malloc(strlen(lpStr) * 2); |
| 38 |
char *lpPtr = lpTmp; |
| 39 |
|
| 40 |
memset(lpTmp, 0, strlen(lpStr) * 2); |
| 41 |
|
| 42 |
while (*lpStart && *lpStart != '"') |
| 43 |
lpStart++; |
| 44 |
|
| 45 |
while (lpEnd > lpStart && *lpEnd != '"') |
| 46 |
lpEnd--; |
| 47 |
|
| 48 |
if (lpEnd == lpStart) { |
| 49 |
strmove(lpTmp, lpStart); |
| 50 |
strcat(lpTmp, "\""); |
| 51 |
strmove(lpStr, lpTmp); |
| 52 |
|
| 53 |
free(lpTmp); |
| 54 |
|
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
*(lpPtr++) = '"'; |
| 59 |
lpStart++; |
| 60 |
|
| 61 |
while (lpStart < lpEnd) { |
| 62 |
if (*lpStart == '"' && *(lpStart - 1) != '\\') |
| 63 |
*(lpPtr++) = '\\'; |
| 64 |
|
| 65 |
*(lpPtr++) = *(lpStart++); |
| 66 |
} |
| 67 |
|
| 68 |
*lpPtr = '"'; |
| 69 |
|
| 70 |
strmove(lpStr, lpTmp); |
| 71 |
|
| 72 |
free(lpTmp); |
| 73 |
} |
| 74 |
|
| 75 |
bool ReadFromBufferEx(char *&lpStart, char **lpParam, bool bSkipWhite = false, bool bSkipCmt = false) |
| 76 |
{ |
| 77 |
char *lpEnd = lpStart; |
| 78 |
bool bWhite; |
| 79 |
bool bCmt; |
| 80 |
|
| 81 |
*lpParam = NULL; |
| 82 |
|
| 83 |
while (*lpStart) { |
| 84 |
bWhite = true; |
| 85 |
bCmt = false; |
| 86 |
|
| 87 |
while (*lpEnd && *lpEnd != '\r' && *lpEnd != '\n') { |
| 88 |
if (!bCmt && bWhite && *lpEnd == '/' && *(lpEnd + 1) == '/') |
| 89 |
bCmt = true; |
| 90 |
|
| 91 |
if (bWhite && *lpEnd != ' ' && *lpEnd != '\t') |
| 92 |
bWhite = false; |
| 93 |
|
| 94 |
lpEnd++; |
| 95 |
} |
| 96 |
|
| 97 |
if (!((bSkipWhite && bWhite) || (bSkipCmt && bCmt))) { |
| 98 |
*lpParam = (char *)malloc(lpEnd - lpStart + 1); |
| 99 |
|
| 100 |
memset(*lpParam, 0, lpEnd - lpStart + 1); |
| 101 |
memcpy(*lpParam, lpStart, lpEnd - lpStart); |
| 102 |
} |
| 103 |
|
| 104 |
lpStart = lpEnd; |
| 105 |
|
| 106 |
if ((*(lpStart + 1) == '\r' || *(lpStart + 1) == '\n') && *(lpStart + 1) != *lpStart) |
| 107 |
lpStart++; |
| 108 |
|
| 109 |
lpStart++; |
| 110 |
lpEnd = lpStart; |
| 111 |
|
| 112 |
if (!((bSkipWhite && bWhite) || (bSkipCmt && bCmt))) |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
bool ReadLanguage(char *&lpRealStart, char **lpLngName, char **lpLngDesc) |
| 120 |
{ |
| 121 |
char *lpStart = lpRealStart; |
| 122 |
char *lpParam = NULL; |
| 123 |
bool bResult = false; |
| 124 |
|
| 125 |
*lpLngName = (char *)malloc(100); |
| 126 |
*lpLngDesc = (char *)malloc(100); |
| 127 |
|
| 128 |
strcpy(*lpLngName, "none"); |
| 129 |
strcpy(*lpLngDesc, "none"); |
| 130 |
|
| 131 |
if (ReadFromBufferEx(lpStart, &lpParam, true, true) && strstr(lpParam, ".Language")) { |
| 132 |
strmove(*lpLngName, strstr(lpParam, "=") + 1); |
| 133 |
strmove(*lpLngDesc, "\""); |
| 134 |
|
| 135 |
strcat(*lpLngDesc, strstr(*lpLngName, ",") + 1); |
| 136 |
strcat(*lpLngDesc, "\""); |
| 137 |
|
| 138 |
char *lpComa = strchr(*lpLngName, ','); |
| 139 |
|
| 140 |
if (lpComa) |
| 141 |
*lpComa = 0; |
| 142 |
|
| 143 |
lpRealStart = lpStart; |
| 144 |
|
| 145 |
while (*lpRealStart == '\r' || *lpRealStart == '\n') |
| 146 |
lpRealStart++; |
| 147 |
|
| 148 |
bResult = true; |
| 149 |
} |
| 150 |
|
| 151 |
if (lpParam) |
| 152 |
free(lpParam); |
| 153 |
|
| 154 |
return bResult; |
| 155 |
} |
| 156 |
|
| 157 |
struct LanguageEntry |
| 158 |
{ |
| 159 |
char *lpBuffer; |
| 160 |
char *lpStart; |
| 161 |
char *lpString; |
| 162 |
char *lpOldStart; |
| 163 |
}; |
| 164 |
|
| 165 |
int main_convertor(int argc, char **argv) |
| 166 |
{ |
| 167 |
printf(".LNG Convertor " VERSION "\n"); |
| 168 |
printf("Copyright (C) 2005-2006 WARP ItSelf & Alex Yaroslavsky\n\n"); |
| 169 |
|
| 170 |
if (argc < 5) { |
| 171 |
printf("Usage: convertor feed_file hpp_file num_of_lng lng_file1 lng_file2 ...\n\r"); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
FDScope hFeedFile(CreateOutputFile(argv[1])); |
| 176 |
if (!hFeedFile.Valid()) { |
| 177 |
printf("ERROR: Can't create the feed file, exiting.\n"); |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
FDScope hHFile(OpenInputFile(argv[2])); |
| 182 |
if (!hHFile.Valid()) { |
| 183 |
printf("ERROR: Can't open the header file, exiting.\n"); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
int dwLangs = atol(argv[3]); |
| 188 |
if (dwLangs <= 0) { |
| 189 |
printf("ERROR: Zero language files to process, exiting.\n"); |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
size_t dwSize = QueryFileSize(hHFile); |
| 194 |
char *pHBuffer = (char *)calloc(1, dwSize + 1); |
| 195 |
|
| 196 |
ReadAll(hHFile, pHBuffer, dwSize); |
| 197 |
|
| 198 |
char *lpStart = pHBuffer; |
| 199 |
|
| 200 |
LanguageEntry *pLangEntries = (LanguageEntry *)malloc(dwLangs * sizeof(LanguageEntry)); |
| 201 |
|
| 202 |
memset(pLangEntries, 0, dwLangs * sizeof(LanguageEntry)); |
| 203 |
|
| 204 |
for (int i = 0; i < dwLangs; i++) { |
| 205 |
FDScope hFile(OpenInputFile(argv[4 + i])); |
| 206 |
if (hFile.Valid()) { |
| 207 |
dwSize = QueryFileSize(hFile); |
| 208 |
|
| 209 |
pLangEntries[i].lpBuffer = (char *)calloc(1, dwSize + 1); |
| 210 |
pLangEntries[i].lpStart = pLangEntries[i].lpBuffer; |
| 211 |
|
| 212 |
memset(pLangEntries[i].lpBuffer, 0, dwSize + 1); |
| 213 |
|
| 214 |
ReadAll(hFile, pLangEntries[i].lpBuffer, dwSize); |
| 215 |
} else |
| 216 |
printf("WARNING: Can't open the language file \"%s\", skiping\n\r", argv[4 + i]); |
| 217 |
} |
| 218 |
|
| 219 |
char *lpTmp = (char *)malloc(0x10000); |
| 220 |
char *lpString; |
| 221 |
|
| 222 |
sprintf(lpTmp, "#hpp file name\r\n%s\r\n#number of languages\r\n%s\r\n", argv[2], argv[3]); |
| 223 |
|
| 224 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 225 |
|
| 226 |
for (int i = 0; i < dwLangs; i++) { |
| 227 |
if (pLangEntries[i].lpBuffer) // or we skipping |
| 228 |
{ |
| 229 |
char *lpLngName, *lpLngDesc; |
| 230 |
|
| 231 |
sprintf(lpTmp, "#id:%d language file name, language name, language description\r\n%s", i, |
| 232 |
argv[4 + i]); |
| 233 |
|
| 234 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 235 |
|
| 236 |
ReadLanguage(pLangEntries[i].lpStart, &lpLngName, &lpLngDesc); |
| 237 |
|
| 238 |
sprintf(lpTmp, " %s %s\r\n", lpLngName, lpLngDesc); |
| 239 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 240 |
|
| 241 |
free(lpLngName); |
| 242 |
free(lpLngDesc); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
sprintf(lpTmp, "\r\n#head of the hpp file\r\n"); |
| 247 |
|
| 248 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 249 |
|
| 250 |
while (ReadFromBufferEx(lpStart, &lpString)) { |
| 251 |
if (!strncmp(lpString, "enum", 4)) { |
| 252 |
free(lpString); |
| 253 |
|
| 254 |
char *lpOldStart = lpStart; |
| 255 |
|
| 256 |
ReadFromBufferEx(lpStart, &lpString); |
| 257 |
|
| 258 |
if (strstr(lpString, "{")) |
| 259 |
lpOldStart = lpStart; |
| 260 |
|
| 261 |
free(lpString); |
| 262 |
|
| 263 |
while (ReadFromBufferEx(lpStart, &lpString, true, true) && !strstr(lpString, "};")) |
| 264 |
free(lpString); |
| 265 |
|
| 266 |
char *lpEnd = lpStart - 2; |
| 267 |
|
| 268 |
while (*lpEnd != '\r' && *lpEnd != '\n') |
| 269 |
lpEnd--; |
| 270 |
|
| 271 |
while (*lpEnd == '\r' || *lpEnd == '\n') |
| 272 |
lpEnd--; |
| 273 |
|
| 274 |
while (*lpEnd != '\r' && *lpEnd != '\n') |
| 275 |
lpEnd--; |
| 276 |
|
| 277 |
*(lpEnd + 1) = 0; |
| 278 |
|
| 279 |
sprintf(lpTmp, "\r\n#tail of the hpp file\r\n"); |
| 280 |
|
| 281 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 282 |
|
| 283 |
while (ReadFromBufferEx(lpStart, &lpString)) { |
| 284 |
sprintf(lpTmp, "htail:%s\r\n", lpString); |
| 285 |
|
| 286 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 287 |
|
| 288 |
free(lpString); |
| 289 |
} |
| 290 |
|
| 291 |
lpStart = lpOldStart; |
| 292 |
|
| 293 |
break; |
| 294 |
} else { |
| 295 |
sprintf(lpTmp, "hhead:%s\r\n", lpString); |
| 296 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 297 |
} |
| 298 |
|
| 299 |
free(lpString); |
| 300 |
} |
| 301 |
|
| 302 |
char *lpOldStart = lpStart; |
| 303 |
|
| 304 |
while (true) { |
| 305 |
WriteAll(hFeedFile, "\r\n", 2); |
| 306 |
|
| 307 |
lpOldStart = lpStart; |
| 308 |
|
| 309 |
while (ReadFromBufferEx(lpStart, &lpString)) { |
| 310 |
TrimStart(lpString); |
| 311 |
|
| 312 |
if (strstr(lpString, "//") != lpString) // "//" at the beginning at the string -> copy, else it will be discarded |
| 313 |
{ |
| 314 |
free(lpString); |
| 315 |
|
| 316 |
lpStart = lpOldStart; |
| 317 |
|
| 318 |
break; |
| 319 |
} else { |
| 320 |
sprintf(lpTmp, "h:%s\r\n", lpString); |
| 321 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 322 |
} |
| 323 |
|
| 324 |
free(lpString); |
| 325 |
|
| 326 |
lpOldStart = lpStart; |
| 327 |
} |
| 328 |
|
| 329 |
if (ReadFromBufferEx(lpStart, &lpString)) { |
| 330 |
char *lpComment = strstr(lpString, "//"); |
| 331 |
|
| 332 |
if (lpComment) |
| 333 |
*lpComment = 0; |
| 334 |
|
| 335 |
char *lpComa = strchr(lpString, ','); |
| 336 |
|
| 337 |
if (lpComa) |
| 338 |
*lpComa = 0; |
| 339 |
|
| 340 |
Trim(lpString); |
| 341 |
|
| 342 |
if (!*lpString) |
| 343 |
continue; |
| 344 |
|
| 345 |
sprintf(lpTmp, "%s\r\n", lpString); |
| 346 |
|
| 347 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 348 |
|
| 349 |
free(lpString); |
| 350 |
|
| 351 |
while (true) { |
| 352 |
bool bSame = true; |
| 353 |
|
| 354 |
for (int i = 0; i < dwLangs; i++) { |
| 355 |
if (pLangEntries[i].lpBuffer) // or we skipping, no file read |
| 356 |
{ |
| 357 |
pLangEntries[i].lpOldStart = pLangEntries[i].lpStart; |
| 358 |
bSame = bSame && ReadFromBufferEx(pLangEntries[i].lpStart, &pLangEntries[i].lpString); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
for (int i = 1; bSame && i < dwLangs; i++) { |
| 363 |
if (pLangEntries[i].lpBuffer) // or we skipping, no file read |
| 364 |
{ |
| 365 |
if (!pLangEntries[i].lpString || (pLangEntries[i].lpString[0] == '"') |
| 366 |
|| strcmp(pLangEntries[0].lpString, pLangEntries[i].lpString)) |
| 367 |
bSame = false; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
if (!bSame) { |
| 372 |
for (int i = 0; i < dwLangs; i++) { |
| 373 |
pLangEntries[i].lpStart = pLangEntries[i].lpOldStart; |
| 374 |
|
| 375 |
if (pLangEntries[i].lpString) |
| 376 |
free(pLangEntries[i].lpString); |
| 377 |
} |
| 378 |
|
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
if (*pLangEntries[0].lpString) { |
| 383 |
sprintf(lpTmp, "l:%s\r\n", pLangEntries[0].lpString); |
| 384 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 385 |
} |
| 386 |
|
| 387 |
for (int i = 0; i < dwLangs; i++) |
| 388 |
free(pLangEntries[i].lpString); |
| 389 |
} |
| 390 |
|
| 391 |
for (int i = 0; i < dwLangs; i++) { |
| 392 |
if (pLangEntries[i].lpBuffer) // or we skipping, no file read |
| 393 |
{ |
| 394 |
while (ReadFromBufferEx(pLangEntries[i].lpStart, &pLangEntries[i].lpString) |
| 395 |
&& pLangEntries[i].lpString[0] != '"') { |
| 396 |
sprintf(lpTmp, "ls:%s\r\n", pLangEntries[i].lpString); |
| 397 |
|
| 398 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 399 |
|
| 400 |
free(pLangEntries[i].lpString); |
| 401 |
} |
| 402 |
|
| 403 |
strmove(lpTmp, pLangEntries[i].lpString); |
| 404 |
|
| 405 |
FixQuotes(lpTmp); |
| 406 |
|
| 407 |
strcat(lpTmp, "\r\n"); |
| 408 |
|
| 409 |
WriteAll(hFeedFile, lpTmp, strlen(lpTmp)); |
| 410 |
|
| 411 |
free(pLangEntries[i].lpString); |
| 412 |
} |
| 413 |
} |
| 414 |
} else |
| 415 |
break; |
| 416 |
} |
| 417 |
|
| 418 |
free(pHBuffer); |
| 419 |
|
| 420 |
for (int i = 0; i < dwLangs; i++) |
| 421 |
free(pLangEntries[i].lpBuffer); |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |