From dd138fc460dcbab37452e90ff070a31516994aa2 Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sun, 6 Dec 2020 21:11:42 +0100 Subject: [PATCH] Codechange: Stringify config file paths. --- src/base_media_func.h | 13 ++++---- src/crashlog.cpp | 4 +-- src/fileio.cpp | 53 +++++++++++++++------------------ src/fileio_func.h | 3 +- src/fios_gui.cpp | 2 +- src/highscore.cpp | 6 ++-- src/hotkeys.cpp | 2 +- src/ini.cpp | 8 ++--- src/ini_load.cpp | 4 +-- src/ini_type.h | 4 +-- src/openttd.cpp | 7 ++--- src/os/windows/crashlog_win.cpp | 2 +- src/os/windows/win32.cpp | 6 ++-- src/screenshot.cpp | 2 +- src/settings.cpp | 2 +- src/window.cpp | 2 +- 16 files changed, 56 insertions(+), 64 deletions(-) diff --git a/src/base_media_func.h b/src/base_media_func.h index c5e2a6da23..703165db5e 100644 --- a/src/base_media_func.h +++ b/src/base_media_func.h @@ -157,17 +157,17 @@ bool BaseMedia::AddFile(const char *filename, size_t basepath_length, Tbase_set *set = new Tbase_set(); IniFile *ini = new IniFile(); - char *path = stredup(filename + basepath_length); + std::string path{ filename + basepath_length }; ini->LoadFromDisk(path, BASESET_DIR); - char *psep = strrchr(path, PATHSEPCHAR); - if (psep != nullptr) { - psep[1] = '\0'; + auto psep = path.rfind(PATHSEPCHAR); + if (psep != std::string::npos) { + path.erase(psep + 1); } else { - *path = '\0'; + path.clear(); } - if (set->FillSetDetails(ini, path, filename)) { + if (set->FillSetDetails(ini, path.c_str(), filename)) { Tbase_set *duplicate = nullptr; for (Tbase_set *c = BaseMedia::available_sets; c != nullptr; c = c->next) { if (c->name == set->name || c->shortname == set->shortname) { @@ -214,7 +214,6 @@ bool BaseMedia::AddFile(const char *filename, size_t basepath_length, } else { delete set; } - free(path); delete ini; return ret; diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 29e650b937..c447019fef 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -368,7 +368,7 @@ char *CrashLog::FillCrashLog(char *buffer, const char *last) const */ bool CrashLog::WriteCrashLog(const char *buffer, char *filename, const char *filename_last) const { - seprintf(filename, filename_last, "%scrash.log", _personal_dir); + seprintf(filename, filename_last, "%scrash.log", _personal_dir.c_str()); FILE *file = FioFOpenFile(filename, "w", NO_DIRECTORY); if (file == nullptr) return false; @@ -403,7 +403,7 @@ bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const try { GamelogEmergency(); - seprintf(filename, filename_last, "%scrash.sav", _personal_dir); + seprintf(filename, filename_last, "%scrash.sav", _personal_dir.c_str()); /* Don't do a threaded saveload. */ return SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK; diff --git a/src/fileio.cpp b/src/fileio.cpp index e409d9d2a4..135a6bee21 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -53,8 +53,8 @@ static Fio _fio; ///< #Fio instance. /** Whether the working directory should be scanned. */ static bool _do_scan_working_directory = true; -extern char *_config_file; -extern char *_highscore_file; +extern std::string _config_file; +extern std::string _highscore_file; /** * Get position in the current file. @@ -336,7 +336,7 @@ char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir) } /* Could not find the directory, fall back to a base path */ - strecpy(buf, _personal_dir, last); + strecpy(buf, _personal_dir.c_str(), last); return buf; } @@ -1064,7 +1064,7 @@ void DetermineBasePaths(const char *exe) char cwd[MAX_PATH]; if (getcwd(cwd, MAX_PATH) == nullptr) *cwd = '\0'; - if (_config_file == nullptr) { + if (_config_file.empty()) { /* Get the path to working directory of OpenTTD. */ if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0'; AppendPathSeparator(tmp, lastof(tmp)); @@ -1073,7 +1073,7 @@ void DetermineBasePaths(const char *exe) _do_scan_working_directory = DoScanWorkingDirectory(); } else { /* Use the folder of the config file as working directory. */ - char *config_dir = stredup(_config_file); + char *config_dir = stredup(_config_file.c_str()); char *end = strrchr(config_dir, PATHSEPCHAR); if (end == nullptr) { free(config_dir); @@ -1120,7 +1120,7 @@ extern void cocoaSetApplicationBundleDir(); } #endif /* defined(_WIN32) */ -const char *_personal_dir; +std::string _personal_dir; /** * Acquire the base paths (personal dir and game data dir), @@ -1149,16 +1149,15 @@ void DeterminePaths(const char *exe) DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]); } - const char *config_dir; - if (_config_file != nullptr) { + std::string config_dir; + if (!_config_file.empty()) { config_dir = _searchpaths[SP_WORKING_DIR]; } else { char personal_dir[MAX_PATH]; if (FioFindFullPath(personal_dir, lastof(personal_dir), BASE_DIR, "openttd.cfg") != nullptr) { char *end = strrchr(personal_dir, PATHSEPCHAR); if (end != nullptr) end[1] = '\0'; - config_dir = stredup(personal_dir); - _config_file = str_fmt("%sopenttd.cfg", config_dir); + config_dir = personal_dir; } else { #if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR) /* No previous configuration file found. Use the configuration folder from XDG. */ @@ -1168,26 +1167,25 @@ void DeterminePaths(const char *exe) SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR }; - config_dir = nullptr; + config_dir.clear(); for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) { if (IsValidSearchPath(new_openttd_cfg_order[i])) { - config_dir = stredup(_searchpaths[new_openttd_cfg_order[i]]); + config_dir = _searchpaths[new_openttd_cfg_order[i]]; break; } } - assert(config_dir != nullptr); #endif - _config_file = str_fmt("%sopenttd.cfg", config_dir); } + _config_file = config_dir + "openttd.cfg"; } - DEBUG(misc, 3, "%s found as config directory", config_dir); + DEBUG(misc, 3, "%s found as config directory", config_dir.c_str()); - _highscore_file = str_fmt("%shs.dat", config_dir); - extern char *_hotkeys_file; - _hotkeys_file = str_fmt("%shotkeys.cfg", config_dir); - extern char *_windows_file; - _windows_file = str_fmt("%swindows.cfg", config_dir); + _highscore_file = config_dir + "hs.dat"; + extern std::string _hotkeys_file; + _hotkeys_file = config_dir + "hotkeys.cfg"; + extern std::string _windows_file; + _windows_file = config_dir + "windows.cfg"; #if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR) if (config_dir == config_home) { @@ -1201,25 +1199,23 @@ void DeterminePaths(const char *exe) } /* Make the necessary folders */ - FioCreateDirectory(config_dir); + FioCreateDirectory(config_dir.c_str()); #if defined(WITH_PERSONAL_DIR) - FioCreateDirectory(_personal_dir); + FioCreateDirectory(_personal_dir.c_str()); #endif - DEBUG(misc, 3, "%s found as personal directory", _personal_dir); + DEBUG(misc, 3, "%s found as personal directory", _personal_dir.c_str()); static const Subdirectory default_subdirs[] = { SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR, SCREENSHOT_DIR }; for (uint i = 0; i < lengthof(default_subdirs); i++) { - char *dir = str_fmt("%s%s", _personal_dir, _subdirs[default_subdirs[i]]); - FioCreateDirectory(dir); - free(dir); + FioCreateDirectory((_personal_dir + _subdirs[default_subdirs[i]]).c_str()); } /* If we have network we make a directory for the autodownloading of content */ - _searchpaths[SP_AUTODOWNLOAD_DIR] = str_fmt("%s%s", _personal_dir, "content_download" PATHSEP); + _searchpaths[SP_AUTODOWNLOAD_DIR] = str_fmt("%s%s", _personal_dir.c_str(), "content_download" PATHSEP); FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]); /* Create the directory for each of the types of content */ @@ -1231,8 +1227,7 @@ void DeterminePaths(const char *exe) } extern std::string _log_file; - _log_file = _personal_dir; - _log_file += "openttd.log"; + _log_file = _personal_dir + "openttd.log"; } /** diff --git a/src/fileio_func.h b/src/fileio_func.h index d951318801..b883a00e80 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -12,6 +12,7 @@ #include "core/enum_type.hpp" #include "fileio_type.h" +#include void FioSeekTo(size_t pos, int mode); void FioSeekToFile(uint8 slot, size_t pos); @@ -64,7 +65,7 @@ void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize); bool FileExists(const char *filename); bool ExtractTar(const char *tar_filename, Subdirectory subdir); -extern const char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc. +extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc. /** Helper for scanning for files with a given name */ class FileScanner { diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 47f7e1e2dd..059bbf0ce9 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -382,7 +382,7 @@ public: break; default: - strecpy(o_dir.name, _personal_dir, lastof(o_dir.name)); + strecpy(o_dir.name, _personal_dir.c_str(), lastof(o_dir.name)); } switch (this->fop) { diff --git a/src/highscore.cpp b/src/highscore.cpp index d8fe348fe0..187df028bd 100644 --- a/src/highscore.cpp +++ b/src/highscore.cpp @@ -20,7 +20,7 @@ #include "safeguards.h" HighScore _highscore_table[SP_HIGHSCORE_END][5]; ///< various difficulty-settings; top 5 -char *_highscore_file; ///< The file to store the highscore data in. +std::string _highscore_file; ///< The file to store the highscore data in. static const StringID _endgame_perf_titles[] = { STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN, @@ -123,7 +123,7 @@ int8 SaveHighScoreValueNetwork() /** Save HighScore table to file */ void SaveToHighScore() { - FILE *fp = fopen(_highscore_file, "wb"); + FILE *fp = fopen(_highscore_file.c_str(), "wb"); if (fp != nullptr) { uint i; @@ -151,7 +151,7 @@ void SaveToHighScore() /** Initialize the highscore table to 0 and if any file exists, load in values */ void LoadFromHighScore() { - FILE *fp = fopen(_highscore_file, "rb"); + FILE *fp = fopen(_highscore_file.c_str(), "rb"); memset(_highscore_table, 0, sizeof(_highscore_table)); diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 6db3a406b3..0a10290c18 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -16,7 +16,7 @@ #include "safeguards.h" -char *_hotkeys_file; +std::string _hotkeys_file; /** * List of all HotkeyLists. diff --git a/src/ini.cpp b/src/ini.cpp index fc9b1e8fd2..036ced6688 100644 --- a/src/ini.cpp +++ b/src/ini.cpp @@ -43,7 +43,7 @@ IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_ * @param filename the file to save to. * @return true if saving succeeded. */ -bool IniFile::SaveToDisk(const char *filename) +bool IniFile::SaveToDisk(const std::string &filename) { /* * First write the configuration to a (temporary) file and then rename @@ -96,7 +96,7 @@ bool IniFile::SaveToDisk(const char *filename) # undef strncpy /* Allocate space for one more \0 character. */ TCHAR tfilename[MAX_PATH + 1], tfile_new[MAX_PATH + 1]; - _tcsncpy(tfilename, OTTD2FS(filename), MAX_PATH); + _tcsncpy(tfilename, OTTD2FS(filename.c_str()), MAX_PATH); _tcsncpy(tfile_new, OTTD2FS(file_new.c_str()), MAX_PATH); /* SHFileOperation wants a double '\0' terminated string. */ tfilename[MAX_PATH - 1] = '\0'; @@ -113,8 +113,8 @@ bool IniFile::SaveToDisk(const char *filename) shfopt.pTo = tfilename; SHFileOperation(&shfopt); #else - if (rename(file_new.c_str(), filename) < 0) { - DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new.c_str(), filename); + if (rename(file_new.c_str(), filename.c_str()) < 0) { + DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new.c_str(), filename.c_str()); } #endif diff --git a/src/ini_load.cpp b/src/ini_load.cpp index 93b6bdf72d..e7c2b95fe6 100644 --- a/src/ini_load.cpp +++ b/src/ini_load.cpp @@ -192,7 +192,7 @@ void IniLoadFile::RemoveGroup(const char *name) * @param subdir the sub directory to load the file from. * @pre nothing has been loaded yet. */ -void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir) +void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir) { assert(this->last_group == &this->group); @@ -204,7 +204,7 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir) uint comment_alloc = 0; size_t end; - FILE *in = this->OpenFile(filename, subdir, &end); + FILE *in = this->OpenFile(filename.c_str(), subdir, &end); if (in == nullptr) return; end += ftell(in); diff --git a/src/ini_type.h b/src/ini_type.h index b5f8398d97..1637174500 100644 --- a/src/ini_type.h +++ b/src/ini_type.h @@ -64,7 +64,7 @@ struct IniLoadFile { IniGroup *GetGroup(const std::string &name, bool create_new = true); void RemoveGroup(const char *name); - void LoadFromDisk(const char *filename, Subdirectory subdir); + void LoadFromDisk(const std::string &filename, Subdirectory subdir); /** * Open the INI file. @@ -88,7 +88,7 @@ struct IniLoadFile { struct IniFile : IniLoadFile { IniFile(const char * const *list_group_names = nullptr); - bool SaveToDisk(const char *filename); + bool SaveToDisk(const std::string &filename); virtual FILE *OpenFile(const char *filename, Subdirectory subdir, size_t *size); virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post); diff --git a/src/openttd.cpp b/src/openttd.cpp index 89cc15c35a..2409e8e4e3 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -88,7 +88,7 @@ bool HandleBootstrap(); extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY); extern void ShowOSErrorBox(const char *buf, bool system); -extern char *_config_file; +extern std::string _config_file; bool _save_config = false; @@ -316,8 +316,6 @@ static void ShutdownGame() /* Uninitialize variables that are allocated dynamically */ GamelogReset(); - free(_config_file); - LinkGraphSchedule::Clear(); PoolBase::Clean(PT_ALL); @@ -566,7 +564,6 @@ int openttd_main(int argc, char *argv[]) _game_mode = GM_MENU; _switch_mode = SM_MENU; - _config_file = nullptr; GetOptData mgo(argc - 1, argv + 1, _options); int ret = 0; @@ -672,7 +669,7 @@ int openttd_main(int argc, char *argv[]) return ret; } case 'G': scanner->generation_seed = strtoul(mgo.opt, nullptr, 10); break; - case 'c': free(_config_file); _config_file = stredup(mgo.opt); break; + case 'c': _config_file = mgo.opt; break; case 'x': scanner->save_config = false; break; case 'h': i = -2; // Force printing of help. diff --git a/src/os/windows/crashlog_win.cpp b/src/os/windows/crashlog_win.cpp index 7a1a524aa5..c96c4cf7c2 100644 --- a/src/os/windows/crashlog_win.cpp +++ b/src/os/windows/crashlog_win.cpp @@ -499,7 +499,7 @@ char *CrashLogWindows::AppendDecodedStacktrace(char *buffer, const char *last) c CONST PMINIDUMP_CALLBACK_INFORMATION); MiniDumpWriteDump_t funcMiniDumpWriteDump = (MiniDumpWriteDump_t)GetProcAddress(dbghelp, "MiniDumpWriteDump"); if (funcMiniDumpWriteDump != nullptr) { - seprintf(filename, filename_last, "%scrash.dmp", _personal_dir); + seprintf(filename, filename_last, "%scrash.dmp", _personal_dir.c_str()); HANDLE file = CreateFile(OTTD2FS(filename), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, 0); HANDLE proc = GetCurrentProcess(); DWORD procid = GetCurrentProcessId(); diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 468234391c..c4e400279a 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -451,7 +451,7 @@ char *getcwd(char *buf, size_t size) return buf; } -extern char *_config_file; +extern std::string _config_file; void DetermineBasePaths(const char *exe) { @@ -482,7 +482,7 @@ void DetermineBasePaths(const char *exe) _searchpaths[SP_SHARED_DIR] = nullptr; #endif - if (_config_file == nullptr) { + if (_config_file.empty()) { /* Get the path to working directory of OpenTTD. */ getcwd(tmp, lengthof(tmp)); AppendPathSeparator(tmp, lastof(tmp)); @@ -490,7 +490,7 @@ void DetermineBasePaths(const char *exe) } else { /* Use the folder of the config file as working directory. */ TCHAR config_dir[MAX_PATH]; - _tcsncpy(path, convert_to_fs(_config_file, path, lengthof(path)), lengthof(path)); + _tcsncpy(path, convert_to_fs(_config_file.c_str(), path, lengthof(path)), lengthof(path)); if (!GetFullPathName(path, lengthof(config_dir), config_dir, nullptr)) { DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError()); _searchpaths[SP_WORKING_DIR] = nullptr; diff --git a/src/screenshot.cpp b/src/screenshot.cpp index b832e2224b..1274a4c50f 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -681,7 +681,7 @@ static const char *MakeScreenshotName(const char *default_fn, const char *ext, b size_t len = strlen(_screenshot_name); seprintf(&_screenshot_name[len], lastof(_screenshot_name), ".%s", ext); - const char *screenshot_dir = crashlog ? _personal_dir : FiosGetScreenshotDir(); + const char *screenshot_dir = crashlog ? _personal_dir.c_str() : FiosGetScreenshotDir(); for (uint serial = 1;; serial++) { if (seprintf(_full_screenshot_name, lastof(_full_screenshot_name), "%s%s", screenshot_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) { diff --git a/src/settings.cpp b/src/settings.cpp index 02cd520049..b5a07fbb25 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -80,7 +80,7 @@ ClientSettings _settings_client; GameSettings _settings_game; ///< Game settings of a running game or the scenario editor. GameSettings _settings_newgame; ///< Game settings for new games (updated from the intro screen). VehicleDefaultSettings _old_vds; ///< Used for loading default vehicles settings from old savegames -char *_config_file; ///< Configuration file of OpenTTD +std::string _config_file; ///< Configuration file of OpenTTD typedef std::list ErrorList; static ErrorList _settings_error_list; ///< Errors while loading minimal settings. diff --git a/src/window.cpp b/src/window.cpp index 6fd4d0d0b6..feabbfdd26 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -86,7 +86,7 @@ SpecialMouseMode _special_mouse_mode; ///< Mode of the mouse. static std::vector *_window_descs = nullptr; /** Config file to store WindowDesc */ -char *_windows_file; +std::string _windows_file; /** Window description constructor. */ WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,