some path stuffs

This commit is contained in:
zetaPRIME 2017-01-21 18:52:17 -05:00
parent 64fa28689f
commit d64c6034a2
9 changed files with 159 additions and 9 deletions

View File

@ -29,7 +29,8 @@ INCLUDES := include
#---------------------------------------------------------------------------------
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
CFLAGS := -g -Wall -Werror -O2 -mword-relocations \
# why was -Werror here?
CFLAGS := -g -Wall -O2 -mword-relocations \
-ffunction-sections \
-fomit-frame-pointer \
$(ARCH)

View File

@ -10,12 +10,16 @@
#include "starlight/Application.h"
#include "starlight/util/FSHelper.h"
using std::string;
using std::ifstream;
using std::ofstream;
using starlight::Application;
using starlight::util::FSHelper;
using starlight::Config;
using starlight::ConfigManager;
@ -70,10 +74,7 @@ void Config::Save() {
std::unordered_map<std::string, std::shared_ptr<Config>> ConfigManager::cfg;
void ConfigManager::Init() {
ensureDirectory("sdmc:/.starlight/");
ensureDirectory("sdmc:/.starlight/config/");
ensureDirectory("sdmc:/.starlight/config/app/");
ensureDirectory("sdmc:/.starlight/config/app/" + Application::AppName());
FSHelper::AssertDirPath("sdmc:/.starlight/config/app/" + Application::AppName());
}
void ConfigManager::End() {

View File

@ -0,0 +1,31 @@
#include "FSHelper.h"
#include <sstream>
#include <unistd.h>
#include <sys/stat.h>
using std::string;
using std::stringstream;
using std::getline;
using starlight::util::FSHelper;
void FSHelper::AssertDirectory(const string& path) {
struct stat st;
if (stat(path.c_str(), &st) != 0) mkdir(path.c_str(), 0);
}
void FSHelper::AssertDirPath(const string& path) {
struct stat st;
stringstream stm(path);
string ac(path.length() + 16, ' ');
string tk = "";
ac.clear();
while (getline(stm, tk, '/')) {
ac.append(tk); ac.append("/");
if (tk.back() == ':') continue; // no further processing on "sdmc:/" etc.
if (stat(ac.c_str(), &st) != 0) mkdir(ac.c_str(), 0);
}
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "starlight/_global.h"
#include <string>
namespace starlight {
namespace util {
class FSHelper {
private:
//
public:
FSHelper() = delete;
static void AssertDirectory(const std::string& path);
static void AssertDirPath(const std::string& path);
};
}
}

View File

@ -0,0 +1,60 @@
#include "Path.h"
#include <sstream>
#include <unistd.h>
#include <sys/stat.h>
using std::string;
using std::stringstream;
using std::getline;
using starlight::util::Path;
Path::Path(const string& path, bool noverify) {
if (noverify) { strpath = path; return; }
size_t sls = path.find('/');
if (sls == string::npos) strpath = "sdmc:/" + path;
else if (path.rfind(':', sls) != sls-1) strpath = "sdmc:/" + path;
else strpath = path;
if (strpath.back() == '/') strpath.pop_back(); // clip trailing slash
}
Path::Path(const Path& path) {
strpath = path.strpath; // no need to check here!
}
Path Path::Up(int levels) {
if (levels < 1) return *this;
size_t spos = IsDirPath() ? strpath.length() - 1 : string::npos;
for (int i = 0; i < levels; i++) {
size_t pos = strpath.rfind('/', spos-1);
if (pos == string::npos) break;
if (strpath[pos] == ':') break; // don't move past root
spos = pos;
}
return Path(strpath.substr(0, spos), true);
}
Path Path::Combine(const string& token) {
string path(strpath);
stringstream st(token);
string tk;
while (getline(st, tk, '/')) {
if (tk == "") { } // ...
else if (tk == ".") { } // ignore
else if (tk == "..") {
size_t pos = path.rfind('/');
if (pos != string::npos && path[pos-1] != ':') path.erase(pos);
}
else if (tk.back() == ':') { // root switch...
path.clear();
path.append(tk);
}
else {
if (path.back() != '/') path.append("/");
path.append(tk);
}
}
return Path(path, true);
}

View File

@ -0,0 +1,30 @@
#pragma once
#include "starlight/_global.h"
#include <string>
#include <vector>
#include <iosfwd>
namespace starlight {
namespace util {
class Path {
private:
std::string strpath;
public:
// vars...
Path(const std::string& path, bool noverify = false);
Path(const Path& path);
~Path() = default;
Path Up(int levels = 1);
Path Combine(const std::string& token);
// semi-obsolete but whatever, it can stay for now
inline bool IsDirPath() const { return strpath.back() == '/'; }
inline operator std::string () const { return strpath; }
};
}
}

View File

@ -2,8 +2,7 @@
today's agenda {
app:/ prefix for local ("local") assets
cfg?
FSHelper, recursive directory assertion etc.
} then {
change Label to move the rect instead of resizing to accomodate drops (add an offset thing to Font)
figure out how I want to do separate forms???

View File

@ -3,9 +3,13 @@ function abort {
echo Make or send failed
exit
}
mode=send
if [ "$1" = "c" ]; then
mode=run
fi
cd libstarlight
make install || abort
cd ../testbed
make clean
make send || abort
make $mode || abort
cd ..

View File

@ -10,6 +10,8 @@
#include "starlight/GFXManager.h"
#include "starlight/gfx/RenderCore.h"
#include "starlight/util/Path.h"
#include "starlight/ui/ParallaxLayer.h"
#include "starlight/ui/ScrollField.h"
#include "starlight/ui/Button.h"
@ -24,6 +26,8 @@ using starlight::ThemeManager;
using starlight::GFXManager;
using starlight::gfx::RenderCore;
using starlight::util::Path;
using starlight::Application;
void Core::Init() {
@ -66,7 +70,8 @@ void Core::Init() {
auto pipf = std::make_shared<sl::ui::Label>(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.");
//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(Path("sdmc:/banana/algorithm/").Combine("porcupine/kumquat/romfs:/annual/sdmc:/puffin/pie"));
parallax->Add(pipf);
clearColor = Color(0.0f, 0.5f, 0.5f);