mirror of https://github.com/OpenTTD/OpenTTD
(svn r26493) -Codechange: use strecat to concatenate script settings instead of manually accounting for the amount of characters that has been written
parent
ae46990636
commit
b4914b91d9
|
@ -51,7 +51,7 @@ static void SaveReal_AIPL(int *index_ptr)
|
||||||
|
|
||||||
_ai_saveload_is_random = config->IsRandom();
|
_ai_saveload_is_random = config->IsRandom();
|
||||||
_ai_saveload_settings[0] = '\0';
|
_ai_saveload_settings[0] = '\0';
|
||||||
config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
|
config->SettingsToString(_ai_saveload_settings, lastof(_ai_saveload_settings));
|
||||||
|
|
||||||
SlObject(NULL, _ai_company);
|
SlObject(NULL, _ai_company);
|
||||||
/* If the AI was active, store his data too */
|
/* If the AI was active, store his data too */
|
||||||
|
|
|
@ -50,7 +50,7 @@ static void SaveReal_GSDT(int *index_ptr)
|
||||||
|
|
||||||
_game_saveload_is_random = config->IsRandom();
|
_game_saveload_is_random = config->IsRandom();
|
||||||
_game_saveload_settings[0] = '\0';
|
_game_saveload_settings[0] = '\0';
|
||||||
config->SettingsToString(_game_saveload_settings, lengthof(_game_saveload_settings));
|
config->SettingsToString(_game_saveload_settings, lastof(_game_saveload_settings));
|
||||||
|
|
||||||
SlObject(NULL, _game_script);
|
SlObject(NULL, _game_script);
|
||||||
Game::Save();
|
Game::Save();
|
||||||
|
|
|
@ -184,27 +184,27 @@ void ScriptConfig::StringToSettings(const char *value)
|
||||||
free(value_copy);
|
free(value_copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptConfig::SettingsToString(char *string, size_t size) const
|
void ScriptConfig::SettingsToString(char *string, const char *last) const
|
||||||
{
|
{
|
||||||
string[0] = '\0';
|
char *s = string;
|
||||||
|
*s = '\0';
|
||||||
for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
||||||
char no[10];
|
char no[10];
|
||||||
seprintf(no, lastof(no), "%d", (*it).second);
|
seprintf(no, lastof(no), "%d", (*it).second);
|
||||||
|
|
||||||
/* Check if the string would fit in the destination */
|
/* Check if the string would fit in the destination */
|
||||||
size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
|
size_t needed_size = strlen((*it).first) + 1 + strlen(no);
|
||||||
/* If it doesn't fit, skip the next settings */
|
/* If it doesn't fit, skip the next settings */
|
||||||
if (size <= needed_size) break;
|
if (string + needed_size > last) break;
|
||||||
size -= needed_size;
|
|
||||||
|
|
||||||
strcat(string, (*it).first);
|
s = strecat(s, last, (*it).first);
|
||||||
strcat(string, "=");
|
s = strecat(s, last, "=");
|
||||||
strcat(string, no);
|
s = strecat(s, last, no);
|
||||||
strcat(string, ",");
|
s = strecat(s, last, ",");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove the last ',', but only if at least one setting was saved. */
|
/* Remove the last ',', but only if at least one setting was saved. */
|
||||||
size_t len = strlen(string);
|
if (s != string) s[-1] = '\0';
|
||||||
if (len > 0) string[len - 1] = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
|
const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
|
||||||
|
|
|
@ -172,7 +172,7 @@ public:
|
||||||
* Convert the custom settings to a string that can be stored in the config
|
* Convert the custom settings to a string that can be stored in the config
|
||||||
* file or savegames.
|
* file or savegames.
|
||||||
*/
|
*/
|
||||||
void SettingsToString(char *string, size_t size) const;
|
void SettingsToString(char *string, const char *last) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search a textfile file next to this script.
|
* Search a textfile file next to this script.
|
||||||
|
|
|
@ -1493,7 +1493,7 @@ static void AISaveConfig(IniFile *ini, const char *grpname)
|
||||||
AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
|
AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
|
||||||
const char *name;
|
const char *name;
|
||||||
char value[1024];
|
char value[1024];
|
||||||
config->SettingsToString(value, lengthof(value));
|
config->SettingsToString(value, lastof(value));
|
||||||
|
|
||||||
if (config->HasScript()) {
|
if (config->HasScript()) {
|
||||||
name = config->GetName();
|
name = config->GetName();
|
||||||
|
@ -1516,7 +1516,7 @@ static void GameSaveConfig(IniFile *ini, const char *grpname)
|
||||||
GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
|
GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
|
||||||
const char *name;
|
const char *name;
|
||||||
char value[1024];
|
char value[1024];
|
||||||
config->SettingsToString(value, lengthof(value));
|
config->SettingsToString(value, lastof(value));
|
||||||
|
|
||||||
if (config->HasScript()) {
|
if (config->HasScript()) {
|
||||||
name = config->GetName();
|
name = config->GetName();
|
||||||
|
|
Loading…
Reference in New Issue