1
0
Fork 0

Codechange: Keep filenames of loaded Fio files in std::strings.

pull/8443/head
Michael Lutz 2020-12-06 21:11:49 +01:00
parent 024a3f6259
commit 358056ec42
6 changed files with 22 additions and 24 deletions

View File

@ -40,13 +40,13 @@
/** Structure for keeping several open files with just one data buffer. */ /** Structure for keeping several open files with just one data buffer. */
struct Fio { struct Fio {
byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
size_t pos; ///< current (system) position in file size_t pos; ///< current (system) position in file
FILE *cur_fh; ///< current file handle FILE *cur_fh; ///< current file handle
const char *filename; ///< current filename std::string filename; ///< current filename
FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open std::array<FILE *, MAX_FILE_SLOTS> handles; ///< array of file handles we can have open
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file std::array<std::string, MAX_FILE_SLOTS> filenames; ///< array of filenames we (should) have open
const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open std::array<std::string, MAX_FILE_SLOTS> shortnames;///< array of short names for spriteloader's use
char *shortnames[MAX_FILE_SLOTS]; ///< array of short names for spriteloader's use
}; };
static Fio _fio; ///< #Fio instance. static Fio _fio; ///< #Fio instance.
@ -73,7 +73,7 @@ size_t FioGetPos()
*/ */
const char *FioGetFilename(uint8 slot) const char *FioGetFilename(uint8 slot)
{ {
return _fio.shortnames[slot]; return _fio.shortnames[slot].c_str();
} }
/** /**
@ -87,7 +87,7 @@ void FioSeekTo(size_t pos, int mode)
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; _fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE;
_fio.pos = pos; _fio.pos = pos;
if (fseek(_fio.cur_fh, _fio.pos, SEEK_SET) < 0) { if (fseek(_fio.cur_fh, _fio.pos, SEEK_SET) < 0) {
DEBUG(misc, 0, "Seeking in %s failed", _fio.filename); DEBUG(misc, 0, "Seeking in %s failed", _fio.filename.c_str());
} }
} }
@ -178,8 +178,7 @@ static inline void FioCloseFile(int slot)
if (_fio.handles[slot] != nullptr) { if (_fio.handles[slot] != nullptr) {
fclose(_fio.handles[slot]); fclose(_fio.handles[slot]);
free(_fio.shortnames[slot]); _fio.shortnames[slot].clear();
_fio.shortnames[slot] = nullptr;
_fio.handles[slot] = nullptr; _fio.handles[slot] = nullptr;
} }
@ -199,27 +198,26 @@ void FioCloseAll()
* @param filename Name of the file at the disk. * @param filename Name of the file at the disk.
* @param subdir The sub directory to search this file in. * @param subdir The sub directory to search this file in.
*/ */
void FioOpenFile(int slot, const char *filename, Subdirectory subdir) void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir)
{ {
FILE *f; FILE *f;
f = FioFOpenFile(filename, "rb", subdir); f = FioFOpenFile(filename, "rb", subdir);
if (f == nullptr) usererror("Cannot open file '%s'", filename); if (f == nullptr) usererror("Cannot open file '%s'", filename.c_str());
long pos = ftell(f); long pos = ftell(f);
if (pos < 0) usererror("Cannot read file '%s'", filename); if (pos < 0) usererror("Cannot read file '%s'", filename.c_str());
FioCloseFile(slot); // if file was opened before, close it FioCloseFile(slot); // if file was opened before, close it
_fio.handles[slot] = f; _fio.handles[slot] = f;
_fio.filenames[slot] = filename; _fio.filenames[slot] = filename;
/* Store the filename without path and extension */ /* Store the filename without path and extension */
const char *t = strrchr(filename, PATHSEPCHAR); auto t = filename.rfind(PATHSEPCHAR);
_fio.shortnames[slot] = stredup(t == nullptr ? filename : t); std::string sn = filename.substr(t != std::string::npos ? t + 1 : 0);
char *t2 = strrchr(_fio.shortnames[slot], '.'); _fio.shortnames[slot] = sn.substr(0, sn.rfind('.'));
if (t2 != nullptr) *t2 = '\0';
strtolower(_fio.shortnames[slot]); strtolower(_fio.shortnames[slot]);
FioSeekToFile(slot, (uint32)pos); FioSeekToFile(slot, (size_t)pos);
} }
static const char * const _subdirs[] = { static const char * const _subdirs[] = {

View File

@ -22,7 +22,7 @@ byte FioReadByte();
uint16 FioReadWord(); uint16 FioReadWord();
uint32 FioReadDword(); uint32 FioReadDword();
void FioCloseAll(); void FioCloseAll();
void FioOpenFile(int slot, const char *filename, Subdirectory subdir); void FioOpenFile(int slot, const std::string &filename, Subdirectory subdir);
void FioReadBlock(void *ptr, size_t size); void FioReadBlock(void *ptr, size_t size);
void FioSkipBytes(int n); void FioSkipBytes(int n);

View File

@ -125,7 +125,7 @@ bool IniFile::SaveToDisk(const std::string &filename)
return true; return true;
} }
/* virtual */ FILE *IniFile::OpenFile(const char *filename, Subdirectory subdir, size_t *size) /* virtual */ FILE *IniFile::OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
{ {
/* Open the text file in binary mode to prevent end-of-line translations /* Open the text file in binary mode to prevent end-of-line translations
* done by ftell() and friends, as defined by K&R. */ * done by ftell() and friends, as defined by K&R. */

View File

@ -204,7 +204,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
uint comment_alloc = 0; uint comment_alloc = 0;
size_t end; size_t end;
FILE *in = this->OpenFile(filename.c_str(), subdir, &end); FILE *in = this->OpenFile(filename, subdir, &end);
if (in == nullptr) return; if (in == nullptr) return;
end += ftell(in); end += ftell(in);

View File

@ -73,7 +73,7 @@ struct IniLoadFile {
* @param[out] size Size of the opened file. * @param[out] size Size of the opened file.
* @return File handle of the opened file, or \c nullptr. * @return File handle of the opened file, or \c nullptr.
*/ */
virtual FILE *OpenFile(const char *filename, Subdirectory subdir, size_t *size) = 0; virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) = 0;
/** /**
* Report an error about the file contents. * Report an error about the file contents.
@ -90,7 +90,7 @@ struct IniFile : IniLoadFile {
bool SaveToDisk(const std::string &filename); bool SaveToDisk(const std::string &filename);
virtual FILE *OpenFile(const char *filename, Subdirectory subdir, size_t *size); virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size);
virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post); virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post);
}; };

View File

@ -165,11 +165,11 @@ struct SettingsIniFile : IniLoadFile {
{ {
} }
virtual FILE *OpenFile(const char *filename, Subdirectory subdir, size_t *size) virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
{ {
/* Open the text file in binary mode to prevent end-of-line translations /* Open the text file in binary mode to prevent end-of-line translations
* done by ftell() and friends, as defined by K&R. */ * done by ftell() and friends, as defined by K&R. */
FILE *in = fopen(filename, "rb"); FILE *in = fopen(filename.c_str(), "rb");
if (in == nullptr) return nullptr; if (in == nullptr) return nullptr;
fseek(in, 0L, SEEK_END); fseek(in, 0L, SEEK_END);