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/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::LoadSettings();
RenderD7::Ftrace::End("app", f2s(RenderD7::LoadSettings));
sound snd("romfs:/thesound.wav", 0);
snd.play();
RenderD7::Ftrace::End("app", "app_init");
while (RenderD7::MainLoop()) {
RenderD7::Ftrace::Beg("app", "app_mainloop");

View File

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

View File

@ -1,8 +1,7 @@
#include <3ds.h>
#include <renderd7/lang.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <filesystem>
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) {
FILE *values;
std::fstream values;
if (access(("romfs:/lang/" + lang + "/app.json").c_str(), F_OK) == 0) {
values = fopen(("romfs:/lang/" + lang + "/app.json").c_str(), "rt");
if (values) {
if(std::filesystem::is_character_file("romfs:/lang/" + lang + "/app.json")) {
values.open("romfs:/lang/" + lang + "/app.json", std::ios::in);
if(values.is_open()) {
appJson = nlohmann::json::parse(values);
fclose(values);
}
if (appJson.is_discarded())
values.close();
if(appJson.is_discarded()) {
appJson = {};
}
return;
} else {
values = fopen("romfs:/lang/en/app.json", "rt");
if (values) {
values.open("romfs:/lang/en/app.json", std::ios::in);
if(values.is_open()) {
appJson = nlohmann::json::parse(values);
fclose(values);
}
if (appJson.is_discarded())
values.close();
if(appJson.is_discarded()) {
appJson = {};
}
return;
}
}

View File

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

View File

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

View File

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