From 21d6eb99ebfce8be86879a3d48b589f69f5bd380 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 19 Aug 2023 16:16:56 +0200 Subject: [PATCH] use std::vector as return value for the FileReader's buffer readers. --- src/common/audio/music/i_music.cpp | 6 ++--- src/common/audio/sound/s_sound.cpp | 19 ++++++++------- src/common/audio/sound/s_soundinternal.h | 2 +- src/common/cutscenes/movieplayer.cpp | 5 ++-- src/common/engine/sc_man.cpp | 4 ++-- src/common/engine/sc_man.h | 5 ++++ src/common/filesystem/files.h | 24 +++++++++---------- src/common/textures/animlib.cpp | 2 +- src/common/textures/animlib.h | 2 +- src/common/textures/formats/anmtexture.cpp | 2 +- src/common/textures/formats/patchtexture.cpp | 4 ++-- src/common/textures/formats/pcxtexture.cpp | 12 +++++----- src/common/textures/formats/qoitexture.cpp | 4 ++-- .../textures/formats/rawpagetexture.cpp | 2 +- src/common/textures/formats/webptexture.cpp | 6 ++--- src/m_misc.cpp | 8 +++---- src/maploader/glnodes.cpp | 18 +++++++------- src/sound/s_doomsound.cpp | 4 ++-- 18 files changed, 66 insertions(+), 63 deletions(-) diff --git a/src/common/audio/music/i_music.cpp b/src/common/audio/music/i_music.cpp index 6b85807246..fd93959708 100644 --- a/src/common/audio/music/i_music.cpp +++ b/src/common/audio/music/i_music.cpp @@ -182,8 +182,8 @@ static void SetupGenMidi() auto data = fileSystem.OpenFileReader(lump); auto genmidi = data.Read(); - if (genmidi.Size() < 8 + 175 * 36 || memcmp(genmidi.Data(), "#OPL_II#", 8)) return; - ZMusic_SetGenMidi(genmidi.Data()+8); + if (genmidi.size() < 8 + 175 * 36 || memcmp(genmidi.data(), "#OPL_II#", 8)) return; + ZMusic_SetGenMidi(genmidi.data()+8); } static void SetupWgOpn() @@ -337,7 +337,7 @@ static ZMusic_MidiSource GetMIDISource(const char *fn) } auto data = wlump.Read(); - auto source = ZMusic_CreateMIDISource(data.Data(), data.Size(), type); + auto source = ZMusic_CreateMIDISource(data.data(), data.size(), type); if (source == nullptr) { diff --git a/src/common/audio/sound/s_sound.cpp b/src/common/audio/sound/s_sound.cpp index 2544d1f877..f2c44a1c17 100644 --- a/src/common/audio/sound/s_sound.cpp +++ b/src/common/audio/sound/s_sound.cpp @@ -744,32 +744,33 @@ sfxinfo_t *SoundEngine::LoadSound(sfxinfo_t *sfx) DPrintf(DMSG_NOTIFY, "Loading sound \"%s\" (%td)\n", sfx->name.GetChars(), sfx - &S_sfx[0]); auto sfxdata = ReadSound(sfx->lumpnum); - int size = sfxdata.Size(); + int size = (int)sfxdata.size(); if (size > 8) { - int32_t dmxlen = LittleLong(((int32_t *)sfxdata.Data())[1]); + auto sfxp = sfxdata.data(); + int32_t dmxlen = LittleLong(((int32_t *)sfxp)[1]); // If the sound is voc, use the custom loader. - if (strncmp ((const char *)sfxdata.Data(), "Creative Voice File", 19) == 0) + if (memcmp (sfxp, "Creative Voice File", 19) == 0) { - sfx->data = GSnd->LoadSoundVoc(sfxdata.Data(), size); + sfx->data = GSnd->LoadSoundVoc(sfxp, size); } // If the sound is raw, just load it as such. else if (sfx->bLoadRAW) { - sfx->data = GSnd->LoadSoundRaw(sfxdata.Data(), size, sfx->RawRate, 1, 8, sfx->LoopStart); + sfx->data = GSnd->LoadSoundRaw(sfxp, size, sfx->RawRate, 1, 8, sfx->LoopStart); } // Otherwise, try the sound as DMX format. - else if (((uint8_t *)sfxdata.Data())[0] == 3 && ((uint8_t *)sfxdata.Data())[1] == 0 && dmxlen <= size - 8) + else if (((uint8_t *)sfxp)[0] == 3 && ((uint8_t *)sfxp)[1] == 0 && dmxlen <= size - 8) { - int frequency = LittleShort(((uint16_t *)sfxdata.Data())[1]); + int frequency = LittleShort(((uint16_t *)sfxp)[1]); if (frequency == 0) frequency = 11025; - sfx->data = GSnd->LoadSoundRaw(sfxdata.Data()+8, dmxlen, frequency, 1, 8, sfx->LoopStart); + sfx->data = GSnd->LoadSoundRaw(sfxp+8, dmxlen, frequency, 1, 8, sfx->LoopStart); } // If that fails, let the sound system try and figure it out. else { - sfx->data = GSnd->LoadSound(sfxdata.Data(), size, sfx->LoopStart, sfx->LoopEnd); + sfx->data = GSnd->LoadSound(sfxp, size, sfx->LoopStart, sfx->LoopEnd); } } diff --git a/src/common/audio/sound/s_soundinternal.h b/src/common/audio/sound/s_soundinternal.h index 782fbe3254..d8c07742cd 100644 --- a/src/common/audio/sound/s_soundinternal.h +++ b/src/common/audio/sound/s_soundinternal.h @@ -216,7 +216,7 @@ private: // Checks if a copy of this sound is already playing. bool CheckSingular(FSoundID sound_id); - virtual TArray ReadSound(int lumpnum) = 0; + virtual std::vector ReadSound(int lumpnum) = 0; protected: virtual bool CheckSoundLimit(sfxinfo_t* sfx, const FVector3& pos, int near_limit, float limit_range, int sourcetype, const void* actor, int channel, float attenuation); diff --git a/src/common/cutscenes/movieplayer.cpp b/src/common/cutscenes/movieplayer.cpp index 94a7e4c60e..ac915bfb00 100644 --- a/src/common/cutscenes/movieplayer.cpp +++ b/src/common/cutscenes/movieplayer.cpp @@ -153,7 +153,7 @@ class AnmPlayer : public MoviePlayer { // This doesn't need its own class type anim_t anim; - TArray buffer; + std::vector buffer; int numframes = 0; int curframe = 1; int frametime = 0; @@ -173,7 +173,7 @@ public: buffer = fr.ReadPadded(1); fr.Close(); - if (ANIM_LoadAnim(&anim, buffer.Data(), buffer.Size() - 1) < 0) + if (ANIM_LoadAnim(&anim, buffer.data(), buffer.size() - 1) < 0) { return; } @@ -231,7 +231,6 @@ public: ~AnmPlayer() { - buffer.Reset(); animtex.Clean(); } diff --git a/src/common/engine/sc_man.cpp b/src/common/engine/sc_man.cpp index 1568f677bd..98de9f0b18 100644 --- a/src/common/engine/sc_man.cpp +++ b/src/common/engine/sc_man.cpp @@ -148,9 +148,9 @@ bool FScanner::OpenFile (const char *name) if (!fr.OpenFile(name)) return false; auto filesize = fr.GetLength(); auto filebuff = fr.Read(); - if (filebuff.Size() == 0 && filesize > 0) return false; + if (filebuff.size() == 0 && filesize > 0) return false; - ScriptBuffer = FString((const char *)filebuff.Data(), filesize); + ScriptBuffer = FString((const char *)filebuff.data(), filesize); ScriptName = name; // This is used for error messages so the full file name is preferable LumpNum = -1; PrepareScript (); diff --git a/src/common/engine/sc_man.h b/src/common/engine/sc_man.h index 4d66b5f215..307831dba7 100644 --- a/src/common/engine/sc_man.h +++ b/src/common/engine/sc_man.h @@ -1,6 +1,7 @@ #ifndef __SC_MAN_H__ #define __SC_MAN_H__ +#include #include "zstring.h" #include "tarray.h" #include "name.h" @@ -74,6 +75,10 @@ public: { OpenMem(name, (const char*)buffer.Data(), buffer.Size()); } + void OpenMem(const char* name, const std::vector& buffer) + { + OpenMem(name, (const char*)buffer.data(), (int)buffer.size()); + } void OpenString(const char *name, FString buffer); void OpenLumpNum(int lump); void Close(); diff --git a/src/common/filesystem/files.h b/src/common/filesystem/files.h index f8b9f458eb..99a3b133f4 100644 --- a/src/common/filesystem/files.h +++ b/src/common/filesystem/files.h @@ -214,28 +214,26 @@ public: return mReader->Read(buffer, (long)len); } - TArray Read(size_t len) + std::vector Read(size_t len) { - TArray buffer((int)len, true); + std::vector buffer(len); Size length = mReader->Read(&buffer[0], (long)len); - buffer.Clamp((int)length); + buffer.resize((size_t)length); return buffer; } - TArray Read() + std::vector Read() { - TArray buffer(mReader->Length, true); - Size length = mReader->Read(&buffer[0], mReader->Length); - if (length < mReader->Length) buffer.Clear(); - return buffer; + return Read(GetLength()); } - TArray ReadPadded(int padding) + std::vector ReadPadded(size_t padding) { - TArray buffer(mReader->Length + padding, true); - Size length = mReader->Read(&buffer[0], mReader->Length); - if (length < mReader->Length) buffer.Clear(); - else memset(buffer.Data() + mReader->Length, 0, padding); + auto len = GetLength(); + std::vector buffer(len + padding); + Size length = mReader->Read(&buffer[0], (long)len); + if (length < len) buffer.clear(); + else memset(buffer.data() + len, 0, padding); return buffer; } diff --git a/src/common/textures/animlib.cpp b/src/common/textures/animlib.cpp index e42386bd99..949027786a 100644 --- a/src/common/textures/animlib.cpp +++ b/src/common/textures/animlib.cpp @@ -217,7 +217,7 @@ static inline void drawframe(anim_t *anim, uint16_t framenumber) } // is the file size, for consistency checking. -int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, int32_t length) +int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, size_t length) { if (memcmp(buffer, "LPF ", 4)) return -1; diff --git a/src/common/textures/animlib.h b/src/common/textures/animlib.h index 661f00fb28..1fdaa354fd 100644 --- a/src/common/textures/animlib.h +++ b/src/common/textures/animlib.h @@ -105,7 +105,7 @@ struct anim_t // //**************************************************************************** -int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, int32_t length); +int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, size_t length); //**************************************************************************** // diff --git a/src/common/textures/formats/anmtexture.cpp b/src/common/textures/formats/anmtexture.cpp index eeea41a898..f4fb332f0d 100644 --- a/src/common/textures/formats/anmtexture.cpp +++ b/src/common/textures/formats/anmtexture.cpp @@ -73,7 +73,7 @@ FImageSource *AnmImage_TryCreate(FileReader & file, int lumpnum) auto buffer = file.ReadPadded(1); anim_t anim; - if (ANIM_LoadAnim(&anim, buffer.Data(), buffer.Size() - 1) < 0) + if (ANIM_LoadAnim(&anim, buffer.data(), buffer.size() - 1) < 0) { return nullptr; } diff --git a/src/common/textures/formats/patchtexture.cpp b/src/common/textures/formats/patchtexture.cpp index ecbd09147d..d64e003e2c 100644 --- a/src/common/textures/formats/patchtexture.cpp +++ b/src/common/textures/formats/patchtexture.cpp @@ -81,7 +81,7 @@ static bool CheckIfPatch(FileReader & file, bool &isalpha) file.Seek(0, FileReader::SeekSet); auto data = file.Read(file.GetLength()); - const patch_t *foo = (const patch_t *)data.Data(); + const patch_t *foo = (const patch_t *)data.data(); int height = LittleShort(foo->height); int width = LittleShort(foo->width); @@ -111,7 +111,7 @@ static bool CheckIfPatch(FileReader & file, bool &isalpha) { // only check this if the texture passed validation. // Here is a good point because we already have a valid buffer of the lump's data. - isalpha = checkPatchForAlpha(data.Data(), (uint32_t)file.GetLength()); + isalpha = checkPatchForAlpha(data.data(), (uint32_t)file.GetLength()); } return !gapAtStart; } diff --git a/src/common/textures/formats/pcxtexture.cpp b/src/common/textures/formats/pcxtexture.cpp index 7e97ba2eff..bc014659f8 100644 --- a/src/common/textures/formats/pcxtexture.cpp +++ b/src/common/textures/formats/pcxtexture.cpp @@ -160,8 +160,8 @@ void FPCXTexture::ReadPCX1bit (uint8_t *dst, FileReader & lump, PCXHeader *hdr) int rle_count = 0; uint8_t rle_value = 0; - TArray srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader)); - uint8_t * src = srcp.Data(); + auto srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader)); + uint8_t * src = srcp.data(); for (y = 0; y < Height; ++y) { @@ -210,8 +210,8 @@ void FPCXTexture::ReadPCX4bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr) TArray line(hdr->bytesPerScanLine, true); TArray colorIndex(Width, true); - TArray srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader)); - uint8_t * src = srcp.Data(); + auto srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader)); + uint8_t * src = srcp.data(); for (y = 0; y < Height; ++y) { @@ -265,7 +265,7 @@ void FPCXTexture::ReadPCX8bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr) int y, bytes; auto srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader)); - uint8_t * src = srcp.Data(); + uint8_t * src = srcp.data(); for (y = 0; y < Height; ++y) { @@ -306,7 +306,7 @@ void FPCXTexture::ReadPCX24bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr int bytes; auto srcp = lump.Read(lump.GetLength() - sizeof(PCXHeader)); - uint8_t * src = srcp.Data(); + uint8_t * src = srcp.data(); for (y = 0; y < Height; ++y) { diff --git a/src/common/textures/formats/qoitexture.cpp b/src/common/textures/formats/qoitexture.cpp index d0aa2fd810..223a0b70e1 100644 --- a/src/common/textures/formats/qoitexture.cpp +++ b/src/common/textures/formats/qoitexture.cpp @@ -142,13 +142,13 @@ int FQOITexture::CopyPixels(FBitmap *bmp, int conversion) auto lump = fileSystem.OpenFileReader(SourceLump); auto bytes = lump.Read(); - if (bytes.Size() < 22) return 0; // error + if (bytes.size() < 22) return 0; // error PalEntry index[64] = {}; PalEntry pe = 0xff000000; size_t p = 14, run = 0; - size_t chunks_len = bytes.Size() - 8; + size_t chunks_len = bytes.size() - 8; for (int h = 0; h < Height; h++) { diff --git a/src/common/textures/formats/rawpagetexture.cpp b/src/common/textures/formats/rawpagetexture.cpp index 09d07d0ce6..d28714d813 100644 --- a/src/common/textures/formats/rawpagetexture.cpp +++ b/src/common/textures/formats/rawpagetexture.cpp @@ -74,7 +74,7 @@ bool CheckIfRaw(FileReader & data, unsigned desiredsize) data.Seek(0, FileReader::SeekSet); auto bits = data.Read(data.GetLength()); - foo = (patch_t *)bits.Data(); + foo = (patch_t *)bits.data(); height = LittleShort(foo->height); width = LittleShort(foo->width); diff --git a/src/common/textures/formats/webptexture.cpp b/src/common/textures/formats/webptexture.cpp index 60a9ccbc38..dd9a9c388d 100644 --- a/src/common/textures/formats/webptexture.cpp +++ b/src/common/textures/formats/webptexture.cpp @@ -59,9 +59,9 @@ FImageSource *WebPImage_TryCreate(FileReader &file, int lumpnum) file.Seek(0, FileReader::SeekSet); auto bytes = file.Read(); - if (WebPGetInfo(bytes.Data(), bytes.Size(), &width, &height)) + if (WebPGetInfo(bytes.data(), bytes.size(), &width, &height)) { - WebPData data{ bytes.Data(), bytes.Size() }; + WebPData data{ bytes.data(), bytes.size() }; WebPData chunk_data; auto mux = WebPMuxCreate(&data, 0); if (mux) @@ -137,7 +137,7 @@ int FWebPTexture::CopyPixels(FBitmap *bmp, int conversion) config.output.u.RGBA.stride = bmp->GetPitch(); config.output.is_external_memory = 1; - (void)WebPDecode(bytes.Data(), bytes.Size(), &config); + (void)WebPDecode(bytes.data(), bytes.size(), &config); return 0; } diff --git a/src/m_misc.cpp b/src/m_misc.cpp index 45af80cec7..5960dfa36e 100644 --- a/src/m_misc.cpp +++ b/src/m_misc.cpp @@ -98,7 +98,7 @@ void M_FindResponseFile (void) else { char **argv; - TArray file; + std::vector file; int argc = 0; int size; size_t argsize = 0; @@ -119,8 +119,8 @@ void M_FindResponseFile (void) Printf ("Found response file %s!\n", Args->GetArg(i) + 1); size = (int)fr.GetLength(); file = fr.Read (size); - file[size] = 0; - argsize = ParseCommandLine ((char*)file.Data(), &argc, NULL); + file.push_back(0); + argsize = ParseCommandLine ((char*)file.data(), &argc, NULL); } } else @@ -132,7 +132,7 @@ void M_FindResponseFile (void) { argv = (char **)M_Malloc (argc*sizeof(char *) + argsize); argv[0] = (char *)argv + argc*sizeof(char *); - ParseCommandLine ((char*)file.Data(), NULL, argv); + ParseCommandLine ((char*)file.data(), NULL, argv); // Create a new argument vector FArgs *newargs = new FArgs; diff --git a/src/maploader/glnodes.cpp b/src/maploader/glnodes.cpp index 38fa976fd6..76f0de303d 100644 --- a/src/maploader/glnodes.cpp +++ b/src/maploader/glnodes.cpp @@ -209,7 +209,7 @@ bool MapLoader::LoadGLVertexes(FileReader &lump) auto gllen=lump.GetLength(); if (gllen < 4) return false; - auto gldata = glbuf.Data(); + auto gldata = glbuf.data(); if (*(int *)gldata == gNd5) { @@ -288,13 +288,13 @@ bool MapLoader::LoadGLSegs(FileReader &lump) const unsigned numverts = Level->vertexes.Size(); const unsigned numlines = Level->lines.Size(); - if (!format5 && memcmp(data.Data(), "gNd3", 4)) + if (!format5 && memcmp(data.data(), "gNd3", 4)) { numsegs/=sizeof(glseg_t); segs.Alloc(numsegs); memset(&segs[0],0,sizeof(seg_t)*numsegs); - glseg_t * ml = (glseg_t*)data.Data(); + glseg_t * ml = (glseg_t*)data.data(); for(i = 0; i < numsegs; i++) { // check for gl-vertices @@ -365,7 +365,7 @@ bool MapLoader::LoadGLSegs(FileReader &lump) segs.Alloc(numsegs); memset(&segs[0],0,sizeof(seg_t)*numsegs); - glseg3_t * ml = (glseg3_t*)(data.Data() + (format5? 0:4)); + glseg3_t * ml = (glseg3_t*)(data.data() + (format5? 0:4)); for(i = 0; i < numsegs; i++) { // check for gl-vertices const unsigned v1idx = checkGLVertex3(LittleLong(ml->v1)); @@ -454,9 +454,9 @@ bool MapLoader::LoadGLSubsectors(FileReader &lump) return false; } - if (!format5 && memcmp(datab.Data(), "gNd3", 4)) + if (!format5 && memcmp(datab.data(), "gNd3", 4)) { - mapsubsector_t * data = (mapsubsector_t*) datab.Data(); + mapsubsector_t * data = (mapsubsector_t*) datab.data(); numsubsectors /= sizeof(mapsubsector_t); Level->subsectors.Alloc(numsubsectors); auto &subsectors = Level->subsectors; @@ -478,7 +478,7 @@ bool MapLoader::LoadGLSubsectors(FileReader &lump) } else { - gl3_mapsubsector_t * data = (gl3_mapsubsector_t*) (datab.Data()+(format5? 0:4)); + gl3_mapsubsector_t * data = (gl3_mapsubsector_t*) (datab.data()+(format5? 0:4)); numsubsectors /= sizeof(gl3_mapsubsector_t); Level->subsectors.Alloc(numsubsectors); auto &subsectors = Level->subsectors; @@ -545,7 +545,7 @@ bool MapLoader::LoadNodes (FileReader &lump) lump.Seek(0, FileReader::SeekSet); auto buf = lump.Read(); - basemn = mn = (mapnode_t*)buf.Data(); + basemn = mn = (mapnode_t*)buf.data(); used.Resize(numnodes); memset (used.Data(), 0, sizeof(uint16_t)*numnodes); @@ -601,7 +601,7 @@ bool MapLoader::LoadNodes (FileReader &lump) lump.Seek(0, FileReader::SeekSet); auto buf = lump.Read(); - basemn = mn = (gl5_mapnode_t*)buf.Data(); + basemn = mn = (gl5_mapnode_t*)buf.data(); used.Resize(numnodes); memset(used.Data(), 0, sizeof(uint16_t)*numnodes); diff --git a/src/sound/s_doomsound.cpp b/src/sound/s_doomsound.cpp index 50509eb3d6..6a67fa446e 100644 --- a/src/sound/s_doomsound.cpp +++ b/src/sound/s_doomsound.cpp @@ -81,7 +81,7 @@ class DoomSoundEngine : public SoundEngine void CalcPosVel(int type, const void* source, const float pt[3], int channum, int chanflags, FSoundID soundid, FVector3* pos, FVector3* vel, FSoundChan *) override; bool ValidatePosVel(int sourcetype, const void* source, const FVector3& pos, const FVector3& vel); - TArray ReadSound(int lumpnum); + std::vector ReadSound(int lumpnum); FSoundID PickReplacement(FSoundID refid); FSoundID ResolveSound(const void *ent, int type, FSoundID soundid, float &attenuation) override; void CacheSound(sfxinfo_t* sfx) override; @@ -1165,7 +1165,7 @@ bool DoomSoundEngine::ValidatePosVel(int sourcetype, const void* source, const F // //========================================================================== -TArray DoomSoundEngine::ReadSound(int lumpnum) +std::vector DoomSoundEngine::ReadSound(int lumpnum) { auto wlump = fileSystem.OpenFileReader(lumpnum); return wlump.Read();