1
0
Fork 0

Codechange: Even more std::string usage in file IO.

pull/8443/head
Michael Lutz 2020-12-06 21:11:47 +01:00
parent 65f65ad2ad
commit 5cbb2da794
9 changed files with 48 additions and 63 deletions

View File

@ -272,7 +272,7 @@ bool IsValidSearchPath(Searchpath sp)
*/ */
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir) bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
{ {
FILE *f = FioFOpenFile(filename.c_str(), "rb", subdir); FILE *f = FioFOpenFile(filename, "rb", subdir);
if (f == nullptr) return false; if (f == nullptr) return false;
FioFCloseFile(f); FioFCloseFile(f);
@ -345,7 +345,7 @@ std::string FioFindDirectory(Subdirectory subdir)
return _personal_dir; return _personal_dir;
} }
static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) static FILE *FioFOpenFileSp(const std::string &filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
{ {
#if defined(_WIN32) && defined(UNICODE) #if defined(_WIN32) && defined(UNICODE)
/* fopen is implemented as a define with ellipses for /* fopen is implemented as a define with ellipses for
@ -390,17 +390,17 @@ static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath s
* @return File handle of the opened file, or \c nullptr if the file is not available. * @return File handle of the opened file, or \c nullptr if the file is not available.
* @note The file is read from within the tar file, and may not return \c EOF after reading the whole file. * @note The file is read from within the tar file, and may not return \c EOF after reading the whole file.
*/ */
FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize) FILE *FioFOpenFileTar(const TarFileListEntry &entry, size_t *filesize)
{ {
FILE *f = fopen(entry->tar_filename, "rb"); FILE *f = fopen(entry.tar_filename, "rb");
if (f == nullptr) return f; if (f == nullptr) return f;
if (fseek(f, entry->position, SEEK_SET) < 0) { if (fseek(f, entry.position, SEEK_SET) < 0) {
fclose(f); fclose(f);
return nullptr; return nullptr;
} }
if (filesize != nullptr) *filesize = entry->size; if (filesize != nullptr) *filesize = entry.size;
return f; return f;
} }
@ -410,7 +410,7 @@ FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize)
* @param subdir Subdirectory to open. * @param subdir Subdirectory to open.
* @return File handle of the opened file, or \c nullptr if the file is not available. * @return File handle of the opened file, or \c nullptr if the file is not available.
*/ */
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize) FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
{ {
FILE *f = nullptr; FILE *f = nullptr;
Searchpath sp; Searchpath sp;
@ -424,11 +424,8 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir,
/* We can only use .tar in case of data-dir, and read-mode */ /* We can only use .tar in case of data-dir, and read-mode */
if (f == nullptr && mode[0] == 'r' && subdir != NO_DIRECTORY) { if (f == nullptr && mode[0] == 'r' && subdir != NO_DIRECTORY) {
static const uint MAX_RESOLVED_LENGTH = 2 * (100 + 100 + 155) + 1; // Enough space to hold two filenames plus link. See 'TarHeader'.
char resolved_name[MAX_RESOLVED_LENGTH];
/* Filenames in tars are always forced to be lowercase */ /* Filenames in tars are always forced to be lowercase */
strecpy(resolved_name, filename, lastof(resolved_name)); std::string resolved_name = filename;
strtolower(resolved_name); strtolower(resolved_name);
/* Resolve ".." */ /* Resolve ".." */
@ -443,36 +440,31 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir,
tokens.push_back(token); tokens.push_back(token);
} }
} }
resolved_name[0] = '\0';
resolved_name.clear();
bool first = true; bool first = true;
for (const std::string &token : tokens) { for (const std::string &token : tokens) {
if (!first) { if (!first) {
strecat(resolved_name, PATHSEP, lastof(resolved_name)); resolved_name += PATHSEP;
} }
strecat(resolved_name, token.c_str(), lastof(resolved_name)); resolved_name += token;
first = false; first = false;
} }
size_t resolved_len = strlen(resolved_name);
/* Resolve ONE directory link */ /* Resolve ONE directory link */
for (TarLinkList::iterator link = _tar_linklist[subdir].begin(); link != _tar_linklist[subdir].end(); link++) { for (TarLinkList::iterator link = _tar_linklist[subdir].begin(); link != _tar_linklist[subdir].end(); link++) {
const std::string &src = link->first; const std::string &src = link->first;
size_t len = src.length(); size_t len = src.length();
if (resolved_len >= len && resolved_name[len - 1] == PATHSEPCHAR && strncmp(src.c_str(), resolved_name, len) == 0) { if (resolved_name.length() >= len && resolved_name[len - 1] == PATHSEPCHAR && src.compare(0, len, resolved_name, 0, len) == 0) {
/* Apply link */ /* Apply link */
char resolved_name2[MAX_RESOLVED_LENGTH]; resolved_name.replace(0, len, link->second);
const std::string &dest = link->second;
strecpy(resolved_name2, &(resolved_name[len]), lastof(resolved_name2));
strecpy(resolved_name, dest.c_str(), lastof(resolved_name));
strecpy(&(resolved_name[dest.length()]), resolved_name2, lastof(resolved_name));
break; // Only resolve one level break; // Only resolve one level
} }
} }
TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name); TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name);
if (it != _tar_filelist[subdir].end()) { if (it != _tar_filelist[subdir].end()) {
f = FioFOpenFileTar(&((*it).second), filesize); f = FioFOpenFileTar(it->second, filesize);
} }
} }
@ -859,7 +851,7 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha
* @param subdir The sub directory the tar is in. * @param subdir The sub directory the tar is in.
* @return false on failure. * @return false on failure.
*/ */
bool ExtractTar(const char *tar_filename, Subdirectory subdir) bool ExtractTar(const std::string &tar_filename, Subdirectory subdir)
{ {
TarList::iterator it = _tar_list[subdir].find(tar_filename); TarList::iterator it = _tar_list[subdir].find(tar_filename);
/* We don't know the file. */ /* We don't know the file. */
@ -869,41 +861,38 @@ bool ExtractTar(const char *tar_filename, Subdirectory subdir)
/* The file doesn't have a sub directory! */ /* The file doesn't have a sub directory! */
if (dirname == nullptr) { if (dirname == nullptr) {
DEBUG(misc, 1, "Extracting %s failed; archive rejected, the contents must be in a sub directory", tar_filename); DEBUG(misc, 1, "Extracting %s failed; archive rejected, the contents must be in a sub directory", tar_filename.c_str());
return false; return false;
} }
char filename[MAX_PATH]; std::string filename = tar_filename;
strecpy(filename, tar_filename, lastof(filename)); auto p = filename.find_last_of(PATHSEPCHAR);
char *p = strrchr(filename, PATHSEPCHAR);
/* The file's path does not have a separator? */ /* The file's path does not have a separator? */
if (p == nullptr) return false; if (p == std::string::npos) return false;
p++; filename.replace(p + 1, std::string::npos, dirname);
strecpy(p, dirname, lastof(filename)); DEBUG(misc, 8, "Extracting %s to directory %s", tar_filename.c_str(), filename.c_str());
DEBUG(misc, 8, "Extracting %s to directory %s", tar_filename, filename);
FioCreateDirectory(filename); FioCreateDirectory(filename);
for (TarFileList::iterator it2 = _tar_filelist[subdir].begin(); it2 != _tar_filelist[subdir].end(); it2++) { for (TarFileList::iterator it2 = _tar_filelist[subdir].begin(); it2 != _tar_filelist[subdir].end(); it2++) {
if (strcmp((*it2).second.tar_filename, tar_filename) != 0) continue; if (tar_filename != it2->second.tar_filename) continue;
strecpy(p, (*it2).first.c_str(), lastof(filename)); filename.replace(p + 1, std::string::npos, it2->first);
DEBUG(misc, 9, " extracting %s", filename); DEBUG(misc, 9, " extracting %s", filename.c_str());
/* First open the file in the .tar. */ /* First open the file in the .tar. */
size_t to_copy = 0; size_t to_copy = 0;
FILE *in = FioFOpenFileTar(&(*it2).second, &to_copy); std::unique_ptr<FILE, FileDeleter> in(FioFOpenFileTar(it2->second, &to_copy));
if (in == nullptr) { if (!in) {
DEBUG(misc, 6, "Extracting %s failed; could not open %s", filename, tar_filename); DEBUG(misc, 6, "Extracting %s failed; could not open %s", filename.c_str(), tar_filename.c_str());
return false; return false;
} }
/* Now open the 'output' file. */ /* Now open the 'output' file. */
FILE *out = fopen(filename, "wb"); std::unique_ptr<FILE, FileDeleter> out(fopen(filename.c_str(), "wb"));
if (out == nullptr) { if (!out) {
DEBUG(misc, 6, "Extracting %s failed; could not open %s", filename, filename); DEBUG(misc, 6, "Extracting %s failed; could not open %s", filename.c_str(), filename.c_str());
fclose(in);
return false; return false;
} }
@ -911,16 +900,12 @@ bool ExtractTar(const char *tar_filename, Subdirectory subdir)
char buffer[4096]; char buffer[4096];
size_t read; size_t read;
for (; to_copy != 0; to_copy -= read) { for (; to_copy != 0; to_copy -= read) {
read = fread(buffer, 1, min(to_copy, lengthof(buffer)), in); read = fread(buffer, 1, min(to_copy, lengthof(buffer)), in.get());
if (read <= 0 || fwrite(buffer, 1, read, out) != read) break; if (read <= 0 || fwrite(buffer, 1, read, out.get()) != read) break;
} }
/* Close everything up. */
fclose(in);
fclose(out);
if (to_copy != 0) { if (to_copy != 0) {
DEBUG(misc, 6, "Extracting %s failed; still %i bytes to copy", filename, (int)to_copy); DEBUG(misc, 6, "Extracting %s failed; still %i bytes to copy", filename.c_str(), (int)to_copy);
return false; return false;
} }
} }

View File

@ -37,7 +37,7 @@ bool IsValidSearchPath(Searchpath sp);
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp)) #define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
void FioFCloseFile(FILE *f); void FioFCloseFile(FILE *f);
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir); bool FioCheckFileExists(const std::string &filename, Subdirectory subdir);
std::string FioFindFullPath(Subdirectory subdir, const char *filename); std::string FioFindFullPath(Subdirectory subdir, const char *filename);
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir); std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
@ -51,7 +51,7 @@ void AppendPathSeparator(std::string &buf);
void DeterminePaths(const char *exe); void DeterminePaths(const char *exe);
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize); void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
bool FileExists(const std::string &filename); bool FileExists(const std::string &filename);
bool ExtractTar(const char *tar_filename, Subdirectory subdir); bool ExtractTar(const std::string &tar_filename, Subdirectory subdir);
extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc. extern std::string _personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.

