diff --git a/src/base_media_base.h b/src/base_media_base.h --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -31,9 +31,9 @@ struct MD5File { CR_NO_FILE, ///< The file did not exist }; - const char *filename; ///< filename + std::string filename; ///< filename uint8 hash[16]; ///< md5 sum of the file - const char *missing_warning; ///< warning when this file is missing + std::string missing_warning; ///< warning when this file is missing ChecksumResult check_result; ///< cached result of md5 check ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const; @@ -73,11 +73,6 @@ struct BaseSet { /** Free everything we allocated */ ~BaseSet() { - for (uint i = 0; i < NUM_FILES; i++) { - free(this->files[i].filename); - free(this->files[i].missing_warning); - } - delete this->next; } @@ -100,7 +95,7 @@ struct BaseSet { return Tnum_files - this->valid_files; } - bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename = true); + bool FillSetDetails(IniFile *ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename = true); /** * Get the description for the given ISO code. @@ -147,7 +142,7 @@ struct BaseSet { const char *GetTextfile(TextfileType type) const { for (uint i = 0; i < NUM_FILES; i++) { - const char *textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename); + const char *textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename.c_str()); if (textfile != nullptr) { return textfile; } @@ -249,7 +244,7 @@ struct GraphicsSet : BaseSet -bool BaseSet::FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename) +bool BaseSet::FillSetDetails(IniFile *ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename) { IniGroup *metadata = ini->GetGroup("metadata"); IniItem *item; @@ -81,15 +81,15 @@ bool BaseSetvalue.has_value()) { - file->filename = nullptr; + file->filename.clear(); /* If we list no file, that file must be valid */ this->valid_files++; this->found_files++; continue; } - const char *filename = item->value->c_str(); - file->filename = str_fmt("%s%s", path, filename); + const std::string &filename = item->value.value(); + file->filename = path + filename; /* Then find the MD5 checksum */ item = md5s->GetItem(filename, false); @@ -122,9 +122,9 @@ bool BaseSetGetItem("default", false); if (item == nullptr || !item->value.has_value()) { Debug(grf, 1, "No origin warning message specified for: {}", filename); - file->missing_warning = stredup(""); + file->missing_warning.clear(); } else { - file->missing_warning = stredup(item->value->c_str()); + file->missing_warning = item->value.value(); } file->check_result = T::CheckMD5(file, BASESET_DIR); @@ -169,7 +169,7 @@ bool BaseMedia::AddFile(const path.clear(); } - if (set->FillSetDetails(ini, path.c_str(), filename.c_str())) { + if (set->FillSetDetails(ini, path, filename)) { Tbase_set *duplicate = nullptr; for (Tbase_set *c = BaseMedia::available_sets; c != nullptr; c = c->next) { if (c->name == set->name || c->shortname == set->shortname) { @@ -282,7 +282,7 @@ template const char *T if (s->GetNumMissing() != 0) continue; if (s->shortname != ci->unique_id) continue; - if (!md5sum) return s->files[0].filename; + if (!md5sum) return s->files[0].filename.c_str(); byte md5[16]; memset(md5, 0, sizeof(md5)); @@ -291,7 +291,7 @@ template const char *T md5[j] ^= s->files[i].hash[j]; } } - if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return s->files[0].filename; + if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return s->files[0].filename.c_str(); } return nullptr; } diff --git a/src/fileio.cpp b/src/fileio.cpp --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -140,7 +140,7 @@ void FioFCloseFile(FILE *f) * @param filename Filename to look for. * @return String containing the path if the path was found, else an empty string. */ -std::string FioFindFullPath(Subdirectory subdir, const char *filename) +std::string FioFindFullPath(Subdirectory subdir, const std::string &filename) { assert(subdir < NUM_SUBDIRS); diff --git a/src/fileio_func.h b/src/fileio_func.h --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -18,7 +18,7 @@ void FioFCloseFile(FILE *f); FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); bool FioCheckFileExists(const std::string &filename, Subdirectory subdir); -std::string FioFindFullPath(Subdirectory subdir, const char *filename); +std::string FioFindFullPath(Subdirectory subdir, const std::string &filename); std::string FioGetDirectory(Searchpath sp, Subdirectory subdir); std::string FioFindDirectory(Subdirectory subdir); void FioCreateDirectory(const std::string &name); diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -42,7 +42,7 @@ static const SpriteID * const _landscape * @param needs_palette_remap Whether the colours in the GRF file need a palette remap. * @return The number of loaded sprites. */ -static uint LoadGrfFile(const char *filename, uint load_index, bool needs_palette_remap) +static uint LoadGrfFile(const std::string &filename, uint load_index, bool needs_palette_remap) { uint load_index_org = load_index; uint sprite_id = 0; @@ -52,7 +52,7 @@ static uint LoadGrfFile(const char *file Debug(sprite, 2, "Reading grf-file '{}'", filename); byte container_ver = file.GetContainerVersion(); - if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename); + if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename.c_str()); ReadGRFSpriteOffsets(file); if (container_ver >= 2) { /* Read compression. */ @@ -79,7 +79,7 @@ static uint LoadGrfFile(const char *file * @param needs_palette_remap Whether the colours in the GRF file need a palette remap. * @return The number of loaded sprites. */ -static void LoadGrfFileIndexed(const char *filename, const SpriteID *index_tbl, bool needs_palette_remap) +static void LoadGrfFileIndexed(const std::string &filename, const SpriteID *index_tbl, bool needs_palette_remap) { uint start; uint sprite_id = 0; @@ -89,7 +89,7 @@ static void LoadGrfFileIndexed(const cha Debug(sprite, 2, "Reading indexed grf-file '{}'", filename); byte container_ver = file.GetContainerVersion(); - if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename); + if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename.c_str()); ReadGRFSpriteOffsets(file); if (container_ver >= 2) { /* Read compression. */ @@ -137,7 +137,7 @@ void CheckExternalFiles() add_pos += seprintf(add_pos, last, "Trying to load graphics set '%s', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", used_set->name.c_str()); for (uint i = 0; i < GraphicsSet::NUM_FILES; i++) { MD5File::ChecksumResult res = GraphicsSet::CheckMD5(&used_set->files[i], BASESET_DIR); - if (res != MD5File::CR_MATCH) add_pos += seprintf(add_pos, last, "\t%s is %s (%s)\n", used_set->files[i].filename, res == MD5File::CR_MISMATCH ? "corrupt" : "missing", used_set->files[i].missing_warning); + if (res != MD5File::CR_MATCH) add_pos += seprintf(add_pos, last, "\t%s is %s (%s)\n", used_set->files[i].filename.c_str(), res == MD5File::CR_MISMATCH ? "corrupt" : "missing", used_set->files[i].missing_warning.c_str()); } add_pos += seprintf(add_pos, last, "\n"); } @@ -149,7 +149,7 @@ void CheckExternalFiles() static_assert(SoundsSet::NUM_FILES == 1); /* No need to loop each file, as long as there is only a single * sound file. */ - add_pos += seprintf(add_pos, last, "\t%s is %s (%s)\n", sounds_set->files->filename, SoundsSet::CheckMD5(sounds_set->files, BASESET_DIR) == MD5File::CR_MISMATCH ? "corrupt" : "missing", sounds_set->files->missing_warning); + add_pos += seprintf(add_pos, last, "\t%s is %s (%s)\n", sounds_set->files->filename.c_str(), SoundsSet::CheckMD5(sounds_set->files, BASESET_DIR) == MD5File::CR_MISMATCH ? "corrupt" : "missing", sounds_set->files->missing_warning.c_str()); } if (add_pos != error_msg) ShowInfoI(error_msg); @@ -201,7 +201,7 @@ static void LoadSpriteTables() ClrBit(master->flags, GCF_INIT_ONLY); /* Baseset extra graphics */ - GRFConfig *extra = new GRFConfig(used_set->files[GFT_EXTRA].filename); + GRFConfig *extra = new GRFConfig(used_set->files[GFT_EXTRA].filename.c_str()); /* We know the palette of the base set, so if the base NewGRF is not * setting one, use the palette of the base set and not the global @@ -355,7 +355,7 @@ void GfxLoadSprites() UpdateCursorSize(); } -bool GraphicsSet::FillSetDetails(IniFile *ini, const char *path, const char *full_filename) +bool GraphicsSet::FillSetDetails(IniFile *ini, const std::string &path, const std::string &full_filename) { bool ret = this->BaseSet::FillSetDetails(ini, path, full_filename, false); if (ret) { diff --git a/src/music.cpp b/src/music.cpp --- a/src/music.cpp +++ b/src/music.cpp @@ -25,7 +25,7 @@ * @return Pointer to string, caller is responsible for freeing memory, * nullptr if entrynum does not exist. */ -char *GetMusicCatEntryName(const char *filename, size_t entrynum) +char *GetMusicCatEntryName(const std::string &filename, size_t entrynum) { if (!FioCheckFileExists(filename, BASESET_DIR)) return nullptr; @@ -52,7 +52,7 @@ char *GetMusicCatEntryName(const char *f * @return Pointer to buffer with data read, caller is responsible for freeind memory, * nullptr if entrynum does not exist. */ -byte *GetMusicCatEntryData(const char *filename, size_t entrynum, size_t &entrylen) +byte *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen) { entrylen = 0; if (!FioCheckFileExists(filename, BASESET_DIR)) return nullptr; @@ -116,7 +116,7 @@ template return BaseMedia::used_set != nullptr; } -bool MusicSet::FillSetDetails(IniFile *ini, const char *path, const char *full_filename) +bool MusicSet::FillSetDetails(IniFile *ini, const std::string &path, const std::string &full_filename) { bool ret = this->BaseSet::FillSetDetails(ini, path, full_filename); if (ret) { @@ -126,8 +126,8 @@ bool MusicSet::FillSetDetails(IniFile *i IniGroup *timingtrim = ini->GetGroup("timingtrim"); uint tracknr = 1; for (uint i = 0; i < lengthof(this->songinfo); i++) { - const char *filename = this->files[i].filename; - if (names == nullptr || StrEmpty(filename) || this->files[i].check_result == MD5File::CR_NO_FILE) { + const std::string &filename = this->files[i].filename; + if (filename.empty() || this->files[i].check_result == MD5File::CR_NO_FILE) { continue; } @@ -149,7 +149,7 @@ bool MusicSet::FillSetDetails(IniFile *i this->songinfo[i].filetype = MTT_STANDARDMIDI; } - const char *trimmed_filename = filename; + const char *trimmed_filename = filename.c_str(); /* As we possibly add a path to the filename and we compare * on the filename with the path as in the .obm, we need to * keep stripping path elements until we find a match. */ diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -849,7 +849,7 @@ bool MidiFile::LoadSong(const MusicSongI { switch (song.filetype) { case MTT_STANDARDMIDI: - return this->LoadFile(song.filename); + return this->LoadFile(song.filename.c_str()); case MTT_MPSMIDI: { size_t songdatalen = 0; @@ -1060,9 +1060,9 @@ std::string MidiFile::GetSMFFile(const M char basename[MAX_PATH]; { - const char *fnstart = strrchr(song.filename, PATHSEPCHAR); + const char *fnstart = strrchr(song.filename.c_str(), PATHSEPCHAR); if (fnstart == nullptr) { - fnstart = song.filename; + fnstart = song.filename.c_str(); } else { fnstart++; } diff --git a/src/sound.cpp b/src/sound.cpp --- a/src/sound.cpp +++ b/src/sound.cpp @@ -23,7 +23,7 @@ static SoundEntry _original_sounds[ORIGINAL_SAMPLE_COUNT]; -static void OpenBankFile(const char *filename) +static void OpenBankFile(const std::string &filename) { /** * The sound file for the original sounds, i.e. those not defined/overridden by a NewGRF. @@ -34,7 +34,7 @@ static void OpenBankFile(const char *fil memset(_original_sounds, 0, sizeof(_original_sounds)); /* If there is no sound file (nosound set), don't load anything */ - if (filename == nullptr) return; + if (filename.empty()) return; original_sound_file.reset(new RandomAccessFile(filename, BASESET_DIR)); size_t pos = original_sound_file->GetPos();