doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LangDict.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "precompiled.h"
30 #pragma hdrstop
31 
32 
33 /*
34 ============
35 idLangDict::idLangDict
36 ============
37 */
39  args.SetGranularity( 256 );
40  hash.SetGranularity( 256 );
41  hash.Clear( 4096, 8192 );
42  baseID = 0;
43 }
44 
45 /*
46 ============
47 idLangDict::~idLangDict
48 ============
49 */
51  Clear();
52 }
53 
54 /*
55 ============
56 idLangDict::Clear
57 ============
58 */
59 void idLangDict::Clear( void ) {
60  args.Clear();
61  hash.Clear();
62 }
63 
64 /*
65 ============
66 idLangDict::Load
67 ============
68 */
69 bool idLangDict::Load( const char *fileName, bool clear /* _D3XP */ ) {
70 
71  if ( clear ) {
72  Clear();
73  }
74 
75  const char *buffer = NULL;
77 
78  int len = idLib::fileSystem->ReadFile( fileName, (void**)&buffer );
79  if ( len <= 0 ) {
80  // let whoever called us deal with the failure (so sys_lang can be reset)
81  return false;
82  }
83  src.LoadMemory( buffer, strlen( buffer ), fileName );
84  if ( !src.IsLoaded() ) {
85  return false;
86  }
87 
88  idToken tok, tok2;
89  src.ExpectTokenString( "{" );
90  while ( src.ReadToken( &tok ) ) {
91  if ( tok == "}" ) {
92  break;
93  }
94  if ( src.ReadToken( &tok2 ) ) {
95  if ( tok2 == "}" ) {
96  break;
97  }
98  idLangKeyValue kv;
99  kv.key = tok;
100  kv.value = tok2;
102  hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
103  }
104  }
105  idLib::common->Printf( "%i strings read from %s\n", args.Num(), fileName );
106  idLib::fileSystem->FreeFile( (void*)buffer );
107 
108  return true;
109 }
110 
111 /*
112 ============
113 idLangDict::Save
114 ============
115 */
116 void idLangDict::Save( const char *fileName ) {
117  idFile *outFile = idLib::fileSystem->OpenFileWrite( fileName );
118  outFile->WriteFloatString( "// string table\n// english\n//\n\n{\n" );
119  for ( int j = 0; j < args.Num(); j++ ) {
120  outFile->WriteFloatString( "\t\"%s\"\t\"", args[j].key.c_str() );
121  int l = args[j].value.Length();
122  char slash = '\\';
123  char tab = 't';
124  char nl = 'n';
125  for ( int k = 0; k < l; k++ ) {
126  char ch = args[j].value[k];
127  if ( ch == '\t' ) {
128  outFile->Write( &slash, 1 );
129  outFile->Write( &tab, 1 );
130  } else if ( ch == '\n' || ch == '\r' ) {
131  outFile->Write( &slash, 1 );
132  outFile->Write( &nl, 1 );
133  } else {
134  outFile->Write( &ch, 1 );
135  }
136  }
137  outFile->WriteFloatString( "\"\n" );
138  }
139  outFile->WriteFloatString( "\n}\n" );
140  idLib::fileSystem->CloseFile( outFile );
141 }
142 
143 /*
144 ============
145 idLangDict::GetString
146 ============
147 */
148 const char *idLangDict::GetString( const char *str ) const {
149 
150  if ( str == NULL || str[0] == '\0' ) {
151  return "";
152  }
153 
154  if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) != 0 ) {
155  return str;
156  }
157 
158  int hashKey = GetHashKey( str );
159  for ( int i = hash.First( hashKey ); i != -1; i = hash.Next( i ) ) {
160  if ( args[i].key.Cmp( str ) == 0 ) {
161  return args[i].value;
162  }
163  }
164 
165  idLib::common->Warning( "Unknown string id %s", str );
166  return str;
167 }
168 
169 /*
170 ============
171 idLangDict::AddString
172 ============
173 */
174 const char *idLangDict::AddString( const char *str ) {
175 
176  if ( ExcludeString( str ) ) {
177  return str;
178  }
179 
180  int c = args.Num();
181  for ( int j = 0; j < c; j++ ) {
182  if ( idStr::Cmp( args[j].value, str ) == 0 ){
183  return args[j].key;
184  }
185  }
186 
187  int id = GetNextId();
188  idLangKeyValue kv;
189  // _D3XP
190  kv.key = va( "#str_%08i", id );
191  // kv.key = va( "#str_%05i", id );
192  kv.value = str;
193  c = args.Append( kv );
195  hash.Add( GetHashKey( kv.key ), c );
196  return args[c].key;
197 }
198 
199 /*
200 ============
201 idLangDict::GetNumKeyVals
202 ============
203 */
204 int idLangDict::GetNumKeyVals( void ) const {
205  return args.Num();
206 }
207 
208 /*
209 ============
210 idLangDict::GetKeyVal
211 ============
212 */
213 const idLangKeyValue * idLangDict::GetKeyVal( int i ) const {
214  return &args[i];
215 }
216 
217 /*
218 ============
219 idLangDict::AddKeyVal
220 ============
221 */
222 void idLangDict::AddKeyVal( const char *key, const char *val ) {
223  idLangKeyValue kv;
224  kv.key = key;
225  kv.value = val;
227  hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
228 }
229 
230 /*
231 ============
232 idLangDict::ExcludeString
233 ============
234 */
235 bool idLangDict::ExcludeString( const char *str ) const {
236  if ( str == NULL ) {
237  return true;
238  }
239 
240  int c = strlen( str );
241  if ( c <= 1 ) {
242  return true;
243  }
244 
245  if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 ) {
246  return true;
247  }
248 
249  if ( idStr::Icmpn( str, "gui::", strlen( "gui::" ) ) == 0 ) {
250  return true;
251  }
252 
253  if ( str[0] == '$' ) {
254  return true;
255  }
256 
257  int i;
258  for ( i = 0; i < c; i++ ) {
259  if ( isalpha( str[i] ) ) {
260  break;
261  }
262  }
263  if ( i == c ) {
264  return true;
265  }
266 
267  return false;
268 }
269 
270 /*
271 ============
272 idLangDict::GetNextId
273 ============
274 */
275 int idLangDict::GetNextId( void ) const {
276  int c = args.Num();
277 
278  //Let and external user supply the base id for this dictionary
279  int id = baseID;
280 
281  if ( c == 0 ) {
282  return id;
283  }
284 
285  idStr work;
286  for ( int j = 0; j < c; j++ ) {
287  work = args[j].key;
288  work.StripLeading( STRTABLE_ID );
289  int test = atoi( work );
290  if ( test > id ) {
291  id = test;
292  }
293  }
294  return id + 1;
295 }
296 
297 /*
298 ============
299 idLangDict::GetHashKey
300 ============
301 */
302 int idLangDict::GetHashKey( const char *str ) const {
303  int hashKey = 0;
304  for ( str += STRTABLE_ID_LENGTH; str[0] != '\0'; str++ ) {
305  assert( str[0] >= '0' && str[0] <= '9' );
306  hashKey = hashKey * 10 + str[0] - '0';
307  }
308  return hashKey;
309 }
GLsizei const GLfloat * value
Definition: glext.h:3614
idList< idLangKeyValue > args
Definition: LangDict.h:64
assert(prefInfo.fullscreenBtn)
int Cmp(const char *text) const
Definition: Str.h:652
int Next(const int index) const
Definition: HashIndex.h:247
void StripLeading(const char c)
Definition: Str.cpp:469
virtual int ReadFile(const char *relativePath, void **buffer, ID_TIME_T *timestamp=NULL)=0
#define STRTABLE_ID
Definition: Common.h:56
void SetGranularity(int newgranularity)
Definition: List.h:305
~idLangDict(void)
Definition: LangDict.cpp:50
Definition: Token.h:71
GLuint src
Definition: glext.h:5390
GLenum GLsizei len
Definition: glext.h:3472
int i
Definition: process.py:33
virtual void FreeFile(void *buffer)=0
int Cmpn(const char *text, int n) const
Definition: Str.h:657
int test(char *url)
Definition: lib500.c:3
const char * AddString(const char *str)
Definition: LangDict.cpp:174
void SetGranularity(const int newGranularity)
Definition: HashIndex.h:369
int First(const int key) const
Definition: HashIndex.h:238
list l
Definition: prepare.py:17
#define STRTABLE_ID_LENGTH
Definition: Common.h:57
int Icmpn(const char *text, int n) const
Definition: Str.h:672
Definition: File.h:50
Definition: Lexer.h:137
static class idFileSystem * fileSystem
Definition: Lib.h:55
const GLubyte * c
Definition: glext.h:4677
idLangDict(void)
Definition: LangDict.cpp:38
#define NULL
Definition: Lib.h:88
const idLangKeyValue * GetKeyVal(int i) const
Definition: LangDict.cpp:213
bool ExcludeString(const char *str) const
Definition: LangDict.cpp:235
GLuint buffer
Definition: glext.h:3108
virtual idFile * OpenFileWrite(const char *relativePath, const char *basePath="fs_savepath")=0
const char * GetString(const char *str) const
Definition: LangDict.cpp:148
bool Load(const char *fileName, bool clear=true)
Definition: LangDict.cpp:69
virtual int WriteFloatString(const char *fmt,...) id_attribute((format(printf
Definition: File.cpp:294
virtual void Printf(const char *fmt,...) id_attribute((format(printf
int LoadMemory(const char *ptr, int length, const char *name, int startLine=1)
Definition: Lexer.cpp:1646
void Clear(void)
Definition: LangDict.cpp:59
int ExpectTokenString(const char *string)
Definition: Lexer.cpp:919
int Append(const type &obj)
Definition: List.h:646
idStr value
Definition: LangDict.h:43
GLuint id
Definition: glext.h:3103
int Num(void) const
Definition: List.h:265
void AddKeyVal(const char *key, const char *val)
Definition: LangDict.cpp:222
virtual int Write(const void *buffer, int len)
Definition: File.cpp:189
Definition: Str.h:116
void Clear(void)
Definition: HashIndex.h:328
void Add(const int key, const int index)
Definition: HashIndex.h:193
GLint j
Definition: qgl.h:264
int IsLoaded(void)
Definition: Lexer.h:158
char * va(const char *fmt,...)
Definition: Str.cpp:1568
virtual void CloseFile(idFile *f)=0
int GetNextId(void) const
Definition: LangDict.cpp:275
virtual void virtual void Warning(const char *fmt,...) id_attribute((format(printf
int ReadToken(idToken *token)
Definition: Lexer.cpp:820
idHashIndex hash
Definition: LangDict.h:68
void Save(const char *fileName)
Definition: LangDict.cpp:116
int baseID
Definition: LangDict.h:74
int GetNumKeyVals(void) const
Definition: LangDict.cpp:204
int GetHashKey(const char *str) const
Definition: LangDict.cpp:302
static class idCommon * common
Definition: Lib.h:53
void Clear(void)
Definition: List.h:184