diff options
-rw-r--r-- | src/fileio.cpp | 138 | ||||
-rw-r--r-- | src/fileio_func.h | 9 | ||||
-rw-r--r-- | src/fios.cpp | 109 | ||||
-rw-r--r-- | src/fios_gui.cpp | 10 | ||||
-rw-r--r-- | src/music/midifile.cpp | 33 | ||||
-rw-r--r-- | src/network/network_content.cpp | 27 | ||||
-rw-r--r-- | src/os/windows/win32.cpp | 40 | ||||
-rw-r--r-- | src/script/script_instance.cpp | 11 | ||||
-rw-r--r-- | src/strings.cpp | 5 | ||||
-rw-r--r-- | src/video/cocoa/cocoa_v.mm | 2 | ||||
-rw-r--r-- | src/video/sdl2_v.cpp | 6 | ||||
-rw-r--r-- | src/video/sdl_v.cpp | 6 |
12 files changed, 180 insertions, 216 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp index aed0106fb..afdbdd256 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -297,65 +297,52 @@ void FioFCloseFile(FILE *f) fclose(f); } -char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename) -{ - assert(subdir < NUM_SUBDIRS); - assert(sp < NUM_SEARCHPATHS); - - seprintf(buf, last, "%s%s%s", _searchpaths[sp].c_str(), _subdirs[subdir], filename); - return buf; -} - /** * Find a path to the filename in one of the search directories. - * @param[out] buf Destination buffer for the path. - * @param last End of the destination buffer. * @param subdir Subdirectory to try. * @param filename Filename to look for. - * @return \a buf containing the path if the path was found, else \c nullptr. + * @return String containing the path if the path was found, else an empty string. */ -char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename) +std::string FioFindFullPath(Subdirectory subdir, const char *filename) { Searchpath sp; assert(subdir < NUM_SUBDIRS); FOR_ALL_SEARCHPATHS(sp) { - FioGetFullPath(buf, last, sp, subdir, filename); - if (FileExists(buf)) return buf; + std::string buf = FioGetDirectory(sp, subdir); + buf += filename; + if (FileExists(buf.c_str())) return buf; #if !defined(_WIN32) /* Be, as opening files, aware that sometimes the filename * might be in uppercase when it is in lowercase on the * disk. Of course Windows doesn't care about casing. */ - if (strtolower(buf + _searchpaths[sp].size() - 1) && FileExists(buf)) return buf; + if (strtolower(buf, _searchpaths[sp].size() - 1) && FileExists(buf.c_str())) return buf; #endif } - return nullptr; + return {}; } -char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir) +std::string FioGetDirectory(Searchpath sp, Subdirectory subdir) { assert(subdir < NUM_SUBDIRS); assert(sp < NUM_SEARCHPATHS); - seprintf(buf, last, "%s%s", _searchpaths[sp].c_str(), _subdirs[subdir]); - return buf; + return _searchpaths[sp] + _subdirs[subdir]; } -char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir) +std::string FioFindDirectory(Subdirectory subdir) { Searchpath sp; /* Find and return the first valid directory */ FOR_ALL_SEARCHPATHS(sp) { - char *ret = FioAppendDirectory(buf, last, sp, subdir); - if (FileExists(buf)) return ret; + std::string ret = FioGetDirectory(sp, subdir); + if (FileExists(ret.c_str())) return ret; } /* Could not find the directory, fall back to a base path */ - strecpy(buf, _personal_dir.c_str(), last); - - return buf; + return _personal_dir; } static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) @@ -545,21 +532,13 @@ void FioCreateDirectory(const char *name) * Appends, if necessary, the path separator character to the end of the string. * It does not add the path separator to zero-sized strings. * @param buf string to append the separator to - * @param last the last element of \a buf. * @return true iff the operation succeeded */ -bool AppendPathSeparator(char *buf, const char *last) +void AppendPathSeparator(std::string &buf) { - size_t s = strlen(buf); + if (buf.empty()) return; - /* Length of string + path separator + '\0' */ - if (s != 0 && buf[s - 1] != PATHSEPCHAR) { - if (&buf[s] >= last) return false; - - seprintf(buf + s, last, "%c", PATHSEPCHAR); - } - - return true; + if (buf.back() != PATHSEPCHAR) buf.push_back(PATHSEPCHAR); } static void TarAddLink(const std::string &srcParam, const std::string &destParam, Subdirectory subdir) @@ -1013,9 +992,9 @@ bool DoScanWorkingDirectory() /* No personal/home directory, so the working directory won't be that. */ if (_searchpaths[SP_PERSONAL_DIR].empty()) return true; - char tmp[MAX_PATH]; - seprintf(tmp, lastof(tmp), "%s%s", _searchpaths[SP_WORKING_DIR].c_str(), PERSONAL_DIR); - AppendPathSeparator(tmp, lastof(tmp)); + std::string tmp = _searchpaths[SP_WORKING_DIR] + PERSONAL_DIR; + AppendPathSeparator(tmp); + return _searchpaths[SP_PERSONAL_DIR] != tmp; } @@ -1025,14 +1004,15 @@ bool DoScanWorkingDirectory() */ void DetermineBasePaths(const char *exe) { - char tmp[MAX_PATH]; + std::string tmp; #if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR) const char *xdg_data_home = xdgDataHome(nullptr); - seprintf(tmp, lastof(tmp), "%s" PATHSEP "%s", xdg_data_home, - PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR); + tmp = xdg_data_home; + tmp += PATHSEP; + tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR; free(xdg_data_home); - AppendPathSeparator(tmp, lastof(tmp)); + AppendPathSeparator(tmp); _searchpaths[SP_PERSONAL_DIR_XDG] = tmp; #endif #if defined(OS2) || !defined(WITH_PERSONAL_DIR) @@ -1060,8 +1040,10 @@ void DetermineBasePaths(const char *exe) if (homedir != nullptr) { ValidateString(homedir); - seprintf(tmp, lastof(tmp), "%s" PATHSEP "%s", homedir, PERSONAL_DIR); - AppendPathSeparator(tmp, lastof(tmp)); + tmp = homedir; + tmp += PATHSEP; + tmp += PERSONAL_DIR; + AppendPathSeparator(tmp); _searchpaths[SP_PERSONAL_DIR] = tmp; free(homedir); @@ -1071,8 +1053,8 @@ void DetermineBasePaths(const char *exe) #endif #if defined(WITH_SHARED_DIR) - seprintf(tmp, lastof(tmp), "%s", SHARED_DIR); - AppendPathSeparator(tmp, lastof(tmp)); + tmp = SHARED_DIR; + AppendPathSeparator(tmp); _searchpaths[SP_SHARED_DIR] = tmp; #else _searchpaths[SP_SHARED_DIR].clear(); @@ -1083,8 +1065,8 @@ void DetermineBasePaths(const char *exe) if (_config_file.empty()) { /* Get the path to working directory of OpenTTD. */ - if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0'; - AppendPathSeparator(tmp, lastof(tmp)); + tmp = cwd; + AppendPathSeparator(tmp); _searchpaths[SP_WORKING_DIR] = tmp; _do_scan_working_directory = DoScanWorkingDirectory(); @@ -1093,8 +1075,8 @@ void DetermineBasePaths(const char *exe) size_t end = _config_file.find_last_of(PATHSEPCHAR); if (end == std::string::npos) { /* _config_file is not in a folder, so use current directory. */ - if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0'; - AppendPathSeparator(tmp, lastof(tmp)); + tmp = cwd; + AppendPathSeparator(tmp); _searchpaths[SP_WORKING_DIR] = tmp; } else { _searchpaths[SP_WORKING_DIR] = _config_file.substr(0, end + 1); @@ -1103,8 +1085,13 @@ void DetermineBasePaths(const char *exe) /* Change the working directory to that one of the executable */ if (ChangeWorkingDirectoryToExecutable(exe)) { - if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0'; - AppendPathSeparator(tmp, lastof(tmp)); + char buf[MAX_PATH]; + if (getcwd(buf, lengthof(buf)) == nullptr) { + tmp.clear(); + } else { + tmp = buf; + } + AppendPathSeparator(tmp); _searchpaths[SP_BINARY_DIR] = tmp; } else { _searchpaths[SP_BINARY_DIR].clear(); @@ -1120,8 +1107,8 @@ void DetermineBasePaths(const char *exe) #if !defined(GLOBAL_DATA_DIR) _searchpaths[SP_INSTALLATION_DIR].clear(); #else - seprintf(tmp, lastof(tmp), "%s", GLOBAL_DATA_DIR); - AppendPathSeparator(tmp, lastof(tmp)); + tmp = GLOBAL_DATA_DIR; + AppendPathSeparator(tmp); _searchpaths[SP_INSTALLATION_DIR] = tmp; #endif #ifdef WITH_COCOA @@ -1146,14 +1133,13 @@ void DeterminePaths(const char *exe) DetermineBasePaths(exe); #if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR) - char config_home[MAX_PATH]; - const char *xdg_config_home = xdgConfigHome(nullptr); - seprintf(config_home, lastof(config_home), "%s" PATHSEP "%s", xdg_config_home, - PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR); + std::string config_home(xdg_config_home); + config_home += PATHSEP; + config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR; free(xdg_config_home); - AppendPathSeparator(config_home, lastof(config_home)); + AppendPathSeparator(config_home); #endif Searchpath sp; @@ -1166,10 +1152,10 @@ void DeterminePaths(const char *exe) 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'; + std::string personal_dir = FioFindFullPath(BASE_DIR, "openttd.cfg"); + if (!personal_dir.empty()) { + auto end = personal_dir.find_last_of(PATHSEPCHAR); + if (end != std::string::npos) personal_dir.erase(end + 1); config_dir = personal_dir; } else { #if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR) @@ -1330,21 +1316,21 @@ static uint ScanPath(FileScanner *fs, const char *extension, const char *path, s while ((dirent = readdir(dir)) != nullptr) { const char *d_name = FS2OTTD(dirent->d_name); - char filename[MAX_PATH]; if (!FiosIsValidFile(path, dirent, &sb)) continue; - seprintf(filename, lastof(filename), "%s%s", path, d_name); + std::string filename(path); + filename += d_name; if (S_ISDIR(sb.st_mode)) { /* Directory */ if (!recursive) continue; if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue; - if (!AppendPathSeparator(filename, lastof(filename))) continue; - num += ScanPath(fs, extension, filename, basepath_length, recursive); + AppendPathSeparator(filename); + num += ScanPath(fs, extension, filename.c_str(), basepath_length, recursive); } else if (S_ISREG(sb.st_mode)) { /* File */ - if (MatchesExtension(extension, filename) && fs->AddFile(filename, basepath_length, nullptr)) num++; + if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename.c_str(), basepath_length, nullptr)) num++; } } @@ -1383,7 +1369,6 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r this->subdir = sd; Searchpath sp; - char path[MAX_PATH]; TarFileList::iterator tar; uint num = 0; @@ -1391,8 +1376,8 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r /* Don't search in the working directory */ if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue; - FioAppendDirectory(path, lastof(path), sp, sd); - num += ScanPath(this, extension, path, strlen(path), recursive); + std::string path = FioGetDirectory(sp, sd); + num += ScanPath(this, extension, path.c_str(), path.size(), recursive); } if (tars && sd != NO_DIRECTORY) { @@ -1425,8 +1410,7 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r */ uint FileScanner::Scan(const char *extension, const char *directory, bool recursive) { - char path[MAX_PATH]; - strecpy(path, directory, lastof(path)); - if (!AppendPathSeparator(path, lastof(path))) return 0; - return ScanPath(this, extension, path, strlen(path), recursive); + std::string path(directory); + AppendPathSeparator(path); + return ScanPath(this, extension, path.c_str(), path.size(), recursive); } diff --git a/src/fileio_func.h b/src/fileio_func.h index a4bdeb007..83e90be17 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -39,16 +39,15 @@ bool IsValidSearchPath(Searchpath sp); void FioFCloseFile(FILE *f); FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); bool FioCheckFileExists(const char *filename, Subdirectory subdir); -char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename); -char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename); -char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir); -char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir); +std::string FioFindFullPath(Subdirectory subdir, const char *filename); +std::string FioGetDirectory(Searchpath sp, Subdirectory subdir); +std::string FioFindDirectory(Subdirectory subdir); void FioCreateDirectory(const char *name); const char *FiosGetScreenshotDir(); void SanitizeFilename(char *filename); -bool AppendPathSeparator(char *buf, const char *last); +void AppendPathSeparator(std::string &buf); void DeterminePaths(const char *exe); void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize); bool FileExists(const char *filename); diff --git a/src/fios.cpp b/src/fios.cpp index 61f08d93a..4e2378a34 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -19,6 +19,8 @@ #include "string_func.h" #include "tar_type.h" #include <sys/stat.h> +#include <functional> +#include <optional> #ifndef _WIN32 # include <unistd.h> @@ -29,8 +31,7 @@ #include "safeguards.h" /* Variables to display file lists */ -static char *_fios_path; -static const char *_fios_path_last; +static std::string *_fios_path = nullptr; SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */ @@ -138,7 +139,7 @@ const FiosItem *FileList::FindItem(const char *file) */ StringID FiosGetDescText(const char **path, uint64 *total_free) { - *path = _fios_path; + *path = _fios_path->c_str(); return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE; } @@ -152,7 +153,8 @@ const char *FiosBrowseTo(const FiosItem *item) switch (item->type) { case FIOS_TYPE_DRIVE: #if defined(_WIN32) || defined(__OS2__) - seprintf(_fios_path, _fios_path_last, "%c:" PATHSEP, item->title[0]); + assert(_fios_path != nullptr); + *_fios_path = std::string{ item->title[0] } + ":" PATHSEP; #endif break; @@ -160,25 +162,28 @@ const char *FiosBrowseTo(const FiosItem *item) break; case FIOS_TYPE_PARENT: { - /* Check for possible nullptr ptr */ - char *s = strrchr(_fios_path, PATHSEPCHAR); - if (s != nullptr && s != _fios_path) { - s[0] = '\0'; // Remove last path separator character, so we can go up one level. + assert(_fios_path != nullptr); + auto s = _fios_path->find_last_of(PATHSEPCHAR); + if (s != std::string::npos && s != 0) { + _fios_path->erase(s); // Remove last path separator character, so we can go up one level. } - s = strrchr(_fios_path, PATHSEPCHAR); - if (s != nullptr) { - s[1] = '\0'; // go up a directory + + s = _fios_path->find_last_of(PATHSEPCHAR); + if (s != std::string::npos) { + _fios_path->erase(s + 1); // go up a directory } break; } case FIOS_TYPE_DIR: - strecat(_fios_path, item->name, _fios_path_last); - strecat(_fios_path, PATHSEP, _fios_path_last); + assert(_fios_path != nullptr); + *_fios_path += item->name; + *_fios_path += PATHSEP; break; case FIOS_TYPE_DIRECT: - seprintf(_fios_path, _fios_path_last, "%s", item->name); + assert(_fios_path != nullptr); + *_fios_path = item->name; break; case FIOS_TYPE_FILE: @@ -227,7 +232,7 @@ void FiosMakeSavegameName(char *buf, const char *name, const char *last) { const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav"; - FiosMakeFilename(buf, _fios_path, name, extension, last); + FiosMakeFilename(buf, _fios_path->c_str(), name, extension, last); } /** @@ -242,7 +247,7 @@ void FiosMakeHeightmapName(char *buf, const char *name, const char *last) ext[0] = '.'; strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext)); - FiosMakeFilename(buf, _fios_path, name, ext, last); + FiosMakeFilename(buf, _fios_path->c_str(), name, ext, last); } /** @@ -365,8 +370,10 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c file_list.Clear(); + assert(_fios_path != nullptr); + /* A parent directory link exists if we are not in the root directory */ - if (!FiosIsRoot(_fios_path)) { + if (!FiosIsRoot(_fios_path->c_str())) { fios = file_list.Append(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; @@ -375,12 +382,12 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c } /* Show subdirectories */ - if ((dir = ttd_opendir(_fios_path)) != nullptr) { + if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) { while ((dirent = readdir(dir)) != nullptr) { strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name)); /* found file must be directory, but not '.' or '..' */ - if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) && + if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) && (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) && strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) { fios = file_list.Append(); @@ -408,7 +415,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c /* Show files */ FiosFileScanner scanner(fop, callback_proc, file_list); if (subdir == NO_DIRECTORY) { - scanner.Scan(nullptr, _fios_path, false); + scanner.Scan(nullptr, _fios_path->c_str(), false); } else { scanner.Scan(nullptr, subdir, true, true); } @@ -491,17 +498,11 @@ FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, co */ void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list) { - static char *fios_save_path = nullptr; - static char *fios_save_path_last = nullptr; + static std::optional<std::string> fios_save_path; - if (fios_save_path == nullptr) { - fios_save_path = MallocT<char>(MAX_PATH); - fios_save_path_last = fios_save_path + MAX_PATH - 1; - FioGetDirectory(fios_save_path, fios_save_path_last, SAVE_DIR); - } + if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR); - _fios_path = fios_save_path; - _fios_path_last = fios_save_path_last; + _fios_path = &(*fios_save_path); FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list); } @@ -546,23 +547,15 @@ static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *f */ void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list) { - static char *fios_scn_path = nullptr; - static char *fios_scn_path_last = nullptr; + static std::optional<std::string> fios_scn_path; /* Copy the default path on first run or on 'New Game' */ - if (fios_scn_path == nullptr) { - fios_scn_path = MallocT<char>(MAX_PATH); - fios_scn_path_last = fios_scn_path + MAX_PATH - 1; - FioGetDirectory(fios_scn_path, fios_scn_path_last, SCENARIO_DIR); - } + if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR); - _fios_path = fios_scn_path; - _fios_path_last = fios_scn_path_last; + _fios_path = &(*fios_scn_path); - char base_path[MAX_PATH]; - FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR); - - Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY; + std::string base_path = FioFindDirectory(SCENARIO_DIR); + Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY; FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list); } @@ -593,10 +586,9 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char * bool match = false; Searchpath sp; FOR_ALL_SEARCHPATHS(sp) { - char buf[MAX_PATH]; - FioAppendDirectory(buf, lastof(buf), sp, HEIGHTMAP_DIR); + std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR); - if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) { + if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) { match = true; break; } @@ -617,22 +609,14 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char * */ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list) { - static char *fios_hmap_path = nullptr; - static char *fios_hmap_path_last = nullptr; - - if (fios_hmap_path == nullptr) { - fios_hmap_path = MallocT<char>(MAX_PATH); - fios_hmap_path_last = fios_hmap_path + MAX_PATH - 1; - FioGetDirectory(fios_hmap_path, fios_hmap_path_last, HEIGHTMAP_DIR); - } + static std::optional<std::string> fios_hmap_path; - _fios_path = fios_hmap_path; - _fios_path_last = fios_hmap_path_last; + if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR); - char base_path[MAX_PATH]; - FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR); + _fios_path = &(*fios_hmap_path); - Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY; + std::string base_path = FioFindDirectory(HEIGHTMAP_DIR); + Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY; FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list); } @@ -642,14 +626,11 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list) */ const char *FiosGetScreenshotDir() { - static char *fios_screenshot_path = nullptr; + static std::optional<std::string> fios_screenshot_path; - if (fios_screenshot_path == nullptr) { - fios_screenshot_path = MallocT<char>(MAX_PATH); - FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR); - } + if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR); - return fios_screenshot_path; + return fios_screenshot_path->c_str(); } /** Basic data to distinguish a scenario. Used in the server list window */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 059bbf0ce..53bb606f3 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -368,22 +368,24 @@ public: /* Select the initial directory. */ o_dir.type = FIOS_TYPE_DIRECT; + std::string dir; switch (this->abstract_filetype) { case FT_SAVEGAME: - FioGetDirectory(o_dir.name, lastof(o_dir.name), SAVE_DIR); + dir = FioFindDirectory(SAVE_DIR); break; case FT_SCENARIO: - FioGetDirectory(o_dir.name, lastof(o_dir.name), SCENARIO_DIR); + dir = FioFindDirectory(SCENARIO_DIR); break; case FT_HEIGHTMAP: - FioGetDirectory(o_dir.name, lastof(o_dir.name), HEIGHTMAP_DIR); + dir = FioFindDirectory(HEIGHTMAP_DIR); break; default: - strecpy(o_dir.name, _personal_dir.c_str(), lastof(o_dir.name)); + dir = _personal_dir; } + strecpy(o_dir.name, dir.c_str(), lastof(o_dir.name)); switch (this->fop) { case SLO_SAVE: diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index 67cc7b192..b15a27911 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -1048,14 +1048,12 @@ bool MidiFile::WriteSMF(const char *filename) std::string MidiFile::GetSMFFile(const MusicSongInfo &song) { if (song.filetype == MTT_STANDARDMIDI) { - char filename[MAX_PATH]; - if (FioFindFullPath(filename, lastof(filename), Subdirectory::BASESET_DIR, song.filename)) { - return std::string(filename); - } else if (FioFindFullPath(filename, lastof(filename), Subdirectory::OLD_GM_DIR, song.filename)) { - return std::string(filename); - } else { - return std::string(); - } + std::string filename = FioFindFullPath(Subdirectory::BASESET_DIR, song.filename); + if (!filename.empty()) return filename; + filename = FioFindFullPath(Subdirectory::OLD_GM_DIR, song.filename); + if (!filename.empty()) return filename; + + return std::string(); } if (song.filetype != MTT_MPSMIDI) return std::string(); @@ -1077,17 +1075,16 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song) *wp++ = '\0'; } - char tempdirname[MAX_PATH]; - FioGetFullPath(tempdirname, lastof(tempdirname), Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR, basename); - if (!AppendPathSeparator(tempdirname, lastof(tempdirname))) return std::string(); - FioCreateDirectory(tempdirname); + std::string tempdirname = FioGetDirectory(Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR); + tempdirname += basename; + AppendPathSeparator(tempdirname); + FioCreateDirectory(tempdirname.c_str()); - char output_filename[MAX_PATH]; - seprintf(output_filename, lastof(output_filename), "%s%d.mid", tempdirname, song.cat_index); + std::string output_filename = tempdirname + std::to_string(song.cat_index) + ".mid"; - if (FileExists(output_filename)) { + if (FileExists(output_filename.c_str())) { /* If the file already exists, assume it's the correct decoded data */ - return std::string(output_filename); + return output_filename; } byte *data; @@ -1102,8 +1099,8 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song) } free(data); - if (midifile.WriteSMF(output_filename)) { - return std::string(output_filename); + if (midifile.WriteSMF(output_filename.c_str())) { + return output_filename; } else { return std::string(); } diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index c4d26355c..dc14a59fc 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -385,14 +385,14 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const Co * @return a statically allocated buffer with the filename or * nullptr when no filename could be made. */ -static char *GetFullFilename(const ContentInfo *ci, bool compressed) +static std::string GetFullFilename(const ContentInfo *ci, bool compressed) { Subdirectory dir = GetContentInfoSubDir(ci->type); - if (dir == NO_DIRECTORY) return nullptr; + if (dir == NO_DIRECTORY) return {}; - static char buf[MAX_PATH]; - FioGetFullPath(buf, lastof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename); - strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf)); + std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir); + buf += ci->filename; + buf += compressed ? ".tar.gz" : ".tar"; return buf; } @@ -408,13 +408,13 @@ static bool GunzipFile(const ContentInfo *ci) bool ret = true; /* Need to open the file with fopen() to support non-ASCII on Windows. */ - FILE *ftmp = fopen(GetFullFilename(ci, true), "rb"); + FILE *ftmp = fopen(GetFullFilename(ci, true).c_str(), "rb"); if (ftmp == nullptr) return false; /* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */ gzFile fin = gzdopen(dup(fileno(ftmp)), "rb"); fclose(ftmp); - FILE *fout = fopen(GetFullFilename(ci, false), "wb"); + FILE *fout = fopen(GetFullFilename(ci, false).c_str(), "wb"); if (fin == nullptr || fout == nullptr) { ret = false; @@ -509,8 +509,8 @@ bool ClientNetworkContentSocketHandler::BeforeDownload() if (this->curInfo->filesize != 0) { /* The filesize is > 0, so we are going to download it */ - const char *filename = GetFullFilename(this->curInfo, true); - if (filename == nullptr || (this->curFile = fopen(filename, "wb")) == nullptr) { + std::string filename = GetFullFilename(this->curInfo, true); + if (filename.empty() || (this->curFile = fopen(filename.c_str(), "wb")) == nullptr) { /* Unless that fails of course... */ DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD); ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR); @@ -532,18 +532,19 @@ void ClientNetworkContentSocketHandler::AfterDownload() this->curFile = nullptr; if (GunzipFile(this->curInfo)) { - unlink(GetFullFilename(this->curInfo, true)); + unlink(GetFullFilename(this->curInfo, true).c_str()); Subdirectory sd = GetContentInfoSubDir(this->curInfo->type); if (sd == NO_DIRECTORY) NOT_REACHED(); TarScanner ts; - ts.AddFile(sd, GetFullFilename(this->curInfo, false)); + std::string fname = GetFullFilename(this->curInfo, false); + ts.AddFile(sd, fname.c_str()); if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) { /* Music can't be in a tar. So extract the tar! */ - ExtractTar(GetFullFilename(this->curInfo, false), BASESET_DIR); - unlink(GetFullFilename(this->curInfo, false)); + ExtractTar(fname.c_str(), BASESET_DIR); + unlink(fname.c_str()); } #ifdef __EMSCRIPTEN__ diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 7553740e7..bbb7e359a 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -458,24 +458,23 @@ void DetermineBasePaths(const char *exe) { extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths; - char tmp[MAX_PATH]; TCHAR path[MAX_PATH]; #ifdef WITH_PERSONAL_DIR if (SUCCEEDED(OTTDSHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) { - strecpy(tmp, FS2OTTD(path), lastof(tmp)); - AppendPathSeparator(tmp, lastof(tmp)); - strecat(tmp, PERSONAL_DIR, lastof(tmp)); - AppendPathSeparator(tmp, lastof(tmp)); + std::string tmp(FS2OTTD(path)); + AppendPathSeparator(tmp); + tmp += PERSONAL_DIR; + AppendPathSeparator(tmp); _searchpaths[SP_PERSONAL_DIR] = tmp; } else { _searchpaths[SP_PERSONAL_DIR].clear(); } if (SUCCEEDED(OTTDSHGetFolderPath(nullptr, CSIDL_COMMON_DOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, path))) { - strecpy(tmp, FS2OTTD(path), lastof(tmp)); - AppendPathSeparator(tmp, lastof(tmp)); - strecat(tmp, PERSONAL_DIR, lastof(tmp)); - AppendPathSeparator(tmp, lastof(tmp)); + std::string tmp(FS2OTTD(path)); + AppendPathSeparator(tmp); + tmp += PERSONAL_DIR; + AppendPathSeparator(tmp); _searchpaths[SP_SHARED_DIR] = tmp; } else { _searchpaths[SP_SHARED_DIR].clear(); @@ -486,10 +485,11 @@ void DetermineBasePaths(const char *exe) #endif if (_config_file.empty()) { - /* Get the path to working directory of OpenTTD. */ - getcwd(tmp, lengthof(tmp)); - AppendPathSeparator(tmp, lastof(tmp)); - _searchpaths[SP_WORKING_DIR] = tmp; + char cwd[MAX_PATH]; + getcwd(cwd, lengthof(cwd)); + std::string cwd_s(cwd); + AppendPathSeparator(cwd_s); + _searchpaths[SP_WORKING_DIR] = cwd_s; } else { /* Use the folder of the config file as working directory. */ TCHAR config_dir[MAX_PATH]; @@ -498,9 +498,10 @@ void DetermineBasePaths(const char *exe) DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError()); _searchpaths[SP_WORKING_DIR].clear(); } else { - strecpy(tmp, convert_from_fs(config_dir, tmp, lengthof(tmp)), lastof(tmp)); - char *s = strrchr(tmp, PATHSEPCHAR); - *(s + 1) = '\0'; + std::string tmp(FS2OTTD(config_dir)); + auto pos = tmp.find_last_of(PATHSEPCHAR); + if (pos != std::string::npos) tmp.erase(pos + 1); + _searchpaths[SP_WORKING_DIR] = tmp; } } @@ -515,9 +516,10 @@ void DetermineBasePaths(const char *exe) DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError()); _searchpaths[SP_BINARY_DIR].clear(); } else { - strecpy(tmp, convert_from_fs(exec_dir, tmp, lengthof(tmp)), lastof(tmp)); - char *s = strrchr(tmp, PATHSEPCHAR); - *(s + 1) = '\0'; + std::string tmp(FS2OTTD(exec_dir)); + auto pos = tmp.find_last_of(PATHSEPCHAR); + if (pos != std::string::npos) tmp.erase(pos + 1); + _searchpaths[SP_BINARY_DIR] = tmp; } } diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index 81283cb77..836bb8246 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -116,17 +116,16 @@ bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirect { char script_name[32]; seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version); - char buf[MAX_PATH]; Searchpath sp; FOR_ALL_SEARCHPATHS(sp) { - FioAppendDirectory(buf, lastof(buf), sp, dir); - strecat(buf, script_name, lastof(buf)); - if (!FileExists(buf)) continue; + std::string buf = FioGetDirectory(sp, dir); + buf += script_name; + if (!FileExists(buf.c_str())) continue; - if (this->engine->LoadScript(buf)) return true; + if (this->engine->LoadScript(buf.c_str())) return true; ScriptLog::Error("Failed to load API compatibility script"); - DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf); + DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf.c_str()); return false; } diff --git a/src/strings.cpp b/src/strings.cpp index ae0ab2c90..9f61ea5fd 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1959,9 +1959,8 @@ void InitializeLanguagePacks() Searchpath sp; FOR_ALL_SEARCHPATHS(sp) { - char path[MAX_PATH]; - FioAppendDirectory(path, lastof(path), sp, LANG_DIR); - GetLanguageList(path); + std::string path = FioGetDirectory(sp, LANG_DIR); + GetLanguageList(path.c_str()); } if (_languages.size() == 0) usererror("No available language packs (invalid versions?)"); diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index f02599799..e4a0a0c6f 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -551,8 +551,8 @@ void cocoaSetApplicationBundleDir() char tmp[MAXPATHLEN]; CFAutoRelease<CFURLRef> url(CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle())); if (CFURLGetFileSystemRepresentation(url.get(), true, (unsigned char*)tmp, MAXPATHLEN)) { - AppendPathSeparator(tmp, lastof(tmp)); _searchpaths[SP_APPLICATION_BUNDLE_DIR] = tmp; + AppendPathSeparator(_searchpaths[SP_APPLICATION_BUNDLE_DIR]); } else { _searchpaths[SP_APPLICATION_BUNDLE_DIR].clear(); } diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 852f7298d..ea2a88bb9 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -285,10 +285,10 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize) return false; } - char icon_path[MAX_PATH]; - if (FioFindFullPath(icon_path, lastof(icon_path), BASESET_DIR, "openttd.32.bmp") != nullptr) { + std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp"); + if (!icon_path.empty()) { /* Give the application an icon */ - SDL_Surface *icon = SDL_LoadBMP(icon_path); + SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str()); if (icon != nullptr) { /* Get the colourkey, which will be magenta */ uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255); diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index 98777a87d..4f5167335 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -266,10 +266,10 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h) if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals"); - char icon_path[MAX_PATH]; - if (FioFindFullPath(icon_path, lastof(icon_path), BASESET_DIR, "openttd.32.bmp") != nullptr) { + std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp"); + if (!icon_path.empty()) { /* Give the application an icon */ - icon = SDL_LoadBMP(icon_path); + icon = SDL_LoadBMP(icon_path.c_str()); if (icon != nullptr) { /* Get the colourkey, which will be magenta */ uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255); |