diff --git a/src/sound/i_soundfont.cpp b/src/sound/i_soundfont.cpp index 4a073bc5e..38b1f70b7 100644 --- a/src/sound/i_soundfont.cpp +++ b/src/sound/i_soundfont.cpp @@ -50,7 +50,7 @@ FSoundFontManager sfmanager; // //========================================================================== -std::pair FSoundFontReader::LookupFile(const char *name) +std::pair FSoundFontReader::LookupFile(const char *name) { if (!IsAbsPath(name)) { @@ -58,12 +58,12 @@ std::pair FSoundFontReader::LookupFile(const char *name) { FString fullname = mPaths[i] + name; auto fr = OpenFile(fullname); - if (fr != nullptr) return std::make_pair(fr, fullname); + if (fr.isOpen()) return std::make_pair(std::move(fr), fullname); } } auto fr = OpenFile(name); - if (fr != nullptr) return std::make_pair(fr, name); - return std::make_pair(nullptr, FString()); + if (!fr.isOpen()) name = ""; + return std::make_pair(std::move(fr), name); } //========================================================================== @@ -117,7 +117,7 @@ FSF2Reader::FSF2Reader(const char *fn) // //========================================================================== -FileReader *FSF2Reader::OpenMainConfigFile() +FileRdr FSF2Reader::OpenMainConfigFile() { if (mMainConfigForSF2.IsNotEmpty()) { @@ -126,15 +126,14 @@ FileReader *FSF2Reader::OpenMainConfigFile() return nullptr; } -FileReader *FSF2Reader::OpenFile(const char *name) +FileRdr FSF2Reader::OpenFile(const char *name) { + FileRdr fr; if (mFilename.CompareNoCase(name) == 0) { - auto fr = new FileReader; - if (fr->Open(name)) return fr; - delete fr; + fr.OpenFile(name); } - return nullptr; + return fr; } //========================================================================== @@ -153,12 +152,12 @@ FZipPatReader::~FZipPatReader() if (resf != nullptr) delete resf; } -FileReader *FZipPatReader::OpenMainConfigFile() +FileRdr FZipPatReader::OpenMainConfigFile() { return OpenFile("timidity.cfg"); } -FileReader *FZipPatReader::OpenFile(const char *name) +FileRdr FZipPatReader::OpenFile(const char *name) { if (resf != nullptr) { @@ -194,8 +193,8 @@ FPatchSetReader::FPatchSetReader(const char *filename) }; #endif mAllowAbsolutePaths = true; - FileReader *fr = new FileReader; - if (fr->Open(filename)) + FileRdr fr; + if (fr.OpenFile(filename)) { mFullPathToConfig = filename; } @@ -204,7 +203,7 @@ FPatchSetReader::FPatchSetReader(const char *filename) for(auto c : paths) { FStringf fullname("%s/%s", c, filename); - if (fr->Open(fullname)) + if (fr.OpenFile(fullname)) { mFullPathToConfig = fullname; } @@ -224,23 +223,21 @@ FPatchSetReader::FPatchSetReader() mAllowAbsolutePaths = true; } -FileReader *FPatchSetReader::OpenMainConfigFile() +FileRdr FPatchSetReader::OpenMainConfigFile() { - auto fr = new FileReader; - if (fr->Open(mFullPathToConfig)) return fr; - delete fr; - return nullptr; + FileRdr fr; + fr.OpenFile(mFullPathToConfig); + return fr; } -FileReader *FPatchSetReader::OpenFile(const char *name) +FileRdr FPatchSetReader::OpenFile(const char *name) { FString path; if (IsAbsPath(name)) path = name; else path = mBasePath + name; - auto fr = new FileReader; - if (fr->Open(path)) return fr; - delete fr; - return nullptr; + FileRdr fr; + fr.OpenFile(path); + return fr; } //========================================================================== @@ -252,7 +249,6 @@ FileReader *FPatchSetReader::OpenFile(const char *name) FLumpPatchSetReader::FLumpPatchSetReader(const char *filename) { mLumpIndex = Wads.CheckNumForFullName(filename); - FileReader *fr = new FileReader; mBasePath = filename; FixPathSeperator(mBasePath); @@ -260,19 +256,19 @@ FLumpPatchSetReader::FLumpPatchSetReader(const char *filename) if (mBasePath.Len() > 0 && mBasePath.Back() != '/') mBasePath += '/'; } -FileReader *FLumpPatchSetReader::OpenMainConfigFile() +FileRdr FLumpPatchSetReader::OpenMainConfigFile() { return Wads.ReopenLumpNum(mLumpIndex); } -FileReader *FLumpPatchSetReader::OpenFile(const char *name) +FileRdr FLumpPatchSetReader::OpenFile(const char *name) { FString path; - if (IsAbsPath(name)) return nullptr; // no absolute paths in the lump directory. + if (IsAbsPath(name)) return FileRdr(); // no absolute paths in the lump directory. path = mBasePath + name; auto index = Wads.CheckNumForFullName(path); - if (index < 0) return nullptr; - return Wads.ReopenLumpNum(index); + if (index < 0) return FileRdr(); + return Wads.ReopenLumpReader(index); } //========================================================================== diff --git a/src/sound/i_soundfont.h b/src/sound/i_soundfont.h index 9208351e4..a6864d9d7 100644 --- a/src/sound/i_soundfont.h +++ b/src/sound/i_soundfont.h @@ -41,9 +41,9 @@ protected: public: virtual ~FSoundFontReader() {} - virtual FileReader *OpenMainConfigFile() = 0; // this is special because it needs to be synthesized for .sf files and set some restrictions for patch sets - virtual FileReader *OpenFile(const char *name) = 0; - std::pair LookupFile(const char *name); + virtual FileRdr OpenMainConfigFile() = 0; // this is special because it needs to be synthesized for .sf files and set some restrictions for patch sets + virtual FileRdr OpenFile(const char *name) = 0; + std::pair LookupFile(const char *name); void AddPath(const char *str); virtual FString basePath() const { @@ -63,8 +63,8 @@ class FSF2Reader : public FSoundFontReader FString mFilename; public: FSF2Reader(const char *filename); - virtual FileReader *OpenMainConfigFile() override; - virtual FileReader *OpenFile(const char *name) override; + virtual FileRdr OpenMainConfigFile() override; + virtual FileRdr OpenFile(const char *name) override; }; //========================================================================== @@ -79,8 +79,8 @@ class FZipPatReader : public FSoundFontReader public: FZipPatReader(const char *filename); ~FZipPatReader(); - virtual FileReader *OpenMainConfigFile() override; - virtual FileReader *OpenFile(const char *name) override; + virtual FileRdr OpenMainConfigFile() override; + virtual FileRdr OpenFile(const char *name) override; bool isOk() { return resf != nullptr; } }; @@ -97,8 +97,8 @@ class FLumpPatchSetReader : public FSoundFontReader public: FLumpPatchSetReader(const char *filename); - virtual FileReader *OpenMainConfigFile() override; - virtual FileReader *OpenFile(const char *name) override; + virtual FileRdr OpenMainConfigFile() override; + virtual FileRdr OpenFile(const char *name) override; virtual FString basePath() const override { return mBasePath; @@ -120,8 +120,8 @@ class FPatchSetReader : public FSoundFontReader public: FPatchSetReader(); FPatchSetReader(const char *filename); - virtual FileReader *OpenMainConfigFile() override; - virtual FileReader *OpenFile(const char *name) override; + virtual FileRdr OpenMainConfigFile() override; + virtual FileRdr OpenFile(const char *name) override; virtual FString basePath() const override { return mBasePath; diff --git a/src/sound/timidity/instrum.cpp b/src/sound/timidity/instrum.cpp index d92f7eb81..32ccd121f 100644 --- a/src/sound/timidity/instrum.cpp +++ b/src/sound/timidity/instrum.cpp @@ -153,7 +153,6 @@ static Instrument *load_instrument(Renderer *song, const char *name, int percuss { Instrument *ip; Sample *sp; - FileReader *fp; GF1PatchHeader header; GF1InstrumentData idata; GF1LayerData layer_data; @@ -164,19 +163,19 @@ static Instrument *load_instrument(Renderer *song, const char *name, int percuss if (!name || gus_sfreader == nullptr) return nullptr; /* Open patch file */ - fp = gus_sfreader->LookupFile(name).first; - if (fp == NULL) + auto fp = gus_sfreader->LookupFile(name).first; + if (!fp.isOpen()) { /* Try with various extensions */ FString tmp = name; tmp += ".pat"; fp = gus_sfreader->LookupFile(tmp).first; - if (fp == NULL) + if (!fp.isOpen()) { #ifdef __unix__ // Windows isn't case-sensitive. tmp.ToUpper(); fp = gus_sfreader->LookupFile(tmp).first; - if (fp == NULL) + if (!fp.isOpen()) #endif { noluck = true; @@ -194,26 +193,23 @@ static Instrument *load_instrument(Renderer *song, const char *name, int percuss /* Read some headers and do cursory sanity checks. */ - if (sizeof(header) != fp->Read(&header, sizeof(header))) + if (sizeof(header) != fp.Read(&header, sizeof(header))) { failread: cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Error reading instrument.\n", name); - delete fp; return 0; } if (strncmp(header.Header, GF1_HEADER_TEXT, HEADER_SIZE - 4) != 0) { cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Not an instrument.\n", name); - delete fp; return 0; } if (strcmp(header.Header + 8, "110") < 0) { cmsg(CMSG_ERROR, VERB_NORMAL, "%s: Is an old and unsupported patch version.\n", name); - delete fp; return 0; } - if (sizeof(idata) != fp->Read(&idata, sizeof(idata))) + if (sizeof(idata) != fp.Read(&idata, sizeof(idata))) { goto failread; } @@ -226,18 +222,16 @@ failread: if (header.Instruments != 1 && header.Instruments != 0) /* instruments. To some patch makers, 0 means 1 */ { cmsg(CMSG_ERROR, VERB_NORMAL, "Can't handle patches with %d instruments.\n", header.Instruments); - delete fp; return 0; } if (idata.Layers != 1 && idata.Layers != 0) /* layers. What's a layer? */ { cmsg(CMSG_ERROR, VERB_NORMAL, "Can't handle instruments with %d layers.\n", idata.Layers); - delete fp; return 0; } - if (sizeof(layer_data) != fp->Read(&layer_data, sizeof(layer_data))) + if (sizeof(layer_data) != fp.Read(&layer_data, sizeof(layer_data))) { goto failread; } @@ -245,7 +239,6 @@ failread: if (layer_data.Samples == 0) { cmsg(CMSG_ERROR, VERB_NORMAL, "Instrument has 0 samples.\n"); - delete fp; return 0; } @@ -255,12 +248,11 @@ failread: memset(ip->sample, 0, sizeof(Sample) * layer_data.Samples); for (i = 0; i < layer_data.Samples; ++i) { - if (sizeof(patch_data) != fp->Read(&patch_data, sizeof(patch_data))) + if (sizeof(patch_data) != fp.Read(&patch_data, sizeof(patch_data))) { fail: cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d.\n", i); delete ip; - delete fp; return 0; } @@ -422,7 +414,7 @@ fail: } sp->data = (sample_t *)safe_malloc(sp->data_length); - if (sp->data_length != fp->Read(sp->data, sp->data_length)) + if (sp->data_length != fp.Read(sp->data, sp->data_length)) goto fail; convert_sample_data(sp, sp->data); @@ -469,7 +461,6 @@ fail: sp->data_length = sp->loop_end; } } - delete fp; return ip; } diff --git a/src/sound/timidity/instrum_font.cpp b/src/sound/timidity/instrum_font.cpp index 41709809c..680641b81 100644 --- a/src/sound/timidity/instrum_font.cpp +++ b/src/sound/timidity/instrum_font.cpp @@ -17,7 +17,7 @@ namespace Timidity FontFile *Fonts; extern std::unique_ptr gus_sfreader; -FontFile *ReadDLS(const char *filename, FileReader *f) +FontFile *ReadDLS(const char *filename, FileRdr &f) { return NULL; } @@ -57,15 +57,14 @@ void font_add(const char *filename, int load_order) } else { - FileReader *fp = gus_sfreader->LookupFile(filename).first; + FileRdr fp = gus_sfreader->LookupFile(filename).first; - if (fp != NULL) + if (fp.isOpen()) { if ((font = ReadSF2(filename, fp)) || (font = ReadDLS(filename, fp))) { font->SetAllOrders(load_order); } - delete fp; } } } diff --git a/src/sound/timidity/instrum_sf2.cpp b/src/sound/timidity/instrum_sf2.cpp index dccbf53ac..02696f63c 100644 --- a/src/sound/timidity/instrum_sf2.cpp +++ b/src/sound/timidity/instrum_sf2.cpp @@ -28,7 +28,7 @@ class CBadVer {}; struct ListHandler { uint32_t ID; - void (*Parser)(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); + void (*Parser)(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); }; enum @@ -162,15 +162,15 @@ static const SFGenComposite DefaultGenerators = -1 // overridingRootKey }; -static void ParseIfil(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseSmpl(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseSm24(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParsePhdr(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseBag(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseMod(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseGen(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseInst(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); -static void ParseShdr(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen); +static void ParseIfil(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseSmpl(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseSm24(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParsePhdr(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseBag(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseMod(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseGen(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseInst(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); +static void ParseShdr(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen); ListHandler INFOHandlers[] = { @@ -225,87 +225,87 @@ static int32_t calc_rate(Renderer *song, int diff, double sec) } -static inline uint32_t read_id(FileReader *f) +static inline uint32_t read_id(FileRdr &f) { uint32_t id; - if (f->Read(&id, 4) != 4) + if (f.Read(&id, 4) != 4) { throw CIOErr(); } return id; } -static inline int read_byte(FileReader *f) +static inline int read_byte(FileRdr &f) { uint8_t x; - if (f->Read(&x, 1) != 1) + if (f.Read(&x, 1) != 1) { throw CIOErr(); } return x; } -static inline int read_char(FileReader *f) +static inline int read_char(FileRdr &f) { int8_t x; - if (f->Read(&x, 1) != 1) + if (f.Read(&x, 1) != 1) { throw CIOErr(); } return x; } -static inline int read_uword(FileReader *f) +static inline int read_uword(FileRdr &f) { uint16_t x; - if (f->Read(&x, 2) != 2) + if (f.Read(&x, 2) != 2) { throw CIOErr(); } return LittleShort(x); } -static inline int read_sword(FileReader *f) +static inline int read_sword(FileRdr &f) { int16_t x; - if (f->Read(&x, 2) != 2) + if (f.Read(&x, 2) != 2) { throw CIOErr(); } return LittleShort(x); } -static inline uint32_t read_dword(FileReader *f) +static inline uint32_t read_dword(FileRdr &f) { uint32_t x; - if (f->Read(&x, 4) != 4) + if (f.Read(&x, 4) != 4) { throw CIOErr(); } return LittleLong(x); } -static inline void read_name(FileReader *f, char name[21]) +static inline void read_name(FileRdr &f, char name[21]) { - if (f->Read(name, 20) != 20) + if (f.Read(name, 20) != 20) { throw CIOErr(); } name[20] = 0; } -static inline void skip_chunk(FileReader *f, uint32_t len) +static inline void skip_chunk(FileRdr &f, uint32_t len) { // RIFF, like IFF, adds an extra pad byte to the end of // odd-sized chunks so that new chunks are always on even // byte boundaries. - if (f->Seek(len + (len & 1), SEEK_CUR) != 0) + if (f.Seek(len + (len & 1), FileRdr::SeekCur) != 0) { throw CIOErr(); } } -static void check_list(FileReader *f, uint32_t id, uint32_t filelen, uint32_t &chunklen) +static void check_list(FileRdr &f, uint32_t id, uint32_t filelen, uint32_t &chunklen) { if (read_id(f) != ID_LIST) { @@ -322,7 +322,7 @@ static void check_list(FileReader *f, uint32_t id, uint32_t filelen, uint32_t &c } } -static void ParseIfil(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseIfil(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { uint16_t major, minor; @@ -341,7 +341,7 @@ static void ParseIfil(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chu sf2->MinorVersion = minor; } -static void ParseLIST(SFFile *sf2, FileReader *f, uint32_t chunklen, ListHandler *handlers) +static void ParseLIST(SFFile *sf2, FileRdr &f, uint32_t chunklen, ListHandler *handlers) { ListHandler *handler; uint32_t id; @@ -375,7 +375,7 @@ static void ParseLIST(SFFile *sf2, FileReader *f, uint32_t chunklen, ListHandler } } -static void ParseINFO(SFFile *sf2, FileReader *f, uint32_t chunklen) +static void ParseINFO(SFFile *sf2, FileRdr &f, uint32_t chunklen) { sf2->MinorVersion = -1; @@ -387,7 +387,7 @@ static void ParseINFO(SFFile *sf2, FileReader *f, uint32_t chunklen) } } -static void ParseSdta(SFFile *sf2, FileReader *f, uint32_t chunklen) +static void ParseSdta(SFFile *sf2, FileRdr &f, uint32_t chunklen) { ParseLIST(sf2, f, chunklen, SdtaHandlers); if (sf2->SampleDataOffset == 0) @@ -404,7 +404,7 @@ static void ParseSdta(SFFile *sf2, FileReader *f, uint32_t chunklen) } } -static void ParseSmpl(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseSmpl(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { // Only use the first smpl chunk. (Or should we reject files with more than one?) if (sf2->SampleDataOffset == 0) @@ -413,13 +413,13 @@ static void ParseSmpl(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chu { // Chunk must be an even number of bytes. throw CBadForm(); } - sf2->SampleDataOffset = f->Tell(); + sf2->SampleDataOffset = (uint32_t)f.Tell(); sf2->SizeSampleData = chunklen >> 1; } skip_chunk(f, chunklen); } -static void ParseSm24(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseSm24(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { // The sm24 chunk is ignored if the file version is < 2.04 if (sf2->MinorVersion >= 4) @@ -427,19 +427,19 @@ static void ParseSm24(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chu // Only use the first sm24 chunk. (Or should we reject files with more than one?) if (sf2->SampleDataLSBOffset == 0) { - sf2->SampleDataLSBOffset = f->Tell(); + sf2->SampleDataLSBOffset = (uint32_t)f.Tell(); sf2->SizeSampleDataLSB = chunklen; } } skip_chunk(f, chunklen); } -static void ParsePdta(SFFile *sf2, FileReader *f, uint32_t chunklen) +static void ParsePdta(SFFile *sf2, FileRdr &f, uint32_t chunklen) { ParseLIST(sf2, f, chunklen, PdtaHandlers); } -static void ParsePhdr(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParsePhdr(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { SFPreset *preset; @@ -477,7 +477,7 @@ static void ParsePhdr(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chu } } -static void ParseBag(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseBag(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { SFBag *bags, *bag; uint16_t prev_mod = 0; @@ -539,7 +539,7 @@ static void ParseBag(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chun } } -static void ParseMod(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseMod(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { // Section 7.4, page 23: // It [the PMOD sub-chunk] is always a multiple of ten bytes in length, @@ -552,7 +552,7 @@ static void ParseMod(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chun skip_chunk(f, chunklen); } -static void ParseGen(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseGen(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { SFGenList *gens, *gen; int numgens; @@ -604,7 +604,7 @@ static void ParseGen(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chun } } -static void ParseInst(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseInst(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { int i; SFInst *inst; @@ -639,7 +639,7 @@ static void ParseInst(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chu } } -static void ParseShdr(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chunklen) +static void ParseShdr(SFFile *sf2, FileRdr &f, uint32_t chunkid, uint32_t chunklen) { int i; SFSample *sample; @@ -698,7 +698,7 @@ static void ParseShdr(SFFile *sf2, FileReader *f, uint32_t chunkid, uint32_t chu } -SFFile *ReadSF2(const char *filename, FileReader *f) +SFFile *ReadSF2(const char *filename, FileRdr &f) { SFFile *sf2 = NULL; uint32_t filelen; @@ -1508,33 +1508,32 @@ void SFFile::ApplyGeneratorsToRegion(SFGenComposite *gen, SFSample *sfsamp, Rend void SFFile::LoadSample(SFSample *sample) { - FileReader *fp = gus_sfreader->LookupFile(Filename).first; + FileRdr fp = gus_sfreader->LookupFile(Filename).first; uint32_t i; - if (fp == NULL) + if (!fp.isOpen()) { return; } sample->InMemoryData = new float[sample->End - sample->Start + 1]; - fp->Seek(SampleDataOffset + sample->Start * 2, SEEK_SET); + fp.Seek(SampleDataOffset + sample->Start * 2, FileRdr::SeekSet); // Load 16-bit sample data. for (i = 0; i < sample->End - sample->Start; ++i) { int16_t samp; - *fp >> samp; + fp >> samp; sample->InMemoryData[i] = samp / 32768.f; } if (SampleDataLSBOffset != 0) { // Load lower 8 bits of 24-bit sample data. - fp->Seek(SampleDataLSBOffset + sample->Start, SEEK_SET); + fp.Seek(SampleDataLSBOffset + sample->Start, FileRdr::SeekSet); for (i = 0; i < sample->End - sample->Start; ++i) { uint8_t samp; - *fp >> samp; + fp >> samp; sample->InMemoryData[i] = ((((int32_t(sample->InMemoryData[i] * 32768) << 8) | samp) << 8) >> 8) / 8388608.f; } } // Final 0 byte is for interpolation. sample->InMemoryData[i] = 0; - delete fp; } diff --git a/src/sound/timidity/sf2.h b/src/sound/timidity/sf2.h index 26d413ab5..ada4ff202 100644 --- a/src/sound/timidity/sf2.h +++ b/src/sound/timidity/sf2.h @@ -315,4 +315,4 @@ struct SFFile : public Timidity::FontFile int NumSamples; }; -SFFile *ReadSF2(const char *filename, FileReader *f); +SFFile *ReadSF2(const char *filename, FileRdr &f); diff --git a/src/sound/timidity/timidity.cpp b/src/sound/timidity/timidity.cpp index 4c9efe0ac..34f66b248 100644 --- a/src/sound/timidity/timidity.cpp +++ b/src/sound/timidity/timidity.cpp @@ -82,7 +82,7 @@ static bool InitReader(const char *config_file) static int read_config_file(const char *name, bool ismain) { - FileReader *fp; + FileRdr fp; char tmp[1024], *cp; ToneBank *bank = NULL; int i, j, k, line = 0, words; @@ -103,12 +103,12 @@ static int read_config_file(const char *name, bool ismain) { fp = gus_sfreader->LookupFile(name).first; } - if (fp == nullptr) + if (!fp .isOpen()) { return -1; } - while (fp->Gets(tmp, sizeof(tmp))) + while (fp.Gets(tmp, sizeof(tmp))) { line++; FCommandLine w(tmp, true); @@ -207,7 +207,6 @@ static int read_config_file(const char *name, bool ismain) if (words < 2) { Printf("%s: line %d: No soundfont given\n", name, line); - delete fp; return -2; } if (words > 2 && !strcmp(w[2], "remove")) @@ -223,7 +222,6 @@ static int read_config_file(const char *name, bool ismain) if (!(cp = strchr(w[i], '='))) { Printf("%s: line %d: bad soundfont option %s\n", name, line, w[i]); - delete fp; return -2; } } @@ -241,7 +239,6 @@ static int read_config_file(const char *name, bool ismain) if (words < 3) { Printf("%s: line %d: syntax error\n", name, line); - delete fp; return -2; } @@ -258,7 +255,6 @@ static int read_config_file(const char *name, bool ismain) else { Printf("%s: line %d: font subcommand must be 'order' or 'exclude'\n", name, line); - delete fp; return -2; } if (i < words) @@ -312,7 +308,6 @@ static int read_config_file(const char *name, bool ismain) if (words < 2) { Printf("%s: line %d: No directory given\n", name, line); - delete fp; return -2; } for (i = 1; i < words; i++) @@ -340,7 +335,6 @@ static int read_config_file(const char *name, bool ismain) if (words != 2) { Printf("%s: line %d: Must specify exactly one patch name\n", name, line); - delete fp; return -2; } def_instr_name = w[1]; @@ -350,14 +344,12 @@ static int read_config_file(const char *name, bool ismain) if (words < 2) { Printf("%s: line %d: No drum set number given\n", name, line); - delete fp; return -2; } i = atoi(w[1]); if (i < 0 || i > 127) { Printf("%s: line %d: Drum set must be between 0 and 127\n", name, line); - delete fp; return -2; } if (drumset[i] == NULL) @@ -371,14 +363,12 @@ static int read_config_file(const char *name, bool ismain) if (words < 2) { Printf("%s: line %d: No bank number given\n", name, line); - delete fp; return -2; } i = atoi(w[1]); if (i < 0 || i > 127) { Printf("%s: line %d: Tone bank must be between 0 and 127\n", name, line); - delete fp; return -2; } if (tonebank[i] == NULL) @@ -392,20 +382,17 @@ static int read_config_file(const char *name, bool ismain) if ((words < 2) || (*w[0] < '0' || *w[0] > '9')) { Printf("%s: line %d: syntax error\n", name, line); - delete fp; return -2; } i = atoi(w[0]); if (i < 0 || i > 127) { Printf("%s: line %d: Program must be between 0 and 127\n", name, line); - delete fp; return -2; } if (bank == NULL) { Printf("%s: line %d: Must specify tone bank or drum set before assignment\n", name, line); - delete fp; return -2; } bank->tone[i].note = bank->tone[i].pan = @@ -440,7 +427,6 @@ static int read_config_file(const char *name, bool ismain) if (!(cp=strchr(w[j], '='))) { Printf("%s: line %d: bad patch option %s\n", name, line, w[j]); - delete fp; return -2; } *cp++ = 0; @@ -454,7 +440,6 @@ static int read_config_file(const char *name, bool ismain) if ((k < 0 || k > 127) || (*cp < '0' || *cp > '9')) { Printf("%s: line %d: note must be between 0 and 127\n", name, line); - delete fp; return -2; } bank->tone[i].note = k; @@ -474,7 +459,6 @@ static int read_config_file(const char *name, bool ismain) { Printf("%s: line %d: panning must be left, right, " "center, or between -100 and 100\n", name, line); - delete fp; return -2; } bank->tone[i].pan = k; @@ -488,7 +472,6 @@ static int read_config_file(const char *name, bool ismain) else { Printf("%s: line %d: keep must be env or loop\n", name, line); - delete fp; return -2; } } @@ -503,28 +486,17 @@ static int read_config_file(const char *name, bool ismain) else { Printf("%s: line %d: strip must be env, loop, or tail\n", name, line); - delete fp; return -2; } } else { Printf("%s: line %d: bad patch option %s\n", name, line, w[j]); - delete fp; return -2; } } } } - /* - if (ferror(fp)) - { - Printf("Can't read %s: %s\n", name, strerror(errno)); - close_file(fp); - return -2; - } - */ - delete fp; return 0; } diff --git a/src/sound/timidity/timidity.h b/src/sound/timidity/timidity.h index 2f923b697..710aecc55 100644 --- a/src/sound/timidity/timidity.h +++ b/src/sound/timidity/timidity.h @@ -21,8 +21,7 @@ #define TIMIDITY_H #include "doomtype.h" - -class FileReader; +#include "files.h" namespace Timidity { @@ -362,7 +361,7 @@ void font_order(int order, int bank, int preset, int keynote); Instrument *load_instrument_font(struct Renderer *song, const char *font, int drum, int bank, int instrument); Instrument *load_instrument_font_order(struct Renderer *song, int order, int drum, int bank, int instrument); -FontFile *ReadDLS(const char *filename, FileReader *f); +FontFile *ReadDLS(const char *filename, FileRdr &f); /* mix.h diff --git a/src/sound/timiditypp/common.cpp b/src/sound/timiditypp/common.cpp index b323a85d7..62f331c87 100644 --- a/src/sound/timiditypp/common.cpp +++ b/src/sound/timiditypp/common.cpp @@ -130,7 +130,7 @@ double flt_rand() struct timidity_file *open_file(const char *name, FSoundFontReader *sfreader) { - FileReader *fr; + FileRdr fr; FString filename; if (name == nullptr) { @@ -140,60 +140,56 @@ struct timidity_file *open_file(const char *name, FSoundFontReader *sfreader) else { auto res = sfreader->LookupFile(name); - fr = res.first; + fr = std::move(res.first); filename = res.second; } - if (fr == nullptr) return nullptr; + if (!fr.isOpen()) return nullptr; auto tf = new timidity_file; - tf->url = fr; + tf->url = std::move(fr); tf->filename = filename.GetChars(); return tf; } /* This closes files opened with open_file */ -void close_file(struct timidity_file *tf) +void tf_close(struct timidity_file *tf) { - if (tf->url != NULL) - { - tf->url->Close(); - delete tf->url; - } + tf->url.Close(); delete tf; } /* This is meant for skipping a few bytes. */ void skip(struct timidity_file *tf, size_t len) { - tf->url->Seek((long)len, SEEK_CUR); + tf->url.Seek((long)len, FileRdr::SeekCur); } char *tf_gets(char *buff, int n, struct timidity_file *tf) { - return tf->url->Gets(buff, n); + return tf->url.Gets(buff, n); } int tf_getc(struct timidity_file *tf) { unsigned char c; - auto read = tf->url->Read(&c, 1); + auto read = tf->url.Read(&c, 1); return read == 0 ? EOF : c; } long tf_read(void *buff, int32_t size, int32_t nitems, struct timidity_file *tf) { - return tf->url->Read(buff, size * nitems) / size; + return (long)tf->url.Read(buff, size * nitems) / size; } long tf_seek(struct timidity_file *tf, long offset, int whence) { - return tf->url->Seek(offset, whence); + return (long)tf->url.Seek(offset, (FileRdr::ESeek)whence); } long tf_tell(struct timidity_file *tf) { - return tf->url->Tell(); + return (long)tf->url.Tell(); } } diff --git a/src/sound/timiditypp/common.h b/src/sound/timiditypp/common.h index d288a1f0f..c0d721a56 100644 --- a/src/sound/timiditypp/common.h +++ b/src/sound/timiditypp/common.h @@ -35,12 +35,12 @@ namespace TimidityPlus struct timidity_file { - FileReader *url; + FileRdr url; std::string filename; }; extern struct timidity_file *open_file(const char *name, FSoundFontReader *); -extern void close_file(struct timidity_file *tf); +extern void tf_close(struct timidity_file *tf); extern void skip(struct timidity_file *tf, size_t len); extern char *tf_gets(char *buff, int n, struct timidity_file *tf); int tf_getc(struct timidity_file *tf); diff --git a/src/sound/timiditypp/configfile.cpp b/src/sound/timiditypp/configfile.cpp index 80b4f54f5..4d728f5ed 100644 --- a/src/sound/timiditypp/configfile.cpp +++ b/src/sound/timiditypp/configfile.cpp @@ -43,7 +43,7 @@ namespace TimidityPlus ctl_cmsg(CMSG_ERROR, VERB_NORMAL, \ "Too many errors... Give up read %s", name); \ reuse_mblock(&varbuf); \ - close_file(tf); return 1; } + tf_close(tf); return 1; } typedef struct { @@ -1427,7 +1427,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_ continue; case READ_CONFIG_RECURSION: reuse_mblock(&varbuf); - close_file(tf); + tf_close(tf); return READ_CONFIG_RECURSION; case READ_CONFIG_FILE_NOT_FOUND: break; @@ -1610,7 +1610,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_ } } reuse_mblock(&varbuf); - close_file(tf); + tf_close(tf); return (errcnt == 0) ? READ_CONFIG_SUCCESS : READ_CONFIG_ERROR; } diff --git a/src/sound/timiditypp/instrum.cpp b/src/sound/timiditypp/instrum.cpp index 988da8730..86053e518 100644 --- a/src/sound/timiditypp/instrum.cpp +++ b/src/sound/timiditypp/instrum.cpp @@ -667,20 +667,20 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr, && memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) { /* don't know what the differences are */ ctl_cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name); - close_file(tf); + tf_close(tf); return 0; } /* instruments. To some patch makers, 0 means 1 */ if (tmp[82] != 1 && tmp[82] != 0) { ctl_cmsg(CMSG_ERROR, VERB_NORMAL, "Can't handle patches with %d instruments", tmp[82]); - close_file(tf); + tf_close(tf); return 0; } if (tmp[151] != 1 && tmp[151] != 0) { /* layers. What's a layer? */ ctl_cmsg(CMSG_ERROR, VERB_NORMAL, "Can't handle instruments with %d layers", tmp[151]); - close_file(tf); + tf_close(tf); return 0; } ip = (Instrument *)safe_malloc(sizeof(Instrument)); @@ -697,7 +697,7 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr, free(ip->sample[j].data); free(ip->sample); free(ip); - close_file(tf); + tf_close(tf); return 0; } sp = &(ip->sample[i]); @@ -964,7 +964,7 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr, ctl_cmsg(CMSG_INFO, VERB_DEBUG, " - Stripping tail"); } } - close_file(tf); + tf_close(tf); store_instrument_cache(ip, name, panning, amp, note_to_use, strip_loop, strip_envelope, strip_tail); return ip; diff --git a/src/sound/timiditypp/smplfile.cpp b/src/sound/timiditypp/smplfile.cpp index e34703004..b482796db 100644 --- a/src/sound/timiditypp/smplfile.cpp +++ b/src/sound/timiditypp/smplfile.cpp @@ -283,10 +283,10 @@ int Instruments::import_wave_discriminant(char *sample_file) if (tf_read(buf, 12, 1, tf) != 1 || memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0) { - close_file(tf); + tf_close(tf); return 1; } - close_file(tf); + tf_close(tf); return 0; } @@ -314,7 +314,7 @@ int Instruments::import_wave_load(char *sample_file, Instrument *inst) if (tf_read(buf, 12, 1, tf) != 1 || memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0) { - close_file(tf); + tf_close(tf); return 1; } //ctl_cmsg(CMSG_INFO,VERB_NOISY,"Loading WAV: %s", sample_file); @@ -378,7 +378,7 @@ int Instruments::import_wave_load(char *sample_file, Instrument *inst) type_index = 4 - (chunk_size & 1); type_size = 8 + (chunk_size & 1); } - close_file(tf); + tf_close(tf); if (chunk_flags & WAVE_CHUNKFLAG_SAMPLER) { uint8_t modes; @@ -557,10 +557,10 @@ int Instruments::import_aiff_discriminant(char *sample_file) || memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0 || (buf[8 + 3] != 'F' && buf[8 + 3] != 'C')) { - close_file(tf); + tf_close(tf); return 1; } - close_file(tf); + tf_close(tf); return 0; } @@ -597,7 +597,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst) || memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0 || (buf[8 + 3] != 'F' && buf[8 + 3] != 'C')) { - close_file(tf); + tf_close(tf); return 1; } compressed = buf[8 + 3] == 'C'; @@ -684,7 +684,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst) { if (marker_data != NULL) free(marker_data); - close_file(tf); + tf_close(tf); return -1; } if (!(chunk_flags & AIFF_CHUNKFLAG_SOUNDREAD)) @@ -693,7 +693,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst) { if (marker_data != NULL) free(marker_data); - close_file(tf); + tf_close(tf); return 1; } } @@ -729,7 +729,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst) } if (marker_data != NULL) free(marker_data); - close_file(tf); + tf_close(tf); return 0; } diff --git a/src/sound/timiditypp/sndfont.cpp b/src/sound/timiditypp/sndfont.cpp index 9e0e51d18..e969e4de4 100644 --- a/src/sound/timiditypp/sndfont.cpp +++ b/src/sound/timiditypp/sndfont.cpp @@ -223,10 +223,7 @@ void Instruments::free_soundfonts() SFInsts *sf, *next; for (sf = sfrecs; sf != NULL; sf = next) { - if ((sf->tf != NULL) && (sf->tf->url != NULL)) - free(sf->tf->url); - if (sf->tf != NULL) - free(sf->tf); + tf_close(sf->tf); reuse_mblock(&sf->pool); next = sf->next; free(sf); @@ -308,7 +305,7 @@ void Instruments::init_sf(SFInsts *rec) free_soundfont(&sfinfo); if (opt_sf_close_each_file) { - close_file(rec->tf); + tf_close(rec->tf); rec->tf = NULL; } } @@ -324,7 +321,7 @@ void Instruments::init_load_soundfont(void) void Instruments::end_soundfont(SFInsts *rec) { if (rec->tf) { - close_file(rec->tf); + tf_close(rec->tf); rec->tf = NULL; } @@ -383,7 +380,7 @@ Instrument *Instruments::try_load_soundfont(SFInsts *rec, int order, int bank,in inst = load_from_file(rec, ip); if (opt_sf_close_each_file) { - close_file(rec->tf); + tf_close(rec->tf); rec->tf = NULL; } diff --git a/src/sound/wildmidi/file_io.cpp b/src/sound/wildmidi/file_io.cpp index fa7910a2c..6d20ccba5 100644 --- a/src/sound/wildmidi/file_io.cpp +++ b/src/sound/wildmidi/file_io.cpp @@ -60,7 +60,7 @@ bool _WM_InitReader(const char *config_file) unsigned char *_WM_BufferFile(const char *filename, unsigned long int *size) { - FileReader *fp; + FileRdr fp; if (filename == nullptr) { @@ -72,13 +72,13 @@ unsigned char *_WM_BufferFile(const char *filename, unsigned long int *size) fp = wm_sfreader->OpenFile(filename); } - if (fp == NULL) + if (!fp.isOpen()) { _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno); return NULL; } - long fsize = fp->GetLength(); + auto fsize = fp.GetLength(); if (fsize > WM_MAXFILESIZE) { @@ -95,9 +95,8 @@ unsigned char *_WM_BufferFile(const char *filename, unsigned long int *size) return NULL; } - fp->Read(data, fsize); - delete fp; + fp.Read(data, fsize); data[fsize] = 0; - *size = fsize; + *size = (long)fsize; return data; } diff --git a/src/sound/wildmidi/wildmidi_lib.cpp b/src/sound/wildmidi/wildmidi_lib.cpp index 87afc16de..ee0f085ee 100644 --- a/src/sound/wildmidi/wildmidi_lib.cpp +++ b/src/sound/wildmidi/wildmidi_lib.cpp @@ -676,7 +676,7 @@ static int WM_LoadConfig(const char *config_file, bool main) { int token_count = 0; auto config_parm = config_file; - FileReader *fr = nullptr; + FileRdr fr; if (main) { if (!_WM_InitReader(config_file)) return -1; // unable to open this as a config file.