From 2999e1cb869dd0009e131c0d90d2cc06a22aa020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 9 May 2021 15:48:36 -0700 Subject: [PATCH] Core: Remove some uses of OpenCPPFile. --- Common/Data/Format/IniFile.cpp | 21 ++++----- Core/FileSystems/VirtualDiscFileSystem.cpp | 17 +++----- Core/TextureReplacer.cpp | 51 ++++++++++------------ UI/GameSettingsScreen.cpp | 7 ++- 4 files changed, 42 insertions(+), 54 deletions(-) diff --git a/Common/Data/Format/IniFile.cpp b/Common/Data/Format/IniFile.cpp index 8e001f802d..94b6f322be 100644 --- a/Common/Data/Format/IniFile.cpp +++ b/Common/Data/Format/IniFile.cpp @@ -18,6 +18,7 @@ #include "Common/Data/Format/IniFile.h" #include "Common/File/VFS/VFS.h" +#include "Common/File/FileUtil.h" #include "Common/Data/Text/Parsers.h" #ifdef _WIN32 @@ -597,32 +598,26 @@ bool IniFile::Load(std::istream &in) { bool IniFile::Save(const char* filename) { - std::ofstream out; -#if defined(_WIN32) && !defined(__MINGW32__) - out.open(ConvertUTF8ToWString(filename), std::ios::out); -#else - out.open(filename, std::ios::out); -#endif - - if (out.fail()) - { + FILE *file = File::OpenCFile(filename, "w"); + if (!file) { return false; } // UTF-8 byte order mark. To make sure notepad doesn't go nuts. - out << "\xEF\xBB\xBF"; + // TODO: Do we still need this? It's annoying. + fprintf(file, "\xEF\xBB\xBF"); for (const Section §ion : sections) { if (!section.name().empty() && (!section.lines.empty() || !section.comment.empty())) { - out << "[" << section.name() << "]" << section.comment << std::endl; + fprintf(file, "[%s]%s\n", section.name().c_str(), section.comment.c_str()); } for (const std::string &s : section.lines) { - out << s << std::endl; + fprintf(file, "%s\n", s.c_str()); } } - out.close(); + fclose(file); return true; } diff --git a/Core/FileSystems/VirtualDiscFileSystem.cpp b/Core/FileSystems/VirtualDiscFileSystem.cpp index c13ef3f1e8..e0da72f16c 100644 --- a/Core/FileSystems/VirtualDiscFileSystem.cpp +++ b/Core/FileSystems/VirtualDiscFileSystem.cpp @@ -82,19 +82,16 @@ void VirtualDiscFileSystem::LoadFileListIndex() { return; } - std::ifstream in; - in.open(filename.c_str(), std::ios::in); - if (in.fail()) { + FILE *f = File::OpenCFile(filename, "r"); + if (!f) { return; } std::string buf; - static const int MAX_LINE_SIZE = 1024; - while (!in.eof()) { - buf.resize(MAX_LINE_SIZE, '\0'); - in.getline(&buf[0], MAX_LINE_SIZE); - - std::string line = buf.data(); + static const int MAX_LINE_SIZE = 2048; + char linebuf[MAX_LINE_SIZE]{}; + while (fgets(linebuf, MAX_LINE_SIZE, f)) { + std::string line = linebuf; // Ignore any UTF-8 BOM. if (line.substr(0, 3) == "\xEF\xBB\xBF") { @@ -163,7 +160,7 @@ void VirtualDiscFileSystem::LoadFileListIndex() { fileList.push_back(entry); } - in.close(); + fclose(f); } void VirtualDiscFileSystem::DoState(PointerWrap &p) diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 05a2ad6da9..cb2f47cb70 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -743,35 +743,32 @@ bool TextureReplacer::GenerateIni(const std::string &gameID, std::string *genera FILE *f = File::OpenCFile(texturesDirectory + INI_FILENAME, "wb"); if (f) { - fwrite("\xEF\xBB\xBF", 0, 3, f); - fclose(f); + fwrite("\xEF\xBB\xBF", 1, 3, f); // Let's also write some defaults. - std::fstream fs; - File::OpenCPPFile(fs, texturesDirectory + INI_FILENAME, std::ios::out | std::ios::ate); - fs << "# This file is optional and describes your textures.\n"; - fs << "# Some information on syntax available here:\n"; - fs << "# https://github.com/hrydgard/ppsspp/wiki/Texture-replacement-ini-syntax\n"; - fs << "[options]\n"; - fs << "version = 1\n"; - fs << "hash = quick\n"; - fs << "ignoreMipmap = false\n"; - fs << "\n"; - fs << "[games]\n"; - fs << "# Used to make it easier to install, and override settings for other regions.\n"; - fs << "# Files still have to be copied to each TEXTURES folder."; - fs << gameID << " = " << INI_FILENAME << "\n"; - fs << "\n"; - fs << "[hashes]\n"; - fs << "# Use / for folders not \\, avoid special characters, and stick to lowercase.\n"; - fs << "# See wiki for more info.\n"; - fs << "\n"; - fs << "[hashranges]\n"; - fs << "\n"; - fs << "[filtering]\n"; - fs << "\n"; - fs << "[reducehashranges]\n"; - fs.close(); + fprintf(f, "# This file is optional and describes your textures.\n"); + fprintf(f, "# Some information on syntax available here:\n"); + fprintf(f, "# https://github.com/hrydgard/ppsspp/wiki/Texture-replacement-ini-syntax \n"); + fprintf(f, "[options]\n"); + fprintf(f, "version = 1\n"); + fprintf(f, "hash = quick\n"); + fprintf(f, "ignoreMipmap = false\n"); + fprintf(f, "\n"); + fprintf(f, "[games]\n"); + fprintf(f, "# Used to make it easier to install, and override settings for other regions.\n"); + fprintf(f, "# Files still have to be copied to each TEXTURES folder."); + fprintf(f, "%s = %s\n", gameID.c_str(), INI_FILENAME.c_str()); + fprintf(f, "\n"); + fprintf(f, "[hashes]\n"); + fprintf(f, "# Use / for folders not \\, avoid special characters, and stick to lowercase.\n"); + fprintf(f, "# See wiki for more info.\n"); + fprintf(f, "\n"); + fprintf(f, "[hashranges]\n"); + fprintf(f, "\n"); + fprintf(f, "[filtering]\n"); + fprintf(f, "\n"); + fprintf(f, "[reducehashranges]\n"); + fclose(f); } return File::Exists(texturesDirectory + INI_FILENAME); } diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index d362388b29..3fceddf23d 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -1150,10 +1150,9 @@ UI::EventReturn GameSettingsScreen::OnSavePathMydoc(UI::EventParams &e) { installed_ = false; g_Config.memStickDirectory = PPSSPPpath + "memstick/"; } else { - std::ofstream myfile; - myfile.open(PPSSPPpath + "installed.txt"); - if (myfile.is_open()){ - myfile.close(); + FILE *f = File::OpenCFile(PPSSPPpath + "installed.txt", "wb"); + if (f) { + fclose(f); } const std::string myDocsPath = W32Util::UserDocumentsPath() + "/PPSSPP/";