LangDict.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #include "precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. ============
  24. idLangDict::idLangDict
  25. ============
  26. */
  27. idLangDict::idLangDict( void ) {
  28. args.SetGranularity( 256 );
  29. hash.SetGranularity( 256 );
  30. hash.Clear( 4096, 8192 );
  31. baseID = 0;
  32. }
  33. /*
  34. ============
  35. idLangDict::~idLangDict
  36. ============
  37. */
  38. idLangDict::~idLangDict( void ) {
  39. Clear();
  40. }
  41. /*
  42. ============
  43. idLangDict::Clear
  44. ============
  45. */
  46. void idLangDict::Clear( void ) {
  47. args.Clear();
  48. hash.Clear();
  49. }
  50. /*
  51. ============
  52. idLangDict::Load
  53. ============
  54. */
  55. bool idLangDict::Load( const char *fileName, bool clear /* _D3XP */ ) {
  56. if ( clear ) {
  57. Clear();
  58. }
  59. const char *buffer = NULL;
  60. idLexer src( LEXFL_NOFATALERRORS | LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  61. int len = idLib::fileSystem->ReadFile( fileName, (void**)&buffer );
  62. if ( len <= 0 ) {
  63. // let whoever called us deal with the failure (so sys_lang can be reset)
  64. return false;
  65. }
  66. src.LoadMemory( buffer, strlen( buffer ), fileName );
  67. if ( !src.IsLoaded() ) {
  68. return false;
  69. }
  70. idToken tok, tok2;
  71. src.ExpectTokenString( "{" );
  72. while ( src.ReadToken( &tok ) ) {
  73. if ( tok == "}" ) {
  74. break;
  75. }
  76. if ( src.ReadToken( &tok2 ) ) {
  77. if ( tok2 == "}" ) {
  78. break;
  79. }
  80. idLangKeyValue kv;
  81. kv.key = tok;
  82. kv.value = tok2;
  83. assert( kv.key.Cmpn( STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 );
  84. hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
  85. }
  86. }
  87. idLib::common->Printf( "%i strings read from %s\n", args.Num(), fileName );
  88. idLib::fileSystem->FreeFile( (void*)buffer );
  89. return true;
  90. }
  91. /*
  92. ============
  93. idLangDict::Save
  94. ============
  95. */
  96. void idLangDict::Save( const char *fileName ) {
  97. idFile *outFile = idLib::fileSystem->OpenFileWrite( fileName );
  98. outFile->WriteFloatString( "// string table\n// english\n//\n\n{\n" );
  99. for ( int j = 0; j < args.Num(); j++ ) {
  100. outFile->WriteFloatString( "\t\"%s\"\t\"", args[j].key.c_str() );
  101. int l = args[j].value.Length();
  102. char slash = '\\';
  103. char tab = 't';
  104. char nl = 'n';
  105. for ( int k = 0; k < l; k++ ) {
  106. char ch = args[j].value[k];
  107. if ( ch == '\t' ) {
  108. outFile->Write( &slash, 1 );
  109. outFile->Write( &tab, 1 );
  110. } else if ( ch == '\n' || ch == '\r' ) {
  111. outFile->Write( &slash, 1 );
  112. outFile->Write( &nl, 1 );
  113. } else {
  114. outFile->Write( &ch, 1 );
  115. }
  116. }
  117. outFile->WriteFloatString( "\"\n" );
  118. }
  119. outFile->WriteFloatString( "\n}\n" );
  120. idLib::fileSystem->CloseFile( outFile );
  121. }
  122. /*
  123. ============
  124. idLangDict::GetString
  125. ============
  126. */
  127. const char *idLangDict::GetString( const char *str ) const {
  128. if ( str == NULL || str[0] == '\0' ) {
  129. return "";
  130. }
  131. if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) != 0 ) {
  132. return str;
  133. }
  134. int hashKey = GetHashKey( str );
  135. for ( int i = hash.First( hashKey ); i != -1; i = hash.Next( i ) ) {
  136. if ( args[i].key.Cmp( str ) == 0 ) {
  137. return args[i].value;
  138. }
  139. }
  140. idLib::common->Warning( "Unknown string id %s", str );
  141. return str;
  142. }
  143. /*
  144. ============
  145. idLangDict::AddString
  146. ============
  147. */
  148. const char *idLangDict::AddString( const char *str ) {
  149. if ( ExcludeString( str ) ) {
  150. return str;
  151. }
  152. int c = args.Num();
  153. for ( int j = 0; j < c; j++ ) {
  154. if ( idStr::Cmp( args[j].value, str ) == 0 ){
  155. return args[j].key;
  156. }
  157. }
  158. int id = GetNextId();
  159. idLangKeyValue kv;
  160. // _D3XP
  161. kv.key = va( "#str_%08i", id );
  162. // kv.key = va( "#str_%05i", id );
  163. kv.value = str;
  164. c = args.Append( kv );
  165. assert( kv.key.Cmpn( STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 );
  166. hash.Add( GetHashKey( kv.key ), c );
  167. return args[c].key;
  168. }
  169. /*
  170. ============
  171. idLangDict::GetNumKeyVals
  172. ============
  173. */
  174. int idLangDict::GetNumKeyVals( void ) const {
  175. return args.Num();
  176. }
  177. /*
  178. ============
  179. idLangDict::GetKeyVal
  180. ============
  181. */
  182. const idLangKeyValue * idLangDict::GetKeyVal( int i ) const {
  183. return &args[i];
  184. }
  185. /*
  186. ============
  187. idLangDict::AddKeyVal
  188. ============
  189. */
  190. void idLangDict::AddKeyVal( const char *key, const char *val ) {
  191. idLangKeyValue kv;
  192. kv.key = key;
  193. kv.value = val;
  194. assert( kv.key.Cmpn( STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 );
  195. hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
  196. }
  197. /*
  198. ============
  199. idLangDict::ExcludeString
  200. ============
  201. */
  202. bool idLangDict::ExcludeString( const char *str ) const {
  203. if ( str == NULL ) {
  204. return true;
  205. }
  206. int c = strlen( str );
  207. if ( c <= 1 ) {
  208. return true;
  209. }
  210. if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 ) {
  211. return true;
  212. }
  213. if ( idStr::Icmpn( str, "gui::", strlen( "gui::" ) ) == 0 ) {
  214. return true;
  215. }
  216. if ( str[0] == '$' ) {
  217. return true;
  218. }
  219. int i;
  220. for ( i = 0; i < c; i++ ) {
  221. if ( isalpha( str[i] ) ) {
  222. break;
  223. }
  224. }
  225. if ( i == c ) {
  226. return true;
  227. }
  228. return false;
  229. }
  230. /*
  231. ============
  232. idLangDict::GetNextId
  233. ============
  234. */
  235. int idLangDict::GetNextId( void ) const {
  236. int c = args.Num();
  237. //Let and external user supply the base id for this dictionary
  238. int id = baseID;
  239. if ( c == 0 ) {
  240. return id;
  241. }
  242. idStr work;
  243. for ( int j = 0; j < c; j++ ) {
  244. work = args[j].key;
  245. work.StripLeading( STRTABLE_ID );
  246. int test = atoi( work );
  247. if ( test > id ) {
  248. id = test;
  249. }
  250. }
  251. return id + 1;
  252. }
  253. /*
  254. ============
  255. idLangDict::GetHashKey
  256. ============
  257. */
  258. int idLangDict::GetHashKey( const char *str ) const {
  259. int hashKey = 0;
  260. for ( str += STRTABLE_ID_LENGTH; str[0] != '\0'; str++ ) {
  261. assert( str[0] >= '0' && str[0] <= '9' );
  262. hashKey = hashKey * 10 + str[0] - '0';
  263. }
  264. return hashKey;
  265. }