1
0
Fork 0

Codechange: use C++ strings for constructing script file paths

pull/10801/head
Rubidium 2023-05-06 13:22:16 +02:00 committed by rubidium42
parent 20ff0bccd7
commit 98972a0748
5 changed files with 12 additions and 17 deletions

View File

@ -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 * @return the number of found files, i.e. the number of times that
* AddFile returned true. * 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); std::string path(directory);
AppendPathSeparator(path); AppendPathSeparator(path);

View File

@ -42,7 +42,7 @@ public:
virtual ~FileScanner() {} virtual ~FileScanner() {}
uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true); 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. * Add a file with the given filename.

View File

@ -410,7 +410,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
/* Show files */ /* Show files */
FiosFileScanner scanner(fop, callback_proc, file_list); FiosFileScanner scanner(fop, callback_proc, file_list);
if (subdir == NO_DIRECTORY) { if (subdir == NO_DIRECTORY) {
scanner.Scan(nullptr, _fios_path->c_str(), false); scanner.Scan(nullptr, *_fios_path, false);
} else { } else {
scanner.Scan(nullptr, subdir, true, true); scanner.Scan(nullptr, subdir, true, true);
} }

View File

@ -28,7 +28,7 @@ bool ScriptScanner::AddFile(const std::string &filename, size_t basepath_length,
this->main_script = filename; this->main_script = filename;
this->tar_file = tar_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.erase(p != std::string::npos ? p + 1 : 0);
this->main_script += "main.nut"; 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); checksum.AddFile(tar.first, 0, tar_filename);
} }
} else { } else {
char path[MAX_PATH];
strecpy(path, info->GetMainScript(), lastof(path));
/* There'll always be at least 1 path separator character in a script /* 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 * main script name as the search algorithm requires the main script to
* be in a subdirectory of the script directory; so <dir>/<path>/main.nut. */ * be in a subdirectory of the script directory; so <dir>/<path>/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); checksum.Scan(".nut", path);
} }

View File

@ -53,18 +53,14 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm)
return SQ_ERROR; return SQ_ERROR;
} }
char path[MAX_PATH];
strecpy(path, si.source, lastof(path));
/* Keep the dir, remove the rest */ /* Keep the dir, remove the rest */
SQChar *s = strrchr(path, PATHSEPCHAR); std::string path = si.source;
if (s != nullptr) { auto p = path.find_last_of(PATHSEPCHAR);
/* Keep the PATHSEPCHAR there, remove the rest */ /* Keep the PATHSEPCHAR there, remove the rest */
s++; if (p != std::string::npos) path.erase(p + 1);
*s = '\0'; path += filename;
}
strecat(path, filename, lastof(path));
#if (PATHSEPCHAR != '/') #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 #endif
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm); Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);