-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLangDict.cpp
323 lines (281 loc) · 6.8 KB
/
LangDict.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
/*****************************************************************************
The Dark Mod GPL Source Code
This file is part of the The Dark Mod Source Code, originally based
on the Doom 3 GPL Source Code as published in 2011.
The Dark Mod Source Code is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version. For details, see LICENSE.TXT.
Project: The Dark Mod (http://www.thedarkmod.com/)
******************************************************************************/
#include "precompiled.h"
#pragma hdrstop
/*
============
idLangDict::idLangDict
============
*/
idLangDict::idLangDict( void ) {
args.SetGranularity( 256 );
hash.SetGranularity( 256 );
hash.ClearFree( 4096, 8192 );
baseID = 0;
}
/*
============
idLangDict::~idLangDict
============
*/
idLangDict::~idLangDict( void ) {
Clear();
}
/*
============
idLangDict::Clear
============
*/
void idLangDict::Clear( void ) {
args.Clear();
hash.Clear();
}
/*
============
idLangDict::Load
============
*/
bool idLangDict::Load( const char *fileName, const bool clear /* _D3XP */, const unsigned int remapcount, const char *remap ) {
if ( clear ) {
Clear();
}
const char *buffer = NULL;
idLexer src( LEXFL_NOFATALERRORS | LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
int len = idLib::fileSystem->ReadFile( fileName, (void**)&buffer );
if ( len <= 0 ) {
// let whoever called us deal with the failure (so sys_lang can be reset)
return false;
}
src.LoadMemory(buffer, static_cast<int>(strlen(buffer)), fileName);
if ( !src.IsLoaded() ) {
return false;
}
idToken tok, tok2;
src.ExpectTokenString( "{" );
while ( src.ReadToken( &tok ) ) {
if ( tok == "}" ) {
break;
}
if ( src.ReadToken( &tok2 ) ) {
if ( tok2 == "}" ) {
break;
}
idLangKeyValue kv;
kv.key = tok;
kv.value = tok2;
if (remap && remapcount > 0)
{
// Tels: fix #2812, some characters like 0xFF ("я" in russian) are not rendered
// in the GUI, so replace them (as the font contains the characters elsewhere).
// If we were given a replacement table, use it to exchange the characters:
kv.value.Remap( remapcount, remap );
}
assert( kv.key.Cmpn( STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 );
hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
}
}
idLib::common->Printf( "I18N: %i strings read from %s\n", args.Num(), fileName );
idLib::fileSystem->FreeFile( (void*)buffer );
return true;
}
/*
============
idLangDict::Save
============
*/
void idLangDict::Save( const char *fileName ) {
idFile *outFile = idLib::fileSystem->OpenFileWrite( fileName );
outFile->WriteFloatString( "// string table\n// english\n//\n\n{\n" );
for ( int j = 0; j < args.Num(); j++ ) {
outFile->WriteFloatString( "\t\"%s\"\t\"", args[j].key.c_str() );
int l = args[j].value.Length();
char slash = '\\';
char tab = 't';
char nl = 'n';
for ( int k = 0; k < l; k++ ) {
char ch = args[j].value[k];
if ( ch == '\t' ) {
outFile->Write( &slash, 1 );
outFile->Write( &tab, 1 );
} else if ( ch == '\n' || ch == '\r' ) {
outFile->Write( &slash, 1 );
outFile->Write( &nl, 1 );
} else {
outFile->Write( &ch, 1 );
}
}
outFile->WriteFloatString( "\"\n" );
}
outFile->WriteFloatString( "\n}\n" );
idLib::fileSystem->CloseFile( outFile );
}
/*
============
idLangDict::GetString
============
*/
const char *idLangDict::GetString( const char *str, const bool dowarn ) const {
if ( str == NULL || str[0] == '\0' ) {
return "";
}
if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) != 0 ) {
return str;
}
int hashKey = GetHashKey( str );
for ( int i = hash.First( hashKey ); i != -1; i = hash.Next( i ) ) {
if ( args[i].key.Cmp( str ) == 0 ) {
return args[i].value;
}
}
if (dowarn)
{
idLib::common->Warning( "Unknown string id %s", str );
}
return str;
}
/*
============
idLangDict::AddString
============
*/
const char *idLangDict::AddString( const char *str ) {
if ( ExcludeString( str ) ) {
return str;
}
int c = args.Num();
for ( int j = 0; j < c; j++ ) {
if ( idStr::Cmp( args[j].value, str ) == 0 ){
return args[j].key;
}
}
int id = GetNextId();
idLangKeyValue kv;
// _D3XP
kv.key = va( "#str_%08i", id );
// kv.key = va( "#str_%05i", id );
kv.value = str;
c = args.Append( kv );
assert( kv.key.Cmpn( STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 );
hash.Add( GetHashKey( kv.key ), c );
return args[c].key;
}
/*
============
idLangDict::GetNumKeyVals
============
*/
int idLangDict::GetNumKeyVals( void ) const {
return args.Num();
}
/*
============
idLangDict::GetKeyVal
============
*/
const idLangKeyValue * idLangDict::GetKeyVal( const int i ) const {
return &args[i];
}
/*
============
idLangDict::AddKeyVal
============
*/
void idLangDict::AddKeyVal( const char *key, const char *val ) {
idLangKeyValue kv;
kv.key = key;
kv.value = val;
assert( kv.key.Cmpn( STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 );
hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
}
/*
============
idLangDict::ExcludeString
============
*/
bool idLangDict::ExcludeString( const char *str ) const {
if ( str == NULL ) {
return true;
}
int c = static_cast<int>(strlen(str));
if ( c <= 1 ) {
return true;
}
if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 ) {
return true;
}
if (idStr::Icmpn(str, "gui::", static_cast<int>(strlen("gui::"))) == 0) {
return true;
}
if ( str[0] == '$' ) {
return true;
}
int i;
for ( i = 0; i < c; i++ ) {
if ( isalpha( str[i] ) ) {
break;
}
}
if ( i == c ) {
return true;
}
return false;
}
/*
============
idLangDict::GetNextId
============
*/
int idLangDict::GetNextId( void ) const {
int c = args.Num();
// Let an external user supply the base id for this dictionary
int id = baseID;
if ( c == 0 ) {
return id;
}
idStr work;
for ( int j = 0; j < c; j++ ) {
work = args[j].key;
work.StripLeading( STRTABLE_ID );
int test = atoi( work );
if ( test > id ) {
id = test;
}
}
return id + 1;
}
/*
============
idLangDict::GetHashKey
============
*/
int idLangDict::GetHashKey( const char *str ) const {
/* int hashKey = 0;
for ( str += STRTABLE_ID_LENGTH; str[0] != '\0'; str++ ) {
assert( str[0] >= '0' && str[0] <= '9' );
hashKey = hashKey * 10 + str[0] - '0';
}*/
//stgatilov #5261: generic string hashing algorithm (djb2), no assert
int hashKey = 5381;
for ( str += STRTABLE_ID_LENGTH; str[0] != '\0'; str++ ) {
hashKey = (hashKey << 5) + hashKey + str[0];
}
return hashKey;
}
/*
============
idLangDict::Print
============
*/
void idLangDict::Print( void ) const {
int c = args.Num();
idLib::common->Printf("idLangDict: %d KB in %d entries.\n", static_cast<int>(args.Size() + hash.Size()) >> 10, c);
//hash.Print();
}