diff --git a/libstarlight/source/starlight/ConfigManager.cpp b/libstarlight/source/starlight/ConfigManager.cpp index 5988740..be8faf5 100644 --- a/libstarlight/source/starlight/ConfigManager.cpp +++ b/libstarlight/source/starlight/ConfigManager.cpp @@ -19,23 +19,14 @@ using std::ofstream; using starlight::Application; using starlight::util::FSHelper; +using starlight::util::Path; using starlight::Config; using starlight::ConfigManager; // helper stuff namespace { - void ensureDirectory(const char* path, bool hidden = false) { - struct stat st; - if (stat(path, &st) != 0) { - mkdir(path, 0); - if (hidden) { - // NYI; no idea how to do this, actually :( - } - } - } - void ensureDirectory(const string& path, bool hidden = false) { ensureDirectory(path.c_str(), hidden); } - + // } //////////// @@ -43,12 +34,14 @@ namespace { //////////// Config::Config(const string& path) : path(path) { - static const constexpr char* cpfx = "sdmc:/.starlight/config/"; + /*static const constexpr char* cpfx = "sdmc:/.starlight/config/"; constexpr std::size_t cpfx_s = std::strlen(cpfx); // reserve string size - fsPath = string(path.length() + cpfx_s + 5, ' '); fsPath.clear(); + string xfsPath(path.length() + cpfx_s + 5, ' '); xfsPath.clear(); // and build - fsPath.append(cpfx); fsPath.append(path); fsPath.append(".json"); + xfsPath.append(cpfx); xfsPath.append(path); xfsPath.append(".json"); + fsPath = Path(xfsPath);*/ + fsPath = Path("sdmc:/.starlight/config", true).Combine(path + ".json"); // init json this->json = std::make_shared(); @@ -58,12 +51,12 @@ Config::Config(const string& path) : path(path) { Config::~Config() { } void Config::Reload() { - ifstream load(fsPath); + ifstream load = fsPath.OpenI(); if (load.good()) load >> *json; } void Config::Save() { - ofstream save(fsPath); + ofstream save = fsPath.OpenO(); if (save.good()) save << *json; } @@ -74,7 +67,7 @@ void Config::Save() { std::unordered_map> ConfigManager::cfg; void ConfigManager::Init() { - FSHelper::AssertDirPath("sdmc:/.starlight/config/app/" + Application::AppName()); + //FSHelper::AssertDirPath("sdmc:/.starlight/config/app/" + Application::AppName()); } void ConfigManager::End() { diff --git a/libstarlight/source/starlight/ConfigManager.h b/libstarlight/source/starlight/ConfigManager.h index 3b82ba1..853d28e 100644 --- a/libstarlight/source/starlight/ConfigManager.h +++ b/libstarlight/source/starlight/ConfigManager.h @@ -6,12 +6,14 @@ #include "starlight/_incLib/json_fwd.hpp" +#include "starlight/util/Path.h" + //#include "starlight/gfx/DrawContext.h" namespace starlight { class Config { private: - std::string fsPath; + util::Path fsPath; std::shared_ptr json; public: const std::string path; diff --git a/libstarlight/source/starlight/util/Path.cpp b/libstarlight/source/starlight/util/Path.cpp index 9a31917..0038973 100644 --- a/libstarlight/source/starlight/util/Path.cpp +++ b/libstarlight/source/starlight/util/Path.cpp @@ -1,6 +1,7 @@ #include "Path.h" #include +#include #include #include @@ -9,9 +10,15 @@ using std::string; using std::stringstream; using std::getline; +using std::fstream; +using std::ifstream; +using std::ofstream; + using starlight::util::Path; -string Path::destructed = ""; +Path::Path() { + strpath = "sdmc:"; +} Path::Path(const string& path, bool noverify) { if (noverify) { strpath = path; return; } @@ -27,8 +34,7 @@ Path::Path(const Path& path) { } Path::~Path() { - destructed.append(strpath); - destructed.append("\n"); + } Path Path::Up(int levels) { @@ -66,8 +72,43 @@ Path Path::Combine(const string& token) { return Path(path, true); } +bool Path::Exists() { + struct stat st; + return stat(strpath.c_str(), &st) == 0; +} + +bool Path::IsFile() { + struct stat st; + if (stat(strpath.c_str(), &st) != 0) return false; + return S_ISREG(st.st_mode); +} + +bool Path::IsDirectory() { + struct stat st; + if (stat(strpath.c_str(), &st) != 0) return false; + return S_ISDIR(st.st_mode); +} + Path& Path::CreateDirectory() { - // todo: actually create the directory :D + if (IsDirectory()) return *this; + if (mkdir(strpath.c_str(), 0) != 0) { // try creating in place, else... + Up().CreateDirectory(); // create parent recursively + mkdir(strpath.c_str(), 0); + } return *this; } + +fstream Path::Open(std::ios_base::openmode mode) { + if (mode & std::ios_base::out) Up().CreateDirectory(); // make sure path exists if writing + return fstream(strpath, mode); +} + +ifstream Path::OpenI() { + return ifstream(strpath); +} + +ofstream Path::OpenO() { + Up().CreateDirectory(); // assert path beforehand + return ofstream(strpath); +} diff --git a/libstarlight/source/starlight/util/Path.h b/libstarlight/source/starlight/util/Path.h index b145a43..8f0992d 100644 --- a/libstarlight/source/starlight/util/Path.h +++ b/libstarlight/source/starlight/util/Path.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace starlight { @@ -13,8 +14,9 @@ namespace starlight { public: // vars... - static std::string destructed; + // + Path(); Path(const std::string& path, bool noverify = false); Path(const Path& path); ~Path(); @@ -23,13 +25,24 @@ namespace starlight { Path Up(int levels = 1); Path Combine(const std::string& token); + // check + bool Exists(); + bool IsFile(); + bool IsDirectory(); + // operation Path& CreateDirectory(); + std::fstream Open(std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out); + std::ifstream OpenI(); + std::ofstream OpenO(); // semi-obsolete but whatever, it can stay for now inline bool IsDirPath() const { return strpath.back() == '/'; } - inline operator std::string () const { return strpath; } + inline bool operator ==(const Path& o) const { return strpath == o.strpath; } + inline bool operator !=(const Path& o) const { return strpath != o.strpath; } + + inline operator std::string() const { return strpath; } }; } } diff --git a/testbed/source/Core.cpp b/testbed/source/Core.cpp index b8cfaec..bad9fa5 100644 --- a/testbed/source/Core.cpp +++ b/testbed/source/Core.cpp @@ -71,10 +71,14 @@ void Core::Init() { auto pipf = std::make_shared(VRect(0,0,400,240)); pipf->SetFont("default.16"); pipf->borderColor = Color::black; //pipf->SetText("I am the very model of something on the top screen. :D\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); - Path("sdmc:/banana/algorithm/").Combine("porcupine/kumquat/romfs:/annual/sdmc:/puffin/pie").CreateDirectory().CreateDirectory().Combine("farticus").Combine("platypus").CreateDirectory().Up(); - pipf->SetText(Path::destructed); + std::stringstream st; + //st << "dir: " << Path("sdmc:/.starlight").IsDirectory() << "\nfile: " << Path("sdmc:/arm9loaderhax.bin").IsFile(); + st << "dir: " << Path("sdmc:/arm9loaderhax.bin").IsDirectory() << "\nfile: " << Path("sdmc:/arm9loaderhax.bin").IsFile() << "\nroot: " << Path("sdmc:/").IsDirectory(); + pipf->SetText(st.str()); parallax->Add(pipf); + Path("sdmc:/all/your/base").CreateDirectory(); + clearColor = Color(0.0f, 0.5f, 0.5f); // test config