OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

strings_sl.cpp
Go to the documentation of this file.
1 /* $Id: strings_sl.cpp 27756 2017-02-26 19:40:53Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../string_func.h"
14 #include "../strings_func.h"
15 #include "saveload_internal.h"
16
17 #include "table/strings.h"
18
19 #include "../safeguards.h"
20
21 static const int NUM_OLD_STRINGS = 512;
22 static const int LEN_OLD_STRINGS = 32;
23 static const int LEN_OLD_STRINGS_TTO = 24;
24
30 StringID RemapOldStringID(StringID s)
31 {
32 switch (s) {
33 case 0x0006: return STR_SV_EMPTY;
34 case 0x7000: return STR_SV_UNNAMED;
35 case 0x70E4: return SPECSTR_COMPANY_NAME_START;
36 case 0x70E9: return SPECSTR_COMPANY_NAME_START;
37 case 0x8864: return STR_SV_TRAIN_NAME;
38 case 0x902B: return STR_SV_ROAD_VEHICLE_NAME;
39 case 0x9830: return STR_SV_SHIP_NAME;
40 case 0xA02F: return STR_SV_AIRCRAFT_NAME;
41
42 default:
43 if (IsInsideMM(s, 0x300F, 0x3030)) {
44 return s - 0x300F + STR_SV_STNAME;
45 } else {
46 return s;
47 }
48 }
49 }
50
52 char *_old_name_array = NULL;
53
61 char *CopyFromOldName(StringID id)
62 {
63 /* Is this name an (old) custom name? */
64 if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return NULL;
65
66 if (IsSavegameVersionBefore(37)) {
67 /* Allow for expansion when converted to UTF-8. */
68 char tmp[LEN_OLD_STRINGS * MAX_CHAR_LENGTH];
69 uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9);
70 const char *strfrom = &_old_name_array[offs];
71 char *strto = tmp;
72
73 for (; *strfrom != '\0'; strfrom++) {
74 WChar c = (byte)*strfrom;
75
76 /* Map from non-ISO8859-15 characters to UTF-8. */
77 switch (c) {
78 case 0xA4: c = 0x20AC; break; // Euro
79 case 0xA6: c = 0x0160; break; // S with caron
80 case 0xA8: c = 0x0161; break; // s with caron
81 case 0xB4: c = 0x017D; break; // Z with caron
82 case 0xB8: c = 0x017E; break; // z with caron
83 case 0xBC: c = 0x0152; break; // OE ligature
84 case 0xBD: c = 0x0153; break; // oe ligature
85 case 0xBE: c = 0x0178; break; // Y with diaresis
86 default: break;
87 }
88
89 /* Check character will fit into our buffer. */
90 if (strto + Utf8CharLen(c) > lastof(tmp)) break;
91
92 strto += Utf8Encode(strto, c);
93 }
94
95 /* Terminate the new string and copy it back to the name array */
96 *strto = '\0';
97
98 return stredup(tmp);
99 } else {
100 /* Name will already be in UTF-8. */
101 return stredup(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
102 }
103 }
104
109 void ResetOldNames()
110 {
111 free(_old_name_array);
112 _old_name_array = NULL;
113 }
114
118 void InitializeOldNames()
119 {
120 free(_old_name_array);
121 _old_name_array = CallocT<char>(NUM_OLD_STRINGS * LEN_OLD_STRINGS); // 200 * 24 would be enough for TTO savegames
122 }
123
127 static void Load_NAME()
128 {
129 int index;
130
131 while ((index = SlIterateArray()) != -1) {
132 if (index >= NUM_OLD_STRINGS) SlErrorCorrupt("Invalid old name index");
133 if (SlGetFieldLength() > (uint)LEN_OLD_STRINGS) SlErrorCorrupt("Invalid old name length");
134
135 SlArray(&_old_name_array[LEN_OLD_STRINGS * index], SlGetFieldLength(), SLE_UINT8);
136 /* Make sure the old name is null terminated */
137 _old_name_array[LEN_OLD_STRINGS * index + LEN_OLD_STRINGS - 1] = '\0';
138 }
139 }
140
142 extern const ChunkHandler _name_chunk_handlers[] = {
143 { 'NAME', NULL, Load_NAME, NULL, NULL, CH_ARRAY | CH_LAST},
144 };