Skip to content

Commit

Permalink
Codechange: move misc settings to std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
rubidium42 committed May 13, 2021
1 parent 77330d0 commit 95386dc
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
10 changes: 5 additions & 5 deletions src/osk_gui.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++) {
Expand Down
46 changes: 20 additions & 26 deletions src/saveload/saveload.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/saveload/saveload.h
Expand Up @@ -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 */
4 changes: 2 additions & 2 deletions src/screenshot.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/screenshot.h
Expand Up @@ -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];
Expand Down
17 changes: 8 additions & 9 deletions src/table/misc_settings.ini
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/textbuf_gui.h
Expand Up @@ -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 */

0 comments on commit 95386dc

Please sign in to comment.