home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / LangDict.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  6KB  |  291 lines

  1.  
  2. #include "precompiled.h"
  3. #pragma hdrstop
  4.  
  5.  
  6. /*
  7. ============
  8. idLangDict::idLangDict
  9. ============
  10. */
  11. idLangDict::idLangDict( void ) {
  12.     args.SetGranularity( 256 );
  13.     hash.SetGranularity( 256 );
  14.     hash.Clear( 4096, 8192 );
  15.     baseID = 0;
  16. }
  17.  
  18. /*
  19. ============
  20. idLangDict::~idLangDict
  21. ============
  22. */
  23. idLangDict::~idLangDict( void ) {
  24.     Clear();
  25. }
  26.  
  27. /*
  28. ============
  29. idLangDict::Clear
  30. ============
  31. */
  32. void idLangDict::Clear( void ) {
  33.     args.Clear();
  34.     hash.Clear();
  35. }
  36.  
  37. /*
  38. ============
  39. idLangDict::Load
  40. ============
  41. */
  42. bool idLangDict::Load( const char *fileName, bool clear ) {
  43.     if ( clear ) {
  44.         Clear();
  45.     }
  46.  
  47.     const char *buffer = NULL;
  48.     idLexer src( LEXFL_NOFATALERRORS | LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  49.  
  50.     int len = idLib::fileSystem->ReadFile( fileName, (void**)&buffer );
  51.     if ( len <= 0 ) {
  52.         // let whoever called us deal with the failure (so sys_lang can be reset)
  53.         return false;
  54.     }
  55.     src.LoadMemory( buffer, strlen( buffer ), fileName );
  56.     if ( !src.IsLoaded() ) {
  57.         return false;
  58.     }
  59.  
  60.     idToken tok, tok2;
  61.     src.ExpectTokenString( "{" );
  62.     while ( src.ReadToken( &tok ) ) {
  63.         if ( tok == "}" ) {
  64.             break;
  65.         }
  66.  
  67.         if ( src.ReadToken( &tok2 ) ) {
  68.             if ( tok2 == "}" ) {
  69.                 break;
  70.             }
  71.             idLangKeyValue kv;
  72.             kv.key = tok;
  73.             kv.value = tok2;
  74. // RAVEN BEGIN
  75.             if( kv.key.CmpPrefix( STRTABLE_ID ) ) {
  76.                 common->Warning( "Invalid token id \'%s\' in \'%s\' line %d", kv.key.c_str(), fileName, src.GetLineNum() );
  77.             } else {
  78.                 hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
  79.             }
  80. // RAVEN END
  81.         }
  82.     }
  83.     idLib::common->Printf( "%i strings read from %s\n", args.Num(), fileName );
  84.     idLib::fileSystem->FreeFile( (void*)buffer );
  85.     
  86.     return true;
  87. }
  88.  
  89. /*
  90. ============
  91. idLangDict::Save
  92. ============
  93. */
  94. void idLangDict::Save( const char *fileName ) {
  95.     idFile *outFile = idLib::fileSystem->OpenFileWrite( fileName );
  96. // RAVEN BEGIN
  97.     if( !outFile ) {
  98.         common->Printf( "Could not open file \'%s\'for writing\n", fileName );
  99.         return;
  100.     }
  101. // RAVEN END
  102.     outFile->WriteFloatString( "// string table" NEWLINE "// english" NEWLINE "//" NEWLINE NEWLINE "{" NEWLINE );
  103.     for ( int j = 0; j < args.Num(); j++ ) {
  104.         outFile->WriteFloatString( "\t\"%s\"\t\"", args[j].key.c_str() );
  105.         int l = args[j].value.Length();
  106.         char slash = '\\';
  107.         char tab = 't';
  108.         char nl = 'n';
  109.         for ( int k = 0; k < l; k++ ) {
  110.             char ch = args[j].value[k];
  111.             if ( ch == '\t' ) {
  112.                 outFile->Write( &slash, 1 );
  113.                 outFile->Write( &tab, 1 );
  114.             } else if ( ch == '\n' || ch == '\r' ) {
  115.                 outFile->Write( &slash, 1 );
  116.                 outFile->Write( &nl, 1 );
  117.             } else {
  118.                 outFile->Write( &ch, 1 );
  119.             }
  120.         }
  121.         outFile->WriteFloatString( "\"" NEWLINE );
  122.     }
  123.     outFile->WriteFloatString( NEWLINE "}" NEWLINE );
  124.     idLib::fileSystem->CloseFile( outFile );
  125. }
  126.  
  127. /*
  128. ============
  129. idLangDict::GetString
  130. ============
  131. */
  132. const char *idLangDict::GetString( const char *str ) const {
  133.  
  134.     if ( str == NULL || str[0] == '\0' ) {
  135.         return "";
  136.     }
  137.  
  138.     if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) != 0 ) {
  139.         return str;
  140.     }
  141.  
  142.     int hashKey = GetHashKey( str );
  143.     for ( int i = hash.First( hashKey ); i != -1; i = hash.Next( i ) ) {
  144.         if ( args[i].key.Cmp( str ) == 0 ) {
  145.             return args[i].value;
  146.         }
  147.     }
  148.  
  149.     idLib::common->Warning( "Unknown string id %s", str );
  150.     return str;
  151. }
  152.  
  153. /*
  154. ============
  155. idLangDict::AddString
  156. ============
  157. */
  158. const char *idLangDict::AddString( const char *str ) {
  159.     
  160.     if ( ExcludeString( str ) ) {
  161.         return str;
  162.     }
  163.  
  164.     int c = args.Num();
  165.     for ( int j = 0; j < c; j++ ) {
  166.         if ( idStr::Cmp( args[j].value, str ) == 0 ){
  167.             return args[j].key;
  168.         }
  169.     }
  170.  
  171.     int id = GetNextId();
  172.     idLangKeyValue kv;
  173.     kv.key = va( "#str_%06i", id );
  174.     kv.value = str;
  175.     c = args.Append( kv );
  176.     assert( kv.key.CmpPrefix( STRTABLE_ID ) == 0 );
  177.     hash.Add( GetHashKey( kv.key ), c );
  178.     return args[c].key;
  179. }
  180.  
  181. /*
  182. ============
  183. idLangDict::GetNumKeyVals
  184. ============
  185. */
  186. int idLangDict::GetNumKeyVals( void ) const {
  187.     return args.Num();
  188. }
  189.  
  190. /*
  191. ============
  192. idLangDict::GetKeyVal
  193. ============
  194. */
  195. const idLangKeyValue * idLangDict::GetKeyVal( int i ) const {
  196.     return &args[i];
  197. }
  198.  
  199. /*
  200. ============
  201. idLangDict::AddKeyVal
  202. ============
  203. */
  204. void idLangDict::AddKeyVal( const char *key, const char *val ) {
  205.     idLangKeyValue kv;
  206.     kv.key = key;
  207.     kv.value = val;
  208.     assert( kv.key.CmpPrefix( STRTABLE_ID ) == 0 );
  209.     hash.Add( GetHashKey( kv.key ), args.Append( kv ) );
  210. }
  211.  
  212. /*
  213. ============
  214. idLangDict::ExcludeString
  215. ============
  216. */
  217. bool idLangDict::ExcludeString( const char *str ) const {
  218.     if ( str == NULL ) {
  219.         return true;
  220.     }
  221.  
  222.     int c = strlen( str );
  223.     if ( c <= 1 ) {
  224.         return true;
  225.     }
  226.  
  227.     if ( idStr::Cmpn( str, STRTABLE_ID, STRTABLE_ID_LENGTH ) == 0 ) {
  228.         return true;
  229.     }
  230.  
  231.     if ( idStr::Icmpn( str, "gui::", strlen( "gui::" ) ) == 0 ) {
  232.         return true;
  233.     }
  234.  
  235.     if ( str[0] == '$' ) {
  236.         return true;
  237.     }
  238.  
  239.     int i;
  240.     for ( i = 0; i < c; i++ ) {
  241.         if ( isalpha( str[i] ) ) {
  242.             break;
  243.         }
  244.     }
  245.     if ( i == c ) {
  246.         return true;
  247.     }
  248.     
  249.     return false;
  250. }
  251.  
  252. /*
  253. ============
  254. idLangDict::GetNextId
  255. ============
  256. */
  257. int idLangDict::GetNextId( void ) const {
  258.     int c = args.Num();
  259.     //Let and external user supply the base id for this dictionary
  260.     int id = baseID;
  261.  
  262.     if ( c == 0 ) {
  263.         return id;
  264.     }
  265.  
  266.     idStr work;
  267.     for ( int j = 0; j < c; j++ ) {
  268.         work = args[j].key;
  269.         work.StripLeading( STRTABLE_ID );
  270.         int test = atoi( work );
  271.         if ( test > id ) {
  272.             id = test;
  273.         }
  274.     }
  275.     return id + 1;
  276. }
  277.  
  278. /*
  279. ============
  280. idLangDict::GetHashKey
  281. ============
  282. */
  283. int idLangDict::GetHashKey( const char *str ) const {
  284.     int hashKey = 0;
  285.     for ( str += STRTABLE_ID_LENGTH; str[0] != '\0'; str++ ) {
  286.         assert( str[0] >= '0' && str[0] <= '9' );
  287.         hashKey = hashKey * 10 + str[0] - '0';
  288.     }
  289.     return hashKey;
  290. }
  291.