Changeset - r27361:5aa53f16c6f7
[Not reviewed]
master
0 5 0
Rubidium - 13 months ago 2023-05-06 11:22:16
rubidium@openttd.org
Codechange: use C++ strings for constructing script file paths
5 files changed with 12 insertions and 17 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -1250,12 +1250,12 @@ uint FileScanner::Scan(const char *exten
 
 * @param extension the extension of files to search for.
 
 * @param directory the sub directory to search in.
 
 * @param recursive whether to search recursively
 
 * @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);
 
	return ScanPath(this, extension, path.c_str(), path.size(), recursive);
 
}
src/fileio_func.h
Show inline comments
 
@@ -39,13 +39,13 @@ protected:
 
	Subdirectory subdir; ///< The current sub directory we are searching through
 
public:
 
	/** Destruct the proper one... */
 
	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.
 
	 * @param filename        the full path to the file to read
 
	 * @param basepath_length amount of characters to chop of before to get a
 
	 *                        filename relative to the search path.
src/fios.cpp
Show inline comments
 
@@ -407,13 +407,13 @@ static void FiosGetFileList(SaveLoadOper
 
	/* This is where to start sorting for the filenames */
 
	sort_start = file_list.size();
 

	
 
	/* 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);
 
	}
 

	
 
	std::sort(file_list.begin() + sort_start, file_list.end());
 

	
src/script/script_scanner.cpp
Show inline comments
 
@@ -25,13 +25,13 @@
 

	
 
bool ScriptScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
 
{
 
	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";
 

	
 
	if (!FioCheckFileExists(filename, this->subdir) || !FioCheckFileExists(this->main_script, this->subdir)) return false;
 

	
 
	this->ResetEngine();
 
@@ -226,18 +226,17 @@ static bool IsSameScript(const ContentIn
 
			const char *ext = strrchr(tar.first.c_str(), '.');
 
			if (ext == nullptr || !StrEqualsIgnoreCase(ext, ".nut")) continue;
 

	
 
			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 <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);
 
	}
 

	
 
	return memcmp(ci->md5sum, checksum.md5sum, sizeof(ci->md5sum)) == 0;
 
}
 

	
src/script/squirrel_std.cpp
Show inline comments
 
@@ -50,24 +50,20 @@ SQInteger SquirrelStd::require(HSQUIRREL
 
	sq_stackinfos(vm, 1, &si);
 
	if (si.source == nullptr) {
 
		Debug(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
 
		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);
 
	bool ret = engine->LoadScript(vm, path);
 

	
 
	/* Reset the top, so the stack stays correct */
0 comments (0 inline, 0 general)