8
8
#include < vector>
9
9
#include < cstdlib>
10
10
#include < set>
11
+ #include < cstring>
11
12
12
13
#define GEN_M 0
13
14
#define GEN_F 1
@@ -28,6 +29,8 @@ template<class T> int isize(const T& x) { return x.size(); }
28
29
29
30
#define NUMLAN 9
30
31
32
+ FILE *f;
33
+
31
34
// language generator
32
35
33
36
std::string current_language;
@@ -42,7 +45,7 @@ template<class T> struct dictionary {
42
45
m.emplace (s, std::move (val));
43
46
}
44
47
else if (val != it->second ) {
45
- printf ( " // #warning Two translations for %s [%s]\n " , escape (s, s), current_language.c_str ());
48
+ fprintf (f, " // #warning Two translations for %s [%s]\n " , escape (s, s), current_language.c_str ());
46
49
}
47
50
}
48
51
T& operator [] (const std::string& s) { return m[s]; }
@@ -125,7 +128,7 @@ std::map<hashcode, std::string> buildHashTable(std::set<std::string>& s) {
125
128
126
129
const char *escape (std::string s, const std::string& dft) {
127
130
if (s == " " ) {
128
- printf ( " /*MISSING*/ " );
131
+ fprintf (f, " /*MISSING*/ " );
129
132
s = dft;
130
133
}
131
134
static std::string t;
@@ -341,16 +344,23 @@ void compute_completeness(const T& dict)
341
344
if (mis1 != " " ) mis1.pop_back ();
342
345
if (exist_in != " " ) exist_in.pop_back ();
343
346
if (in_important && mis != " " )
344
- printf ( " // #warning Missing [%s : %s] from [%s]: %s\n " , mis.c_str (), mis1.c_str (), exist_in.c_str (), escape (elt, " ?" ));
347
+ fprintf (f, " // #warning Missing [%s : %s] from [%s]: %s\n " , mis.c_str (), mis1.c_str (), exist_in.c_str (), escape (elt, " ?" ));
345
348
346
349
completeness[0 ]++;
347
350
for (int i=1 ; i<NUMLAN; i++) if (dict[i].count (elt)) completeness[i]++;
348
351
}
349
352
}
350
353
351
- int main () {
354
+ int main (int argc, char ** argv) {
355
+
356
+ f = stdout;
357
+ if (argc == 3 && strcmp (argv[1 ], " -o" ) == 0 ) f = fopen (argv[2 ], " wt" );
358
+ else if (argc != 1 ) {
359
+ printf (" Usage: langen -o <filename>, or without arguments to output to standard output\n " );
360
+ exit (1 );
361
+ }
352
362
353
- printf ( " // DO NOT EDIT -- this file is generated automatically with langen\n\n " );
363
+ fprintf (f, " // DO NOT EDIT -- this file is generated automatically with langen\n\n " );
354
364
355
365
nothe.insert (" R'Lyeh" );
356
366
nothe.insert (" Camelot" );
@@ -395,24 +405,24 @@ int main() {
395
405
for (auto && elt : allchars) {
396
406
if (isize (elt) >= 2 ) { javastring += elt; vchars.push_back (elt); }
397
407
}
398
- printf ( " \n " );
399
- printf ( " #if HDR\n " );
400
- printf ( " #if CAP_TRANS\n " );
401
- printf ( " #define NUMEXTRA %d\n " , isize (vchars));
402
- printf ( " #define NATCHARS {" );
403
- for (auto && elt : vchars) printf ( " \" %s\" ," , elt.c_str ());
404
- printf ( " }\n " );
405
- printf ( " extern const char* natchars[NUMEXTRA];\n " );
406
- printf ( " #endif\n " );
407
- printf ( " #endif\n " );
408
- printf ( " const char* natchars[NUMEXTRA] = NATCHARS;\n " );
409
- printf ( " //javastring = \" %s\" ;\n " , javastring.c_str ());
408
+ fprintf (f, " \n " );
409
+ fprintf (f, " #if HDR\n " );
410
+ fprintf (f, " #if CAP_TRANS\n " );
411
+ fprintf (f, " #define NUMEXTRA %d\n " , isize (vchars));
412
+ fprintf (f, " #define NATCHARS {" );
413
+ for (auto && elt : vchars) fprintf (f, " \" %s\" ," , elt.c_str ());
414
+ fprintf (f, " }\n " );
415
+ fprintf (f, " extern const char* natchars[NUMEXTRA];\n " );
416
+ fprintf (f, " #endif\n " );
417
+ fprintf (f, " #endif\n " );
418
+ fprintf (f, " const char* natchars[NUMEXTRA] = NATCHARS;\n " );
419
+ fprintf (f, " //javastring = \" %s\" ;\n " , javastring.c_str ());
410
420
411
- printf ( " \n EX int transcompleteness[NUMLAN] = {" );
412
- for (int i=0 ; i<NUMLAN; i++) printf ( " %d, " , completeness[i]);
413
- printf ( " };\n " );
421
+ fprintf (f, " \n EX int transcompleteness[NUMLAN] = {" );
422
+ for (int i=0 ; i<NUMLAN; i++) fprintf (f, " %d, " , completeness[i]);
423
+ fprintf (f, " };\n " );
414
424
415
- printf ( " \n //statistics\n " );
425
+ fprintf (f, " \n //statistics\n " );
416
426
for (auto && elt : d[1 ].m )
417
427
d[0 ][elt.first ] = elt.first ;
418
428
for (auto && elt : nouns[1 ].m ) {
@@ -421,7 +431,7 @@ int main() {
421
431
nouns[0 ][elt.first ] = n;
422
432
}
423
433
424
- printf ( " // total: %5d nouns, %5d sentences\n " , isize (nouns[1 ].m ), isize (d[1 ].m ));
434
+ fprintf (f, " // total: %5d nouns, %5d sentences\n " , isize (nouns[1 ].m ), isize (d[1 ].m ));
425
435
426
436
for (int i=0 ; i<NUMLAN; i++) {
427
437
size_t bnouns = 0 ;
@@ -437,7 +447,7 @@ int main() {
437
447
bnouns += n.abl .size ();
438
448
}
439
449
440
- printf ( " // %s: %5dB nouns, %5dB sentences\n " ,
450
+ fprintf (f, " // %s: %5dB nouns, %5dB sentences\n " ,
441
451
d[i][" EN" ].c_str (), int (bnouns), int (bdict));
442
452
}
443
453
@@ -453,43 +463,44 @@ int main() {
453
463
454
464
do {
455
465
hashval = rand ();
456
- printf ( " // check hash: %x\n " , hashval);
466
+ fprintf (f, " // check hash: %x\n " , hashval);
457
467
ms = buildHashTable (allsent);
458
468
mn = buildHashTable (allnouns);
459
469
}
460
470
while (ms.size () != allsent.size () || mn.size () != allnouns.size ());
461
471
462
- printf ( " hashcode hashval = 0x%x;\n\n " , hashval);
472
+ fprintf (f, " hashcode hashval = 0x%x;\n\n " , hashval);
463
473
464
- printf ( " sentence all_sentences[] = {\n " );
474
+ fprintf (f, " sentence all_sentences[] = {\n " );
465
475
466
476
for (auto && elt : ms) {
467
477
const std::string& s = elt.second ;
468
- printf ( " {0x%x, { // %s\n " , elt.first , escape (s, s));
469
- for (int i=1 ; i<NUMLAN; i++) printf ( " %s,\n " , escape (d[i][s], s));
470
- printf ( " }},\n " );
478
+ fprintf (f, " {0x%x, { // %s\n " , elt.first , escape (s, s));
479
+ for (int i=1 ; i<NUMLAN; i++) fprintf (f, " %s,\n " , escape (d[i][s], s));
480
+ fprintf (f, " }},\n " );
471
481
}
472
- printf ( " };\n\n " );
482
+ fprintf (f, " };\n\n " );
473
483
474
- printf ( " fullnoun all_nouns[] = {\n " );
484
+ fprintf (f, " fullnoun all_nouns[] = {\n " );
475
485
476
486
for (auto && elt : mn) {
477
487
const std::string& s = elt.second ;
478
- printf ( " {0x%x, %d, { // \" %s\"\n " , elt.first ,
488
+ fprintf (f, " {0x%x, %d, { // \" %s\"\n " , elt.first ,
479
489
(nothe.count (s) ? 1 :0 ) + (plural.count (s) ? 2 :0 ),
480
490
escape (s, s));
481
491
482
492
for (int i=1 ; i<NUMLAN; i++) {
483
- printf ( " {%d" , nouns[i][s].genus );
484
- printf ( " , %s" , escape (nouns[i][s].nom , s));
485
- printf ( " , %s" , escape (nouns[i][s].nomp , s));
486
- printf ( " , %s" , escape (nouns[i][s].acc , s));
487
- printf ( " , %s},\n " , escape (nouns[i][s].abl , s));
493
+ fprintf (f, " {%d" , nouns[i][s].genus );
494
+ fprintf (f, " , %s" , escape (nouns[i][s].nom , s));
495
+ fprintf (f, " , %s" , escape (nouns[i][s].nomp , s));
496
+ fprintf (f, " , %s" , escape (nouns[i][s].acc , s));
497
+ fprintf (f, " , %s},\n " , escape (nouns[i][s].abl , s));
488
498
}
489
499
490
- printf ( " }},\n " );
500
+ fprintf (f, " }},\n " );
491
501
}
492
502
493
- printf ( " };\n " );
503
+ fprintf (f, " };\n " );
494
504
505
+ if (f != stdout) fclose (f);
495
506
}
0 commit comments