From 95386dc2b8404519c91b9e0ad728c39fb1d9e118 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Wed, 28 Apr 2021 17:32:33 +0200 Subject: [PATCH] Codechange: move misc settings to std::string --- src/osk_gui.cpp | 10 ++++---- src/saveload/saveload.cpp | 46 ++++++++++++++++--------------------- src/saveload/saveload.h | 2 +- src/screenshot.cpp | 4 ++-- src/screenshot.h | 2 +- src/table/misc_settings.ini | 17 +++++++------- src/textbuf_gui.h | 3 +-- 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 155343f041dbc..92b15eaba4748 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -24,7 +24,7 @@ #include "safeguards.h" -char _keyboard_opt[2][OSK_KEYBOARD_ENTRIES * 4 + 1]; +std::string _keyboard_opt[2]; static WChar _keyboard[2][OSK_KEYBOARD_ENTRIES]; enum KeyStateBits { @@ -356,16 +356,16 @@ void GetKeyboardLayout() char errormark[2][OSK_KEYBOARD_ENTRIES + 1]; // used for marking invalid chars bool has_error = false; // true when an invalid char is detected - if (StrEmpty(_keyboard_opt[0])) { + if (_keyboard_opt[0].empty()) { GetString(keyboard[0], STR_OSK_KEYBOARD_LAYOUT, lastof(keyboard[0])); } else { - strecpy(keyboard[0], _keyboard_opt[0], lastof(keyboard[0])); + strecpy(keyboard[0], _keyboard_opt[0].c_str(), lastof(keyboard[0])); } - if (StrEmpty(_keyboard_opt[1])) { + if (_keyboard_opt[1].empty()) { GetString(keyboard[1], STR_OSK_KEYBOARD_LAYOUT_CAPS, lastof(keyboard[1])); } else { - strecpy(keyboard[1], _keyboard_opt[1], lastof(keyboard[1])); + strecpy(keyboard[1], _keyboard_opt[1].c_str(), lastof(keyboard[1])); } for (uint j = 0; j < 2; j++) { diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index f5e823a0dfed4..504df30ab9a52 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -61,11 +61,11 @@ extern const SaveLoadVersion SAVEGAME_VERSION = (SaveLoadVersion)(SL_MAX_VERSION SavegameType _savegame_type; ///< type of savegame we are loading FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. -uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) -SaveLoadVersion _sl_version; ///< the major savegame version identifier -byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! -char _savegame_format[8]; ///< how to compress savegames -bool _do_autosave; ///< are we doing an autosave at the moment? +uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) +SaveLoadVersion _sl_version; ///< the major savegame version identifier +byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! +std::string _savegame_format; ///< how to compress savegames +bool _do_autosave; ///< are we doing an autosave at the moment? /** What are we currently doing? */ enum SaveLoadAction { @@ -2351,36 +2351,33 @@ static const SaveLoadFormat _saveload_formats[] = { /** * Return the savegameformat of the game. Whether it was created with ZLIB compression * uncompressed, or another type - * @param s Name of the savegame format. If nullptr it picks the first available one + * @param full_name Name of the savegame format. If empty it picks the first available one * @param compression_level Output for telling what compression level we want. * @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame */ -static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level) +static const SaveLoadFormat *GetSavegameFormat(const std::string &full_name, byte *compression_level) { const SaveLoadFormat *def = lastof(_saveload_formats); /* find default savegame format, the highest one with which files can be written */ while (!def->init_write) def--; - if (!StrEmpty(s)) { + if (!full_name.empty()) { /* Get the ":..." of the compression level out of the way */ - char *complevel = strrchr(s, ':'); - if (complevel != nullptr) *complevel = '\0'; + size_t separator = full_name.find(':'); + bool has_comp_level = separator != std::string::npos; + const std::string name(full_name, 0, has_comp_level ? separator : full_name.size()); for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) { - if (slf->init_write != nullptr && strcmp(s, slf->name) == 0) { + if (slf->init_write != nullptr && name.compare(slf->name) == 0) { *compression_level = slf->default_compression; - if (complevel != nullptr) { - /* There is a compression level in the string. - * First restore the : we removed to do proper name matching, - * then move the the begin of the actual version. */ - *complevel = ':'; - complevel++; - - /* Get the version and determine whether all went fine. */ - char *end; - long level = strtol(complevel, &end, 10); - if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) { + if (has_comp_level) { + const std::string complevel(full_name, separator + 1); + + /* Get the level and determine whether all went fine. */ + size_t processed; + long level = std::stol(complevel, &processed, 10); + if (processed == 0 || level != Clamp(level, slf->min_compression, slf->max_compression)) { SetDParamStr(0, complevel); ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL); } else { @@ -2391,12 +2388,9 @@ static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level) } } - SetDParamStr(0, s); + SetDParamStr(0, name); SetDParamStr(1, def->name); ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL); - - /* Restore the string by adding the : back */ - if (complevel != nullptr) *complevel = ':'; } *compression_level = def->default_compression; return def; diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 28513c70c15a3..50185902d10d6 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -934,7 +934,7 @@ static inline void SlSkipBytes(size_t length) for (; length != 0; length--) SlReadByte(); } -extern char _savegame_format[8]; +extern std::string _savegame_format; extern bool _do_autosave; #endif /* SAVELOAD_H */ diff --git a/src/screenshot.cpp b/src/screenshot.cpp index 0e3bf3d8e2f5d..6f42038daabef 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -34,7 +34,7 @@ static const char * const SCREENSHOT_NAME = "screenshot"; ///< Default filename of a saved screenshot. static const char * const HEIGHTMAP_NAME = "heightmap"; ///< Default filename of a saved heightmap. -char _screenshot_format_name[8]; ///< Extension of the current screenshot format (corresponds with #_cur_screenshot_format). +std::string _screenshot_format_name; ///< Extension of the current screenshot format (corresponds with #_cur_screenshot_format). uint _num_screenshot_formats; ///< Number of available screenshot formats. uint _cur_screenshot_format; ///< Index of the currently selected screenshot format in #_screenshot_formats. static char _screenshot_name[128]; ///< Filename of the screenshot file. @@ -584,7 +584,7 @@ void InitializeScreenshotFormats() { uint j = 0; for (uint i = 0; i < lengthof(_screenshot_formats); i++) { - if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) { + if (_screenshot_format_name.compare(_screenshot_formats[i].extension) != 0) { j = i; break; } diff --git a/src/screenshot.h b/src/screenshot.h index e65813573f1ea..6324cab11fcb3 100644 --- a/src/screenshot.h +++ b/src/screenshot.h @@ -31,7 +31,7 @@ void MakeScreenshotWithConfirm(ScreenshotType t); bool MakeScreenshot(ScreenshotType t, std::string name, uint32 width = 0, uint32 height = 0); bool MakeMinimapWorldScreenshot(); -extern char _screenshot_format_name[8]; +extern std::string _screenshot_format_name; extern uint _num_screenshot_formats; extern uint _cur_screenshot_format; extern char _full_screenshot_name[MAX_PATH]; diff --git a/src/table/misc_settings.ini b/src/table/misc_settings.ini index 39df34b78afb7..37994644a6211 100644 --- a/src/table/misc_settings.ini +++ b/src/table/misc_settings.ini @@ -23,7 +23,6 @@ static const SettingDescGlobVarList _misc_settings[] = { SDTG_LIST = SDTG_LIST($name, $type, $length, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_MMANY = SDTG_MMANY($name, $type, $flags, $guiflags, $var, $def, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_OMANY = SDTG_OMANY($name, $type, $flags, $guiflags, $var, $def, $max, $full, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), -SDTG_STR = SDTG_STR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_SSTR = SDTG_SSTR($name, $type, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_BOOL = SDTG_BOOL($name, $flags, $guiflags, $var, $def, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), SDTG_VAR = SDTG_VAR($name, $type, $flags, $guiflags, $var, $def, $min, $max, $interval, $str, $strhelp, $strval, $proc, $from, $to, $cat, $extra, $startup), @@ -156,16 +155,16 @@ var = _cur_resolution def = ""0,0"" cat = SC_BASIC -[SDTG_STR] +[SDTG_SSTR] name = ""screenshot_format"" -type = SLE_STRB +type = SLE_STR var = _screenshot_format_name def = nullptr cat = SC_EXPERT -[SDTG_STR] +[SDTG_SSTR] name = ""savegame_format"" -type = SLE_STRB +type = SLE_STR var = _savegame_format def = nullptr cat = SC_EXPERT @@ -308,16 +307,16 @@ min = 0 max = 0xFF cat = SC_BASIC -[SDTG_STR] +[SDTG_SSTR] name = ""keyboard"" -type = SLE_STRB +type = SLE_STR var = _keyboard_opt[0] def = nullptr cat = SC_EXPERT -[SDTG_STR] +[SDTG_SSTR] name = ""keyboard_caps"" -type = SLE_STRB +type = SLE_STR var = _keyboard_opt[1] def = nullptr cat = SC_EXPERT diff --git a/src/textbuf_gui.h b/src/textbuf_gui.h index c96f4e2e06c98..200d62582b0ad 100644 --- a/src/textbuf_gui.h +++ b/src/textbuf_gui.h @@ -37,8 +37,7 @@ static const uint OSK_KEYBOARD_ENTRIES = 50; /** * The number of characters has to be OSK_KEYBOARD_ENTRIES. However, these * have to be UTF-8 encoded, which means up to 4 bytes per character. - * Furthermore the string needs to be '\0'-terminated. */ -extern char _keyboard_opt[2][OSK_KEYBOARD_ENTRIES * 4 + 1]; +extern std::string _keyboard_opt[2]; #endif /* TEXTBUF_GUI_H */