FILE to fstream

Lets move away from  the c standart FILE and use c++ fstream as well as c++17 filesystem!!!
This commit is contained in:
tobid7 2023-08-28 17:03:00 +02:00
parent 5cca34ede4
commit 267f9ce5c3
8 changed files with 52 additions and 49 deletions

View File

@ -2,4 +2,5 @@
#include <renderd7/StealConsole.hpp> #include <renderd7/StealConsole.hpp>
#include <renderd7/bmp.hpp> #include <renderd7/bmp.hpp>
#include <renderd7/renderd7.hpp> #include <renderd7/renderd7.hpp>
#include <renderd7/sound.hpp>

BIN
rd7tf/romfs/thesound.wav Normal file

Binary file not shown.

View File

@ -8,6 +8,8 @@ int main() {
RenderD7::Ftrace::Beg("app", f2s(RenderD7::LoadSettings)); RenderD7::Ftrace::Beg("app", f2s(RenderD7::LoadSettings));
RenderD7::LoadSettings(); RenderD7::LoadSettings();
RenderD7::Ftrace::End("app", f2s(RenderD7::LoadSettings)); RenderD7::Ftrace::End("app", f2s(RenderD7::LoadSettings));
sound snd("romfs:/thesound.wav", 0);
snd.play();
RenderD7::Ftrace::End("app", "app_init"); RenderD7::Ftrace::End("app", "app_init");
while (RenderD7::MainLoop()) { while (RenderD7::MainLoop()) {
RenderD7::Ftrace::Beg("app", "app_mainloop"); RenderD7::Ftrace::Beg("app", "app_mainloop");

View File

@ -6,6 +6,8 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION
#include <renderd7/external/stb_image_write.h> #include <renderd7/external/stb_image_write.h>
#include <fstream>
RenderD7::NFontApi::NFontApi() {} RenderD7::NFontApi::NFontApi() {}
RenderD7::NFontApi::~NFontApi() {} RenderD7::NFontApi::~NFontApi() {}
@ -14,13 +16,16 @@ void RenderD7::NFontApi::LoadTTF(std::string path) {
/////READ FILE /////READ FILE
unsigned char *buffer; unsigned char *buffer;
long size = 0; long size = 0;
FILE *ttf__ = fopen(path.c_str(), "rb"); std::fstream ttf__(path, std::ios::in | std::ios::binary);
fseek(ttf__, 0, SEEK_END); if(!ttf__.is_open()) {
size = ftell(ttf__); return; // Add Error Handler in future
fseek(ttf__, 0, SEEK_SET); }
ttf__.seekg(0, std::ios::end);
size = ttf__.tellg();
ttf__.seekg(0, std::ios::beg);
buffer = (unsigned char *)malloc(size); buffer = (unsigned char *)malloc(size);
fread(buffer, size, 1, ttf__); ttf__.read(reinterpret_cast<char*>(buffer), size);
fclose(ttf__); ttf__.close();
/////Setup Font /////Setup Font
if (!stbtt_InitFont(&font, buffer, 0)) { if (!stbtt_InitFont(&font, buffer, 0)) {
printf("failed\n"); printf("failed\n");

View File

@ -1,8 +1,7 @@
#include <3ds.h> #include <3ds.h>
#include <renderd7/lang.hpp> #include <renderd7/lang.hpp>
#include <stdio.h> #include <fstream>
#include <stdlib.h> #include <filesystem>
#include <unistd.h>
static nlohmann::json appJson; static nlohmann::json appJson;
@ -73,26 +72,27 @@ std::string RenderD7::Lang::get(const std::string &key) {
} }
void RenderD7::Lang::load(const std::string &lang) { void RenderD7::Lang::load(const std::string &lang) {
FILE *values; std::fstream values;
if (access(("romfs:/lang/" + lang + "/app.json").c_str(), F_OK) == 0) { if(std::filesystem::is_character_file("romfs:/lang/" + lang + "/app.json")) {
values = fopen(("romfs:/lang/" + lang + "/app.json").c_str(), "rt"); values.open("romfs:/lang/" + lang + "/app.json", std::ios::in);
if (values) { if(values.is_open()) {
appJson = nlohmann::json::parse(values); appJson = nlohmann::json::parse(values);
fclose(values);
} }
if (appJson.is_discarded()) values.close();
if(appJson.is_discarded()) {
appJson = {}; appJson = {};
}
return; return;
} else { } else {
values = fopen("romfs:/lang/en/app.json", "rt"); values.open("romfs:/lang/en/app.json", std::ios::in);
if (values) { if(values.is_open()) {
appJson = nlohmann::json::parse(values); appJson = nlohmann::json::parse(values);
fclose(values);
} }
if (appJson.is_discarded()) values.close();
if(appJson.is_discarded()) {
appJson = {}; appJson = {};
}
return; return;
} }
} }

View File

@ -1,5 +1,6 @@
#include <renderd7/log.hpp> #include <renderd7/log.hpp>
#include <fstream>
#include <filesystem>
#include <memory> #include <memory>
std::string Log::format(const std::string &fmt_str, ...) { std::string Log::format(const std::string &fmt_str, ...) {
@ -28,12 +29,12 @@ void Log::Init(const char *filename) {
printf("%s\n", filename); printf("%s\n", filename);
std::string fn = filename; std::string fn = filename;
std::string name = fn + ".txt"; std::string name = fn + ".txt";
this->filename = name.c_str(); this->filename = name;
if ((access(name.c_str(), F_OK) == 0)) { if(std::filesystem::exists(name)) {
// Do nothing
} else { } else {
FILE *logfile = fopen((name.c_str()), "w"); std::fstream f(name, std::ios::out);
fclose(logfile); f.close();
} }
} }

View File

@ -666,14 +666,7 @@ void RenderD7::DrawSTObject(std::vector<RenderD7::TObject> tobject,
} }
bool RenderD7::FS::FileExist(const std::string &path) { bool RenderD7::FS::FileExist(const std::string &path) {
FILE *test = fopen(path.c_str(), "r"); return std::filesystem::exists(path);
if (test != NULL) {
fclose(test);
return true;
}
return false;
} }
bool RenderD7::IsNdspInit() { bool RenderD7::IsNdspInit() {

View File

@ -1,9 +1,8 @@
#include <renderd7/sound.hpp> #include <renderd7/sound.hpp>
#include <cstdio> #include <fstream>
#include <cstdlib>
#include <cstring>
#include <string> #include <string>
#include <cstring>
extern bool isndspinit; extern bool isndspinit;
using std::string; using std::string;
@ -31,19 +30,20 @@ sound::sound(const string &path, int channel, bool toloop) {
ndspSetOutputCount(2); // Num of buffers ndspSetOutputCount(2); // Num of buffers
// Reading wav file // Reading wav file
FILE *fp = fopen(path.c_str(), "rb"); std::fstream fp(path, std::ios::in | std::ios::binary);
if (!fp) { if (!fp.is_open()) {
printf("Could not open the WAV file: %s\n", path.c_str()); printf("Could not open the WAV file: %s\n", path.c_str());
return; return;
} }
WavHeader wavHeader; WavHeader wavHeader;
size_t read = fread(&wavHeader, 1, sizeof(wavHeader), fp); fp.read(reinterpret_cast<char*>(&wavHeader), sizeof(WavHeader));
size_t read = fp.tellg();
if (read != sizeof(wavHeader)) { if (read != sizeof(wavHeader)) {
// Short read. // Short read.
printf("WAV file header is too short: %s\n", path.c_str()); printf("WAV file header is too short: %s\n", path.c_str());
fclose(fp); fp.close();
return; return;
} }
@ -52,7 +52,7 @@ sound::sound(const string &path, int channel, bool toloop) {
if (memcmp(wavHeader.magic, RIFF_magic, sizeof(wavHeader.magic)) != 0) { if (memcmp(wavHeader.magic, RIFF_magic, sizeof(wavHeader.magic)) != 0) {
// Incorrect magic number. // Incorrect magic number.
printf("Wrong file format.\n"); printf("Wrong file format.\n");
fclose(fp); fp.close();
return; return;
} }
@ -61,19 +61,20 @@ sound::sound(const string &path, int channel, bool toloop) {
(wavHeader.bits_per_sample != 8 && wavHeader.bits_per_sample != 16)) { (wavHeader.bits_per_sample != 8 && wavHeader.bits_per_sample != 16)) {
// Unsupported WAV file. // Unsupported WAV file.
printf("Corrupted wav file.\n"); printf("Corrupted wav file.\n");
fclose(fp); fp.close();
return; return;
} }
// Get the file size. // Get the file size.
fseek(fp, 0, SEEK_END); fp.seekg(0, std::ios::end);
dataSize = ftell(fp) - sizeof(wavHeader); dataSize = fp.tellg();
dataSize -= sizeof(WavHeader);
// Allocating and reading samples // Allocating and reading samples
data = static_cast<u8 *>(linearAlloc(dataSize)); data = static_cast<u8 *>(linearAlloc(dataSize));
fseek(fp, 44, SEEK_SET); fp.seekg(44, std::ios::beg);
fread(data, 1, dataSize, fp); fp.read(reinterpret_cast<char*>(data), dataSize);
fclose(fp); fp.close();
dataSize /= 2; // FIXME: 16-bit or stereo? dataSize /= 2; // FIXME: 16-bit or stereo?
// Find the right format // Find the right format