OpenTTD
strings_sl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../string_func.h"
12 #include "../strings_func.h"
13 #include "saveload_internal.h"
14 
15 #include "table/strings.h"
16 
17 #include "../safeguards.h"
18 
19 static const int NUM_OLD_STRINGS = 512;
20 static const int LEN_OLD_STRINGS = 32;
21 static const int LEN_OLD_STRINGS_TTO = 24;
22 
29 {
30  switch (s) {
31  case 0x0006: return STR_SV_EMPTY;
32  case 0x7000: return STR_SV_UNNAMED;
33  case 0x70E4: return SPECSTR_COMPANY_NAME_START;
34  case 0x70E9: return SPECSTR_COMPANY_NAME_START;
35  case 0x8864: return STR_SV_TRAIN_NAME;
36  case 0x902B: return STR_SV_ROAD_VEHICLE_NAME;
37  case 0x9830: return STR_SV_SHIP_NAME;
38  case 0xA02F: return STR_SV_AIRCRAFT_NAME;
39 
40  default:
41  if (IsInsideMM(s, 0x300F, 0x3030)) {
42  return s - 0x300F + STR_SV_STNAME;
43  } else {
44  return s;
45  }
46  }
47 }
48 
50 char *_old_name_array = nullptr;
51 
60 {
61  /* Is this name an (old) custom name? */
62  if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return nullptr;
63 
65  /* Allow for expansion when converted to UTF-8. */
66  char tmp[LEN_OLD_STRINGS * MAX_CHAR_LENGTH];
67  uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9);
68  const char *strfrom = &_old_name_array[offs];
69  char *strto = tmp;
70 
71  for (; *strfrom != '\0'; strfrom++) {
72  WChar c = (byte)*strfrom;
73 
74  /* Map from non-ISO8859-15 characters to UTF-8. */
75  switch (c) {
76  case 0xA4: c = 0x20AC; break; // Euro
77  case 0xA6: c = 0x0160; break; // S with caron
78  case 0xA8: c = 0x0161; break; // s with caron
79  case 0xB4: c = 0x017D; break; // Z with caron
80  case 0xB8: c = 0x017E; break; // z with caron
81  case 0xBC: c = 0x0152; break; // OE ligature
82  case 0xBD: c = 0x0153; break; // oe ligature
83  case 0xBE: c = 0x0178; break; // Y with diaeresis
84  default: break;
85  }
86 
87  /* Check character will fit into our buffer. */
88  if (strto + Utf8CharLen(c) > lastof(tmp)) break;
89 
90  strto += Utf8Encode(strto, c);
91  }
92 
93  /* Terminate the new string and copy it back to the name array */
94  *strto = '\0';
95 
96  return stredup(tmp);
97  } else {
98  /* Name will already be in UTF-8. */
99  return stredup(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
100  }
101 }
102 
108 {
110  _old_name_array = nullptr;
111 }
112 
117 {
119  _old_name_array = CallocT<char>(NUM_OLD_STRINGS * LEN_OLD_STRINGS); // 200 * 24 would be enough for TTO savegames
120 }
121 
125 static void Load_NAME()
126 {
127  int index;
128 
129  while ((index = SlIterateArray()) != -1) {
130  if (index >= NUM_OLD_STRINGS) SlErrorCorrupt("Invalid old name index");
131  if (SlGetFieldLength() > (uint)LEN_OLD_STRINGS) SlErrorCorrupt("Invalid old name length");
132 
133  SlArray(&_old_name_array[LEN_OLD_STRINGS * index], SlGetFieldLength(), SLE_UINT8);
134  /* Make sure the old name is null terminated */
135  _old_name_array[LEN_OLD_STRINGS * index + LEN_OLD_STRINGS - 1] = '\0';
136  }
137 }
138 
140 extern const ChunkHandler _name_chunk_handlers[] = {
141  { 'NAME', nullptr, Load_NAME, nullptr, nullptr, CH_ARRAY | CH_LAST},
142 };
const ChunkHandler _name_chunk_handlers[]
Chunk handlers related to strings.
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:354
37 7182
Definition: saveload.h:86
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
static StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:23
void InitializeOldNames()
Initialize the old names table memory.
Definition: strings_sl.cpp:116
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:743
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:995
TTO savegame.
Definition: saveload.h:335
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:264
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition: strings_type.h:18
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:97
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:57
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
Handlers and description of chunk.
Definition: saveload.h:356
char * _old_name_array
Location to load the old names to.
Definition: strings_sl.cpp:50
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static void Load_NAME()
Load the NAME chunk.
Definition: strings_sl.cpp:125
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:28
static const int NUM_OLD_STRINGS
The number of custom strings stored in old savegames.
Definition: strings_sl.cpp:19
size_t Utf8Encode(char *buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:486
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
static const int LEN_OLD_STRINGS
The number of characters per string.
Definition: strings_sl.cpp:20
void ResetOldNames()
Free the memory of the old names array.
Definition: strings_sl.cpp:107
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:59
static const int LEN_OLD_STRINGS_TTO
The number of characters per string in TTO savegames.
Definition: strings_sl.cpp:21
Declaration of functions used in more save/load files.
Last chunk in this array.
Definition: saveload.h:391