From 1d3062b33f1e0ddee41c511e4c0222f6ca9293c6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 26 Aug 2019 14:37:19 -0400 Subject: [PATCH] General: Make use of FopenUnique where applicable Migrates to the hecl Fopen variant that automatically closes its contained file handle if it goes out of scope. --- DataSpec/DNACommon/PAK.cpp | 5 ++--- DataSpec/DNACommon/TXTR.cpp | 42 +++++++++++------------------------ DataSpec/DNAMP1/SCLY.cpp | 4 ++-- DataSpec/SpecBase.cpp | 12 +++++----- DataSpec/SpecMP1.cpp | 10 ++++----- Editor/ViewManager.cpp | 42 ++++++++++++++++++++--------------- Runtime/CMemoryCardSysNix.cpp | 6 ++--- Runtime/CMemoryCardSysOSX.cpp | 6 ++--- Runtime/CMemoryCardSysWin.cpp | 6 ++--- 9 files changed, 60 insertions(+), 73 deletions(-) diff --git a/DataSpec/DNACommon/PAK.cpp b/DataSpec/DNACommon/PAK.cpp index b9a0c0306..1fdb109d1 100644 --- a/DataSpec/DNACommon/PAK.cpp +++ b/DataSpec/DNACommon/PAK.cpp @@ -466,9 +466,8 @@ bool PAKRouter::extractResources(const BRIDGETYPE& pakBridge, bool f if (force || cooked.isNone()) { cooked.makeDirChain(false); PAKEntryReadStream s = entryPtr->beginReadStream(*node); - FILE* fout = hecl::Fopen(cooked.getAbsolutePath().data(), _SYS_STR("wb")); - fwrite(s.data(), 1, s.length(), fout); - fclose(fout); + const auto fout = hecl::FopenUnique(cooked.getAbsolutePath().data(), _SYS_STR("wb")); + std::fwrite(s.data(), 1, s.length(), fout.get()); } if (extractor.func_a) /* Doesn't need PAKRouter access */ diff --git a/DataSpec/DNACommon/TXTR.cpp b/DataSpec/DNACommon/TXTR.cpp index ef0e59962..c33145784 100644 --- a/DataSpec/DNACommon/TXTR.cpp +++ b/DataSpec/DNACommon/TXTR.cpp @@ -812,13 +812,13 @@ bool TXTR::Extract(PAKEntryReadStream& rs, const hecl::ProjectPath& outPath) { uint16_t height = rs.readUint16Big(); uint32_t numMips = rs.readUint32Big(); - FILE* fp = hecl::Fopen(outPath.getAbsolutePath().data(), _SYS_STR("wb")); - if (!fp) { + auto fp = hecl::FopenUnique(outPath.getAbsolutePath().data(), _SYS_STR("wb")); + if (fp == nullptr) { Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for writing")), outPath.getAbsolutePath()); return false; } png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGErr, PNGWarn); - png_init_io(png, fp); + png_init_io(png, fp.get()); png_infop info = png_create_info_struct(png); png_text textStruct = {}; @@ -862,7 +862,6 @@ bool TXTR::Extract(PAKEntryReadStream& rs, const hecl::ProjectPath& outPath) { png_write_end(png, info); png_write_flush(png); png_destroy_write_struct(&png, &info); - fclose(fp); return true; } @@ -1017,18 +1016,17 @@ static int GetNumPaletteEntriesForGCN(png_structp png, png_infop info) { } bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPath) { - FILE* inf = hecl::Fopen(inPath.getAbsolutePath().data(), _SYS_STR("rb")); - if (!inf) { + auto inf = hecl::FopenUnique(inPath.getAbsolutePath().data(), _SYS_STR("rb")); + if (inf == nullptr) { Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for reading")), inPath.getAbsolutePath()); return false; } /* Validate PNG */ char header[8]; - fread(header, 1, 8, inf); + std::fread(header, 1, sizeof(header), inf.get()); if (png_sig_cmp((png_const_bytep)header, 0, 8)) { Log.report(logvisor::Error, fmt(_SYS_STR("invalid PNG signature in '{}'")), inPath.getAbsolutePath()); - fclose(inf); return false; } @@ -1036,25 +1034,22 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat png_structp pngRead = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if (!pngRead) { Log.report(logvisor::Error, fmt("unable to initialize libpng")); - fclose(inf); return false; } png_infop info = png_create_info_struct(pngRead); if (!info) { Log.report(logvisor::Error, fmt("unable to initialize libpng info")); - fclose(inf); png_destroy_read_struct(&pngRead, nullptr, nullptr); return false; } if (setjmp(png_jmpbuf(pngRead))) { Log.report(logvisor::Error, fmt(_SYS_STR("unable to initialize libpng I/O for '{}'")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } - png_init_io(pngRead, inf); + png_init_io(pngRead, inf.get()); png_set_sig_bytes(pngRead, 8); png_read_info(pngRead, info); @@ -1066,7 +1061,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat if (width < 4 || height < 4) { Log.report(logvisor::Error, fmt("image must be 4x4 or larger")); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1092,7 +1086,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat if (bitDepth != 8) { Log.report(logvisor::Error, fmt(_SYS_STR("'{}' is not 8 bits-per-channel")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1124,7 +1117,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat break; default: Log.report(logvisor::Error, fmt(_SYS_STR("unsupported color type in '{}'")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1145,7 +1137,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat if (setjmp(png_jmpbuf(pngRead))) { Log.report(logvisor::Fatal, fmt(_SYS_STR("unable to read image in '{}'")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1178,7 +1169,7 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat } png_destroy_read_struct(&pngRead, &info, nullptr); - fclose(inf); + inf.reset(); /* Reduce mipmaps to minimum allowed dimensions */ unsigned minDimX, minDimY; @@ -1338,18 +1329,17 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat } bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPath) { - FILE* inf = hecl::Fopen(inPath.getAbsolutePath().data(), _SYS_STR("rb")); - if (!inf) { + auto inf = hecl::FopenUnique(inPath.getAbsolutePath().data(), _SYS_STR("rb")); + if (inf == nullptr) { Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for reading")), inPath.getAbsolutePath()); return false; } /* Validate PNG */ char header[8]; - fread(header, 1, 8, inf); + std::fread(header, 1, sizeof(header), inf.get()); if (png_sig_cmp((png_const_bytep)header, 0, 8)) { Log.report(logvisor::Error, fmt(_SYS_STR("invalid PNG signature in '{}'")), inPath.getAbsolutePath()); - fclose(inf); return false; } @@ -1357,25 +1347,22 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP png_structp pngRead = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if (!pngRead) { Log.report(logvisor::Error, fmt("unable to initialize libpng")); - fclose(inf); return false; } png_infop info = png_create_info_struct(pngRead); if (!info) { Log.report(logvisor::Error, fmt("unable to initialize libpng info")); - fclose(inf); png_destroy_read_struct(&pngRead, nullptr, nullptr); return false; } if (setjmp(png_jmpbuf(pngRead))) { Log.report(logvisor::Error, fmt(_SYS_STR("unable to initialize libpng I/O for '{}'")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } - png_init_io(pngRead, inf); + png_init_io(pngRead, inf.get()); png_set_sig_bytes(pngRead, 8); png_read_info(pngRead, info); @@ -1406,7 +1393,6 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP if (bitDepth != 8) { Log.report(logvisor::Error, fmt(_SYS_STR("'{}' is not 8 bits-per-channel")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1436,7 +1422,6 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP break; default: Log.report(logvisor::Error, fmt(_SYS_STR("unsupported color type in '{}'")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1455,7 +1440,6 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP if (setjmp(png_jmpbuf(pngRead))) { Log.report(logvisor::Fatal, fmt(_SYS_STR("unable to read image in '{}'")), inPath.getAbsolutePath()); - fclose(inf); png_destroy_read_struct(&pngRead, &info, nullptr); return false; } @@ -1521,7 +1505,7 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP } png_destroy_read_struct(&pngRead, &info, nullptr); - fclose(inf); + inf.reset(); /* Perform box-filter mipmap */ if (numMips > 1) { diff --git a/DataSpec/DNAMP1/SCLY.cpp b/DataSpec/DNAMP1/SCLY.cpp index 8d8889902..cc56ef7fc 100644 --- a/DataSpec/DNAMP1/SCLY.cpp +++ b/DataSpec/DNAMP1/SCLY.cpp @@ -42,8 +42,8 @@ void SCLY::exportToLayerDirectories(const PAK::Entry& entry, PAKRouter bool { @@ -700,10 +701,9 @@ struct SpecMP1 : SpecBase { return false; })) { resTag.id = path.hash().val32(); - fclose(fp); + fp.reset(); return resTag; } - fclose(fp); } return {}; } diff --git a/Editor/ViewManager.cpp b/Editor/ViewManager.cpp index c55cd6ae7..cb53a63fb 100644 --- a/Editor/ViewManager.cpp +++ b/Editor/ViewManager.cpp @@ -198,28 +198,28 @@ ViewManager::ViewManager(hecl::Runtime::FileStoreManager& fileMgr, hecl::CVarMan char path[2048]; hecl::Sstat theStat; - FILE* fp = hecl::Fopen(m_recentProjectsPath.c_str(), _SYS_STR("r"), hecl::FileLockType::Read); + auto fp = hecl::FopenUnique(m_recentProjectsPath.c_str(), _SYS_STR("r"), hecl::FileLockType::Read); if (fp) { - while (fgets(path, 2048, fp)) { + while (std::fgets(path, std::size(path), fp.get())) { std::string pathStr(path); pathStr.pop_back(); hecl::SystemStringConv pathStrView(pathStr); - if (!hecl::Stat(pathStrView.c_str(), &theStat) && S_ISDIR(theStat.st_mode)) + if (!hecl::Stat(pathStrView.c_str(), &theStat) && S_ISDIR(theStat.st_mode)) { m_recentProjects.emplace_back(pathStrView.sys_str()); + } } - fclose(fp); } - fp = hecl::Fopen(m_recentFilesPath.c_str(), _SYS_STR("r"), hecl::FileLockType::Read); + fp = hecl::FopenUnique(m_recentFilesPath.c_str(), _SYS_STR("r"), hecl::FileLockType::Read); if (fp) { - while (fgets(path, 2048, fp)) { + while (std::fgets(path, std::size(path), fp.get())) { std::string pathStr(path); pathStr.pop_back(); hecl::SystemStringConv pathStrView(pathStr); - if (!hecl::Stat(pathStrView.c_str(), &theStat) && S_ISDIR(theStat.st_mode)) + if (!hecl::Stat(pathStrView.c_str(), &theStat) && S_ISDIR(theStat.st_mode)) { m_recentFiles.emplace_back(pathStrView.sys_str()); + } } - fclose(fp); } } @@ -231,11 +231,14 @@ void ViewManager::pushRecentProject(hecl::SystemStringView path) { return; } m_recentProjects.emplace_back(path); - FILE* fp = hecl::Fopen(m_recentProjectsPath.c_str(), _SYS_STR("w"), hecl::FileLockType::Write); - if (fp) { - for (hecl::SystemString& pPath : m_recentProjects) - fmt::print(fp, fmt("{}\n"), hecl::SystemUTF8Conv(pPath)); - fclose(fp); + + const auto fp = hecl::FopenUnique(m_recentProjectsPath.c_str(), _SYS_STR("w"), hecl::FileLockType::Write); + if (fp == nullptr) { + return; + } + + for (const hecl::SystemString& pPath : m_recentProjects) { + fmt::print(fp.get(), fmt("{}\n"), hecl::SystemUTF8Conv(pPath)); } } @@ -245,11 +248,14 @@ void ViewManager::pushRecentFile(hecl::SystemStringView path) { return; } m_recentFiles.emplace_back(path); - FILE* fp = hecl::Fopen(m_recentFilesPath.c_str(), _SYS_STR("w"), hecl::FileLockType::Write); - if (fp) { - for (hecl::SystemString& pPath : m_recentFiles) - fmt::print(fp, fmt("{}\n"), hecl::SystemUTF8Conv(pPath)); - fclose(fp); + + const auto fp = hecl::FopenUnique(m_recentFilesPath.c_str(), _SYS_STR("w"), hecl::FileLockType::Write); + if (fp == nullptr) { + return; + } + + for (const hecl::SystemString& pPath : m_recentFiles) { + fmt::print(fp.get(), fmt("{}\n"), hecl::SystemUTF8Conv(pPath)); } } diff --git a/Runtime/CMemoryCardSysNix.cpp b/Runtime/CMemoryCardSysNix.cpp index 9b9df6476..8c4921a17 100644 --- a/Runtime/CMemoryCardSysNix.cpp +++ b/Runtime/CMemoryCardSysNix.cpp @@ -39,10 +39,10 @@ kabufuda::SystemString CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot sl return {}; path += fmt::format(fmt("/MemoryCard{:c}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B'); - FILE* fp = hecl::Fopen(path.c_str(), "wb"); - if (!fp) + const auto fp = hecl::FopenUnique(path.c_str(), "wb"); + if (fp == nullptr) { return {}; - fclose(fp); + } return path; } diff --git a/Runtime/CMemoryCardSysOSX.cpp b/Runtime/CMemoryCardSysOSX.cpp index 7b5edbf78..e1e659506 100644 --- a/Runtime/CMemoryCardSysOSX.cpp +++ b/Runtime/CMemoryCardSysOSX.cpp @@ -29,10 +29,10 @@ kabufuda::SystemString CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot sl return {}; path += fmt::format(fmt("/MemoryCard{:c}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B'); - FILE* fp = hecl::Fopen(path.c_str(), "wb"); - if (!fp) + const auto fp = hecl::FopenUnique(path.c_str(), "wb"); + if (fp == nullptr) { return {}; - fclose(fp); + } return path; } diff --git a/Runtime/CMemoryCardSysWin.cpp b/Runtime/CMemoryCardSysWin.cpp index 47a5704cb..e5db4be8e 100644 --- a/Runtime/CMemoryCardSysWin.cpp +++ b/Runtime/CMemoryCardSysWin.cpp @@ -100,10 +100,10 @@ kabufuda::SystemString CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot sl path += fmt::format(fmt(_SYS_STR("/MemoryCard{}.USA.raw")), slot == kabufuda::ECardSlot::SlotA ? _SYS_STR('A') : _SYS_STR('B')); - FILE* fp = hecl::Fopen(path.c_str(), _SYS_STR("wb")); - if (!fp) + const auto fp = hecl::FopenUnique(path.c_str(), _SYS_STR("wb")); + if (fp == nullptr) { return {}; - fclose(fp); + } return path; }