From e38cf01f07def8dbfedd683b9d8b354191b287a4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 24 Jan 2020 19:04:48 -0600 Subject: [PATCH] [nostalgia/core/userland] Replace C file I/O with C++ file I/O to comply with handbook --- src/nostalgia/core/userland/media.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/nostalgia/core/userland/media.cpp b/src/nostalgia/core/userland/media.cpp index f900d20f..edd7a03a 100644 --- a/src/nostalgia/core/userland/media.cpp +++ b/src/nostalgia/core/userland/media.cpp @@ -6,25 +6,26 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include +#include + +#include #include "../media.hpp" namespace nostalgia::core { uint8_t *loadRom(const char *path) { - auto file = fopen(path, "r"); - if (file) { - fseek(file, 0, SEEK_END); - const auto size = ftell(file); - rewind(file); - auto buff = new uint8_t[size]; - fread(buff, size, 1, file); - fclose(file); - return buff; - } else { + std::ifstream file(path, std::ios::binary | std::ios::ate); + if (!file.good()) { + oxTrace("nostalgia::core::userland::loadRom") << "Read failed:" << path; return nullptr; } + + const std::size_t size = file.tellg(); + file.seekg(0, std::ios::beg); + auto buff = new uint8_t[size]; + file.read(reinterpret_cast(buff), size); + return buff; } void unloadRom(uint8_t *rom) {