1
0
Fork 0

Codechange: Return std::string from GetMusicCatEntryName.

Return std::string instead of pointer to C-string with manual memory management.
pull/12653/head
Peter Nelson 2024-03-10 11:22:46 +00:00 committed by Peter Nelson
parent dc22edc556
commit 31c306c6cd
2 changed files with 17 additions and 19 deletions

View File

@ -313,7 +313,7 @@ static const uint NUM_SONGS_AVAILABLE = 1 + NUM_SONG_CLASSES * NUM_SONGS_CLASS;
static const uint NUM_SONGS_PLAYLIST = 32; 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 */
char *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); uint8_t *GetMusicCatEntryData(const std::string &filename, size_t entrynum, size_t &entrylen);
enum MusicTrackType { enum MusicTrackType {

View File

@ -8,6 +8,7 @@
/** @file music.cpp The songs that OpenTTD knows. */ /** @file music.cpp The songs that OpenTTD knows. */
#include "stdafx.h" #include "stdafx.h"
#include "string_func.h"
/** The type of set we're replacing */ /** The type of set we're replacing */
@ -22,26 +23,24 @@
* Read the name of a music CAT file entry. * Read the name 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 whose name to read * @param entrynum Index of entry whose name to read
* @return Pointer to string, caller is responsible for freeing memory, * @return Name of CAT file entry if it could be read.
* nullptr if entrynum does not exist.
*/ */
char *GetMusicCatEntryName(const std::string &filename, size_t entrynum) std::optional<std::string> 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); 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); file.SeekTo(entrynum * 8, SEEK_SET);
file.SeekTo(file.ReadDword(), SEEK_SET); file.SeekTo(file.ReadDword(), SEEK_SET);
uint8_t namelen = file.ReadByte(); uint8_t namelen = file.ReadByte();
char *name = MallocT<char>(namelen + 1);
file.ReadBlock(name, namelen); std::string name(namelen, '\0');
name[namelen] = '\0'; file.ReadBlock(name.data(), namelen);
return name; return StrMakeValid(name);
}
return nullptr;
} }
/** /**
@ -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 */ /* Song has a CAT file index, assume it's MPS MIDI format */
this->songinfo[i].filetype = MTT_MPSMIDI; this->songinfo[i].filetype = MTT_MPSMIDI;
this->songinfo[i].cat_index = atoi(item->value->c_str()); this->songinfo[i].cat_index = atoi(item->value->c_str());
char *songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index); auto songname = GetMusicCatEntryName(filename, this->songinfo[i].cat_index);
if (songname == nullptr) { if (!songname.has_value()) {
Debug(grf, 0, "Base music set song missing from CAT file: {}/{}", filename, this->songinfo[i].cat_index); Debug(grf, 0, "Base music set song missing from CAT file: {}/{}", filename, this->songinfo[i].cat_index);
continue; continue;
} }
this->songinfo[i].songname = songname; this->songinfo[i].songname = *songname;
free(songname);
} else { } else {
this->songinfo[i].filetype = MTT_STANDARDMIDI; this->songinfo[i].filetype = MTT_STANDARDMIDI;
} }