From 191f2d9d76813256f8ad1a4be93caab94a03e0f5 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 10 Nov 2018 14:19:55 +0100 Subject: [PATCH] - use TArray and FString in resource file management. --- src/d_iwad.cpp | 4 +-- src/d_main.cpp | 2 +- src/resourcefiles/file_7z.cpp | 4 +-- src/resourcefiles/file_directory.cpp | 8 ++--- src/resourcefiles/file_grp.cpp | 3 +- src/resourcefiles/file_lump.cpp | 20 ++++++------ src/resourcefiles/file_pak.cpp | 3 +- src/resourcefiles/file_wad.cpp | 6 ++-- src/resourcefiles/file_zip.cpp | 12 ++++---- src/resourcefiles/resourcefile.cpp | 46 ++++++++++------------------ src/resourcefiles/resourcefile.h | 8 ++--- src/tarray.h | 6 ++-- src/w_wad.cpp | 6 ++-- 13 files changed, 55 insertions(+), 73 deletions(-) diff --git a/src/d_iwad.cpp b/src/d_iwad.cpp index bb2c091a3..2716d1eb1 100644 --- a/src/d_iwad.cpp +++ b/src/d_iwad.cpp @@ -257,7 +257,7 @@ FIWadManager::FIWadManager(const char *fn) if (lmp->Namespace == ns_global && !stricmp(lmp->Name, "IWADINFO")) { // Found one! - ParseIWadInfo(resfile->Filename, (const char*)lmp->CacheLump(), lmp->LumpSize); + ParseIWadInfo(resfile->FileName, (const char*)lmp->CacheLump(), lmp->LumpSize); break; } } @@ -349,7 +349,7 @@ int FIWadManager::CheckIWADInfo(const char *fn) try { FIWADInfo result; - ParseIWadInfo(resfile->Filename, (const char*)lmp->CacheLump(), lmp->LumpSize, &result); + ParseIWadInfo(resfile->FileName, (const char*)lmp->CacheLump(), lmp->LumpSize, &result); delete resfile; for (unsigned i = 0, count = mIWadInfos.Size(); i < count; ++i) diff --git a/src/d_main.cpp b/src/d_main.cpp index 01c97b2ae..0a02f6d99 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1905,7 +1905,7 @@ static FString CheckGameInfo(TArray & pwads) if (lmp->Namespace == ns_global && !stricmp(lmp->Name, "GAMEINFO")) { // Found one! - FString iwad = ParseGameInfo(pwads, resfile->Filename, (const char*)lmp->CacheLump(), lmp->LumpSize); + FString iwad = ParseGameInfo(pwads, resfile->FileName, (const char*)lmp->CacheLump(), lmp->LumpSize); delete resfile; return iwad; } diff --git a/src/resourcefiles/file_7z.cpp b/src/resourcefiles/file_7z.cpp index 79ecea583..01bd043ef 100644 --- a/src/resourcefiles/file_7z.cpp +++ b/src/resourcefiles/file_7z.cpp @@ -230,7 +230,7 @@ bool F7ZFile::Open(bool quiet) Archive = NULL; if (!quiet) { - Printf("\n" TEXTCOLOR_RED "%s: ", Filename); + Printf("\n" TEXTCOLOR_RED "%s: ", FileName.GetChars()); if (res == SZ_ERROR_UNSUPPORTED) { Printf("Decoder does not support this archive\n"); @@ -309,7 +309,7 @@ bool F7ZFile::Open(bool quiet) if (SZ_OK != Archive->Extract(Lumps[0].Position, &temp[0])) { - if (!quiet) Printf("\n%s: unsupported 7z/LZMA file!\n", Filename); + if (!quiet) Printf("\n%s: unsupported 7z/LZMA file!\n", FileName.GetChars()); return false; } } diff --git a/src/resourcefiles/file_directory.cpp b/src/resourcefiles/file_directory.cpp index 03b33e400..1f2d63d18 100644 --- a/src/resourcefiles/file_directory.cpp +++ b/src/resourcefiles/file_directory.cpp @@ -102,9 +102,9 @@ FDirectory::FDirectory(const char * directory) #ifdef _WIN32 free((void *)directory); #endif - dirname.ReplaceChars('\\', '/'); + FixPathSeperator(dirname); if (dirname[dirname.Len()-1] != '/') dirname += '/'; - Filename = copystring(dirname); + FileName = dirname; } @@ -248,7 +248,7 @@ int FDirectory::AddDirectory(const char *dirpath) bool FDirectory::Open(bool quiet) { - NumLumps = AddDirectory(Filename); + NumLumps = AddDirectory(FileName); if (!quiet) Printf(", %d lumps\n", NumLumps); PostProcessArchive(&Lumps[0], sizeof(FDirectoryLump)); return true; @@ -268,7 +268,7 @@ void FDirectory::AddEntry(const char *fullpath, int size) lump_p->mFullPath = fullpath; // [mxd] Convert name to lowercase - FString name = fullpath + strlen(Filename); + FString name = fullpath + strlen(FileName); name.ToLower(); // The lump's name is only the part relative to the main directory diff --git a/src/resourcefiles/file_grp.cpp b/src/resourcefiles/file_grp.cpp index c0458276a..ba0493dd6 100644 --- a/src/resourcefiles/file_grp.cpp +++ b/src/resourcefiles/file_grp.cpp @@ -85,7 +85,6 @@ public: FGrpFile::FGrpFile(const char *filename, FileReader &file) : FUncompressedFile(filename, file) { - Lumps = NULL; } //========================================================================== @@ -104,7 +103,7 @@ bool FGrpFile::Open(bool quiet) GrpLump *fileinfo = new GrpLump[NumLumps]; Reader.Read (fileinfo, NumLumps * sizeof(GrpLump)); - Lumps = new FUncompressedLump[NumLumps]; + Lumps.Resize(NumLumps); int Position = sizeof(GrpInfo) + NumLumps * sizeof(GrpLump); diff --git a/src/resourcefiles/file_lump.cpp b/src/resourcefiles/file_lump.cpp index d7eb68e88..da458303a 100644 --- a/src/resourcefiles/file_lump.cpp +++ b/src/resourcefiles/file_lump.cpp @@ -69,17 +69,17 @@ FLumpFile::FLumpFile(const char *filename, FileReader &file) bool FLumpFile::Open(bool quiet) { - FString name(ExtractFileBase (Filename)); + FString name(ExtractFileBase (FileName)); - Lumps = new FUncompressedLump[1]; // must use array allocator - uppercopy(Lumps->Name, name); - Lumps->Name[8] = 0; - Lumps->Owner = this; - Lumps->Position = 0; - Lumps->LumpSize = (int)Reader.GetLength(); - Lumps->Namespace = ns_global; - Lumps->Flags = 0; - Lumps->FullName = NULL; + Lumps.Resize(1); + uppercopy(Lumps[0].Name, name); + Lumps[0].Name[8] = 0; + Lumps[0].Owner = this; + Lumps[0].Position = 0; + Lumps[0].LumpSize = (int)Reader.GetLength(); + Lumps[0].Namespace = ns_global; + Lumps[0].Flags = 0; + Lumps[0].FullName = NULL; NumLumps = 1; if (!quiet) { diff --git a/src/resourcefiles/file_pak.cpp b/src/resourcefiles/file_pak.cpp index c2705a97f..d7f091c02 100644 --- a/src/resourcefiles/file_pak.cpp +++ b/src/resourcefiles/file_pak.cpp @@ -80,7 +80,6 @@ public: FPakFile::FPakFile(const char *filename, FileReader &file) : FUncompressedFile(filename, file) { - Lumps = NULL; } //========================================================================== @@ -101,7 +100,7 @@ bool FPakFile::Open(bool quiet) Reader.Seek (header.dirofs, FileReader::SeekSet); Reader.Read (fileinfo, NumLumps * sizeof(dpackfile_t)); - Lumps = new FUncompressedLump[NumLumps]; + Lumps.Resize(NumLumps); if (!quiet && !batchrun) Printf(", %d lumps\n", NumLumps); diff --git a/src/resourcefiles/file_wad.cpp b/src/resourcefiles/file_wad.cpp index a0ae033db..c1f5b3fdb 100644 --- a/src/resourcefiles/file_wad.cpp +++ b/src/resourcefiles/file_wad.cpp @@ -165,7 +165,7 @@ bool FWadFile::Open(bool quiet) // Check again to detect broken wads if (InfoTableOfs + NumLumps*sizeof(wadlump_t) > (unsigned)wadSize) { - I_Error("Cannot load broken WAD file %s\n", Filename); + I_Error("Cannot load broken WAD file %s\n", FileName.GetChars()); } } @@ -194,7 +194,7 @@ bool FWadFile::Open(bool quiet) { if (Lumps[i].LumpSize != 0) { - Printf(PRINT_HIGH, "%s: Lump %s contains invalid positioning info and will be ignored\n", Filename, Lumps[i].Name); + Printf(PRINT_HIGH, "%s: Lump %s contains invalid positioning info and will be ignored\n", FileName.GetChars(), Lumps[i].Name); Lumps[i].Name[0] = 0; } Lumps[i].LumpSize = Lumps[i].Position = 0; @@ -439,7 +439,7 @@ void FWadFile::SkinHack () "The maps in %s will not be loaded because it has a skin.\n" TEXTCOLOR_BLUE "You should remove the skin from the wad to play these maps.\n", - Filename); + FileName.GetChars()); } } diff --git a/src/resourcefiles/file_zip.cpp b/src/resourcefiles/file_zip.cpp index b3bfa7a2d..04430384e 100644 --- a/src/resourcefiles/file_zip.cpp +++ b/src/resourcefiles/file_zip.cpp @@ -181,7 +181,7 @@ bool FZipFile::Open(bool quiet) if (centraldir == 0) { - if (!quiet) Printf(TEXTCOLOR_RED "\n%s: ZIP file corrupt!\n", Filename); + if (!quiet) Printf(TEXTCOLOR_RED "\n%s: ZIP file corrupt!\n", FileName.GetChars()); return false; } @@ -193,7 +193,7 @@ bool FZipFile::Open(bool quiet) if (info.NumEntries != info.NumEntriesOnAllDisks || info.FirstDisk != 0 || info.DiskNumber != 0) { - if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Multipart Zip files are not supported.\n", Filename); + if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Multipart Zip files are not supported.\n", FileName.GetChars()); return false; } @@ -229,7 +229,7 @@ bool FZipFile::Open(bool quiet) if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file. { free(directory); - if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", Filename); + if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", FileName.GetChars()); return false; } @@ -300,7 +300,7 @@ bool FZipFile::Open(bool quiet) if (dirptr > ((char*)directory) + dirsize) // This directory entry goes beyond the end of the file. { free(directory); - if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", Filename); + if (!quiet) Printf(TEXTCOLOR_RED "\n%s: Central directory corrupted.", FileName.GetChars()); return false; } @@ -320,7 +320,7 @@ bool FZipFile::Open(bool quiet) zip_fh->Method != METHOD_IMPLODE && zip_fh->Method != METHOD_SHRINK) { - if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' uses an unsupported compression algorithm (#%d).\n", Filename, name.GetChars(), zip_fh->Method); + if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' uses an unsupported compression algorithm (#%d).\n", FileName.GetChars(), name.GetChars(), zip_fh->Method); skipped++; continue; } @@ -328,7 +328,7 @@ bool FZipFile::Open(bool quiet) zip_fh->Flags = LittleShort(zip_fh->Flags); if (zip_fh->Flags & ZF_ENCRYPTED) { - if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' is encrypted. Encryption is not supported.\n", Filename, name.GetChars()); + if (!quiet) Printf(TEXTCOLOR_YELLOW "\n%s: '%s' is encrypted. Encryption is not supported.\n", FileName.GetChars(), name.GetChars()); skipped++; continue; } diff --git a/src/resourcefiles/resourcefile.cpp b/src/resourcefiles/resourcefile.cpp index 419bd4707..1cb1f4e9f 100644 --- a/src/resourcefiles/resourcefile.cpp +++ b/src/resourcefiles/resourcefile.cpp @@ -156,7 +156,7 @@ static bool IsWadInFolder(const FResourceFile* const archive, const char* const return false; } - const FString dirName = ExtractFileBase(archive->Filename); + const FString dirName = ExtractFileBase(archive->FileName); const FString fileName = ExtractFileBase(resPath, true); const FString filePath = dirName + '/' + fileName; @@ -323,9 +323,8 @@ FResourceFile *FResourceFile::OpenDirectory(const char *filename, bool quiet) //========================================================================== FResourceFile::FResourceFile(const char *filename) + : FileName(filename) { - if (filename != NULL) Filename = copystring(filename); - else Filename = NULL; } FResourceFile::FResourceFile(const char *filename, FileReader &r) @@ -336,7 +335,6 @@ FResourceFile::FResourceFile(const char *filename, FileReader &r) FResourceFile::~FResourceFile() { - if (Filename != NULL) delete [] Filename; } int lumpcmp(const void * a, const void * b) @@ -653,12 +651,6 @@ FUncompressedFile::FUncompressedFile(const char *filename, FileReader &r) : FResourceFile(filename, r) {} -FUncompressedFile::~FUncompressedFile() -{ - if (Lumps != NULL) delete [] Lumps; -} - - //========================================================================== // @@ -667,9 +659,8 @@ FUncompressedFile::~FUncompressedFile() //========================================================================== FExternalLump::FExternalLump(const char *_filename, int filesize) + : Filename(_filename) { - filename = _filename? copystring(_filename) : NULL; - if (filesize == -1) { FileReader f; @@ -690,11 +681,6 @@ FExternalLump::FExternalLump(const char *_filename, int filesize) } -FExternalLump::~FExternalLump() -{ - if (filename != NULL) delete [] filename; -} - //========================================================================== // // Caches a lump's content and increases the reference counter @@ -707,7 +693,7 @@ int FExternalLump::FillCache() Cache = new char[LumpSize]; FileReader f; - if (f.OpenFile(filename)) + if (f.OpenFile(Filename)) { f.Read(Cache, LumpSize); } @@ -722,19 +708,19 @@ int FExternalLump::FillCache() bool FMemoryFile::Open(bool quiet) { - FString name(ExtractFileBase(Filename)); - FString fname(ExtractFileBase(Filename, true)); + FString name(ExtractFileBase(FileName)); + FString fname(ExtractFileBase(FileName, true)); - Lumps = new FUncompressedLump[1]; // must use array allocator - uppercopy(Lumps->Name, name); - Lumps->Name[8] = 0; - Lumps->FullName = fname; - Lumps->Owner = this; - Lumps->Position = 0; - Lumps->LumpSize = (int)Reader.GetLength(); - Lumps->Namespace = ns_global; - Lumps->Flags = 0; - Lumps->FullName = NULL; + Lumps.Resize(1); + uppercopy(Lumps[0].Name, name); + Lumps[0].Name[8] = 0; + Lumps[0].FullName = fname; + Lumps[0].Owner = this; + Lumps[0].Position = 0; + Lumps[0].LumpSize = (int)Reader.GetLength(); + Lumps[0].Namespace = ns_global; + Lumps[0].Flags = 0; + Lumps[0].FullName = nullptr; NumLumps = 1; return true; } diff --git a/src/resourcefiles/resourcefile.h b/src/resourcefiles/resourcefile.h index 9b39b0908..9ad2e43cf 100644 --- a/src/resourcefiles/resourcefile.h +++ b/src/resourcefiles/resourcefile.h @@ -82,7 +82,7 @@ class FResourceFile { public: FileReader Reader; - const char *Filename; + FString FileName; protected: uint32_t NumLumps; @@ -133,21 +133,19 @@ struct FUncompressedLump : public FResourceLump class FUncompressedFile : public FResourceFile { protected: - FUncompressedLump * Lumps = nullptr; + TArray Lumps; FUncompressedFile(const char *filename); FUncompressedFile(const char *filename, FileReader &r); - virtual ~FUncompressedFile(); virtual FResourceLump *GetLump(int no) { return ((unsigned)no < NumLumps)? &Lumps[no] : NULL; } }; struct FExternalLump : public FResourceLump { - const char *filename; // the actual file name. This is not necessarily the same as the lump name! + FString Filename; FExternalLump(const char *_filename, int filesize = -1); - ~FExternalLump(); virtual int FillCache(); }; diff --git a/src/tarray.h b/src/tarray.h index 77fc2fa5c..e5f58b548 100644 --- a/src/tarray.h +++ b/src/tarray.h @@ -431,11 +431,11 @@ public: } // Reserves amount entries at the end of the array, but does nothing // with them. - unsigned int Reserve (unsigned int amount) + unsigned int Reserve (size_t amount) { - Grow (amount); + Grow ((unsigned)amount); unsigned int place = Count; - Count += amount; + Count += (unsigned)amount; if (Count > 0) ConstructEmpty(place, Count - 1); return place; } diff --git a/src/w_wad.cpp b/src/w_wad.cpp index 177a8b802..9c0dd3760 100644 --- a/src/w_wad.cpp +++ b/src/w_wad.cpp @@ -1413,7 +1413,7 @@ const char *FWadCollection::GetWadName (int wadnum) const return NULL; } - name = Files[wadnum]->Filename; + name = Files[wadnum]->FileName; slash = strrchr (name, '/'); return slash != NULL ? slash+1 : name; } @@ -1476,10 +1476,10 @@ const char *FWadCollection::GetWadFullName (int wadnum) const { if ((unsigned int)wadnum >= Files.Size()) { - return NULL; + return nullptr; } - return Files[wadnum]->Filename; + return Files[wadnum]->FileName; }