View File

@ -66,7 +66,7 @@ void NORETURN CDECL strgen_fatal(const char *s, ...)
LanguageStrings ReadRawLanguageStrings(const std::string &file) LanguageStrings ReadRawLanguageStrings(const std::string &file)
{ {
size_t to_read; size_t to_read;
FILE *fh = FioFOpenFile(file.c_str(), "rb", GAME_DIR, &to_read); FILE *fh = FioFOpenFile(file, "rb", GAME_DIR, &to_read);
if (fh == nullptr) return LanguageStrings(); if (fh == nullptr) return LanguageStrings();
FileCloser fhClose(fh); FileCloser fhClose(fh);

View File

@ -543,7 +543,7 @@ void ClientNetworkContentSocketHandler::AfterDownload()
if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) { if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
/* Music can't be in a tar. So extract the tar! */ /* Music can't be in a tar. So extract the tar! */
ExtractTar(fname.c_str(), BASESET_DIR); ExtractTar(fname, BASESET_DIR);
unlink(fname.c_str()); unlink(fname.c_str());
} }

View File

@ -102,7 +102,7 @@ uint32 NewGRFProfiler::Finish()
std::string filename = this->GetOutputFilename(); std::string filename = this->GetOutputFilename();
IConsolePrintF(CC_DEBUG, "Finished profile of NewGRF [%08X], writing %u events to %s", BSWAP32(this->grffile->grfid), (uint)this->calls.size(), filename.c_str()); IConsolePrintF(CC_DEBUG, "Finished profile of NewGRF [%08X], writing %u events to %s", BSWAP32(this->grffile->grfid), (uint)this->calls.size(), filename.c_str());
FILE *f = FioFOpenFile(filename.c_str(), "wt", Subdirectory::NO_DIRECTORY); FILE *f = FioFOpenFile(filename, "wt", Subdirectory::NO_DIRECTORY);
FileCloser fcloser(f); FileCloser fcloser(f);
uint32 total_microseconds = 0; uint32 total_microseconds = 0;

