summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2020-12-06 21:11:50 +0100
committerMichael Lutz <michi@icosahedron.de>2020-12-27 13:19:25 +0100
commitb408fe77f792650ef569f9852165759f960fd52f (patch)
tree6d1f0a134da31bd1d53dee052721d78955d688e6 /src
parent358056ec428d1938b8c9219a60c6fb400115f152 (diff)
downloadopenttd-b408fe77f792650ef569f9852165759f960fd52f.tar.xz
Codechange: Use std::string in file scanners.
Diffstat (limited to 'src')
-rw-r--r--src/ai/ai_scanner.cpp3
-rw-r--r--src/base_media_base.h2
-rw-r--r--src/base_media_func.h10
-rw-r--r--src/fileio.cpp38
-rw-r--r--src/fileio_func.h6
-rw-r--r--src/fios.cpp49
-rw-r--r--src/fios.h2
-rw-r--r--src/game/game_text.cpp8
-rw-r--r--src/newgrf_config.cpp6
-rw-r--r--src/openttd.cpp2
-rw-r--r--src/saveload/oldloader.cpp2
-rw-r--r--src/script/script_info.cpp7
-rw-r--r--src/script/script_info.hpp10
-rw-r--r--src/script/script_scanner.cpp48
-rw-r--r--src/script/script_scanner.hpp12
-rw-r--r--src/tar_type.h18
16 files changed, 91 insertions, 132 deletions
diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp
index 1431a2820..9b3613712 100644
--- a/src/ai/ai_scanner.cpp
+++ b/src/ai/ai_scanner.cpp
@@ -32,8 +32,7 @@ void AIScannerInfo::Initialize()
ScriptAllocatorScope alloc_scope(this->engine);
/* Create the dummy AI */
- free(this->main_script);
- this->main_script = stredup("%_dummy");
+ this->main_script = "%_dummy";
extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
}
diff --git a/src/base_media_base.h b/src/base_media_base.h
index 881fdc3f3..790bb9d38 100644
--- a/src/base_media_base.h
+++ b/src/base_media_base.h
@@ -167,7 +167,7 @@ protected:
static Tbase_set *duplicate_sets; ///< All sets that aren't available, but needed for not downloading base sets when a newer version than the one on BaNaNaS is loaded.
static const Tbase_set *used_set; ///< The currently used set
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
/**
* Get the extension that is used to identify this set.
diff --git a/src/base_media_func.h b/src/base_media_func.h
index 703165db5..8491f0e51 100644
--- a/src/base_media_func.h
+++ b/src/base_media_func.h
@@ -150,14 +150,14 @@ bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const
}
template <class Tbase_set>
-bool BaseMedia<Tbase_set>::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
+bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
bool ret = false;
- DEBUG(grf, 1, "Checking %s for base " SET_TYPE " set", filename);
+ DEBUG(grf, 1, "Checking %s for base " SET_TYPE " set", filename.c_str());
Tbase_set *set = new Tbase_set();
IniFile *ini = new IniFile();
- std::string path{ filename + basepath_length };
+ std::string path{ filename, basepath_length };
ini->LoadFromDisk(path, BASESET_DIR);
auto psep = path.rfind(PATHSEPCHAR);
@@ -167,7 +167,7 @@ bool BaseMedia<Tbase_set>::AddFile(const char *filename, size_t basepath_length,
path.clear();
}
- if (set->FillSetDetails(ini, path.c_str(), filename)) {
+ if (set->FillSetDetails(ini, path.c_str(), filename.c_str())) {
Tbase_set *duplicate = nullptr;
for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != nullptr; c = c->next) {
if (c->name == set->name || c->shortname == set->shortname) {
@@ -377,7 +377,7 @@ template <class Tbase_set>
#define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
template std::string repl_type::ini_set; \
template const char *repl_type::GetExtension(); \
- template bool repl_type::AddFile(const char *filename, size_t pathlength, const char *tar_filename); \
+ template bool repl_type::AddFile(const std::string &filename, size_t pathlength, const std::string &tar_filename); \
template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
template bool repl_type::SetSet(const std::string &name); \
template char *repl_type::GetSetsList(char *p, const char *last); \
diff --git a/src/fileio.cpp b/src/fileio.cpp
index 7df22921e..cba18873a 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -246,7 +246,7 @@ static_assert(lengthof(_subdirs) == NUM_SUBDIRS);
* current operating system.
*/
std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
-TarList _tar_list[NUM_SUBDIRS];
+std::array<TarList, NUM_SUBDIRS> _tar_list;
TarFileList _tar_filelist[NUM_SUBDIRS];
typedef std::map<std::string, std::string> TarLinkList;
@@ -390,7 +390,7 @@ static FILE *FioFOpenFileSp(const std::string &filename, const char *mode, Searc
*/
FILE *FioFOpenFileTar(const TarFileListEntry &entry, size_t *filesize)
{
- FILE *f = fopen(entry.tar_filename, "rb");
+ FILE *f = fopen(entry.tar_filename.c_str(), "rb");
if (f == nullptr) return f;
if (fseek(f, entry.position, SEEK_SET) < 0) {
@@ -613,16 +613,16 @@ uint TarScanner::DoScan(Subdirectory sd)
* @param filename The name of the file to add.
* @return True if the additions went correctly.
*/
-bool TarScanner::AddFile(Subdirectory sd, const char *filename)
+bool TarScanner::AddFile(Subdirectory sd, const std::string &filename)
{
this->subdir = sd;
return this->AddFile(filename, 0);
}
-bool TarScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
+bool TarScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
/* No tar within tar. */
- assert(tar_filename == nullptr);
+ assert(tar_filename.empty());
/* The TAR-header, repeated for every file */
struct TarHeader {
@@ -650,16 +650,14 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
TarList::iterator it = _tar_list[this->subdir].find(filename);
if (it != _tar_list[this->subdir].end()) return false;
- FILE *f = fopen(filename, "rb");
+ FILE *f = fopen(filename.c_str(), "rb");
/* Although the file has been found there can be
* a number of reasons we cannot open the file.
* Most common case is when we simply have not
* been given read access. */
if (f == nullptr) return false;
- const char *dupped_filename = stredup(filename);
- _tar_list[this->subdir][filename].filename = dupped_filename;
- _tar_list[this->subdir][filename].dirname = nullptr;
+ _tar_list[this->subdir][filename] = std::string{};
TarLinkList links; ///< Temporary list to collect links
@@ -684,7 +682,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
/* If we have only zeros in the block, it can be an end-of-file indicator */
if (memcmp(&th, &empty[0], 512) == 0) continue;
- DEBUG(misc, 0, "The file '%s' isn't a valid tar-file", filename);
+ DEBUG(misc, 0, "The file '%s' isn't a valid tar-file", filename.c_str());
fclose(f);
return false;
}
@@ -714,7 +712,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
/* Store this entry in the list */
TarFileListEntry entry;
- entry.tar_filename = dupped_filename;
+ entry.tar_filename = filename;
entry.size = skip;
entry.position = pos;
@@ -782,7 +780,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
}
if (destpos >= lastof(dest)) {
- DEBUG(misc, 0, "The length of a link in tar-file '%s' is too large (malformed?)", filename);
+ DEBUG(misc, 0, "The length of a link in tar-file '%s' is too large (malformed?)", filename.c_str());
fclose(f);
return false;
}
@@ -803,7 +801,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
/* Store the first directory name we detect */
DEBUG(misc, 6, "Found dir in tar: %s", name);
- if (_tar_list[this->subdir][filename].dirname == nullptr) _tar_list[this->subdir][filename].dirname = stredup(name);
+ if (_tar_list[this->subdir][filename].empty()) _tar_list[this->subdir][filename] = name;
break;
default:
@@ -814,14 +812,14 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
/* Skip to the next block.. */
skip = Align(skip, 512);
if (fseek(f, skip, SEEK_CUR) < 0) {
- DEBUG(misc, 0, "The file '%s' can't be read as a valid tar-file", filename);
+ DEBUG(misc, 0, "The file '%s' can't be read as a valid tar-file", filename.c_str());
fclose(f);
return false;
}
pos += skip;
}
- DEBUG(misc, 1, "Found tar '%s' with " PRINTF_SIZE " new files", filename, num);
+ DEBUG(misc, 1, "Found tar '%s' with " PRINTF_SIZE " new files", filename.c_str(), num);
fclose(f);
/* Resolve file links and store directory links.
@@ -855,10 +853,10 @@ bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
/* We don't know the file. */
if (it == _tar_list[subdir].end()) return false;
- const char *dirname = (*it).second.dirname;
+ const auto &dirname = (*it).second;
/* The file doesn't have a sub directory! */
- if (dirname == nullptr) {
+ if (dirname.empty()) {
DEBUG(misc, 1, "Extracting %s failed; archive rejected, the contents must be in a sub directory", tar_filename.c_str());
return false;
}
@@ -1308,7 +1306,7 @@ static uint ScanPath(FileScanner *fs, const char *extension, const char *path, s
num += ScanPath(fs, extension, filename.c_str(), basepath_length, recursive);
} else if (S_ISREG(sb.st_mode)) {
/* File */
- if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename.c_str(), basepath_length, nullptr)) num++;
+ if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename, basepath_length, {})) num++;
}
}
@@ -1326,9 +1324,9 @@ static uint ScanPath(FileScanner *fs, const char *extension, const char *path, s
static uint ScanTar(FileScanner *fs, const char *extension, TarFileList::iterator tar)
{
uint num = 0;
- const char *filename = (*tar).first.c_str();
+ const auto &filename = (*tar).first;
- if (MatchesExtension(extension, filename) && fs->AddFile(filename, 0, (*tar).second.tar_filename)) num++;
+ if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename, 0, (*tar).second.tar_filename)) num++;
return num;
}
diff --git a/src/fileio_func.h b/src/fileio_func.h
index 014050b4b..019573906 100644
--- a/src/fileio_func.h
+++ b/src/fileio_func.h
@@ -74,7 +74,7 @@ public:
* @param tar_filename the name of the tar file the file is read from.
* @return true if the file is added.
*/
- virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) = 0;
+ virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) = 0;
};
/** Helper for scanning for files with tar as extension */
@@ -92,9 +92,9 @@ public:
ALL = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything.
};
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename = nullptr) override;
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename = nullptr) override;
- bool AddFile(Subdirectory sd, const char *filename);
+ bool AddFile(Subdirectory sd, const std::string &filename);
/** Do the scan for Tars. */
static uint DoScan(TarScanner::Mode mode);
diff --git a/src/fios.cpp b/src/fios.cpp
index e7341b1f5..698a0b559 100644
--- a/src/fios.cpp
+++ b/src/fios.cpp
@@ -42,7 +42,7 @@ extern void FiosGetDrives(FileList &file_list);
extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
/* get the name of an oldstyle savegame */
-extern void GetOldSaveGameName(const char *file, char *title, const char *last);
+extern void GetOldSaveGameName(const std::string &file, char *title, const char *last);
/**
* Compare two FiosItem's. Used with sort when sorting the file list.
@@ -260,7 +260,7 @@ bool FiosDelete(const char *name)
return unlink(filename.c_str()) == 0;
}
-typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const char *filename, const char *ext, char *title, const char *last);
+typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const std::string &filename, const char *ext, char *title, const char *last);
/**
* Scanner to scan for a particular type of FIOS file.
@@ -280,7 +280,7 @@ public:
fop(fop), callback_proc(callback_proc), file_list(file_list)
{}
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
};
/**
@@ -289,25 +289,26 @@ public:
* @param basepath_length amount of characters to chop of before to get a relative filename
* @return true if the file is added.
*/
-bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
+bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
- const char *ext = strrchr(filename, '.');
- if (ext == nullptr) return false;
+ auto sep = filename.rfind('.');
+ if (sep == std::string::npos) return false;
+ std::string ext = filename.substr(sep);
char fios_title[64];
fios_title[0] = '\0'; // reset the title;
- FiosType type = this->callback_proc(this->fop, filename, ext, fios_title, lastof(fios_title));
+ FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
if (type == FIOS_TYPE_INVALID) return false;
for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
- if (strcmp(fios->name, filename) == 0) return false;
+ if (filename == fios->name) return false;
}
FiosItem *fios = file_list.Append();
#ifdef _WIN32
// Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
- HANDLE fh = CreateFile(OTTD2FS(filename), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
+ HANDLE fh = CreateFile(OTTD2FS(filename.c_str()), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
if (fh != INVALID_HANDLE_VALUE) {
FILETIME ft;
@@ -326,7 +327,7 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons
CloseHandle(fh);
#else
struct stat sb;
- if (stat(filename, &sb) == 0) {
+ if (stat(filename.c_str(), &sb) == 0) {
fios->mtime = sb.st_mtime;
#endif
} else {
@@ -334,13 +335,13 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons
}
fios->type = type;
- strecpy(fios->name, filename, lastof(fios->name));
+ strecpy(fios->name, filename.c_str(), lastof(fios->name));
/* If the file doesn't have a title, use its filename */
const char *t = fios_title;
if (StrEmpty(fios_title)) {
- t = strrchr(filename, PATHSEPCHAR);
- t = (t == nullptr) ? filename : (t + 1);
+ auto ps = filename.rfind(PATHSEPCHAR);
+ t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
}
strecpy(fios->title, t, lastof(fios->title));
str_validate(fios->title, lastof(fios->title));
@@ -433,11 +434,10 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
* @param last the last element in the title buffer
* @param subdir the sub directory to search in
*/
-static void GetFileTitle(const char *file, char *title, const char *last, Subdirectory subdir)
+static void GetFileTitle(const std::string &file, char *title, const char *last, Subdirectory subdir)
{
- char buf[MAX_PATH];
- strecpy(buf, file, lastof(buf));
- strecat(buf, ".title", lastof(buf));
+ std::string buf = file;
+ buf += ".title";
FILE *f = FioFOpenFile(buf, "r", subdir);
if (f == nullptr) return;
@@ -460,7 +460,7 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir
* @see FiosGetFileList
* @see FiosGetSavegameList
*/
-FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
+FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show savegame files
* .SAV OpenTTD saved game
@@ -515,7 +515,7 @@ void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
* @see FiosGetFileList
* @see FiosGetScenarioList
*/
-static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
+static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show scenario files
* .SCN OpenTTD style scenario file
@@ -556,7 +556,7 @@ void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
}
-static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
+static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show heightmap files
* .PNG PNG Based heightmap files
@@ -669,7 +669,7 @@ public:
this->scanned = true;
}
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
{
FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
if (f == nullptr) return false;
@@ -678,19 +678,16 @@ public:
int fret = fscanf(f, "%u", &id.scenid);
FioFCloseFile(f);
if (fret != 1) return false;
- strecpy(id.filename, filename, lastof(id.filename));
+ strecpy(id.filename, filename.c_str(), lastof(id.filename));
Md5 checksum;
uint8 buffer[1024];
- char basename[MAX_PATH]; ///< \a filename without the extension.
size_t len, size;
/* open the scenario file, but first get the name.
* This is safe as we check on extension which
* must always exist. */
- strecpy(basename, filename, lastof(basename));
- *strrchr(basename, '.') = '\0';
- f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
+ f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
if (f == nullptr) return false;
/* calculate md5sum */
diff --git a/src/fios.h b/src/fios.h
index e211396c1..8d8faac07 100644
--- a/src/fios.h
+++ b/src/fios.h
@@ -223,6 +223,6 @@ bool FiosDelete(const char *name);
std::string FiosMakeHeightmapName(const char *name);
std::string FiosMakeSavegameName(const char *name);
-FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last);
+FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last);
#endif /* FIOS_H */
diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp
index f704dbc2e..24f41c93a 100644
--- a/src/game/game_text.cpp
+++ b/src/game/game_text.cpp
@@ -206,7 +206,7 @@ public:
this->FileScanner::Scan(".txt", directory, false);
}
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
{
if (exclude == filename) return true;
@@ -244,9 +244,9 @@ GameStrings *LoadTranslations()
LanguageScanner scanner(gs, filename);
std::string ldir = basename + "lang" PATHSEP;
- const char *tar_filename = info->GetTarFile();
+ const std::string tar_filename = info->GetTarFile();
TarList::iterator iter;
- if (tar_filename != nullptr && (iter = _tar_list[GAME_DIR].find(tar_filename)) != _tar_list[GAME_DIR].end()) {
+ if (!tar_filename.empty() && (iter = _tar_list[GAME_DIR].find(tar_filename)) != _tar_list[GAME_DIR].end()) {
/* The main script is in a tar file, so find all files that
* are in the same tar and add them to the langfile scanner. */
TarFileList::iterator tar;
@@ -258,7 +258,7 @@ GameStrings *LoadTranslations()
if (tar->first.size() <= ldir.size() || tar->first.compare(0, ldir.size(), ldir) != 0) continue;
if (tar->first.compare(tar->first.size() - 4, 4, ".txt") != 0) continue;
- scanner.AddFile(tar->first.c_str(), 0, tar_filename);
+ scanner.AddFile(tar->first, 0, tar_filename);
}
} else {
/* Scan filesystem */
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
index 75c1977f2..5ba54f26c 100644
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -586,7 +586,7 @@ public:
{
}
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
/** Do the scan for GRFs. */
static uint DoScan()
@@ -600,9 +600,9 @@ public:
}
};
-bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
+bool GRFFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
- GRFConfig *c = new GRFConfig(filename + basepath_length);
+ GRFConfig *c = new GRFConfig(filename.c_str() + basepath_length);
bool added = true;
if (FillGRFDetails(c, false)) {
diff --git a/src/openttd.cpp b/src/openttd.cpp
index aa98da726..6315b8fa1 100644
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -628,7 +628,7 @@ int openttd_main(int argc, char *argv[])
/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
auto t = _file_to_saveload.name.find_last_of('.');
if (t != std::string::npos) {
- FiosType ft = FiosGetSavegameListCallback(SLO_LOAD, _file_to_saveload.name.c_str(), _file_to_saveload.name.substr(t).c_str(), nullptr, nullptr);
+ FiosType ft = FiosGetSavegameListCallback(SLO_LOAD, _file_to_saveload.name, _file_to_saveload.name.substr(t).c_str(), nullptr, nullptr);
if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft);
}
diff --git a/src/saveload/oldloader.cpp b/src/saveload/oldloader.cpp
index 09f15e183..42970c9a2 100644
--- a/src/saveload/oldloader.cpp
+++ b/src/saveload/oldloader.cpp
@@ -317,7 +317,7 @@ bool LoadOldSaveGame(const std::string &file)
return true;
}
-void GetOldSaveGameName(const char *file, char *title, const char *last)
+void GetOldSaveGameName(const std::string &file, char *title, const char *last)
{
FILE *f = FioFOpenFile(file, "rb", NO_DIRECTORY);
diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp
index 4eebd548d..d02bbe324 100644
--- a/src/script/script_info.cpp
+++ b/src/script/script_info.cpp
@@ -39,8 +39,6 @@ ScriptInfo::~ScriptInfo()
free(this->date);
free(this->instance_name);
free(this->url);
- free(this->main_script);
- free(this->tar_file);
free(this->SQ_instance);
}
@@ -81,9 +79,8 @@ bool ScriptInfo::CheckMethod(const char *name) const
}
/* Get location information of the scanner */
- info->main_script = stredup(info->scanner->GetMainScript());
- const char *tar_name = info->scanner->GetTarFile();
- if (tar_name != nullptr) info->tar_file = stredup(tar_name);
+ info->main_script = info->scanner->GetMainScript();
+ info->tar_file = info->scanner->GetTarFile();
/* Cache the data the info file gives us. */
if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
diff --git a/src/script/script_info.hpp b/src/script/script_info.hpp
index 5f90e9b23..c2e952b82 100644
--- a/src/script/script_info.hpp
+++ b/src/script/script_info.hpp
@@ -32,8 +32,6 @@ public:
ScriptInfo() :
engine(nullptr),
SQ_instance(nullptr),
- main_script(nullptr),
- tar_file(nullptr),
author(nullptr),
name(nullptr),
short_name(nullptr),
@@ -89,12 +87,12 @@ public:
/**
* Get the filename of the main.nut script.
*/
- const char *GetMainScript() const { return this->main_script; }
+ const char *GetMainScript() const { return this->main_script.c_str(); }
/**
* Get the filename of the tar the script is in.
*/
- const char *GetTarFile() const { return this->tar_file; }
+ std::string GetTarFile() const { return this->tar_file; }
/**
* Check if a given method exists.
@@ -152,8 +150,8 @@ protected:
ScriptConfigItemList config_list; ///< List of settings from this Script.
private:
- char *main_script; ///< The full path of the script.
- char *tar_file; ///< If, which tar file the script was in.
+ std::string main_script; ///< The full path of the script.
+ std::string tar_file; ///< If, which tar file the script was in.
const char *author; ///< Author of the script.
const char *name; ///< Full name of the script.
const char *short_name; ///< Short name (4 chars) which uniquely identifies the script.
diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp
index dc3feb58c..6fa88bfee 100644
--- a/src/script/script_scanner.cpp
+++ b/src/script/script_scanner.cpp
@@ -23,47 +23,29 @@
#include "../safeguards.h"
-bool ScriptScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
+bool ScriptScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
- free(this->main_script);
- this->main_script = stredup(filename);
- if (this->main_script == nullptr) return false;
-
- free(this->tar_file);
- if (tar_filename != nullptr) {
- this->tar_file = stredup(tar_filename);
- if (this->tar_file == nullptr) return false;
- } else {
- this->tar_file = nullptr;
- }
-
- const char *end = this->main_script + strlen(this->main_script) + 1;
- char *p = strrchr(this->main_script, PATHSEPCHAR);
- if (p == nullptr) {
- p = this->main_script;
- } else {
- /* Skip over the path separator character. We don't need that. */
- p++;
- }
+ this->main_script = filename;
+ this->tar_file = tar_filename;
- strecpy(p, "main.nut", end);
+ auto p = this->main_script.rfind(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();
try {
- this->engine->LoadScript(filename);
+ this->engine->LoadScript(filename.c_str());
} catch (Script_FatalError &e) {
- DEBUG(script, 0, "Fatal error '%s' when trying to load the script '%s'.", e.GetErrorMessage(), filename);
+ DEBUG(script, 0, "Fatal error '%s' when trying to load the script '%s'.", e.GetErrorMessage(), filename.c_str());
return false;
}
return true;
}
ScriptScanner::ScriptScanner() :
- engine(nullptr),
- main_script(nullptr),
- tar_file(nullptr)
+ engine(nullptr)
{
}
@@ -87,8 +69,6 @@ ScriptScanner::~ScriptScanner()
{
this->Reset();
- free(this->main_script);
- free(this->tar_file);
delete this->engine;
}
@@ -194,7 +174,7 @@ struct ScriptFileChecksumCreator : FileScanner {
}
/* Add the file and calculate the md5 sum. */
- virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
+ virtual bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
Md5 checksum;
uint8 buffer[1024];
@@ -202,7 +182,7 @@ struct ScriptFileChecksumCreator : FileScanner {
byte tmp_md5sum[16];
/* Open the file ... */
- FILE *f = FioFOpenFile(filename, "rb", this->dir, &size);
+ FILE *f = FioFOpenFile(filename.c_str(), "rb", this->dir, &size);
if (f == nullptr) return false;
/* ... calculate md5sum... */
@@ -239,9 +219,9 @@ static bool IsSameScript(const ContentInfo *ci, bool md5sum, ScriptInfo *info, S
if (!md5sum) return true;
ScriptFileChecksumCreator checksum(dir);
- const char *tar_filename = info->GetTarFile();
+ auto tar_filename = info->GetTarFile();
TarList::iterator iter;
- if (tar_filename != nullptr && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
+ if (!tar_filename.empty() && (iter = _tar_list[dir].find(tar_filename)) != _tar_list[dir].end()) {
/* The main script is in a tar file, so find all files that
* are in the same tar and add them to the MD5 checksumming. */
TarFileList::iterator tar;
@@ -253,7 +233,7 @@ static bool IsSameScript(const ContentInfo *ci, bool md5sum, ScriptInfo *info, S
const char *ext = strrchr(tar->first.c_str(), '.');
if (ext == nullptr || strcasecmp(ext, ".nut") != 0) continue;
- checksum.AddFile(tar->first.c_str(), 0, tar_filename);
+ checksum.AddFile(tar->first, 0, tar_filename);
}
} else {
char path[MAX_PATH];
diff --git a/src/script/script_scanner.hpp b/src/script/script_scanner.hpp
index eb65a197e..ca9068ca1 100644
--- a/src/script/script_scanner.hpp
+++ b/src/script/script_scanner.hpp
@@ -32,12 +32,12 @@ public:
/**
* Get the current main script the ScanDir is currently tracking.
*/
- const char *GetMainScript() { return this->main_script; }
+ std::string GetMainScript() { return this->main_script; }
/**
* Get the current tar file the ScanDir is currently tracking.
*/
- const char *GetTarFile() { return this->tar_file; }
+ std::string GetTarFile() { return this->tar_file; }
/**
* Get the list of all registered scripts.
@@ -75,7 +75,7 @@ public:
*/
const char *FindMainScript(const ContentInfo *ci, bool md5sum);
- bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
+ bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
/**
* Rescan the script dir.
@@ -83,9 +83,9 @@ public:
void RescanDir();
protected:
- class Squirrel *engine; ///< The engine we're scanning with.
- char *main_script; ///< The full path of the script.
- char *tar_file; ///< If, which tar file the script was in.
+ class Squirrel *engine; ///< The engine we're scanning with.
+ std::string main_script; ///< The full path of the script.
+ std::string tar_file; ///< If, which tar file the script was in.
ScriptInfoList info_list; ///< The list of all script.
ScriptInfoList info_single_list; ///< The list of all unique script. The best script (highest version) is shown.
diff --git a/src/tar_type.h b/src/tar_type.h
index e428bc249..c4e72df85 100644
--- a/src/tar_type.h
+++ b/src/tar_type.h
@@ -12,30 +12,20 @@
#include <map>
#include <string>
+#include <array>
#include "fileio_type.h"
-/** The define of a TarList. */
-struct TarListEntry {
- const char *filename;
- const char *dirname;
-
- /* MSVC goes copying around this struct after initialisation, so it tries
- * to free filename, which isn't set at that moment... but because it
- * initializes the variable with garbage, it's going to segfault. */
- TarListEntry() : filename(nullptr), dirname(nullptr) {}
- ~TarListEntry() { free(this->filename); free(this->dirname); }
-};
struct TarFileListEntry {
- const char *tar_filename;
+ std::string tar_filename;
size_t size;
size_t position;
};
-typedef std::map<std::string, TarListEntry> TarList;
+typedef std::map<std::string, std::string> TarList; ///< Map of tar file to tar directory.
typedef std::map<std::string, TarFileListEntry> TarFileList;
-extern TarList _tar_list[NUM_SUBDIRS];
+extern std::array<TarList, NUM_SUBDIRS> _tar_list;
extern TarFileList _tar_filelist[NUM_SUBDIRS];
#define FOR_ALL_TARS(tar, sd) for (tar = _tar_filelist[sd].begin(); tar != _tar_filelist[sd].end(); tar++)