Add File class and implement inside Settings
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
#include <iostream> // std::ios
|
||||||
|
#include <memory> // std::make_unique
|
||||||
|
|
||||||
|
#include "inferno/file.h"
|
||||||
|
|
||||||
|
namespace Inferno {
|
||||||
|
|
||||||
|
std::string File::read(const std::string &path)
|
||||||
|
{
|
||||||
|
// Create input stream object and open file
|
||||||
|
std::ifstream file(path.c_str());
|
||||||
|
NF_CORE_ASSERT(file.is_open(), "File could not open: %s", path.c_str());
|
||||||
|
|
||||||
|
// Check if file exists
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get length of the file
|
||||||
|
file.seekg(0, std::ios::end);
|
||||||
|
int length = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
NF_CORE_ASSERT(length != -1, "File could not determine length: %s", path.c_str());
|
||||||
|
|
||||||
|
// Check for valid file length
|
||||||
|
if (length == -1) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate memory filled with zeros
|
||||||
|
auto buffer = std::make_unique<char[]>(length + 1);
|
||||||
|
|
||||||
|
// Fill buffer with file contents
|
||||||
|
file.read(buffer.get(), length);
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
// Create string from the buffer and return
|
||||||
|
return std::string(buffer.get(), length + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#ifndef FILE_H
|
||||||
|
#define FILE_H
|
||||||
|
|
||||||
|
#include <fstream> // std::ifstream, std::ofstream
|
||||||
|
#include <iomanip> // std::setfill, std::setw
|
||||||
|
#include <string> // std::string
|
||||||
|
|
||||||
|
#include "inferno/core.h"
|
||||||
|
#include "inferno/log.h"
|
||||||
|
|
||||||
|
namespace Inferno {
|
||||||
|
|
||||||
|
class File {
|
||||||
|
public:
|
||||||
|
static std::string read(const std::string &path);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static void ioRead(T &t, const std::string &path)
|
||||||
|
{
|
||||||
|
std::ifstream file(path);
|
||||||
|
NF_CORE_ASSERT(file.is_open(), "File could not open: %s", path.c_str());
|
||||||
|
|
||||||
|
if (file.is_open()) {
|
||||||
|
file >> t;
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static void ioWrite(T &t, const std::string &path)
|
||||||
|
{
|
||||||
|
std::ofstream file (path);
|
||||||
|
NF_CORE_ASSERT(file.is_open(), "File could not open: %s", path.c_str());
|
||||||
|
|
||||||
|
if (file.is_open()) {
|
||||||
|
// Write file with single tabs, nicely formatted
|
||||||
|
file << std::setfill ('\t') << std::setw(1) << t << std::endl;
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // FILE_H
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <string> // std::string
|
#include <string> // std::string
|
||||||
|
|
||||||
#include "inferno/core.h"
|
#include "inferno/core.h"
|
||||||
|
#include "inferno/file.h"
|
||||||
#include "inferno/log.h"
|
#include "inferno/log.h"
|
||||||
#include "inferno/settings.h"
|
#include "inferno/settings.h"
|
||||||
|
|
||||||
@@ -26,14 +27,14 @@ namespace Inferno {
|
|||||||
|
|
||||||
void Settings::initialize()
|
void Settings::initialize()
|
||||||
{
|
{
|
||||||
nlohmann::json m_json = this->load();
|
nlohmann::json json = this->load();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
m_properties.window.title = strdup(m_json["window"]["title"].get<std::string>().c_str());
|
m_properties.window.title = strdup(json["window"]["title"].get<std::string>().c_str());
|
||||||
m_properties.window.width = m_json["window"]["width"].get<int>();
|
m_properties.window.width = json["window"]["width"].get<int>();
|
||||||
m_properties.window.height = m_json["window"]["height"].get<int>();
|
m_properties.window.height = json["window"]["height"].get<int>();
|
||||||
m_properties.window.fullscreen = strdup(m_json["window"]["fullscreen"].get<std::string>().c_str());
|
m_properties.window.fullscreen = strdup(json["window"]["fullscreen"].get<std::string>().c_str());
|
||||||
m_properties.window.vsync = m_json["window"]["vsync"].get<bool>();
|
m_properties.window.vsync = json["window"]["vsync"].get<bool>();
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
NF_CORE_WARN("Settings syntax error: using default values");
|
NF_CORE_WARN("Settings syntax error: using default values");
|
||||||
@@ -51,15 +52,7 @@ namespace Inferno {
|
|||||||
{
|
{
|
||||||
nlohmann::json json;
|
nlohmann::json json;
|
||||||
|
|
||||||
std::ifstream file(m_path);
|
File::ioRead(json, m_path);
|
||||||
NF_CORE_ASSERT(file.is_open(), "Could not open settings file!");
|
|
||||||
|
|
||||||
if (file.is_open()) {
|
|
||||||
// Read the JSON file
|
|
||||||
file >> json;
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
NF_CORE_INFO("Settings loaded");
|
NF_CORE_INFO("Settings loaded");
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
@@ -74,15 +67,7 @@ namespace Inferno {
|
|||||||
json["window"]["fullscreen"] = m_properties.window.fullscreen;
|
json["window"]["fullscreen"] = m_properties.window.fullscreen;
|
||||||
json["window"]["vsync"] = m_properties.window.vsync;
|
json["window"]["vsync"] = m_properties.window.vsync;
|
||||||
|
|
||||||
std::ofstream file (m_path);
|
File::ioWrite(json, m_path);
|
||||||
NF_CORE_ASSERT(file.is_open(), "Could not open settings file!");
|
|
||||||
|
|
||||||
if (file.is_open()) {
|
|
||||||
// Write the JSON file with single tabs, nicely formatted
|
|
||||||
file << std::setfill ('\t') << std::setw(1) << json << std::endl;
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
NF_CORE_INFO("Settings saved");
|
NF_CORE_INFO("Settings saved");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user