From e327139b5285d8b86b2f2ebc4f7a5f45316e95bc Mon Sep 17 00:00:00 2001 From: Kronos2308 <36446521+Kronos2308@users.noreply.github.com> Date: Fri, 8 Apr 2022 11:53:12 +0100 Subject: [PATCH] add easy read and write json --- Makefile | 2 +- include/utils.h | 3 +++ source/Utils.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8948124..688ba4c 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ CFLAGS := -g -Wall -O3 -ffunction-sections \ CFLAGS += $(INCLUDE) -D__SWITCH__ -DVERSION='"$(APP_VERSION)"' `freetype-config --libs` -CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions `freetype-config --cflags` +CXXFLAGS := $(CFLAGS) -fno-rtti -fexceptions `freetype-config --cflags` ASFLAGS := -g $(ARCH) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) diff --git a/include/utils.h b/include/utils.h index 52fcb33..41fb9fc 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include struct AmiiboEntry { @@ -21,6 +22,8 @@ struct AmiiboCreatorData }; bool checkIfFileExists(char* path); +bool read_DB(nlohmann::json& base,std::string path); +bool write_DB(nlohmann::json base,std::string path); std::vector scanForAmiibo(const char* path); std::vector getListOfSeries(); std::vector getAmiibosFromSeries(std::string series); diff --git a/source/Utils.cpp b/source/Utils.cpp index b7e3ce7..106925c 100644 --- a/source/Utils.cpp +++ b/source/Utils.cpp @@ -10,12 +10,58 @@ #include #include #include +#include bool checkIfFileExists(char* path) { return !access(path, F_OK); } +bool read_DB(nlohmann::json& base,std::string path){ + std::ifstream inf(path); + if(!inf.fail()) { + std::string tempjson=""; + for(int f = 0; !inf.eof(); f++) + { + std::string TempLine = ""; + getline(inf, TempLine); + tempjson += TempLine; + } + inf.close(); + if(nlohmann::json::accept(tempjson)) + { + //Parse and use the JSON data + base = nlohmann::json::parse(tempjson); + std::cout << "Json Readed... "<< path << std::endl; + return true; + } + } + return false; +} + +bool write_DB(nlohmann::json base,std::string path){ + std::string pathtemp = path+".tmp"; + std::stringstream strm; + try{ + strm << std::setw(4) << base; +// strm << base; + std::string A = strm.str(); + + std::ofstream otf(pathtemp); + otf << A; + otf.close(); + remove(path.c_str()); + rename(pathtemp.c_str(),path.c_str()); + + } catch(...) { + std::cout << "Json: write Error... "<< path << std::endl; + return false; + } + std::cout << "Json: writhen... "<< path << std::endl; + return true; +} + + std::vector scanForAmiibo(const char* path) { //Open path