mirror of
https://github.com/zetaPRIME/libstarlight.git
synced 2025-06-26 21:52:47 +00:00
moar path stuffs
This commit is contained in:
parent
0ee45d7efb
commit
942732522b
@ -19,23 +19,14 @@ using std::ofstream;
|
|||||||
using starlight::Application;
|
using starlight::Application;
|
||||||
|
|
||||||
using starlight::util::FSHelper;
|
using starlight::util::FSHelper;
|
||||||
|
using starlight::util::Path;
|
||||||
|
|
||||||
using starlight::Config;
|
using starlight::Config;
|
||||||
using starlight::ConfigManager;
|
using starlight::ConfigManager;
|
||||||
|
|
||||||
// helper stuff
|
// helper stuff
|
||||||
namespace {
|
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) {
|
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);
|
constexpr std::size_t cpfx_s = std::strlen(cpfx);
|
||||||
// reserve string size
|
// reserve string size
|
||||||
fsPath = string(path.length() + cpfx_s + 5, ' '); fsPath.clear();
|
string xfsPath(path.length() + cpfx_s + 5, ' '); xfsPath.clear();
|
||||||
// and build
|
// 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
|
// init json
|
||||||
this->json = std::make_shared<nlohmann::json>();
|
this->json = std::make_shared<nlohmann::json>();
|
||||||
@ -58,12 +51,12 @@ Config::Config(const string& path) : path(path) {
|
|||||||
Config::~Config() { }
|
Config::~Config() { }
|
||||||
|
|
||||||
void Config::Reload() {
|
void Config::Reload() {
|
||||||
ifstream load(fsPath);
|
ifstream load = fsPath.OpenI();
|
||||||
if (load.good()) load >> *json;
|
if (load.good()) load >> *json;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::Save() {
|
void Config::Save() {
|
||||||
ofstream save(fsPath);
|
ofstream save = fsPath.OpenO();
|
||||||
if (save.good()) save << *json;
|
if (save.good()) save << *json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +67,7 @@ void Config::Save() {
|
|||||||
std::unordered_map<std::string, std::shared_ptr<Config>> ConfigManager::cfg;
|
std::unordered_map<std::string, std::shared_ptr<Config>> ConfigManager::cfg;
|
||||||
|
|
||||||
void ConfigManager::Init() {
|
void ConfigManager::Init() {
|
||||||
FSHelper::AssertDirPath("sdmc:/.starlight/config/app/" + Application::AppName());
|
//FSHelper::AssertDirPath("sdmc:/.starlight/config/app/" + Application::AppName());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigManager::End() {
|
void ConfigManager::End() {
|
||||||
|
@ -6,12 +6,14 @@
|
|||||||
|
|
||||||
#include "starlight/_incLib/json_fwd.hpp"
|
#include "starlight/_incLib/json_fwd.hpp"
|
||||||
|
|
||||||
|
#include "starlight/util/Path.h"
|
||||||
|
|
||||||
//#include "starlight/gfx/DrawContext.h"
|
//#include "starlight/gfx/DrawContext.h"
|
||||||
|
|
||||||
namespace starlight {
|
namespace starlight {
|
||||||
class Config {
|
class Config {
|
||||||
private:
|
private:
|
||||||
std::string fsPath;
|
util::Path fsPath;
|
||||||
std::shared_ptr<nlohmann::json> json;
|
std::shared_ptr<nlohmann::json> json;
|
||||||
public:
|
public:
|
||||||
const std::string path;
|
const std::string path;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Path.h"
|
#include "Path.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -9,9 +10,15 @@ using std::string;
|
|||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::getline;
|
using std::getline;
|
||||||
|
|
||||||
|
using std::fstream;
|
||||||
|
using std::ifstream;
|
||||||
|
using std::ofstream;
|
||||||
|
|
||||||
using starlight::util::Path;
|
using starlight::util::Path;
|
||||||
|
|
||||||
string Path::destructed = "";
|
Path::Path() {
|
||||||
|
strpath = "sdmc:";
|
||||||
|
}
|
||||||
|
|
||||||
Path::Path(const string& path, bool noverify) {
|
Path::Path(const string& path, bool noverify) {
|
||||||
if (noverify) { strpath = path; return; }
|
if (noverify) { strpath = path; return; }
|
||||||
@ -27,8 +34,7 @@ Path::Path(const Path& path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Path::~Path() {
|
Path::~Path() {
|
||||||
destructed.append(strpath);
|
|
||||||
destructed.append("\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Path Path::Up(int levels) {
|
Path Path::Up(int levels) {
|
||||||
@ -66,8 +72,43 @@ Path Path::Combine(const string& token) {
|
|||||||
return Path(path, true);
|
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() {
|
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;
|
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);
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <ios>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
|
|
||||||
namespace starlight {
|
namespace starlight {
|
||||||
@ -13,8 +14,9 @@ namespace starlight {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// vars...
|
// vars...
|
||||||
static std::string destructed;
|
//
|
||||||
|
|
||||||
|
Path();
|
||||||
Path(const std::string& path, bool noverify = false);
|
Path(const std::string& path, bool noverify = false);
|
||||||
Path(const Path& path);
|
Path(const Path& path);
|
||||||
~Path();
|
~Path();
|
||||||
@ -23,13 +25,24 @@ namespace starlight {
|
|||||||
Path Up(int levels = 1);
|
Path Up(int levels = 1);
|
||||||
Path Combine(const std::string& token);
|
Path Combine(const std::string& token);
|
||||||
|
|
||||||
|
// check
|
||||||
|
bool Exists();
|
||||||
|
bool IsFile();
|
||||||
|
bool IsDirectory();
|
||||||
|
|
||||||
// operation
|
// operation
|
||||||
Path& CreateDirectory();
|
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
|
// semi-obsolete but whatever, it can stay for now
|
||||||
inline bool IsDirPath() const { return strpath.back() == '/'; }
|
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; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,10 +71,14 @@ void Core::Init() {
|
|||||||
auto pipf = std::make_shared<sl::ui::Label>(VRect(0,0,400,240));
|
auto pipf = std::make_shared<sl::ui::Label>(VRect(0,0,400,240));
|
||||||
pipf->SetFont("default.16"); pipf->borderColor = Color::black;
|
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.");
|
//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();
|
std::stringstream st;
|
||||||
pipf->SetText(Path::destructed);
|
//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);
|
parallax->Add(pipf);
|
||||||
|
|
||||||
|
Path("sdmc:/all/your/base").CreateDirectory();
|
||||||
|
|
||||||
clearColor = Color(0.0f, 0.5f, 0.5f);
|
clearColor = Color(0.0f, 0.5f, 0.5f);
|
||||||
|
|
||||||
// test config
|
// test config
|
||||||
|
Loading…
x
Reference in New Issue
Block a user