OpenTTD
strings_sl.cpp
Go to the documentation of this file.
1 /* $Id: strings_sl.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
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 "saveload_internal.h"
15 
16 #include "table/strings.h"
17 
18 #include "../safeguards.h"
19 
20 static const int NUM_OLD_STRINGS = 512;
21 static const int LEN_OLD_STRINGS = 32;
22 static const int LEN_OLD_STRINGS_TTO = 24;
23 
30 {
31  switch (s) {
32  case 0x0006: return STR_SV_EMPTY;
33  case 0x7000: return STR_SV_UNNAMED;
34  case 0x70E4: return SPECSTR_COMPANY_NAME_START;
35  case 0x70E9: return SPECSTR_COMPANY_NAME_START;
36  case 0x8864: return STR_SV_TRAIN_NAME;
37  case 0x902B: return STR_SV_ROAD_VEHICLE_NAME;
38  case 0x9830: return STR_SV_SHIP_NAME;
39  case 0xA02F: return STR_SV_AIRCRAFT_NAME;
40 
41  default:
42  if (IsInsideMM(s, 0x300F, 0x3030)) {
43  return s - 0x300F + STR_SV_STNAME;
44  } else {
45  return s;
46  }
47  }
48 }
49 
51 char *_old_name_array = NULL;
52 
61 {
62  /* Is this name an (old) custom name? */
63  if (GB(id, 11, 5) != 15) return NULL;
64 
65  if (IsSavegameVersionBefore(37)) {
66  /* Allow for expansion when converted to UTF-8. */
67  char tmp[LEN_OLD_STRINGS * MAX_CHAR_LENGTH];
68  uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9);
69  const char *strfrom = &_old_name_array[offs];
70  char *strto = tmp;
71 
72  for (; *strfrom != '\0'; strfrom++) {
73  WChar c = (byte)*strfrom;
74 
75  /* Map from non-ISO8859-15 characters to UTF-8. */
76  switch (c) {
77  case 0xA4: c = 0x20AC; break; // Euro
78  case 0xA6: c = 0x0160; break; // S with caron
79  case 0xA8: c = 0x0161; break; // s with caron
80  case 0xB4: c = 0x017D; break; // Z with caron
81  case 0xB8: c = 0x017E; break; // z with caron
82  case 0xBC: c = 0x0152; break; // OE ligature
83  case 0xBD: c = 0x0153; break; // oe ligature
84  case 0xBE: c = 0x0178; break; // Y with diaresis
85  default: break;
86  }
87 
88  /* Check character will fit into our buffer. */
89  if (strto + Utf8CharLen(c) > lastof(tmp)) break;
90 
91  strto += Utf8Encode(strto, c);
92  }
93 
94  /* Terminate the new string and copy it back to the name array */
95  *strto = '\0';
96 
97  return stredup(tmp);
98  } else {
99  /* Name will already be in UTF-8. */
100  return stredup(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
101  }
102 }
103 
109 {
111  _old_name_array = NULL;
112 }
113 
118 {
120  _old_name_array = CallocT<char>(NUM_OLD_STRINGS * LEN_OLD_STRINGS); // 200 * 24 would be enough for TTO savegames
121 }
122 
126 static void Load_NAME()
127 {
128  int index;
129 
130  while ((index = SlIterateArray()) != -1) {
131  if (index >= NUM_OLD_STRINGS) SlErrorCorrupt("Invalid old name index");
132  if (SlGetFieldLength() > (uint)LEN_OLD_STRINGS) SlErrorCorrupt("Invalid old name length");
133 
134  SlArray(&_old_name_array[LEN_OLD_STRINGS * index], SlGetFieldLength(), SLE_UINT8);
135  /* Make sure the old name is null terminated */
136  _old_name_array[LEN_OLD_STRINGS * index + LEN_OLD_STRINGS - 1] = '\0';
137  }
138 }
139 
141 extern const ChunkHandler _name_chunk_handlers[] = {
142  { 'NAME', NULL, Load_NAME, NULL, NULL, CH_ARRAY | CH_LAST},
143 };