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 */
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 {
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.
* @param filename Name of CAT file to read from.
* @param entrynum Index of entry to read
* @param[out] entrylen Receives length of data read
* @return Pointer to buffer with data read, caller is responsible for freeind memory,
* nullptr if entrynum does not exist.
* @return Data of CAT file entry.
*/
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 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);
size_t entrypos = file.ReadDword();
entrylen = file.ReadDword();
file.SeekTo(entrypos, SEEK_SET);
file.SkipBytes(file.ReadByte());
uint8_t *data = MallocT<uint8_t>(entrylen);
file.ReadBlock(data, entrylen);
return data;
}
return nullptr;
if (entrynum >= entry_count) return std::nullopt;
file.SeekTo(entrynum * 8, SEEK_SET);
size_t entrypos = file.ReadDword();
size_t entrylen = file.ReadDword();
file.SeekTo(entrypos, SEEK_SET);
file.SkipBytes(file.ReadByte());
std::vector<uint8_t> data(entrylen);
file.ReadBlock(data.data(), entrylen);
return data;
}
INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<MusicSet>, MusicSet)

View File

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