summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/fileio.cpp32
-rw-r--r--src/fileio_func.h2
-rw-r--r--src/ini.cpp2
-rw-r--r--src/ini_load.cpp2
-rw-r--r--src/ini_type.h4
-rw-r--r--src/settingsgen/settingsgen.cpp4
6 files changed, 22 insertions, 24 deletions
diff --git a/src/fileio.cpp b/src/fileio.cpp
index 5431df814..7df22921e 100644
--- a/src/fileio.cpp
+++ b/src/fileio.cpp
@@ -40,13 +40,13 @@
/** Structure for keeping several open files with just one data buffer. */
struct Fio {
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
FILE *cur_fh; ///< current file handle
- const char *filename; ///< current filename
- FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open
- byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
- const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
- char *shortnames[MAX_FILE_SLOTS]; ///< array of short names for spriteloader's use
+ std::string filename; ///< current filename
+ std::array<FILE *, MAX_FILE_SLOTS> handles; ///< array of file handles we can have open
+ std::array<std::string, MAX_FILE_SLOTS> filenames; ///< array of filenames we (should) have open
+ std::array<std::string, MAX_FILE_SLOTS> shortnames;///< array of short names for spriteloader's use
};
static Fio _fio; ///< #Fio instance.
@@ -73,7 +73,7 @@ size_t FioGetPos()
*/
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.pos = pos;
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) {
fclose(_fio.handles[slot]);
- free(_fio.shortnames[slot]);
- _fio.shortnames[slot] = nullptr;
+ _fio.shortnames[slot].clear();
_fio.handles[slot] = nullptr;
}
@@ -199,27 +198,26 @@ void FioCloseAll()
* @param filename Name of the file at the disk.
* @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;
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);
- 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
_fio.handles[slot] = f;
_fio.filenames[slot] = filename;
/* Store the filename without path and extension */
- const char *t = strrchr(filename, PATHSEPCHAR);
- _fio.shortnames[slot] = stredup(t == nullptr ? filename : t);
- char *t2 = strrchr(_fio.shortnames[slot], '.');
- if (t2 != nullptr) *t2 = '\0';
+ auto t = filename.rfind(PATHSEPCHAR);
+ std::string sn = filename.substr(t != std::string::npos ? t + 1 : 0);
+ _fio.shortnames[slot] = sn.substr(0, sn.rfind('.'));
strtolower(_fio.shortnames[slot]);
- FioSeekToFile(slot, (uint32)pos);
+ FioSeekToFile(slot, (size_t)pos);
}
static const char * const _subdirs[] = {
diff --git a/src/fileio_func.h b/src/fileio_func.h
index 72d461863..014050b4b 100644
--- a/src/fileio_func.h
+++ b/src/fileio_func.h
@@ -22,7 +22,7 @@ byte FioReadByte();
uint16 FioReadWord();
uint32 FioReadDword();
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 FioSkipBytes(int n);
diff --git a/src/ini.cpp b/src/ini.cpp
index 036ced668..e70cc268a 100644
--- a/src/ini.cpp
+++ b/src/ini.cpp
@@ -125,7 +125,7 @@ bool IniFile::SaveToDisk(const std::string &filename)
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
* done by ftell() and friends, as defined by K&R. */
diff --git a/src/ini_load.cpp b/src/ini_load.cpp
index e7c2b95fe..022e2bf18 100644
--- a/src/ini_load.cpp
+++ b/src/ini_load.cpp
@@ -204,7 +204,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
uint comment_alloc = 0;
size_t end;
- FILE *in = this->OpenFile(filename.c_str(), subdir, &end);
+ FILE *in = this->OpenFile(filename, subdir, &end);
if (in == nullptr) return;
end += ftell(in);
diff --git a/src/ini_type.h b/src/ini_type.h
index 163717450..160ba7619 100644
--- a/src/ini_type.h
+++ b/src/ini_type.h
@@ -73,7 +73,7 @@ struct IniLoadFile {
* @param[out] size Size of the opened file.
* @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.
@@ -90,7 +90,7 @@ struct IniFile : IniLoadFile {
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);
};
diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp
index b2a3c76f6..513b4c754 100644
--- a/src/settingsgen/settingsgen.cpp
+++ b/src/settingsgen/settingsgen.cpp
@@ -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
* 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;
fseek(in, 0L, SEEK_END);