strings_sl.cpp

Go to the documentation of this file.
00001 /* $Id: strings_sl.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "../stdafx.h"
00006 #include "../core/alloc_func.hpp"
00007 #include "../string_func.h"
00008 #include "saveload_internal.h"
00009 
00010 #include "table/strings.h"
00011 
00017 StringID RemapOldStringID(StringID s)
00018 {
00019   switch (s) {
00020     case 0x0006: return STR_SV_EMPTY;
00021     case 0x7000: return STR_SV_UNNAMED;
00022     case 0x70E4: return SPECSTR_PLAYERNAME_ENGLISH;
00023     case 0x70E9: return SPECSTR_PLAYERNAME_ENGLISH;
00024     case 0x8864: return STR_SV_TRAIN_NAME;
00025     case 0x902B: return STR_SV_ROADVEH_NAME;
00026     case 0x9830: return STR_SV_SHIP_NAME;
00027     case 0xA02F: return STR_SV_AIRCRAFT_NAME;
00028 
00029     default:
00030       if (IsInsideMM(s, 0x300F, 0x3030)) {
00031         return s - 0x300F + STR_SV_STNAME;
00032       } else {
00033         return s;
00034       }
00035   }
00036 }
00037 
00039 char *_old_name_array = NULL;
00040 
00048 char *CopyFromOldName(StringID id)
00049 {
00050   /* Is this name an (old) custom name? */
00051   if (GB(id, 11, 5) != 15) return NULL;
00052 
00053   if (CheckSavegameVersion(37)) {
00054     /* Old names were 24/32 characters long, so 128 characters should be
00055      * plenty to allow for expansion when converted to UTF-8. */
00056     char tmp[128];
00057     uint offs = _savegame_type == SGT_TTO ? 24 * GB(id, 0, 8) : 32 * GB(id, 0, 9);
00058     const char *strfrom = &_old_name_array[offs];
00059     char *strto = tmp;
00060 
00061     for (; *strfrom != '\0'; strfrom++) {
00062       WChar c = (byte)*strfrom;
00063 
00064       /* Map from non-ISO8859-15 characters to UTF-8. */
00065       switch (c) {
00066         case 0xA4: c = 0x20AC; break; // Euro
00067         case 0xA6: c = 0x0160; break; // S with caron
00068         case 0xA8: c = 0x0161; break; // s with caron
00069         case 0xB4: c = 0x017D; break; // Z with caron
00070         case 0xB8: c = 0x017E; break; // z with caron
00071         case 0xBC: c = 0x0152; break; // OE ligature
00072         case 0xBD: c = 0x0153; break; // oe ligature
00073         case 0xBE: c = 0x0178; break; // Y with diaresis
00074         default: break;
00075       }
00076 
00077       /* Check character will fit into our buffer. */
00078       if (strto + Utf8CharLen(c) > lastof(tmp)) break;
00079 
00080       strto += Utf8Encode(strto, c);
00081     }
00082 
00083     /* Terminate the new string and copy it back to the name array */
00084     *strto = '\0';
00085 
00086     return strdup(tmp);
00087   } else {
00088     /* Name will already be in UTF-8. */
00089     return strdup(&_old_name_array[32 * GB(id, 0, 9)]);
00090   }
00091 }
00092 
00097 void ResetOldNames()
00098 {
00099   free(_old_name_array);
00100   _old_name_array = NULL;
00101 }
00102 
00106 void InitializeOldNames()
00107 {
00108   free(_old_name_array);
00109   _old_name_array = CallocT<char>(512 * 32); // 200 * 24 would be enough for TTO savegames
00110 }
00111 
00112 static void Load_NAME()
00113 {
00114   int index;
00115 
00116   while ((index = SlIterateArray()) != -1) {
00117     SlArray(&_old_name_array[32 * index], SlGetFieldLength(), SLE_UINT8);
00118   }
00119 }
00120 
00121 extern const ChunkHandler _name_chunk_handlers[] = {
00122   { 'NAME', NULL, Load_NAME, CH_ARRAY | CH_LAST},
00123 };

Generated on Sun Mar 15 22:49:49 2009 for openttd by  doxygen 1.5.6