From 31c306c6cde36628f88c9a239ac8fc19e42b86bf Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 10 Mar 2024 11:22:46 +0000 Subject: [PATCH] Codechange: Return std::string from GetMusicCatEntryName. Return std::string instead of pointer to C-string with manual memory management. --- src/base_media_base.h | 2 +- src/music.cpp | 34 ++++++++++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/base_media_base.h b/src/base_media_base.h index 92396cd616..a9a70c2278 100644 --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -313,7 +313,7 @@ static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS; static const uint NUM_SONGS_PLAYLIST = 32; /* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */ -char *GetMusicCatEntryName(const std::string &filename, size_t entrynum); +std::optional GetMusicCatEntryName(const std::string &filename, size_t entrynum); uint8_t *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen); enum MusicTrackType { diff --git a/src/music.cpp b/src/music.cpp index 64601cb769..1974ee261b 100644 --- a/src/music.cpp +++ b/src/music.cpp @@ -8,6 +8,7 @@ /** @file music.cpp The songs that OpenTTD knows. */ #include "stdafx.h" +#include "string_func.h" /** The type of set we're replacing */ @@ -22,26 +23,24 @@ * Read the name of a music CAT file entry. * @param filename Name of CAT file to read from * @param entrynum Index of entry whose name to read - * @return Pointer to string, caller is responsible for freeing memory, - * nullptr if entrynum does not exist. + * @return Name of CAT file entry if it could be read. */ -char *GetMusicCatEntryName(const std::string &filename, size_t entrynum) +std::optional GetMusicCatEntryName(const std::string &filename, size_t entrynum) { - if (!FioCheckFileExists(filename, BASESET_DIR)) return nullptr; + if (!FioCheckFileExists(filename, BASESET_DIR)) return std::nullopt; RandomAccessFile file(filename, BASESET_DIR); uint32_t ofs = file.ReadDword(); size_t entry_count = ofs / 8; - if (entrynum < entry_count) { - file.SeekTo(entrynum * 8, SEEK_SET); - file.SeekTo(file.ReadDword(), SEEK_SET); - uint8_t namelen = file.ReadByte(); - char *name = MallocT(namelen + 1); - file.ReadBlock(name, namelen); - name[namelen] = '\0'; - return name; - } - return nullptr; + if (entrynum >= entry_count) return std::nullopt; + + file.SeekTo(entrynum * 8, SEEK_SET); + file.SeekTo(file.ReadDword(), SEEK_SET); + uint8_t namelen = file.ReadByte(); + + std::string name(namelen, '\0'); + file.ReadBlock(name.data(), namelen); + return StrMakeValid(name); } /** @@ -138,13 +137,12 @@ bool MusicSet::FillSetDetails(const IniFile &ini, const std::string &path, const /* Song has a CAT file index, assume it's MPS MIDI format */ this->songinfo[i].filetype = MTT_MPSMIDI; this->songinfo[i].cat_index = atoi(item->value->c_str()); - char *songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index); - if (songname == nullptr) { + auto songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index); + if (!songname.has_value()) { Debug(grf, 0, "Base music set song missing from CAT file: {}/{}", filename, this->songinfo[i].cat_index); continue; } - this->songinfo[i].songname = songname; - free(songname); + this->songinfo[i].songname = *songname; } else { this->songinfo[i].filetype = MTT_STANDARDMIDI; }