Codechange: Return std::vector from GetMusicCatEntryData.

Return std::vector instead of pointer to array with manual memory management.
This commit is contained in:
Peter Nelson 2024-03-10 11:24:03 +00:00 committed by Peter Nelson
parent 31c306c6cd
commit 733284cc16
3 changed files with 21 additions and 30 deletions

View File

@ -314,7 +314,7 @@ 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 */ /* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum); std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum);
uint8_t *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen); std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum);
enum MusicTrackType { enum MusicTrackType {
MTT_STANDARDMIDI, ///< Standard MIDI file MTT_STANDARDMIDI, ///< Standard MIDI file

View File

@ -47,29 +47,26 @@ std::optional<std::string> GetMusicCatEntryName(const std::string &filename, siz
* Read the full data of a music CAT file entry. * Read the full data of a music CAT file entry.
* @param filename Name of CAT file to read from. * @param filename Name of CAT file to read from.
* @param entrynum Index of entry to read * @param entrynum Index of entry to read
* @param[out] entrylen Receives length of data read * @return Data of CAT file entry.
* @return Pointer to buffer with data read, caller is responsible for freeind memory,
* nullptr if entrynum does not exist.
*/ */
uint8_t *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen) std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum)
{ {
entrylen = 0; if (!FioCheckFileExists(filename, BASESET_DIR)) return std::nullopt;
if (!FioCheckFileExists(filename, BASESET_DIR)) return nullptr;
RandomAccessFile file(filename, BASESET_DIR); RandomAccessFile file(filename, BASESET_DIR);
uint32_t ofs = file.ReadDword(); uint32_t ofs = file.ReadDword();
size_t entry_count = ofs / 8; size_t entry_count = ofs / 8;
if (entrynum < entry_count) { if (entrynum >= entry_count) return std::nullopt;
file.SeekTo(entrynum * 8, SEEK_SET);
size_t entrypos = file.ReadDword(); file.SeekTo(entrynum * 8, SEEK_SET);
entrylen = file.ReadDword(); size_t entrypos = file.ReadDword();
file.SeekTo(entrypos, SEEK_SET); size_t entrylen = file.ReadDword();
file.SkipBytes(file.ReadByte()); file.SeekTo(entrypos, SEEK_SET);
uint8_t *data = MallocT<uint8_t>(entrylen); file.SkipBytes(file.ReadByte());
file.ReadBlock(data, entrylen);
return data; std::vector<uint8_t> data(entrylen);
} file.ReadBlock(data.data(), entrylen);
return nullptr; return data;
} }
INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<MusicSet>, MusicSet) INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<MusicSet>, MusicSet)

View File

@ -843,11 +843,9 @@ bool MidiFile::LoadSong(const MusicSongInfo &song)
return this->LoadFile(song.filename); return this->LoadFile(song.filename);
case MTT_MPSMIDI: case MTT_MPSMIDI:
{ {
size_t songdatalen = 0; auto songdata = GetMusicCatEntryData(song.filename, song.cat_index);
uint8_t *songdata = GetMusicCatEntryData(song.filename, song.cat_index, songdatalen); if (songdata.has_value()) {
if (songdata != nullptr) { bool result = this->LoadMpsData(songdata->data(), songdata->size());
bool result = this->LoadMpsData(songdata, songdatalen);
free(songdata);
return result; return result;
} else { } else {
return false; return false;
@ -1078,17 +1076,13 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
return output_filename; return output_filename;
} }
uint8_t *data; auto songdata = GetMusicCatEntryData(song.filename, song.cat_index);
size_t datalen; if (!songdata.has_value()) return std::string();
data = GetMusicCatEntryData(song.filename, song.cat_index, datalen);
if (data == nullptr) return std::string();
MidiFile midifile; MidiFile midifile;
if (!midifile.LoadMpsData(data, datalen)) { if (!midifile.LoadMpsData(songdata->data(), songdata->size())) {
free(data);
return std::string(); return std::string();
} }
free(data);
if (midifile.WriteSMF(output_filename)) { if (midifile.WriteSMF(output_filename)) {
return output_filename; return output_filename;