Skip to content

Commit

Permalink
fs: Remove cwd from config and clean up fs/config code
Browse files Browse the repository at this point in the history
  • Loading branch information
joel16 committed Aug 2, 2022
1 parent 0a94bd9 commit ee42d02
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 260 deletions.
3 changes: 2 additions & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include <string>
#include <switch.h>

typedef struct {
int lang = 1;
bool dev_options = false;
bool image_filename = false;
char cwd[FS_MAX_PATH + 1] = "/";
} config_t;

extern config_t cfg;
extern char cwd[FS_MAX_PATH];

namespace Config {
int Save(config_t &config);
Expand Down
2 changes: 1 addition & 1 deletion include/fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vector>

extern FsFileSystem *fs;
extern FsFileSystem devices[4];
extern std::vector<FsFileSystem> devices;

typedef enum FileType {
FileTypeNone,
Expand Down
57 changes: 25 additions & 32 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@
#include "fs.hpp"
#include "log.hpp"

#define CONFIG_VERSION 3
#define CONFIG_VERSION 4

config_t cfg;

namespace Config {
static const char *config_file = "{\n\t\"config_ver\": %d,\n\t\"lang\": %d,\n\t\"dev_options\": %d,\n\t\"image_filename\": %d,\n\t\"last_dir\": \"%s\"\n}";
static const char *config_file = "{\n\t\"config_version\": %d,\n\t\"language\": %d,\n\t\"dev_options\": %d,\n\t\"image_filename\": %d\n}";
static int config_version_holder = 0;
static const int buf_size = 128 + FS_MAX_PATH;
static const int buf_size = 128;

int Save(config_t &config) {
Result ret = 0;
char *buf = new char[buf_size];
u64 len = std::snprintf(buf, buf_size, config_file, CONFIG_VERSION, config.lang, config.dev_options, config.image_filename, config.cwd);
u64 len = std::snprintf(buf, buf_size, config_file, CONFIG_VERSION, config.lang, config.dev_options, config.image_filename);

// Delete and re-create the file, we don't care about the return value here.
fsFsDeleteFile(fs, "/switch/NX-Shell/config.json");
fsFsCreateFile(fs, "/switch/NX-Shell/config.json", len, 0);

FsFile file;
if (R_FAILED(ret = fsFsOpenFile(fs, "/switch/NX-Shell/config.json", FsOpenMode_Write, &file))) {
if (R_FAILED(ret = fsFsOpenFile(fs, "/switch/NX-Shell/config.json", FsOpenMode_Write, std::addressof(file)))) {
Log::Error("fsFsOpenFile(/switch/NX-Shell/config.json) failed: 0x%x\n", ret);
delete[] buf;
return ret;
}

if (R_FAILED(ret = fsFileWrite(&file, 0, buf, len, FsWriteOption_Flush))) {
if (R_FAILED(ret = fsFileWrite(std::addressof(file), 0, buf, len, FsWriteOption_Flush))) {
Log::Error("fsFileWrite(/switch/NX-Shell/config.json) failed: 0x%x\n", ret);
delete[] buf;
fsFileClose(&file);
fsFileClose(std::addressof(file));
return ret;
}

fsFileClose(&file);
fsFileClose(std::addressof(file));
delete[] buf;
return 0;
}
Expand All @@ -47,7 +47,6 @@ namespace Config {
config.lang = 1;
config.dev_options = false;
config.image_filename = false;
std::strncpy(config.cwd, "/", 2);
}

int Load(void) {
Expand All @@ -64,59 +63,53 @@ namespace Config {
}

FsFile file;
if (R_FAILED(ret = fsFsOpenFile(fs, "/switch/NX-Shell/config.json", FsOpenMode_Read, &file)))
if (R_FAILED(ret = fsFsOpenFile(fs, "/switch/NX-Shell/config.json", FsOpenMode_Read, std::addressof(file))))
return ret;

s64 size = 0;
if (R_FAILED(ret = fsFileGetSize(&file, &size))) {
fsFileClose(&file);
if (R_FAILED(ret = fsFileGetSize(std::addressof(file), std::addressof(size)))) {
fsFileClose(std::addressof(file));
return ret;
}

char *buf = new char[size + 1];
if (R_FAILED(ret = fsFileRead(&file, 0, buf, static_cast<u64>(size) + 1, FsReadOption_None, nullptr))) {
if (R_FAILED(ret = fsFileRead(std::addressof(file), 0, buf, static_cast<u64>(size) + 1, FsReadOption_None, nullptr))) {
delete[] buf;
fsFileClose(&file);
fsFileClose(std::addressof(file));
return ret;
}

fsFileClose(&file);
fsFileClose(std::addressof(file));

json_t *root;
json_error_t error;
root = json_loads(buf, 0, &error);
root = json_loads(buf, 0, std::addressof(error));
delete[] buf;

if (!root) {
printf("error: on line %d: %s\n", error.line, error.text);
return -1;
}

json_t *config_ver = json_object_get(root, "config_ver");
json_t *config_ver = json_object_get(root, "config_version");
config_version_holder = json_integer_value(config_ver);

json_t *lang = json_object_get(root, "lang");
cfg.lang = json_integer_value(lang);

json_t *dev_options = json_object_get(root, "dev_options");
cfg.dev_options = json_integer_value(dev_options);

json_t *image_filename = json_object_get(root, "image_filename");
cfg.image_filename = json_integer_value(image_filename);

json_t *last_dir = json_object_get(root, "last_dir");
std::strcpy(cfg.cwd, json_string_value(last_dir));

if (!FS::DirExists(cfg.cwd))
std::strcpy(cfg.cwd, "/");

// Delete config file if config file is updated. This will rarely happen.
if (config_version_holder < CONFIG_VERSION) {
fsFsDeleteFile(fs, "/switch/NX-Shell/config.json");
Config::SetDefault(cfg);
return Config::Save(cfg);
}

json_t *language = json_object_get(root, "language");
cfg.lang = json_integer_value(language);

json_t *dev_options = json_object_get(root, "dev_options");
cfg.dev_options = json_integer_value(dev_options);

json_t *image_filename = json_object_get(root, "image_filename");
cfg.image_filename = json_integer_value(image_filename);

json_decref(root);
return 0;
}
Expand Down
Loading

0 comments on commit ee42d02

Please sign in to comment.