View File

@ -968,7 +968,7 @@ bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileTy
_game_mode = newgm; _game_mode = newgm;
switch (lf == nullptr ? SaveOrLoad(filename.c_str(), fop, dft, subdir) : LoadWithFilter(lf)) { switch (lf == nullptr ? SaveOrLoad(filename, fop, dft, subdir) : LoadWithFilter(lf)) {
case SL_OK: return true; case SL_OK: return true;
case SL_REINIT: case SL_REINIT:
@ -1127,7 +1127,7 @@ void SwitchToMode(SwitchMode new_mode)
case SM_SAVE_GAME: // Save game. case SM_SAVE_GAME: // Save game.
/* Make network saved games on pause compatible to singleplayer */ /* Make network saved games on pause compatible to singleplayer */
if (SaveOrLoad(_file_to_saveload.name.c_str(), SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) { if (SaveOrLoad(_file_to_saveload.name, SLO_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) {
SetDParamStr(0, GetSaveLoadErrorString()); SetDParamStr(0, GetSaveLoadErrorString());
ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
} else { } else {

View File

@ -271,7 +271,7 @@ static SavegameType DetermineOldSavegameType(FILE *f, char *title, const char *l
typedef bool LoadOldMainProc(LoadgameState *ls); typedef bool LoadOldMainProc(LoadgameState *ls);
bool LoadOldSaveGame(const char *file) bool LoadOldSaveGame(const std::string &file)
{ {
LoadgameState ls; LoadgameState ls;
@ -283,7 +283,7 @@ bool LoadOldSaveGame(const char *file)
ls.file = FioFOpenFile(file, "rb", NO_DIRECTORY); ls.file = FioFOpenFile(file, "rb", NO_DIRECTORY);
if (ls.file == nullptr) { if (ls.file == nullptr) {
DEBUG(oldloader, 0, "Cannot open file '%s'", file); DEBUG(oldloader, 0, "Cannot open file '%s'", file.c_str());
return false; return false;
} }

View File

@ -2413,7 +2413,7 @@ static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
/* actual loader/saver function */ /* actual loader/saver function */
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings); void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
extern bool AfterLoadGame(); extern bool AfterLoadGame();
extern bool LoadOldSaveGame(const char *file); extern bool LoadOldSaveGame(const std::string &file);
/** /**
* Clear temporary data that is passed between various saveload phases. * Clear temporary data that is passed between various saveload phases.
@ -2769,7 +2769,7 @@ SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
* @param threaded True when threaded saving is allowed * @param threaded True when threaded saving is allowed
* @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game) * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
*/ */
SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded) SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
{ {
/* An instance of saving is already active, so don't go saving again */ /* An instance of saving is already active, so don't go saving again */
if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) { if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
@ -2833,7 +2833,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, Detaile
} }
if (fop == SLO_SAVE) { // SAVE game if (fop == SLO_SAVE) { // SAVE game
DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename); DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename.c_str());
if (_network_server || !_settings_client.gui.threaded_saves) threaded = false; if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
return DoSave(new FileWriter(fh), threaded); return DoSave(new FileWriter(fh), threaded);
@ -2841,7 +2841,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, Detaile
/* LOAD game */ /* LOAD game */
assert(fop == SLO_LOAD || fop == SLO_CHECK); assert(fop == SLO_LOAD || fop == SLO_CHECK);
DEBUG(desync, 1, "load: %s", filename); DEBUG(desync, 1, "load: %s", filename.c_str());
return DoLoad(new FileReader(fh), fop == SLO_CHECK); return DoLoad(new FileReader(fh), fop == SLO_CHECK);
} catch (...) { } catch (...) {
/* This code may be executed both for old and new save games. */ /* This code may be executed both for old and new save games. */

View File

@ -362,7 +362,7 @@ extern FileToSaveLoad _file_to_saveload;
void GenerateDefaultSaveName(char *buf, const char *last); void GenerateDefaultSaveName(char *buf, const char *last);
void SetSaveLoadError(StringID str); void SetSaveLoadError(StringID str);
const char *GetSaveLoadErrorString(); const char *GetSaveLoadErrorString();
SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true); SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true);
void WaitTillSaved(); void WaitTillSaved();
void ProcessAsyncSaveFinish(); void ProcessAsyncSaveFinish();
void DoExitSave(); void DoExitSave();