Repository navigation
Expand file tree
/
Copy pathTranslationCreator.cpp
More file actions
502 lines (323 loc) · 8.59 KB
/
Copy pathTranslationCreator.cpp
File metadata and controls
502 lines (323 loc) · 8.59 KB
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
#include <stdio.h>
#include <string>
#include <list>
typedef std::list< std::string > StringList;
#define LINE_MAX_LENGTH 512
#define NO_DOUBLE_LINEBREAK 1
static void trim(std::string & str)
{
if (!str.empty() )
{
const char * whiteSpace = " \t\v\r\n\f";
std::size_t start = str.find_first_not_of(whiteSpace);
std::size_t end = str.find_last_not_of(whiteSpace);
if (start == std::string::npos && end == std::string::npos) // spaces only
str.clear();
else // trim
str = str.substr(start, end - start + 1);
}
}
static void skip_BOM(FILE * file)
{
bool bom_found = false;
unsigned char bytes[3];
size_t bytes_read = fread(bytes, 1, 3, file);
if (bytes_read == 3)
{
if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) // UTF-8 BOM
{
bom_found = true;
}
}
if (!bom_found)
{
fseek(file, 0, SEEK_SET);
}
}
// debugging purposes
void print_list(StringList & strList)
{
int i = 0;
for (std::string & line : strList)
{
printf("%d-%s\n", i, line.c_str() );
i++;
}
printf("\n");
}
// ---- list content manipulation
void remove_translations(StringList & strList)
{
for (std::string & line : strList)
{
// removes everything past the '='
size_t equal_index = line.find('=');
if (equal_index != std::string::npos)
line.erase(equal_index);
// removes any whitespaces
trim(line);
}
}
void remove_keys(StringList & strList)
{
for (std::string & line : strList)
{
// removes everything BEFORE the '=', or if empty, keeps the former
size_t equal_index = line.find('=');
if (equal_index != std::string::npos)
{
std::string value;
if (equal_index + 1 < line.length() )
{
value = std::string(line, equal_index + 1);
trim(value);
}
if (!value.empty() )
line = value;
else
line.erase(equal_index);
}
// removes any whitespaces
trim(line);
}
}
bool combine_translation(StringList & resultList, StringList & baseList, StringList & translationList)
{
if (baseList.size() != translationList.size() )
{
printf("Warning: lists have diferent sizes: %d / %d\n", baseList.size(), translationList.size() );
//return false;
}
resultList.clear();
// builds the resulting list
StringList::iterator it1 = baseList.begin();
StringList::iterator it2 = translationList.begin();
while (it1 != baseList.end() && it2 != translationList.end() )
{
std::string result_string;
// doesn't add an '=' if empty line
if ( !it1->empty() )
{
result_string = *it1 + " = ";
// if already equal, leave blank
if (*it1 != *it2)
result_string += *it2;
}
resultList.push_back(result_string);
++it1;
++it2;
}
return true;
}
bool split_key_and_value(const std::string & line, std::string & key, std::string & value)
{
// search for the '=' divisor to get the key
size_t equal_index = line.find('=');
if (equal_index != std::string::npos)
{
key = std::string(line, 0, equal_index);
const size_t key_length = key.length();
if (key_length > 0)
{
// get the value
if (line.length() > equal_index + 1)
{
value = std::string(line, equal_index + 1);
// remove white spaces
trim(key);
trim(value);
if (!key.empty() && !value.empty() )
return true;
}
}
}
return false;
}
bool match_case(std::string & word1, std::string & word2)
{
if (!word1.empty() && !word2.empty() )
{
// dont mess with unicode characters
if (word1[0] >= 65 && word1[0] <= 122 &&
word2[0] >= 65 && word2[0] <= 122)
{
bool word1_is_uppercase = isupper(word1[0]);
bool word2_is_uppercase = isupper(word2[0]);
if (word1_is_uppercase && !word2_is_uppercase)
{
word2[0] = toupper(word2[0]);
return true;
}
else if (!word1_is_uppercase && word2_is_uppercase)
{
word2[0] = tolower(word2[0]);
return true;
}
}
}
return false;
}
void match_key_value_case(StringList & strList)
{
int changes = 0;
for (std::string & line : strList)
{
std::string key;
std::string value;
if (split_key_and_value(line, key, value) )
{
if (match_case(key, value) )
{
line = key + " = " + value;
changes++;
}
}
}
printf("Changes: %d\n", changes);
}
// ----
// ---- file manipulation
bool load(StringList & strList, const char * filename)
{
strList.clear();
FILE * file = fopen(filename, "r");
if (file)
{
skip_BOM(file);
bool previous_empty = false;
while( !feof(file) )
{
char linec[LINE_MAX_LENGTH];
if (fgets(linec, LINE_MAX_LENGTH, file) )
{
std::string line(linec);
trim(line);
const bool current_empty = line.empty();
#ifdef NO_DOUBLE_LINEBREAK
if ( !(current_empty && previous_empty) )
#endif
strList.push_back(line);
previous_empty = current_empty;
}
}
printf("Read %d lines\n", strList.size() );
fclose(file);
return true;
}
printf("Error reading %s\n", filename);
return false;
}
bool save(StringList & strList, const char * filename)
{
FILE * file = fopen(filename, "w");
if (file)
{
fprintf(file, "%c%c%c", '\xEF', '\xBB', '\xBF'); // utf-8 BOM
for (std::string & line : strList)
{
fprintf(file, "%s\n", line.c_str() );
}
fclose(file);
return true;
}
printf("Error creating %s\n", filename);
return false;
}
bool get_line(FILE * file, std::string & str)
{
std::string result;
char linec[LINE_MAX_LENGTH];
if (fgets(linec, LINE_MAX_LENGTH, file) )
{
result = linec;
trim(result);
if (!result.empty() )
{
str = result;
return true;
}
}
return false;
}
// ----
int main()
{
int option = 0;
// file names
std::string translated = "target.txt";
std::string keys = "keys.txt";
std::string values = "values.txt";
std::string exported = "result.txt";
// try to load the filename list
FILE * file = fopen("filenames.txt", "r");
if (file)
{
get_line(file, translated);
get_line(file, keys);
get_line(file, values);
get_line(file, exported);
fclose(file);
}
// menu
printf("Output filename: %s\n\n", exported.c_str() );
printf("Select an option\n");
printf("1- Remove translations from %s\n", translated.c_str() );
printf("2- Remove keys from %s\n", translated.c_str() );
printf("3- Create translation file from %s and %s\n", keys.c_str(), values.c_str() );
printf("4- Fix case of %s\n", translated.c_str() );
scanf("%d", &option);
if (option == 1) // remove translations (and leave keys)
{
StringList lista1;
if (load(lista1, translated.c_str() ) )
{
remove_translations(lista1);
if ( save(lista1, exported.c_str() ) )
printf("Saved %s suscessfully\n", exported.c_str() );
}
else printf("Error loading %s\n", translated.c_str() );
}
else if (option == 2) // remove keys (and leave translations)
{
StringList lista1;
if (load(lista1, translated.c_str() ) )
{
remove_keys(lista1);
if ( save(lista1, exported.c_str() ) )
printf("Saved %s suscessfully\n", exported.c_str() );
}
else printf("Error loading %s\n", translated.c_str() );
}
else if (option == 3) // create translation
{
StringList lista_base;
if ( load(lista_base, keys.c_str() ) )
{
StringList lista_traducoes;
if ( load(lista_traducoes, values.c_str() ) )
{
StringList lista_resultado;
if ( combine_translation(lista_resultado, lista_base, lista_traducoes) )
{
if ( save(lista_resultado, exported.c_str() ) )
printf("Translation saved as %s suscessfully\n", exported.c_str() );
}
}
else printf("Error loading %s\n", values.c_str() );
}
else printf("Error loading %s\n", keys.c_str() );
}
else if (option == 4) // fix case
{
StringList lista1;
if (load(lista1, translated.c_str() ) )
{
match_key_value_case(lista1);
if ( save(lista1, exported.c_str() ) )
printf("Saved %s suscessfully\n", exported.c_str() );
}
else printf("Error loading %s\n", translated.c_str() );
}
else printf("No option\n");
return 0;
}