-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.cpp
764 lines (646 loc) · 17.6 KB
/
main.cpp
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "pch.h"
#include <algorithm>
using namespace std;
using namespace std::filesystem;
typedef vector<string> vecstr;
///---- INFO
// This is a tool program that generates sr.pot for translations
// It reads tags (strings) from *en_tag.xml file
// and searches for their references in .cpp sources and
// in Gui .layout, it also finds widget hierarchy
//
// Find [Setup] to setup paths and params
// look for //par for tweaking params
// and //test for adding more info in log
//-----
/// single string to translate, from xml tag
//---------------------------------------------------
struct Tag
{
// <!-- last comment before tag (group name or such) -->
string cmt;
// <Tag name="name">text</Tag>
// actual string (tag) and its english text
string name, text;
// occurrences of string
// in gui .layout files
string gui;
// in source files .cpp
string src;
};
//---------------------------------------------------
// in a xml line, get value of attribute
// e.g. for line = "<Tag name="aa" />" and attr = "name" it will return "aa"
string GetAttr(const string& line, const string& attr, const string& end="\"")
{
string ss;
string::size_type p = line.find(attr), p2;
if (p != string::npos)
{ p += attr.length(); // len
p2 = line.find(end, p); // attr val
if (p2 != string::npos)
ss = line.substr(p, p2-p);
}
return ss;
}
// string utils
inline bool found(const string& s, const string& ss)
{
return s.find(ss) != string::npos;
}
bool IsAlpha(const string& s)
{
const static char* aA0 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return s.find_first_of(aA0) != string::npos;
}
// trim spaces and tabs from front and back
void Trim(string& s)
{
string::size_type p1 = s.find_first_not_of("\t "), p2 = s.find_last_not_of("\t ");
if (p1 != string::npos && p2 != string::npos)
s = s.substr(p1,p2);
}
// trim color eg. #80C0FF
void TrimClr(string& s)
{
if (s.length() < 7) return;
if (s[0] != '#') return;
if (s.find_first_not_of("#0123456789ABCDEFabcdef") >= 7)
s = s.substr(7);
}
// list Files in dir 'path' and push to vector 'v'
//-------------------------------------------------------
bool GetFiles(string path, vecstr& v)
{
directory_iterator it(path), end_it;
if (it == end_it)
{
cout << "! Empty dir:" << path << endl;
//log("Empty dir:" << path);
return false;
}
for (; it != end_it; ++it)
{
string name = (*it).path().filename().string();
//if (name != "." && name != "..")
{
bool isDir = is_directory(it->status());
if (!isDir) // file
{
if (!found(name,".h")) // headers don't have transl
v.push_back(path + "/" + name);
//cout << name.c_str() << endl;
}
} }
return true;
}
// translate tags
///--------------------------------------------------------------------------------------------------------
map<string, string> transl;
string replaceTagsPass(const string& _line, bool& _replaceResult)
{
// from MyGUI
_replaceResult = false;
string line(_line);
auto end = line.end();
for (auto it = line.begin(); it != end; )
{
if (*it == '#')
{
++it;
if (it == end)
return line;
else
{
if (*it != '{')
{
++it;
continue;
}
auto iter2 = it;
++iter2;
while (true)
{
if (iter2 == end)
return line;
if (*iter2 == '}')
{
size_t start = it - line.begin();
size_t len = (iter2 - line.begin()) - start - 1;
const string& tag = line.substr(start + 1, len);
string replacement;
bool find = true;
// try to find in loaded from resources language strings
auto replace = transl.find(tag);
if (replace != transl.end())
replacement = replace->second;
else
find = false;
// try to ask user if event assigned or use #{_tag} instead
if (!find)
{
it = line.insert(it, '#') + size_t(len + 2);
end = line.end();
break;
}
_replaceResult = true;
it = line.erase(it - size_t(1), iter2 + size_t(1));
size_t pos = it - line.begin();
line.insert(pos, replacement);
it = line.begin() + pos + replacement.length();
end = line.end();
if (it == end)
return line;
break;
}
++iter2;
}
} }
else
++it;
}
return line;
}
// translate, replaces all tags #{..} with transl map
string Transl(const string& _line)
{
string str(_line);
TrimClr(str); //clr
bool replace = false;
do
{
str = replaceTagsPass(str, replace);
}
while (replace);
// remove spaces (web display issue)
str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());
// replace spaces with _
//for (int i=0; i < str.length(); ++i)
// if (str[i] == ' ') str[i] = '_';
return str;
}
/// main
///--------------------------------------------------------------------------------------------------------
#ifdef _WIN32
int WinMain(int argc, _TCHAR* argv[])
#else
int main(int argc, char* argv[])
#endif
{
/// ---- [Setup] ----
// data subdir location
#if 1
const string path = "../../"; // test in bin/Release etc
const string outdir = "../../locale/";
#else
const string path = "../"; // in locale/
const string outdir = "";
#endif
string pxml = "data/gui/core_language_en_tag.xml";
const string pot = outdir + "sr.pot"; // output file
const int bar = 50; // text progress length
const bool bLog = 1; // use log file
const string slog = outdir + "sr.log"; // log file
// log
#define log(s) if (bLog) ofl << s << endl;
ofstream ofl;
if (bLog)
ofl.open(slog.c_str(), ios_base::out);
cout << "*** Start ***" << endl;
// get date, time
time_t now = time(nullptr);
tm ti = *localtime(&now);
stringstream dt;
dt << put_time(&ti, "%Y-%m-%d %H:%M");
// get timezone
tm utc = *gmtime(&now); utc.tm_isdst = -1;
time_t ut = mktime(&utc);
long z = (now - ut) / 60; // in sec
dt << (z > 0 ? "+" : "-");
dt.width(2); dt.fill('0'); dt << z / 60;
dt.width(2); dt.fill('0'); dt << z % 60;
cout << endl << "Date: " << dt.str() << endl;
log(endl << "Date: " << dt.str());
const int si = 1020; // max file line len
char s[si+4]; // temp buf
vector <Tag> tags;
size_t i,ii;
int tip=0,inp=0,und=0;
ifstream fi;
// open xml with all tags
//-----------------------------------------------------------------------------
pxml = path + pxml;
cout << "Reading xml: " << pxml << endl;
log("Reading xml: " << pxml << endl);
fi.open(pxml.c_str(), ios_base::in);
if (fi.fail())
{ cout << "Can't open !" << endl;
log("Can't open !");
return 0;
}
map<string, string> dupl;
string sCmt;
while (!fi.eof())
{
fi.getline(s,si);
if (strlen(s) > 0)
{
string ss = s;
// comment (will be group name for all below)
if (found(ss, "<!--"))
{
//eg. <!-- General -->
string sc = GetAttr(ss, "<!--", "-->");
if (IsAlpha(sc))
{ Trim(sc);
sCmt = sc; // set
//log(sCmt); //test
} }
else
{ // tag (string)
//eg. <Tag name="Yes">Yes</Tag>
string name = GetAttr(ss, "<Tag name=\"", "\">");
string text = GetAttr(ss, "\">", "</");
if (!name.empty() && !text.empty())
if (!found(name,"LANG_")) /// ignored tags [Setup]
if (name != "GameVersion" && name != "PageURL")
{
//log(/*sCmt.c_str() << " " <<*/ name.c_str() << " " << text.c_str()); //test
// add tag
Tag t;
t.name = name;
t.text = text;
t.cmt = sCmt;
tags.push_back(t);
transl[name] = text; //+
// stats-
if (name.substr(0,5) == "Input") ++inp;
if (name.substr(0,3) == "Tip") ++tip;
if (found(name,"_")) ++und;
if (dupl[text].empty()) // duplicates check
dupl[text] = name;
else
{ string dut = dupl[text];
if (!found(name, "Hint-"))
{ log("WARN: duplicate tag text: " << text << endl <<
" for tag: " << name << endl << " is in: " << dut);
} }
}
if (!name.empty() && text.empty())
log("ERR: tag text empty !");
}
} }
fi.close();
cout << endl << "Tags Count: " << tags.size()
<< " Input: " << inp << " Tips: " << tip << " _: " << und
<< " Normal: " << tags.size()-inp-tip-und << endl << endl;
if (tags.size()==0)
{ cout << "No tags !" << endl;
log("No tags !");
return 0;
}
///-----------------------------------------------------------------------------
// get gui .layout files [Setup] //par
///-----------------------------------------------------------------------------
cout << "Listing source dirs" << endl;
string pLay = path + "data/gui/";
vecstr lay, lay_n;
const vecstr lays = // todo: list dir, auto
{ "Common",
"Editor",
"Editor_Help",
"Editor_Main",
"Editor_Objects",
"Editor_Options",
"Editor_Tools",
"Editor_Track",
"Editor_Utils",
"Game",
"Game_Help",
"Game_Main",
"Game_Options",
"Game_Replay",
"Game_Tweak",
"Game_Utils",
"MaterialEditor",
"MessageBox"
};
for (auto s : lays)
{ lay.push_back(s + ".layout"); lay_n.push_back(s);
}
// sources dirs to search (where transl uses are)
//-----------------------------------------------------------------------------
const string srcDir = "src/";
const string layDir = "data/gui/";
const string pSrc = path + srcDir;
vecstr dir;
dir.push_back("game");
dir.push_back("editor");
dir.push_back("network");
dir.push_back("common");
//dir.push_back("ogre/common/data"); // not needed
//dir.push_back("road");
//dir.push_back("vdrift");
// get layout files --------
vecstr file_lay;
for (i=0; i < lay.size(); ++i)
file_lay.push_back(pLay + lay[i]);
// get source files --------
vecstr file_src;
for (i=0; i < dir.size(); ++i)
{
string ps = pSrc + dir[i];
GetFiles(ps, file_src);
}
cout << "Source files: " << file_src.size() << " gui: " << file_lay.size() << endl;
//getchar();
// shorter widget type names
map<string,string> map_wt;
map_wt["TextBox"] = "Text"; map_wt["TabItem"] = "Tab";
map_wt["EditBox"] = "Edit"; map_wt["ImageBox"] = "Image";
map_wt["ComboBox"] = "Combo"; map_wt["TabControl"] = "TabC";
// read layout file lines (all contents)
///-----------------------------------------------------------------------------
vector<vecstr*> lay_lin, lay_use;
ii = file_lay.size();
for (i=0; i < ii; ++i)
{
//cout << "read lay file: " << i << " " << int(100.f*float(i)/float(ii)) << "%" << endl;
vecstr* ll = new vecstr(); // new
lay_lin.push_back(ll);
vecstr* lu = new vecstr();
lay_use.push_back(lu);
string sf = file_lay[i];
//cout << sf << endl;
ifstream fi;
fi.open(sf.c_str(), ios_base::in);
if (fi.fail())
cout << "Can't open: " << sf.c_str() << endl;
// widgets captions hierarchy (parents)
vecstr wh, wt;
while (!fi.eof())
{
fi.getline(s,si);
string ss = s;
// new widget
if (found(ss,"<Widget type="))
if (!found(ss,"/>")) // but not 1 line widget
{
string type = GetAttr(ss, "type=\"");
#if 0 // not needed, too much
string name = GetAttr(ss, "name=\"");
//string ww = !name.empty() ? name : !type.empty() ? type : "w";
// use type if doesn't have a name
string ww = !name.empty() ? name : type;
#else
string ww;
#endif
string ty = map_wt[type]; // shorter
if (!ty.empty())
type = ty;
if (found(ss,"CheckBox") /*&& type == "Button"*/)
type = "Check";
wh.push_back(ww); wt.push_back(type);
}
// closing
if (found(ss,"</Widget>"))
{
wh.pop_back(); wt.pop_back();
}
if (!wh.empty())
{
// set caption upd prev use info
if (found(ss,"key=\"Caption\""))
{
//eg. value="#804060#{HDRTab}"/>
string v = GetAttr(ss, "value=\"");
v = Transl(v);
//if (v.length() > 14) // max len
// v = v.substr(0,14);
wh[wh.size()-1] = v;
}
//key="tip"
}
lay_lin[i]->push_back(s);
// combine hierarchy to 1 string
string su,z;
int nn = wh.size()/*-1 /*not last*/, n;
for (n=0; n < nn; ++n)
{
string z = wh[n]; // captions
// type for last
if (n == nn-1)
z = wt[n];
if (!z.empty())
{ su += z;
if (n < nn-1)
su += "."; ///par concat char
} }
if (su.empty())
su = "Root";
lay_use[i]->push_back(su);
//log(su); //test
}
fi.close();
}
// read source file lines (content of all files)
//-----------------------------------------------------------------------------
vector<vecstr*> src_lin;
cout << endl << "Reading source files" << endl;
// progress var, bar
ii = bar;
float pc_add = 100.f / ii, pc_next = pc_add, pc = 0.f;
for (i=1; i < ii; ++i)
cout << ".";
cout << endl;
ii = file_src.size();
for (i=0; i < ii; ++i)
{
//- progress
pc = 100.f * float(i) / float(ii);
if (pc > pc_next)
{
cout << "-";
pc_next += pc_add;
}
//cout << "Read file: " << i << " " << int(pc) << "%" << endl;
vecstr* ll = new vecstr(); // new
src_lin.push_back(ll);
string sf = file_src[i];
//cout << sf << endl;
ifstream fi;
fi.open(sf.c_str(), ios_base::in);
if (fi.fail())
cout << "Can't open: " << sf.c_str() << endl;
vecstr vsu;
while (!fi.eof())
{
fi.getline(s,si);
src_lin[i]->push_back(s);
}
fi.close();
}
cout << endl;
///-----------------------------------------------------------------------------
// for each tag search through all files for occurrences
///-----------------------------------------------------------------------------
cout << endl << "Searching tags occurrences in sources" << endl;
size_t t, tt = tags.size(), l, ll;
const vecstr* ln, *lu; string tag, su_old;
string::size_type p;
// progress
ii = bar;
pc_add = 100.f / ii; pc_next = pc_add; pc = 0.f;
for (i=1; i < ii; ++i)
cout << ".";
cout << endl;
// for each tag
for (t=0; t < tt; ++t)
{
//- progress
pc = 100.f * float(t) / float(tt);
if (pc > pc_next)
{
cout << "|";
pc_next += pc_add;
}
Tag& ta = tags[t];
stringstream og, os;
tag = "#{" + ta.name + "}"; // to search
// for each gui file
//--------------------------------
ii = file_lay.size();
bool nLn = false; su_old = "";
for (i=0; i < ii; ++i)
{
ln = lay_lin[i]; lu = lay_use[i];
bool fname = true;
// for each line
ll = ln->size();
for (l=0; l < ll; ++l)
{
///---- tag in layout -----
if (found((*ln)[l], tag))
{
if (nLn)
{ og << endl << "#: "; }
if (fname)
{
if (!nLn)
og << "#: ";
og << layDir << lay[i]; // lay file name
}
if (nLn) nLn = false;
og << ":" << l; // lay line number
if (fname)
og << endl << "#: ";
fname = false;
const string& su = (*lu)[l]; // hierarchy
if (su != su_old)
{ og << ".." << su; nLn = true; } ///par concat, start
su_old = su;
} }
if (!fname)
nLn = true;
}
// save
//log(og.str()); //test
ta.gui = og.str();
// for each src file
//--------------------------------
nLn = false;
ii = file_src.size();
for (i=0; i < ii; ++i)
{
string sf = file_src[i].substr(pSrc.length());
ln = src_lin[i];
bool fname = true;
// for each line
ll = ln->size();
for (l=0; l < ll; ++l)
{
///---- tag in source -----
if (found((*ln)[l], tag))
{
if (fname)
{
if (nLn)
{ os << endl; nLn = false; }
os << "#: " << srcDir << sf; // file name
}
fname = false;
os << ":" << l; // line number
} }
if (!fname)
nLn = true;
}
// save
//if (!os.str().empty()) log(os.str()); //test
ta.src = os.str();
}
cout << endl;
// delete ----
ii = lay_lin.size();
for (i=0; i < ii; ++i)
delete lay_lin[i];
ii = src_lin.size();
for (i=0; i < ii; ++i)
delete src_lin[i];
/// write output .pot file
//-----------------------------------------------------------------------------
ofstream of;
of.open(pot.c_str(), ios_base::out);
stringstream oe, ol;
// header
of << "# Stunt Rally translation.\n";
of << "# .pot template by SR-Translator tool, 2022-04-09.\n"; // last tool update
of << "#\n";
of << "#, fuzzy\n";
of << "msgid \"\"\n";
of << "msgstr \"\"\n";
of << "\"Project-Id-Version: PACKAGE VERSION\\n\"\n";
of << "\"Report-Msgid-Bugs-To: \\n\"\n";
//of << "\"POT-Creation-Date: 2022-01-01 00:00+0200\\n\"\n";
of << "\"POT-Creation-Date: " << dt.str() << "\\n\"\n";
of << "\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n";
of << "\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n";
of << "\"Language-Team: LANGUAGE <[email protected]>\\n\"\n";
of << "\"Language: \\n\"\n";
of << "\"MIME-Version: 1.0\\n\"\n";
of << "\"Content-Type: text/plain; charset=UTF-8\\n\"\n";
of << "\"Content-Transfer-Encoding: 8bit\\n\"\n";
of << "\n";
// for each tag
for (t=0; t < tt; ++t)
{
const Tag& ta = tags[t];
const string& s = ta.name;
if (ta.cmt.empty())
log(s << "COMMENT empty for: " << s);
of << "#. " << ta.cmt << endl; // comment
// of << "#: ";
if (!ta.gui.empty())
of << ta.gui << endl; // occurrences
if (!ta.src.empty())
of << ta.src << endl;
l = ta.gui.length() + ta.src .length();
if (l > 300) ///par 200
ol << " " << s << " " << l << endl;
if (ta.gui.empty() && ta.src.empty())
if (!found(s,"SC_") && !found(s,"CarDesc_") && // false, are used
!found(s,"Hint-") && !found(s,"InputMap") &&
!found(s,"LS_") && !found(s,"CarType_") &&
!found(s,"MessageBox_") && !found(s,"Diff"))
oe << " " << s << endl;
of << "msgctxt \"" << s << "\"" << endl; // context, tag name in xml
of << "msgid \"" << ta.text << "\"" << endl; // english text
of << "msgstr \"\"" << endl; // empty, translation
of << endl;
}
log(endl << "EMPTY tag occurrences for: ");
log(oe.str());
log("LONG tag occurrences for: ");
log(ol.str());
cout << endl << "*** End ***" << endl;
return 0;
}