Skip to content

Commit 9bf2f59

Browse files
committed
langen now can output to a file
1 parent 22b3bef commit 9bf2f59

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

langen.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <vector>
99
#include <cstdlib>
1010
#include <set>
11+
#include <cstring>
1112

1213
#define GEN_M 0
1314
#define GEN_F 1
@@ -28,6 +29,8 @@ template<class T> int isize(const T& x) { return x.size(); }
2829

2930
#define NUMLAN 9
3031

32+
FILE *f;
33+
3134
// language generator
3235

3336
std::string current_language;
@@ -42,7 +45,7 @@ template<class T> struct dictionary {
4245
m.emplace(s, std::move(val));
4346
}
4447
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());
4649
}
4750
}
4851
T& operator [] (const std::string& s) { return m[s]; }
@@ -125,7 +128,7 @@ std::map<hashcode, std::string> buildHashTable(std::set<std::string>& s) {
125128

126129
const char *escape(std::string s, const std::string& dft) {
127130
if(s == "") {
128-
printf("/*MISSING*/ ");
131+
fprintf(f, "/*MISSING*/ ");
129132
s = dft;
130133
}
131134
static std::string t;
@@ -341,16 +344,23 @@ void compute_completeness(const T& dict)
341344
if(mis1 != "") mis1.pop_back();
342345
if(exist_in != "") exist_in.pop_back();
343346
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, "?"));
345348

346349
completeness[0]++;
347350
for(int i=1; i<NUMLAN; i++) if(dict[i].count(elt)) completeness[i]++;
348351
}
349352
}
350353

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+
}
352362

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");
354364

355365
nothe.insert("R'Lyeh");
356366
nothe.insert("Camelot");
@@ -395,24 +405,24 @@ int main() {
395405
for(auto&& elt : allchars) {
396406
if(isize(elt) >= 2) { javastring += elt; vchars.push_back(elt); }
397407
}
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());
410420

411-
printf("\nEX int transcompleteness[NUMLAN] = {");
412-
for(int i=0; i<NUMLAN; i++) printf("%d, ", completeness[i]);
413-
printf("};\n");
421+
fprintf(f, "\nEX int transcompleteness[NUMLAN] = {");
422+
for(int i=0; i<NUMLAN; i++) fprintf(f, "%d, ", completeness[i]);
423+
fprintf(f, "};\n");
414424

415-
printf("\n//statistics\n");
425+
fprintf(f, "\n//statistics\n");
416426
for(auto&& elt : d[1].m)
417427
d[0][elt.first] = elt.first;
418428
for(auto&& elt : nouns[1].m) {
@@ -421,7 +431,7 @@ int main() {
421431
nouns[0][elt.first] = n;
422432
}
423433

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));
425435

426436
for(int i=0; i<NUMLAN; i++) {
427437
size_t bnouns = 0;
@@ -437,7 +447,7 @@ int main() {
437447
bnouns += n.abl.size();
438448
}
439449

440-
printf("// %s: %5dB nouns, %5dB sentences\n",
450+
fprintf(f, "// %s: %5dB nouns, %5dB sentences\n",
441451
d[i]["EN"].c_str(), int(bnouns), int(bdict));
442452
}
443453

@@ -453,43 +463,44 @@ int main() {
453463

454464
do {
455465
hashval = rand();
456-
printf("// check hash: %x\n", hashval);
466+
fprintf(f, "// check hash: %x\n", hashval);
457467
ms = buildHashTable(allsent);
458468
mn = buildHashTable(allnouns);
459469
}
460470
while(ms.size() != allsent.size() || mn.size() != allnouns.size());
461471

462-
printf("hashcode hashval = 0x%x;\n\n", hashval);
472+
fprintf(f, "hashcode hashval = 0x%x;\n\n", hashval);
463473

464-
printf("sentence all_sentences[] = {\n");
474+
fprintf(f, "sentence all_sentences[] = {\n");
465475

466476
for(auto&& elt : ms) {
467477
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");
471481
}
472-
printf(" };\n\n");
482+
fprintf(f, " };\n\n");
473483

474-
printf("fullnoun all_nouns[] = {\n");
484+
fprintf(f, "fullnoun all_nouns[] = {\n");
475485

476486
for(auto&& elt : mn) {
477487
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,
479489
(nothe.count(s) ? 1:0) + (plural.count(s) ? 2:0),
480490
escape(s, s));
481491

482492
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));
488498
}
489499

490-
printf(" }},\n");
500+
fprintf(f, " }},\n");
491501
}
492502

493-
printf(" };\n");
503+
fprintf(f, " };\n");
494504

505+
if(f != stdout) fclose(f);
495506
}

0 commit comments

Comments
 (0)