From 98972a0748ea49ba4033a3141e80a4ecd87ceaa7 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 6 May 2023 13:22:16 +0200 Subject: [PATCH] Codechange: use C++ strings for constructing script file paths --- src/fileio.cpp | 2 +- src/fileio_func.h | 2 +- src/fios.cpp | 2 +- src/script/script_scanner.cpp | 7 +++---- src/script/squirrel_std.cpp | 16 ++++++---------- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index 2345f7583b..f0bef68d7c 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -1253,7 +1253,7 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r * @return the number of found files, i.e. the number of times that * AddFile returned true. */ -uint FileScanner::Scan(const char *extension, const char *directory, bool recursive) +uint FileScanner::Scan(const char *extension, const std::string &directory, bool recursive) { std::string path(directory); AppendPathSeparator(path); diff --git a/src/fileio_func.h b/src/fileio_func.h index 93b6123dca..a963b8203b 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -42,7 +42,7 @@ public: virtual ~FileScanner() {} uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true); - uint Scan(const char *extension, const char *directory, bool recursive = true); + uint Scan(const char *extension, const std::string &directory, bool recursive = true); /** * Add a file with the given filename. diff --git a/src/fios.cpp b/src/fios.cpp index 265a333c61..6cf83341ec 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -410,7 +410,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->c_str(), false); + scanner.Scan(nullptr, *_fios_path, false); } else { scanner.Scan(nullptr, subdir, true, true); } diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index 4035dd649a..cb43fb0d0c 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -28,7 +28,7 @@ bool ScriptScanner::AddFile(const std::string &filename, size_t basepath_length, this->main_script = filename; this->tar_file = tar_filename; - auto p = this->main_script.rfind(PATHSEPCHAR); + auto p = this->main_script.find_last_of(PATHSEPCHAR); this->main_script.erase(p != std::string::npos ? p + 1 : 0); this->main_script += "main.nut"; @@ -229,12 +229,11 @@ static bool IsSameScript(const ContentInfo *ci, bool md5sum, ScriptInfo *info, S checksum.AddFile(tar.first, 0, tar_filename); } } else { - char path[MAX_PATH]; - strecpy(path, info->GetMainScript(), lastof(path)); /* There'll always be at least 1 path separator character in a script * main script name as the search algorithm requires the main script to * be in a subdirectory of the script directory; so //main.nut. */ - *strrchr(path, PATHSEPCHAR) = '\0'; + const std::string &main_script = info->GetMainScript(); + std::string path = main_script.substr(0, main_script.find_last_of(PATHSEPCHAR)); checksum.Scan(".nut", path); } diff --git a/src/script/squirrel_std.cpp b/src/script/squirrel_std.cpp index e04ebf4cc9..abcac023ec 100644 --- a/src/script/squirrel_std.cpp +++ b/src/script/squirrel_std.cpp @@ -53,18 +53,14 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm) return SQ_ERROR; } - char path[MAX_PATH]; - strecpy(path, si.source, lastof(path)); /* Keep the dir, remove the rest */ - SQChar *s = strrchr(path, PATHSEPCHAR); - if (s != nullptr) { - /* Keep the PATHSEPCHAR there, remove the rest */ - s++; - *s = '\0'; - } - strecat(path, filename, lastof(path)); + std::string path = si.source; + auto p = path.find_last_of(PATHSEPCHAR); + /* Keep the PATHSEPCHAR there, remove the rest */ + if (p != std::string::npos) path.erase(p + 1); + path += filename; #if (PATHSEPCHAR != '/') - for (char *n = path; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR; + std::transform(path.begin(), path.end(), path.begin(), [](char &c) { return c == '/' ? PATHSEPCHAR : c; }); #endif Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);