From ab8382c0db353d9d1ba6b7a9690d56aa2415258c Mon Sep 17 00:00:00 2001 From: peter1138 Date: Sat, 12 Jan 2008 19:58:06 +0000 Subject: (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is no limit to the amount of names. -Fix: NewGRF engines could not be renamed. --- src/misc.cpp | 74 ++++++++++++++++-------------------------------------------- 1 file changed, 19 insertions(+), 55 deletions(-) (limited to 'src/misc.cpp') diff --git a/src/misc.cpp b/src/misc.cpp index c3d078338..1c65671c2 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -116,18 +116,6 @@ bool IsCustomName(StringID id) return GB(id, 11, 5) == 15; } -void DeleteName(StringID id) -{ - if (IsCustomName(id)) { - memset(_name_array[id & 0x1FF], 0, sizeof(_name_array[id & 0x1FF])); - } -} - -char *GetName(char *buff, StringID id, const char* last) -{ - return strecpy(buff, _name_array[id & ~0x600], last); -} - static void InitializeCheats() { @@ -140,40 +128,22 @@ static void InitializeNameMgr() memset(_name_array, 0, sizeof(_name_array)); } -StringID RealAllocateName(const char *name, byte skip, bool check_double) -{ - char (*free_item)[lengthof(*_name_array)] = NULL; - char (*i)[lengthof(*_name_array)]; - - for (i = _name_array; i != endof(_name_array); ++i) { - if ((*i)[0] == '\0') { - if (free_item == NULL) free_item = i; - } else if (check_double && strncmp(*i, name, lengthof(*i) - 1) == 0) { - _error_message = STR_0132_CHOSEN_NAME_IN_USE_ALREADY; - return 0; - } - } - - if (free_item != NULL) { - ttd_strlcpy(*free_item, name, lengthof(*free_item)); - return (free_item - _name_array) | 0x7800 | (skip << 8); - } else { - _error_message = STR_0131_TOO_MANY_NAMES_DEFINED; - return 0; - } -} - -void ConvertNameArray() +/* Copy and convert old custom names to UTF-8 */ +char *CopyFromOldName(StringID id) { - uint i; + if (!IsCustomName(id)) return NULL; - for (i = 0; i < lengthof(_name_array); i++) { - const char *strfrom = _name_array[i]; - char tmp[sizeof(*_name_array)]; + if (CheckSavegameVersion(37)) { + /* Old names were 32 characters long, so 128 characters should be + * plenty to allow for expansion when converted to UTF-8. */ + char tmp[128]; + const char *strfrom = _name_array[GB(id, 0, 9)]; char *strto = tmp; for (; *strfrom != '\0'; strfrom++) { WChar c = (byte)*strfrom; + + /* Map from non-ISO8859-15 characters to UTF-8. */ switch (c) { case 0xA4: c = 0x20AC; break; // Euro case 0xA6: c = 0x0160; break; // S with caron @@ -185,13 +155,20 @@ void ConvertNameArray() case 0xBE: c = 0x0178; break; // Y with diaresis default: break; } + + /* Check character will fit into our buffer. */ if (strto + Utf8CharLen(c) > lastof(tmp)) break; + strto += Utf8Encode(strto, c); } /* Terminate the new string and copy it back to the name array */ *strto = '\0'; - memcpy(_name_array[i], tmp, sizeof(*_name_array)); + + return strdup(tmp); + } else { + /* Name will already be in UTF-8. */ + return strdup(_name_array[GB(id, 0, 9)]); } } @@ -206,19 +183,6 @@ void InitializeLandscapeVariables(bool only_constants) } } - -static void Save_NAME() -{ - int i; - - for (i = 0; i != lengthof(_name_array); ++i) { - if (_name_array[i][0] != '\0') { - SlSetArrayIndex(i); - SlArray(_name_array[i], (uint)strlen(_name_array[i]), SLE_UINT8); - } - } -} - static void Load_NAME() { int index; @@ -584,7 +548,7 @@ extern const ChunkHandler _misc_chunk_handlers[] = { { 'MAPE', Save_MAP6, Load_MAP6, CH_RIFF }, { 'MAP7', Save_MAP7, Load_MAP7, CH_RIFF }, - { 'NAME', Save_NAME, Load_NAME, CH_ARRAY}, + { 'NAME', NULL, Load_NAME, CH_ARRAY}, { 'DATE', SaveLoad_DATE, SaveLoad_DATE, CH_RIFF}, { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, CH_RIFF}, { 'CHTS', Save_CHTS, Load_CHTS, CH_RIFF | CH_LAST} -- cgit v1.2.3-54-g00ecf