/
main.cpp
369 lines (301 loc) · 11.3 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
#include <iostream>
#include <assert.h>
#include <algorithm>
#include <sstream>
//model
#include "ts_model.h"
//Qt
#include <QString>
#include <QCoreApplication>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QTextStream>
#include <QRegularExpression>
#include <QFileInfo>
#include <QDir>
#define VERSION "2.6"
void toTXT(const QString &inputFile, const QString &outputDir, bool with_unfinished, bool with_vanished, bool unfinished_only);
void toTS(const QString &inputDir, const QString &outputFile, const QString &langid);
//SHOULD BE IN SAME ORDER AS in args[]
enum EArgID {
arg_unknown = -1
, arg_help = 0
, arg_src
, arg_dst
, arg_mode
, arg_langid
, arg_with_unfinished
, arg_with_vanished
, arg_unfinished_only
};
struct argument_info
{
EArgID id;
const char *name;
const char *description;
bool flag;
};
//SHOULD BE IN SAME ORDER AS in EArgID
static const argument_info args[] = {
{arg_help, "--help", "Show this help", true}
, {arg_src, "--src", "Source file or directory", false}
, {arg_dst, "--dst", "Destination file or directory", false}
, {arg_mode, "--mode", "Mode: TXT i.e. .ts to .txt | TS i.e. .txt to .ts", false}
, {arg_langid, "--langid", "Language id. For example: en_US, de_DE, ja. According to QtLibguist specification. If not set leave default [Work only in TS mode]", false}
, {arg_with_unfinished, "--with-unfinished", "Include unfinished translations. By default: ignore. [Work only in TXT mode]", true}
, {arg_with_vanished, "--with-vanished", "Include obsolete translations. By default: ignore. [Work only in TXT mode]", true}
, {arg_unfinished_only, "--unfinished-only", "Only unfinished records. By default: ignore. [Work only in TXT mode]", true}
};
void show_help(int exit_code)
{
std::cout << "ts_tool v" VERSION " CODIJY 2018" << std::endl;
std::cout << "Arguments:" << std::endl;
std::for_each(args, args+sizeof(args)/sizeof(argument_info), [](const argument_info &nfo)
{
std::cout << "\t" << nfo.name << " " << nfo.description << std::endl;
}
);
exit(exit_code);
}
EArgID get_arg_id(const char *value)
{
EArgID id = arg_unknown;
std::any_of(args, args+sizeof(args)/sizeof(argument_info), [&id, value](const argument_info &nfo) -> bool
{
if(0 == strcmp(nfo.name, value)) {
id = nfo.id;
}
return arg_unknown != id;
}
);
return id;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("td_tool");
QCoreApplication::setApplicationVersion(VERSION);
QString src, dst, mode, langid;
bool with_unfinished = false, with_vanished = false, unfinished_only = false;
if(1 == argc) {
show_help(0);
}
QString *value = nullptr;
for(int n = 1; n < argc; ++n)
{
EArgID argId = get_arg_id(argv[n]);
switch(argId)
{
case arg_unknown: show_help(-1); break;
case arg_src: value = &src; break;
case arg_dst: value = &dst; break;
case arg_mode: value = &mode; break;
case arg_langid: value = &langid; break;
case arg_with_unfinished: with_unfinished = true; break;
case arg_with_vanished: with_vanished = true; break;
case arg_unfinished_only: unfinished_only = true; break;
}
if(value) {
*value = argv[++n];
}
value = nullptr;
}
if(src.isEmpty() || dst.isEmpty() || mode.isEmpty()) {
std::cout << "You may use at least first 3 args" << std::endl;
show_help(-1);
}
if("TXT" == mode)
{
toTXT(src, dst, with_unfinished, with_vanished, unfinished_only);
}
else if("TS" == mode)
{
toTS(src, dst, langid);
}
else
{
std::cout << "Invalid mode" << std::endl;
show_help(-1);
}
return 0;
}
base_node::base_node_ptr parse_ts_file(const QString &inputFile)
{
QFile iFile(inputFile);
iFile.open(QIODevice::ReadOnly);
QXmlStreamReader xmlReader(&iFile);
base_node::base_node_ptr root, current;
QString text;
enum EStates {
st_Unstate = 0
, st_WaitForStartElement = 0x01
, st_WaitForText = 0x02
, st_WaitForEndElement = 0x04
};
int states = st_WaitForStartElement;
while(!xmlReader.atEnd())
{
QXmlStreamReader::TokenType tt = xmlReader.readNext();
switch(tt)
{
case QXmlStreamReader::StartDocument:
{
root = std::make_shared<document_node>();
current = root;
} break;
case QXmlStreamReader::DTD:
{
current->add_child(std::make_shared<DTD_node>("<!DOCTYPE TS>"));
} break;
case QXmlStreamReader::StartElement:
{
assert(states & st_WaitForStartElement);
QString name = xmlReader.name().toString();
QXmlStreamAttributes attrs = xmlReader.attributes();
if("message" == name) {
current = current->add_child(std::make_shared<element_node>(element_node::ent_message, name, attrs));
} else if("source" == name) {
current = current->add_child(std::make_shared<element_node>(element_node::ent_source, name, attrs));
} else if("translation" == name) {
current = current->add_child(std::make_shared<element_node>(element_node::ent_translation, name, attrs));
} else if("TS" == name) {
current = current->add_child(std::make_shared<TS_node>(name, attrs));
} else {
current = current->add_child(std::make_shared<element_node>(element_node::ent_element, name, attrs));
}
states = st_WaitForText|st_WaitForStartElement|st_WaitForEndElement;
} break;
case QXmlStreamReader::Characters:
{
if(states & st_WaitForText)
{
text = xmlReader.text().toString();
states = st_WaitForEndElement|st_WaitForStartElement;
}
} break;
case QXmlStreamReader::EndElement:
{
assert(states & st_WaitForEndElement);
assert(current->kind() & base_node::nt_Element);
((element_node*)current.get())->set_text(text);
text.clear();
states = st_WaitForStartElement|st_WaitForEndElement;
current = current->parent();
} break;
}
}
return root;
}
bool parse_txt_file(const QString &inputFile, visitors::map_QStringQString &strings)
{
QFile iFile(inputFile);
iFile.open(QFile::ReadOnly|QFile::Text);
QTextStream txts(&iFile);
txts.setCodec("UTF-8");
const QString rgxp("^(?<id>\\[\\[\\[[A-F0-9]{8}\\]\\]\\])\\s*[\\\",“,”](?<text>.*)[\\\",“,”]$");
QRegularExpression rxp(rgxp);
unsigned int line_counter = 0;
while(!txts.atEnd())
{
QString str = txts.readLine();
QRegularExpressionMatch rm = rxp.match(str);
QString id = rm.captured("id");
QString text = rm.captured("text");
if(id.isEmpty() || text.isEmpty())
{
std::cout << "Error in line: " << line_counter << " , file: " << inputFile.toUtf8().constData() << " , source line: " << str.toUtf8().constData() << std::endl;
return false;
}
strings.insert(visitors::map_QStringQString::value_type(id, text));
line_counter++;
}
return true;
}
void toTXT(const QString &inputFile, const QString &outputDir, bool with_unfinished, bool with_vanished, bool unfinished_only)
{
using namespace visitors;
QFileInfo fiI(inputFile);
if(!fiI.exists()) {
std::cout << "Input file not exist!" << std::endl;
show_help(-1);
}
QFileInfo fiO(outputDir);
if(!fiO.exists()) {
QDir().mkdir(outputDir);
}
QString outputXmlFileName = QDir(outputDir).path() + "/" + fiI.fileName();
QString outputTextFile = QDir(outputDir).path() + "/" + fiI.baseName() + ".txt";
unsigned int files_in_out_dir = QDir(outputDir).entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).count();
if( !fiO.exists()
|| 2 < files_in_out_dir
|| (2 == files_in_out_dir && !QFileInfo(outputXmlFileName).exists() && !QFileInfo(outputTextFile).exists()) )
{
std::cout << "Cant create output directory OR directory is not empty!" << std::endl;
show_help(-1);
}
QFile oFile(outputXmlFileName);
oFile.open(QIODevice::WriteOnly);
//pares ts file
base_node::base_node_ptr root = parse_ts_file(inputFile);
//replace strings
map_hashQString strings;
string_extractor_replacer ser(strings, with_unfinished, with_vanished, unfinished_only);
root->visit(ser);
//write text file
QFile sFile(outputTextFile);
sFile.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream txts(&sFile);
txts.setCodec("UTF-8");
std::for_each(strings.begin(), strings.end(), [&txts](const map_hashQString::value_type &vt){ txts << vt.second << "\n"; });
//write modified ts file
QXmlStreamWriter xmlWriter(&oFile);
xmlWriter.setAutoFormatting(true);
document_dump ddv(xmlWriter);
root->visit(ddv);
}
void toTS(const QString &inputDir, const QString &outputFile, const QString &langid)
{
using namespace visitors;
QFileInfo fiI(inputDir);
if(!fiI.exists()) {
std::cout << "Input directory not exist!" << std::endl;
show_help(-1);
}
const QFileInfoList &fil = QDir(inputDir).entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries);
unsigned int files_in_input_dir = fil.count();
QString tsFile, txtFile;
if(2 == files_in_input_dir)
{
QFileInfo if0(QDir(inputDir).path() + "/" + fil[0].baseName() + ".ts");
QFileInfo if1(QDir(inputDir).path() + "/" + fil[0].baseName() + ".txt");
if(if0.isFile() && if1.isFile())
{
tsFile = if0.filePath();
txtFile = if1.filePath();
}
}
if(2 < files_in_input_dir || 0 == files_in_input_dir || tsFile.isEmpty() || txtFile.isEmpty())
{
std::cout << "Input directory should contain only txt and ts file with same name!" << std::endl;
show_help(-1);
}
//pares ts file
base_node::base_node_ptr root = parse_ts_file(tsFile);
//parse txt file
map_QStringQString strings;
if(!parse_txt_file(txtFile, strings)) {
std::cout << "Parsing error: " << txtFile.toUtf8().constData() << " !" << std::endl;
show_help(-1);
}
//replace strings
back_string_replacer bsr(strings, langid);
root->visit(bsr);
//dump to file
QFile oFile(outputFile);
oFile.open(QIODevice::WriteOnly);
QXmlStreamWriter xmlWriter(&oFile);
xmlWriter.setAutoFormatting(true);
xmlWriter.setCodec("UTF-8");
document_dump ddv(xmlWriter);
root->visit(ddv);
}