[nostalgia/core/userland] Replace C file I/O with C++ file I/O to comply with handbook

This commit is contained in:
Gary Talent 2020-01-24 19:04:48 -06:00
parent e8760cd714
commit e38cf01f07

View File

@ -6,25 +6,26 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <cstdio> #include <fstream>
#include <ox/std/trace.hpp>
#include "../media.hpp" #include "../media.hpp"
namespace nostalgia::core { namespace nostalgia::core {
uint8_t *loadRom(const char *path) { uint8_t *loadRom(const char *path) {
auto file = fopen(path, "r"); std::ifstream file(path, std::ios::binary | std::ios::ate);
if (file) { if (!file.good()) {
fseek(file, 0, SEEK_END); oxTrace("nostalgia::core::userland::loadRom") << "Read failed:" << path;
const auto size = ftell(file);
rewind(file);
auto buff = new uint8_t[size];
fread(buff, size, 1, file);
fclose(file);
return buff;
} else {
return nullptr; 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<char*>(buff), size);
return buff;
} }
void unloadRom(uint8_t *rom) { void unloadRom(uint8_t *rom) {