From 416a358bce1d85418064efb5428bf084f07b3859 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Thu, 19 Jun 2025 01:29:01 -0400 Subject: [PATCH] Replace nowide with C++17 filesystem Signed-off-by: Isaac Marovitz --- core/hw/maple/maple_jvs.cpp | 23 +++++++++++-------- core/ui/boxart/boxart.cpp | 46 ++++++++++++++++++------------------- core/ui/boxart/gamesdb.cpp | 35 ++++++++++++++-------------- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/core/hw/maple/maple_jvs.cpp b/core/hw/maple/maple_jvs.cpp index bd6631ef79..c0a8325b19 100644 --- a/core/hw/maple/maple_jvs.cpp +++ b/core/hw/maple/maple_jvs.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #define LOGJVS(...) DEBUG_LOG(JVS, __VA_ARGS__) @@ -1355,13 +1357,14 @@ maple_naomi_jamma::maple_naomi_jamma() } } - std::string eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom"; - FILE* f = nowide::fopen(eeprom_file.c_str(), "rb"); - if (f) + std::filesystem::path eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom"; + std::ifstream file(eeprom_file, std::ios::in | std::ios::binary); + if (file.is_open()) { - if (std::fread(eeprom, 1, 0x80, f) != 0x80) + file.read(reinterpret_cast(eeprom), sizeof(eeprom)); + if (file.gcount() != sizeof(eeprom)) WARN_LOG(MAPLE, "Failed or truncated read of EEPROM '%s'", eeprom_file.c_str()); - std::fclose(f); + file.close(); DEBUG_LOG(MAPLE, "Loaded EEPROM from %s", eeprom_file.c_str()); } else if (naomi_default_eeprom != NULL) @@ -1667,12 +1670,12 @@ void maple_naomi_jamma::handle_86_subcommand() size = std::min((int)sizeof(eeprom) - address, size); memcpy(eeprom + address, dma_buffer_in + 4, size); - std::string eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom"; - FILE* f = nowide::fopen(eeprom_file.c_str(), "wb"); - if (f) + std::filesystem::path eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom"; + std::ofstream f(eeprom_file, std::ios::out | std::ios::binary); + if (f.is_open()) { - std::fwrite(eeprom, 1, sizeof(eeprom), f); - std::fclose(f); + f.write(reinterpret_cast(eeprom), sizeof(eeprom)); + f.close(); INFO_LOG(MAPLE, "Saved EEPROM to %s", eeprom_file.c_str()); } else diff --git a/core/ui/boxart/boxart.cpp b/core/ui/boxart/boxart.cpp index 6702a808ef..223e6509ee 100644 --- a/core/ui/boxart/boxart.cpp +++ b/core/ui/boxart/boxart.cpp @@ -22,6 +22,8 @@ #include "oslib/oslib.h" #include "cfg/option.h" #include +#include +#include GameBoxart Boxart::getBoxart(const GameMedia& media) { @@ -154,10 +156,10 @@ void Boxart::saveDatabase() { if (!databaseDirty) return; - std::string basePath = getSaveDirectory(); - std::string db_name = basePath + DB_NAME; - FILE *file = nowide::fopen(db_name.c_str(), "wt"); - if (file == nullptr) + std::filesystem::path basePath = getSaveDirectory(); + std::filesystem::path db_name = basePath / DB_NAME; + std::ofstream file(db_name, std::ios::out | std::ios::trunc); + if (!file.is_open()) { WARN_LOG(COMMON, "Can't save boxart database to %s: error %d", db_name.c_str(), errno); return; @@ -172,9 +174,13 @@ void Boxart::saveDatabase() array.push_back(game.second.to_json(basePath)); } std::string serialized = array.dump(4); - fwrite(serialized.c_str(), 1, serialized.size(), file); - fclose(file); - databaseDirty = false; + file.write(serialized.c_str(), serialized.size()); + if (!file.good()) + { + WARN_LOG(COMMON, "Error writing to boxart database %s", + db_name.string().c_str()); + } + databaseDirty = false; } void Boxart::loadDatabase() @@ -183,25 +189,19 @@ void Boxart::loadDatabase() return; databaseLoaded = true; databaseDirty = false; - std::string save_dir = getSaveDirectory(); - if (!file_exists(save_dir)) - make_directory(save_dir); - std::string db_name = save_dir + DB_NAME; - FILE *f = nowide::fopen(db_name.c_str(), "rt"); - if (f == nullptr) + std::filesystem::path save_dir = getSaveDirectory(); + if (!std::filesystem::exists(save_dir)) + std::filesystem::create_directory(save_dir); + std::filesystem::path db_name = save_dir / DB_NAME; + std::ifstream file(db_name, std::ios::in); + if (!file.is_open()) return; DEBUG_LOG(COMMON, "Loading boxart database from %s", db_name.c_str()); - std::string all_data; - char buf[4096]; - while (true) - { - int s = fread(buf, 1, sizeof(buf), f); - if (s <= 0) - break; - all_data.append(buf, s); - } - fclose(f); + std::string all_data((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + file.close(); + try { std::lock_guard guard(mutex); diff --git a/core/ui/boxart/gamesdb.cpp b/core/ui/boxart/gamesdb.cpp index 8a092d7d5e..1baaf0ae0e 100644 --- a/core/ui/boxart/gamesdb.cpp +++ b/core/ui/boxart/gamesdb.cpp @@ -21,6 +21,9 @@ #include "stdclass.h" #include "emulator.h" +#include +#include + #define APIKEY "3fcc5e726a129924972be97abfd577ac5311f8f12398a9d9bcb5a377d4656fa8" std::string TheGamesDb::makeUrl(const std::string& endpoint) @@ -46,29 +49,27 @@ TheGamesDb::~TheGamesDb() void TheGamesDb::copyFile(const std::string& from, const std::string& to) { - FILE *ffrom = nowide::fopen(from.c_str(), "rb"); - if (ffrom == nullptr) + std::ifstream ffrom(from, std::ios::in | std::ios::binary); + if (!ffrom.is_open()) { - WARN_LOG(COMMON, "Can't open %s: error %d", from.c_str(), errno); + WARN_LOG(COMMON, "Can't open %s:", from.c_str()); return; } - FILE *fto = nowide::fopen(to.c_str(), "wb"); - if (fto == nullptr) + std::ofstream fto(to, std::ios::out | std::ios::binary); + if (!fto.is_open()) { - WARN_LOG(COMMON, "Can't open %s: error %d", to.c_str(), errno); - fclose(ffrom); + WARN_LOG(COMMON, "Can't open %s:", to.c_str()); + ffrom.close(); return; } - u8 buffer[4096]; - while (true) - { - int l = fread(buffer, 1, sizeof(buffer), ffrom); - if (l == 0) - break; - fwrite(buffer, 1, l, fto); - } - fclose(ffrom); - fclose(fto); + + u8 buffer[4096]; + while (ffrom.read(reinterpret_cast(buffer), sizeof(buffer)) || ffrom.gcount() > 0) + { + fto.write(reinterpret_cast(buffer), ffrom.gcount()); + } + ffrom.close(); + fto.close(); } json TheGamesDb::httpGet(const std::string& url)