2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-08 22:27:43 +00:00

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

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