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.
This commit is contained in:
Lioncash 2019-08-26 14:37:19 -04:00
parent f48ebefa84
commit 1d3062b33f
9 changed files with 60 additions and 73 deletions

View File

@ -466,9 +466,8 @@ bool PAKRouter<BRIDGETYPE>::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 */

View File

@ -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) {

View File

@ -42,8 +42,8 @@ void SCLY::exportToLayerDirectories(const PAK::Entry& entry, PAKRouter<PAKBridge
layerPath.makeDirChain(true);
if (active) {
hecl::ProjectPath activePath(layerPath, "!defaultactive");
fclose(hecl::Fopen(activePath.getAbsolutePath().data(), _SYS_STR("wb")));
const hecl::ProjectPath activePath(layerPath, "!defaultactive");
[[maybe_unused]] const auto fp = hecl::FopenUnique(activePath.getAbsolutePath().data(), _SYS_STR("wb"));
}
hecl::ProjectPath yamlFile(layerPath, _SYS_STR("!objects.yaml"));

View File

@ -721,18 +721,17 @@ void SpecBase::extractRandomStaticEntropy(const uint8_t* buf, const hecl::Projec
hecl::ProjectPath entropyPath(noAramPath, _SYS_STR("RandomStaticEntropy.png"));
hecl::ProjectPath catalogPath(noAramPath, _SYS_STR("!catalog.yaml"));
if (FILE* fp = hecl::Fopen(catalogPath.getAbsolutePath().data(), _SYS_STR("a"))) {
fmt::print(fp, fmt("RandomStaticEntropy: {}\n"), entropyPath.getRelativePathUTF8());
fclose(fp);
if (const auto fp = hecl::FopenUnique(catalogPath.getAbsolutePath().data(), _SYS_STR("a"))) {
fmt::print(fp.get(), fmt("RandomStaticEntropy: {}\n"), entropyPath.getRelativePathUTF8());
}
FILE* fp = hecl::Fopen(entropyPath.getAbsolutePath().data(), _SYS_STR("wb"));
if (!fp) {
auto fp = hecl::FopenUnique(entropyPath.getAbsolutePath().data(), _SYS_STR("wb"));
if (fp == nullptr) {
Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for writing")), entropyPath.getAbsolutePath());
return;
}
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 = {};
@ -756,7 +755,6 @@ void SpecBase::extractRandomStaticEntropy(const uint8_t* buf, const hecl::Projec
png_write_end(png, info);
png_write_flush(png);
png_destroy_write_struct(&png, &info);
fclose(fp);
}
void SpecBase::clearTagCache() {

View File

@ -613,12 +613,13 @@ struct SpecMP1 : SpecBase {
} else if (hecl::IsPathPNG(path)) {
return {SBIG('TXTR'), path.hash().val32()};
} else if (hecl::IsPathYAML(path)) {
FILE* fp = hecl::Fopen(path.getAbsolutePath().data(), _SYS_STR("r"));
if (!fp)
auto fp = hecl::FopenUnique(path.getAbsolutePath().data(), _SYS_STR("r"));
if (fp == nullptr) {
return {};
}
athena::io::YAMLDocReader reader;
yaml_parser_set_input_file(reader.getParser(), fp);
yaml_parser_set_input_file(reader.getParser(), fp.get());
urde::SObjectTag resTag;
if (reader.ClassTypeOperation([&](const char* className) -> bool {
@ -700,10 +701,9 @@ struct SpecMP1 : SpecBase {
return false;
})) {
resTag.id = path.hash().val32();
fclose(fp);
fp.reset();
return resTag;
}
fclose(fp);
}
return {};
}

View File

@ -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));
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}