Files
@ r25247:ac01ac3a49c2
Branch filter:
Location: cpp/openttd-patchpack/source/src/strgen/strgen.cpp - annotation
r25247:ac01ac3a49c2
16.9 KiB
text/x-c
Codechange: encapsulate the logic about how many bytes can be sent from a buffer in to a Packet
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r9111:983de9c5a848 r5584:545d748cc681 r8135:48d8ab4fe495 r8214:9a3935f9ef4e r10249:a19adec3c686 r17348:6f426dbe5bc8 r8264:d493cb51fe8a r8264:d493cb51fe8a r18717:72ad783c2b30 r18717:72ad783c2b30 r5584:545d748cc681 r18707:6e17ad6090f8 r5584:545d748cc681 r23088:6710c4c79ac6 r5584:545d748cc681 r5587:034e5e185dc2 r5584:545d748cc681 r5584:545d748cc681 r23088:6710c4c79ac6 r5584:545d748cc681 r23088:6710c4c79ac6 r5584:545d748cc681 r11728:6ea57f11e4d4 r5584:545d748cc681 r21383:942c32fb8b0e r21383:942c32fb8b0e r5584:545d748cc681 r5584:545d748cc681 r14533:80e1809325a6 r5584:545d748cc681 r14592:4ad80b2e3423 r5584:545d748cc681 r5584:545d748cc681 r18718:5bbab0953a81 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r21407:7c26f4904e46 r5584:545d748cc681 r14533:80e1809325a6 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r18718:5bbab0953a81 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r21407:7c26f4904e46 r5584:545d748cc681 r14533:80e1809325a6 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r18718:5bbab0953a81 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r21407:7c26f4904e46 r5584:545d748cc681 r14533:80e1809325a6 r14533:80e1809325a6 r14533:80e1809325a6 r14533:80e1809325a6 r18707:6e17ad6090f8 r5584:545d748cc681 r5584:545d748cc681 r18718:5bbab0953a81 r5584:545d748cc681 r18718:5bbab0953a81 r18718:5bbab0953a81 r18718:5bbab0953a81 r21407:7c26f4904e46 r18718:5bbab0953a81 r18718:5bbab0953a81 r18718:5bbab0953a81 r18718:5bbab0953a81 r18718:5bbab0953a81 r18718:5bbab0953a81 r18717:72ad783c2b30 r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r23607:36c15679007d r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r23497:a0ab44ebd2fa r18714:8567eeb9db4b r21420:4fef35a3b1ea r18714:8567eeb9db4b r18714:8567eeb9db4b r23497:a0ab44ebd2fa r18714:8567eeb9db4b r23497:a0ab44ebd2fa r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r5584:545d748cc681 r5584:545d748cc681 r23607:36c15679007d r5584:545d748cc681 r16386:eb4cef957770 r5584:545d748cc681 r16386:eb4cef957770 r5584:545d748cc681 r16386:eb4cef957770 r10249:a19adec3c686 r10249:a19adec3c686 r16386:eb4cef957770 r10249:a19adec3c686 r16386:eb4cef957770 r10249:a19adec3c686 r10249:a19adec3c686 r10249:a19adec3c686 r11734:763f2e9260c2 r11734:763f2e9260c2 r16386:eb4cef957770 r11734:763f2e9260c2 r11734:763f2e9260c2 r16386:eb4cef957770 r12687:28f71c3eb732 r12687:28f71c3eb732 r16386:eb4cef957770 r10367:2521c24ffcf6 r11341:5f61a8391e15 r23607:36c15679007d r12203:3db1a39994e3 r10367:2521c24ffcf6 r10367:2521c24ffcf6 r16386:eb4cef957770 r11341:5f61a8391e15 r11341:5f61a8391e15 r23607:36c15679007d r11341:5f61a8391e15 r11341:5f61a8391e15 r11341:5f61a8391e15 r16386:eb4cef957770 r5584:545d748cc681 r18714:8567eeb9db4b r10647:62911ec68e89 r5584:545d748cc681 r5584:545d748cc681 r10647:62911ec68e89 r5584:545d748cc681 r23607:36c15679007d r16439:74a7ff9185db r16439:74a7ff9185db r16439:74a7ff9185db r5584:545d748cc681 r5584:545d748cc681 r18714:8567eeb9db4b r10647:62911ec68e89 r5584:545d748cc681 r5584:545d748cc681 r10647:62911ec68e89 r5584:545d748cc681 r23607:36c15679007d r16439:74a7ff9185db r16439:74a7ff9185db r16439:74a7ff9185db r5584:545d748cc681 r5584:545d748cc681 r19585:5d8a500e4d2b r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r12170:efb6af98962f r23607:36c15679007d r5584:545d748cc681 r12170:efb6af98962f r23607:36c15679007d r23026:d7c7dcc4c3c8 r23026:d7c7dcc4c3c8 r23026:d7c7dcc4c3c8 r5584:545d748cc681 r12170:efb6af98962f r5584:545d748cc681 r12170:efb6af98962f r12170:efb6af98962f r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r17389:80d374d6031a r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r21410:ae5961f02724 r18707:6e17ad6090f8 r5584:545d748cc681 r23607:36c15679007d r18707:6e17ad6090f8 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r23607:36c15679007d r16469:87a391fc00eb r16469:87a391fc00eb r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r19944:25a78576fb5e r23607:36c15679007d r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r25222:842e6129b326 r14532:b3eed94f4dfd r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r25222:842e6129b326 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r20980:130d63431543 r20980:130d63431543 r20980:130d63431543 r20980:130d63431543 r20980:130d63431543 r20980:130d63431543 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r25222:842e6129b326 r5584:545d748cc681 r18707:6e17ad6090f8 r18712:1884177d9b32 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r24597:afde5721a3b6 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r24106:767d6a29b194 r25222:842e6129b326 r25222:842e6129b326 r25222:842e6129b326 r25222:842e6129b326 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r23088:6710c4c79ac6 r18708:683e5888dbd3 r23088:6710c4c79ac6 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r14532:b3eed94f4dfd r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r20984:a4da9302878d r20984:a4da9302878d r20984:a4da9302878d r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r20984:a4da9302878d r20984:a4da9302878d r23088:6710c4c79ac6 r20984:a4da9302878d r5584:545d748cc681 r20984:a4da9302878d r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r15610:623a23fb6560 r15610:623a23fb6560 r19944:25a78576fb5e r19944:25a78576fb5e r15613:193c12018337 r21386:3ba07c8f5bce r5584:545d748cc681 r21386:3ba07c8f5bce r5584:545d748cc681 r19944:25a78576fb5e r21386:3ba07c8f5bce r21386:3ba07c8f5bce r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r23458:e40bbeca908c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r23458:e40bbeca908c r23458:e40bbeca908c r23458:e40bbeca908c r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r12170:efb6af98962f r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r23620:1030ace82d57 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r10647:62911ec68e89 r5584:545d748cc681 r8812:554e8dcfb34c r5584:545d748cc681 r23607:36c15679007d r5584:545d748cc681 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r5584:545d748cc681 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r11727:7655e60d3923 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r20194:41e83f35a1c4 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r11727:7655e60d3923 r17348:6f426dbe5bc8 r11722:3cf9566d1ed3 r17348:6f426dbe5bc8 r19483:51ddc355db53 r17348:6f426dbe5bc8 r19483:51ddc355db53 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r11722:3cf9566d1ed3 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r11750:95be9306ca47 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r5584:545d748cc681 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r5584:545d748cc681 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r17348:6f426dbe5bc8 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r23607:36c15679007d r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18707:6e17ad6090f8 r21386:3ba07c8f5bce r5584:545d748cc681 r18714:8567eeb9db4b r22552:475c75b14fe3 r18714:8567eeb9db4b r18714:8567eeb9db4b r18707:6e17ad6090f8 r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r21386:3ba07c8f5bce r5584:545d748cc681 r18707:6e17ad6090f8 r18711:473a1ca6a250 r18712:1884177d9b32 r23234:1141c41fc0c2 r18716:adddcf514d59 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r21386:3ba07c8f5bce r5584:545d748cc681 r22552:475c75b14fe3 r18707:6e17ad6090f8 r18714:8567eeb9db4b r18714:8567eeb9db4b r18714:8567eeb9db4b r18716:adddcf514d59 r18716:adddcf514d59 r5584:545d748cc681 r18716:adddcf514d59 r18716:adddcf514d59 r23607:36c15679007d r18716:adddcf514d59 r18716:adddcf514d59 r18716:adddcf514d59 r18716:adddcf514d59 r18716:adddcf514d59 r23607:36c15679007d r5584:545d748cc681 r18716:adddcf514d59 r18716:adddcf514d59 r23607:36c15679007d r21386:3ba07c8f5bce r18707:6e17ad6090f8 r18716:adddcf514d59 r18716:adddcf514d59 r18716:adddcf514d59 r5584:545d748cc681 r18716:adddcf514d59 r18716:adddcf514d59 r18716:adddcf514d59 r18716:adddcf514d59 r18707:6e17ad6090f8 r5584:545d748cc681 r18707:6e17ad6090f8 r18707:6e17ad6090f8 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 r5584:545d748cc681 | /*
* This file is part of OpenTTD.
* OpenTTD 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, version 2.
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file strgen.cpp Tool to create computer readable (stand-alone) translation files. */
#include "../stdafx.h"
#include "../core/endian_func.hpp"
#include "../string_func.h"
#include "../strings_type.h"
#include "../misc/getoptdata.h"
#include "../table/control_codes.h"
#include "strgen.h"
#include <stdarg.h>
#include <exception>
#if !defined(_WIN32) || defined(__CYGWIN__)
#include <unistd.h>
#include <sys/stat.h>
#endif
#if defined(_WIN32) || defined(__WATCOMC__)
#include <direct.h>
#endif /* _WIN32 || __WATCOMC__ */
#include "../table/strgen_tables.h"
#include "../safeguards.h"
#ifdef _MSC_VER
# define LINE_NUM_FMT(s) "%s (%d): warning: %s (" s ")\n"
#else
# define LINE_NUM_FMT(s) "%s:%d: " s ": %s\n"
#endif
void CDECL strgen_warning(const char *s, ...)
{
char buf[1024];
va_list va;
va_start(va, s);
vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, buf);
_warnings++;
}
void CDECL strgen_error(const char *s, ...)
{
char buf[1024];
va_list va;
va_start(va, s);
vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("error"), _file, _cur_line, buf);
_errors++;
}
void NORETURN CDECL strgen_fatal(const char *s, ...)
{
char buf[1024];
va_list va;
va_start(va, s);
vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, buf);
#ifdef _MSC_VER
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
#endif
throw std::exception();
}
void NORETURN CDECL error(const char *s, ...)
{
char buf[1024];
va_list va;
va_start(va, s);
vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, buf);
#ifdef _MSC_VER
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
#endif
exit(2);
}
/** A reader that simply reads using fopen. */
struct FileStringReader : StringReader {
FILE *fh; ///< The file we are reading.
/**
* Create the reader.
* @param data The data to fill during reading.
* @param file The file we are reading.
* @param master Are we reading the master file?
* @param translation Are we reading a translation?
*/
FileStringReader(StringData &data, const char *file, bool master, bool translation) :
StringReader(data, file, master, translation)
{
this->fh = fopen(file, "rb");
if (this->fh == nullptr) error("Could not open %s", file);
}
/** Free/close the file. */
virtual ~FileStringReader()
{
fclose(this->fh);
}
char *ReadLine(char *buffer, const char *last) override
{
return fgets(buffer, ClampToU16(last - buffer + 1), this->fh);
}
void HandlePragma(char *str) override;
void ParseFile() override
{
this->StringReader::ParseFile();
if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) {
error("Language must include ##name, ##ownname and ##isocode");
}
}
};
void FileStringReader::HandlePragma(char *str)
{
if (!memcmp(str, "id ", 3)) {
this->data.next_string_id = strtoul(str + 3, nullptr, 0);
} else if (!memcmp(str, "name ", 5)) {
strecpy(_lang.name, str + 5, lastof(_lang.name));
} else if (!memcmp(str, "ownname ", 8)) {
strecpy(_lang.own_name, str + 8, lastof(_lang.own_name));
} else if (!memcmp(str, "isocode ", 8)) {
strecpy(_lang.isocode, str + 8, lastof(_lang.isocode));
} else if (!memcmp(str, "textdir ", 8)) {
if (!memcmp(str + 8, "ltr", 3)) {
_lang.text_dir = TD_LTR;
} else if (!memcmp(str + 8, "rtl", 3)) {
_lang.text_dir = TD_RTL;
} else {
error("Invalid textdir %s", str + 8);
}
} else if (!memcmp(str, "digitsep ", 9)) {
str += 9;
strecpy(_lang.digit_group_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_group_separator));
} else if (!memcmp(str, "digitsepcur ", 12)) {
str += 12;
strecpy(_lang.digit_group_separator_currency, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_group_separator_currency));
} else if (!memcmp(str, "decimalsep ", 11)) {
str += 11;
strecpy(_lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_decimal_separator));
} else if (!memcmp(str, "winlangid ", 10)) {
const char *buf = str + 10;
long langid = strtol(buf, nullptr, 16);
if (langid > (long)UINT16_MAX || langid < 0) {
error("Invalid winlangid %s", buf);
}
_lang.winlangid = (uint16)langid;
} else if (!memcmp(str, "grflangid ", 10)) {
const char *buf = str + 10;
long langid = strtol(buf, nullptr, 16);
if (langid >= 0x7F || langid < 0) {
error("Invalid grflangid %s", buf);
}
_lang.newgrflangid = (uint8)langid;
} else if (!memcmp(str, "gender ", 7)) {
if (this->master) error("Genders are not allowed in the base translation.");
char *buf = str + 7;
for (;;) {
const char *s = ParseWord(&buf);
if (s == nullptr) break;
if (_lang.num_genders >= MAX_NUM_GENDERS) error("Too many genders, max %d", MAX_NUM_GENDERS);
strecpy(_lang.genders[_lang.num_genders], s, lastof(_lang.genders[_lang.num_genders]));
_lang.num_genders++;
}
} else if (!memcmp(str, "case ", 5)) {
if (this->master) error("Cases are not allowed in the base translation.");
char *buf = str + 5;
for (;;) {
const char *s = ParseWord(&buf);
if (s == nullptr) break;
if (_lang.num_cases >= MAX_NUM_CASES) error("Too many cases, max %d", MAX_NUM_CASES);
strecpy(_lang.cases[_lang.num_cases], s, lastof(_lang.cases[_lang.num_cases]));
_lang.num_cases++;
}
} else {
StringReader::HandlePragma(str);
}
}
bool CompareFiles(const char *n1, const char *n2)
{
FILE *f2 = fopen(n2, "rb");
if (f2 == nullptr) return false;
FILE *f1 = fopen(n1, "rb");
if (f1 == nullptr) {
fclose(f2);
error("can't open %s", n1);
}
size_t l1, l2;
do {
char b1[4096];
char b2[4096];
l1 = fread(b1, 1, sizeof(b1), f1);
l2 = fread(b2, 1, sizeof(b2), f2);
if (l1 != l2 || memcmp(b1, b2, l1)) {
fclose(f2);
fclose(f1);
return false;
}
} while (l1 != 0);
fclose(f2);
fclose(f1);
return true;
}
/** Base class for writing data to disk. */
struct FileWriter {
FILE *fh; ///< The file handle we're writing to.
const char *filename; ///< The file name we're writing to.
/**
* Open a file to write to.
* @param filename The file to open.
*/
FileWriter(const char *filename)
{
this->filename = stredup(filename);
this->fh = fopen(this->filename, "wb");
if (this->fh == nullptr) {
error("Could not open %s", this->filename);
}
}
/** Finalise the writing. */
void Finalise()
{
fclose(this->fh);
this->fh = nullptr;
}
/** Make sure the file is closed. */
virtual ~FileWriter()
{
/* If we weren't closed an exception was thrown, so remove the temporary file. */
if (fh != nullptr) {
fclose(this->fh);
unlink(this->filename);
}
free(this->filename);
}
};
struct HeaderFileWriter : HeaderWriter, FileWriter {
/** The real file name we eventually want to write to. */
const char *real_filename;
/** The previous string ID that was printed. */
int prev;
uint total_strings;
/**
* Open a file to write to.
* @param filename The file to open.
*/
HeaderFileWriter(const char *filename) : FileWriter("tmp.xxx"),
real_filename(stredup(filename)), prev(0), total_strings(0)
{
fprintf(this->fh, "/* This file is automatically generated. Do not modify */\n\n");
fprintf(this->fh, "#ifndef TABLE_STRINGS_H\n");
fprintf(this->fh, "#define TABLE_STRINGS_H\n");
}
/** Free the filename. */
~HeaderFileWriter()
{
free(real_filename);
}
void WriteStringID(const char *name, int stringid)
{
if (prev + 1 != stringid) fprintf(this->fh, "\n");
fprintf(this->fh, "static const StringID %s = 0x%X;\n", name, stringid);
prev = stringid;
total_strings++;
}
void Finalise(const StringData &data)
{
/* Find the plural form with the most amount of cases. */
int max_plural_forms = 0;
for (uint i = 0; i < lengthof(_plural_forms); i++) {
max_plural_forms = std::max(max_plural_forms, _plural_forms[i].plural_count);
}
fprintf(this->fh,
"\n"
"static const uint LANGUAGE_PACK_VERSION = 0x%X;\n"
"static const uint LANGUAGE_MAX_PLURAL = %u;\n"
"static const uint LANGUAGE_MAX_PLURAL_FORMS = %d;\n"
"static const uint LANGUAGE_TOTAL_STRINGS = %u;\n"
"\n",
(uint)data.Version(), (uint)lengthof(_plural_forms), max_plural_forms, total_strings
);
fprintf(this->fh, "#endif /* TABLE_STRINGS_H */\n");
this->FileWriter::Finalise();
if (CompareFiles(this->filename, this->real_filename)) {
/* files are equal. tmp.xxx is not needed */
unlink(this->filename);
} else {
/* else rename tmp.xxx into filename */
# if defined(_WIN32)
unlink(this->real_filename);
# endif
if (rename(this->filename, this->real_filename) == -1) error("rename() failed");
}
}
};
/** Class for writing a language to disk. */
struct LanguageFileWriter : LanguageWriter, FileWriter {
/**
* Open a file to write to.
* @param filename The file to open.
*/
LanguageFileWriter(const char *filename) : FileWriter(filename)
{
}
void WriteHeader(const LanguagePackHeader *header)
{
this->Write((const byte *)header, sizeof(*header));
}
void Finalise()
{
if (fputc(0, this->fh) == EOF) {
error("Could not write to %s", this->filename);
}
this->FileWriter::Finalise();
}
void Write(const byte *buffer, size_t length)
{
if (fwrite(buffer, sizeof(*buffer), length, this->fh) != length) {
error("Could not write to %s", this->filename);
}
}
};
/** Multi-OS mkdirectory function */
static inline void ottd_mkdir(const char *directory)
{
/* Ignore directory creation errors; they'll surface later on, and most
* of the time they are 'directory already exists' errors anyhow. */
#if defined(_WIN32) || defined(__WATCOMC__)
mkdir(directory);
#else
mkdir(directory, 0755);
#endif
}
/**
* Create a path consisting of an already existing path, a possible
* path separator and the filename. The separator is only appended if the path
* does not already end with a separator
*/
static inline char *mkpath(char *buf, const char *last, const char *path, const char *file)
{
strecpy(buf, path, last); // copy directory into buffer
char *p = strchr(buf, '\0'); // add path separator if necessary
if (p[-1] != PATHSEPCHAR && p != last) *p++ = PATHSEPCHAR;
strecpy(p, file, last); // concatenate filename at end of buffer
return buf;
}
#if defined(_WIN32)
/**
* On MingW, it is common that both / as \ are accepted in the
* params. To go with those flow, we rewrite all incoming /
* simply to \, so internally we can safely assume \, and do
* this for all Windows machines to keep identical behaviour,
* no matter what your compiler was.
*/
static inline char *replace_pathsep(char *s)
{
for (char *c = s; *c != '\0'; c++) if (*c == '/') *c = '\\';
return s;
}
#else
static inline char *replace_pathsep(char *s) { return s; }
#endif
/** Options of strgen. */
static const OptionData _opts[] = {
GETOPT_NOVAL( 'v', "--version"),
GETOPT_GENERAL('C', '\0', "-export-commands", ODF_NO_VALUE),
GETOPT_GENERAL('L', '\0', "-export-plurals", ODF_NO_VALUE),
GETOPT_GENERAL('P', '\0', "-export-pragmas", ODF_NO_VALUE),
GETOPT_NOVAL( 't', "--todo"),
GETOPT_NOVAL( 'w', "--warning"),
GETOPT_NOVAL( 'h', "--help"),
GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE),
GETOPT_VALUE( 's', "--source_dir"),
GETOPT_VALUE( 'd', "--dest_dir"),
GETOPT_END(),
};
int CDECL main(int argc, char *argv[])
{
char pathbuf[MAX_PATH];
const char *src_dir = ".";
const char *dest_dir = nullptr;
GetOptData mgo(argc - 1, argv + 1, _opts);
for (;;) {
int i = mgo.GetOpt();
if (i == -1) break;
switch (i) {
case 'v':
puts("$Revision$");
return 0;
case 'C':
printf("args\tflags\tcommand\treplacement\n");
for (const CmdStruct *cs = _cmd_structs; cs < endof(_cmd_structs); cs++) {
char flags;
if (cs->proc == EmitGender) {
flags = 'g'; // Command needs number of parameters defined by number of genders
} else if (cs->proc == EmitPlural) {
flags = 'p'; // Command needs number of parameters defined by plural value
} else if (cs->flags & C_DONTCOUNT) {
flags = 'i'; // Command may be in the translation when it is not in base
} else {
flags = '0'; // Command needs no parameters
}
printf("%i\t%c\t\"%s\"\t\"%s\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd, "STRING") ? "STRING" : cs->cmd);
}
return 0;
case 'L':
printf("count\tdescription\tnames\n");
for (const PluralForm *pf = _plural_forms; pf < endof(_plural_forms); pf++) {
printf("%i\t\"%s\"\t%s\n", pf->plural_count, pf->description, pf->names);
}
return 0;
case 'P':
printf("name\tflags\tdefault\tdescription\n");
for (size_t i = 0; i < lengthof(_pragmas); i++) {
printf("\"%s\"\t%s\t\"%s\"\t\"%s\"\n",
_pragmas[i][0], _pragmas[i][1], _pragmas[i][2], _pragmas[i][3]);
}
return 0;
case 't':
_show_todo |= 1;
break;
case 'w':
_show_todo |= 2;
break;
case 'h':
puts(
"strgen - $Revision$\n"
" -v | --version print version information and exit\n"
" -t | --todo replace any untranslated strings with '<TODO>'\n"
" -w | --warning print a warning for any untranslated strings\n"
" -h | -? | --help print this help message and exit\n"
" -s | --source_dir search for english.txt in the specified directory\n"
" -d | --dest_dir put output file in the specified directory, create if needed\n"
" -export-commands export all commands and exit\n"
" -export-plurals export all plural forms and exit\n"
" -export-pragmas export all pragmas and exit\n"
" Run without parameters and strgen will search for english.txt and parse it,\n"
" creating strings.h. Passing an argument, strgen will translate that language\n"
" file using english.txt as a reference and output <language>.lng."
);
return 0;
case 's':
src_dir = replace_pathsep(mgo.opt);
break;
case 'd':
dest_dir = replace_pathsep(mgo.opt);
break;
case -2:
fprintf(stderr, "Invalid arguments\n");
return 0;
}
}
if (dest_dir == nullptr) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
try {
/* strgen has two modes of operation. If no (free) arguments are passed
* strgen generates strings.h to the destination directory. If it is supplied
* with a (free) parameter the program will translate that language to destination
* directory. As input english.txt is parsed from the source directory */
if (mgo.numleft == 0) {
mkpath(pathbuf, lastof(pathbuf), src_dir, "english.txt");
/* parse master file */
StringData data(TEXT_TAB_END);
FileStringReader master_reader(data, pathbuf, true, false);
master_reader.ParseFile();
if (_errors != 0) return 1;
/* write strings.h */
ottd_mkdir(dest_dir);
mkpath(pathbuf, lastof(pathbuf), dest_dir, "strings.h");
HeaderFileWriter writer(pathbuf);
writer.WriteHeader(data);
writer.Finalise(data);
if (_errors != 0) return 1;
} else if (mgo.numleft >= 1) {
char *r;
mkpath(pathbuf, lastof(pathbuf), src_dir, "english.txt");
StringData data(TEXT_TAB_END);
/* parse master file and check if target file is correct */
FileStringReader master_reader(data, pathbuf, true, false);
master_reader.ParseFile();
for (int i = 0; i < mgo.numleft; i++) {
data.FreeTranslation();
const char *translation = replace_pathsep(mgo.argv[i]);
const char *file = strrchr(translation, PATHSEPCHAR);
FileStringReader translation_reader(data, translation, false, file == nullptr || strcmp(file + 1, "english.txt") != 0);
translation_reader.ParseFile(); // target file
if (_errors != 0) return 1;
/* get the targetfile, strip any directories and append to destination path */
r = strrchr(mgo.argv[i], PATHSEPCHAR);
mkpath(pathbuf, lastof(pathbuf), dest_dir, (r != nullptr) ? &r[1] : mgo.argv[i]);
/* rename the .txt (input-extension) to .lng */
r = strrchr(pathbuf, '.');
if (r == nullptr || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
strecpy(r, ".lng", lastof(pathbuf));
LanguageFileWriter writer(pathbuf);
writer.WriteLang(data);
writer.Finalise();
/* if showing warnings, print a summary of the language */
if ((_show_todo & 2) != 0) {
fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf);
}
}
}
} catch (...) {
return 2;
}
return 0;
}
|