-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyValues.cpp
414 lines (395 loc) · 12.5 KB
/
KeyValues.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
#include "KeyValues.h"
#include "translator.h"
#include <QFile>
#include <QFileDevice>
#include <QTextStream>
#include <QChar>
#include <QDebug>
#include <QRegularExpression>
static bool s_bMultiLineValue = false;
static KeyValueData EmptyKeyValues()
{
KeyValueData data;
data.Value.clear();
data.Key.clear();
return data;
}
static KeyValueData s_MultiLineValue = EmptyKeyValues();
KeyValues::KeyValues( const char *setName )
{
m_Language = "";
m_Name = setName;
m_Keys.clear();
}
void KeyValues::SetString( const QString szKey, const QString szValue )
{
KeyValueData data;
data.Key = szKey;
data.Value = szValue;
m_Keys.push_back( data );
}
bool KeyValues::SaveFile( const QString szPath, const QString szLangauge )
{
if ( szPath.isEmpty() ) return false;
std::string szSTDLangFile = szPath.toStdString();
std::string szLanguageStripped = szLangauge.toStdString();
ReplaceStringInPlace( szLanguageStripped, "lang_", "" );
std::string szReplaceString = "_" + szLanguageStripped + ".txt";
if ( !ReplaceStringInPlace( szSTDLangFile, ".txt", szReplaceString ) )
szSTDLangFile += szReplaceString;
QFile file( QString::fromStdString(szSTDLangFile) );
if (!file.open(QIODevice::WriteOnly)) return false;
QTextStream stream( &file );
stream.setEncoding( QStringConverter::Encoding::Utf16LE );
stream.setGenerateByteOrderMark( true );
//Output will be this:
/*
* "lang"
* {
* "Language" "english"
* "Tokens"
* {
* "UI_EXAMPLE" "My Sample Text"
* ...
* }
* }
*/
stream << "\"lang\"\n";
stream << "{\n";
stream << "\t\"Language\"\t\"" << QString::fromStdString(szLanguageStripped) << "\"\n";
stream << "\t\"Tokens\"\n";
stream << "\t{\n";
for (const auto &data : m_Keys)
{
// Check for special keys, if true, then add [$FOO] at the end.
bool bFoundSpecialKey = false;
QString SpecialString;
for (const auto &var : data.Key)
{
if ( '$' == var )
bFoundSpecialKey = true;
if ( bFoundSpecialKey )
SpecialString += var;
}
// If the special key is empty, do the normal output
if ( SpecialString.isEmpty() )
stream << "\t\t\"" << data.Key << "\"\t\"" << data.Value << "\"\n";
else
{
// We can't use data.Key directly, as it's a const. So we need to copy it temporarily and replace the string.
QString newKey = data.Key;
stream << "\t\t\"" << newKey.replace(SpecialString, "") << "\"\t\"" << data.Value << "\"\t[" << SpecialString << "]\n";
}
}
stream << "\t}\n";
stream << "}\n";
stream.flush();
file.close();
return true;
}
KeyValueRead_e KeyValues::LoadFile( const QString szFile )
{
QFile f( szFile );
if ( f.open(QIODevice::ReadOnly) )
{
int lines = 0;
s_bMultiLineValue = false;
enum TranslationReading
{
ReadNone = 0,
ReadLang,
ReadTokens
};
TranslationReading eTranslationKV = ReadNone;
QTextStream in(&f);
while (!in.atEnd())
{
QString line = in.readLine();
KeyValueData data = ReadLine( line, (eTranslationKV == ReadTokens) );
lines++;
switch (eTranslationKV)
{
case ReadNone:
{
if ( data.Key == "lang" )
eTranslationKV = ReadLang;
}
break;
case ReadLang:
{
if ( data.Key == "Language" )
m_Language = data.Value.toLower();
else if ( data.Key == "Tokens" && m_Language != "" )
eTranslationKV = ReadTokens;
}
break;
case ReadTokens:
{
// Did the previous data not have an end qoute?
if ( s_bMultiLineValue ) continue;
// Ignore comments etc.
if ( IsLineIgnored( line ) ) continue;
// We can't add nothing
if ( data.Key == "" ) continue;
m_Keys.push_back( data );
}
break;
}
}
f.flush();
f.close();
// if equals to 1, then it may be a bloat file
if ( lines == 1 )
return LoadBloatFile( szFile );
if ( eTranslationKV == ReadNone )
return KVRead_ERR_NOTLANG;
return (m_Keys.size() > 0) ? KVRead_OK : KVRead_ERR_LANG_NOT_FOUND;
}
return KVRead_ERR_NOTLANG;
}
KeyValueRead_e KeyValues::LoadBloatFile( const QString szFile )
{
QFile f( szFile );
if ( f.open(QIODevice::ReadOnly) )
{
s_bMultiLineValue = false;
enum TranslationReading
{
ReadNone = 0,
ReadLang,
ReadTokens
};
TranslationReading eTranslationKV = ReadNone;
QTextStream in(&f);
QString rawdata = in.readAll();
static QRegularExpression s_BloatCheck("\r");
QList<QString> lines = rawdata.split(s_BloatCheck);
for (const auto &line : lines)
{
//qInfo() << "Line: [" << line.toStdString() << "]";
KeyValueData data = ReadLine( line, (eTranslationKV == ReadTokens) );
switch (eTranslationKV)
{
case ReadNone:
{
if ( data.Key == "lang" )
eTranslationKV = ReadLang;
}
break;
case ReadLang:
{
if ( data.Key == "Language" )
m_Language = data.Value.toLower();
else if ( data.Key == "Tokens" && m_Language != "" )
eTranslationKV = ReadTokens;
}
break;
case ReadTokens:
{
// Did the previous data not have an end qoute?
if ( s_bMultiLineValue ) continue;
// Ignore comments etc.
if ( IsLineIgnored( line ) ) continue;
// We can't add nothing
if ( data.Key == "" ) continue;
m_Keys.push_back( data );
}
break;
}
}
f.flush();
f.close();
if ( eTranslationKV == ReadNone )
return KVRead_ERR_NOTLANG;
return (m_Keys.size() > 0) ? KVRead_OK : KVRead_ERR_LANG_NOT_FOUND;
}
return KVRead_ERR_NOTLANG;
}
void KeyValues::Export( const QString szPath, const QString szFile, Json::Value JsonData )
{
m_Keys.clear();
// Exports the json data to KeyValues
std::vector<std::string> langgroups = JsonData.getMemberNames();
for (const auto &language : langgroups)
{
std::vector<std::string> keygroups = JsonData[language].getMemberNames();
for (const auto &key : keygroups)
{
SetString( QString::fromStdString(key), QString::fromStdString(JsonData[language][key].asString()) );
}
SaveFile( szPath, QString::fromStdString(language) );
m_Keys.clear();
}
}
bool KeyValues::IsLineIgnored( QString szLine )
{
if ( szLine.indexOf("//") != -1 ) return true;
return false;
}
KeyValueData KeyValues::ReadLine( QString szLine, bool bTokens )
{
KeyValueData data;
enum QouteState
{
BEGIN = 0,
KEY_START,
KEY_END,
VALUE_START,
VALUE_END,
VALUE_SPECIAL_KEY_FOUND,
VALUE_SPECIAL_KEY_VALID,
VALUE_SPECIAL_KEY_END
};
QouteState eState = BEGIN;
bool bFoundAValidToken = false;
bool bSlashQoute = false;
bool bMultilineReading = s_bMultiLineValue;
if ( !bTokens )
bMultilineReading = false;
// If not multiline
if ( !bMultilineReading )
{
for (const auto &var : szLine)
{
// We don't want to include the qoute
// But if we had a backslash before this qoute, then we simply add it.
if ( '"' == var && !bSlashQoute )
{
switch( eState )
{
case BEGIN: eState = KEY_START; bFoundAValidToken = true; break;
case KEY_START: eState = KEY_END; break;
case KEY_END: eState = VALUE_START; break;
case VALUE_START: eState = VALUE_END; break;
default: break;
}
continue;
}
// Grab the Key
if ( eState == KEY_START )
data.Key += var;
// Grab the Value
else if ( eState == VALUE_START )
data.Value += var;
// Do this last, since we want to check if this current var has a slash
if ( '\\' == var )
bSlashQoute = true;
else
bSlashQoute = false;
// At the very end, check if we got a special key assigned.
if ( eState >= VALUE_END )
{
switch( eState )
{
case VALUE_END:
{
if ( '[' == var )
eState = VALUE_SPECIAL_KEY_FOUND;
}
break;
case VALUE_SPECIAL_KEY_FOUND:
{
if ( '$' == var )
eState = VALUE_SPECIAL_KEY_VALID;
}
break;
case VALUE_SPECIAL_KEY_VALID:
{
if ( ']' == var )
eState = VALUE_SPECIAL_KEY_END;
}
break;
default: break;
}
if ( eState == VALUE_SPECIAL_KEY_VALID )
{
// Apply the special key
data.Key += var;
}
}
}
}
else
{
for (const auto &var : szLine)
{
// We don't want to include the qoute
// But if we had a backslash before this qoute, then we simply add it.
if ( '"' == var && !bSlashQoute )
{
eState = VALUE_END;
break;
}
else
s_MultiLineValue.Value += var;
// Do this last, since we want to check if this current var has a slash
if ( '\\' == var )
bSlashQoute = true;
else
bSlashQoute = false;
// At the very end, check if we got a special key assigned.
if ( eState >= VALUE_END )
{
switch( eState )
{
case VALUE_END:
{
if ( '[' == var )
eState = VALUE_SPECIAL_KEY_FOUND;
}
break;
case VALUE_SPECIAL_KEY_FOUND:
{
if ( '$' == var )
eState = VALUE_SPECIAL_KEY_VALID;
}
break;
case VALUE_SPECIAL_KEY_VALID:
{
if ( ']' == var )
eState = VALUE_SPECIAL_KEY_END;
}
break;
default: break;
}
if ( eState == VALUE_SPECIAL_KEY_VALID )
{
// Apply the special key
s_MultiLineValue.Key += var;
}
}
}
}
if (bTokens)
{
if ( !bFoundAValidToken && !s_bMultiLineValue )
{
data.Key.clear();
data.Value.clear();
return data;
}
// If eState is VALUE_END (or higher), then we ain't a multiline value
if ( eState >= VALUE_END )
{
if ( s_bMultiLineValue )
{
s_bMultiLineValue = false;
data = s_MultiLineValue;
}
}
else
{
if ( !s_bMultiLineValue )
{
data.Value += "\n";
s_MultiLineValue = data;
s_bMultiLineValue = true;
}
}
}
return data;
}
void KeyValues::GrabKeyValues( std::list<KeyValueData> &keys )
{
keys = m_Keys;
}