Moved fs utils into MoonFS

This commit is contained in:
Alex 2021-05-23 05:56:39 -05:00
parent 9db5593844
commit 3db839f163
2 changed files with 23 additions and 16 deletions

View File

@ -6,12 +6,18 @@
#include <algorithm> #include <algorithm>
#include <filesystem> #include <filesystem>
namespace fs = std::filesystem;
using namespace std; using namespace std;
using namespace miniz_cpp; using namespace miniz_cpp;
namespace fs = std::filesystem;
bool isDirectory; bool isDirectory;
zip_file zipFile;
StrawFile::StrawFile(string path){
this->path = MoonFS::normalize(path);
}
namespace MoonFS {
string normalize(string path){ string normalize(string path){
replace(path.begin(), path.end(), '\\', '/'); replace(path.begin(), path.end(), '\\', '/');
return path; return path;
@ -20,12 +26,8 @@ string normalize(string path){
string joinPath(string base, string file){ string joinPath(string base, string file){
return normalize((fs::path(base) / fs::path(file)).string()); return normalize((fs::path(base) / fs::path(file)).string());
} }
StrawFile::StrawFile(string path){
this->path = normalize(path);
} }
zip_file zipFile;
void StrawFile::open(){ void StrawFile::open(){
if(!(isDirectory = fs::is_directory(this->path))) if(!(isDirectory = fs::is_directory(this->path)))
@ -33,14 +35,14 @@ void StrawFile::open(){
} }
bool StrawFile::exists(string path){ bool StrawFile::exists(string path){
return isDirectory ? fs::exists(joinPath(this->path, path)) : zipFile.has_file(path); return isDirectory ? fs::exists(MoonFS::joinPath(this->path, path)) : zipFile.has_file(path);
} }
vector<string> StrawFile::entries(){ vector<string> StrawFile::entries(){
if(isDirectory) { if(isDirectory) {
vector<string> fileList; vector<string> fileList;
for (auto & entry : fs::recursive_directory_iterator(this->path)){ for (auto & entry : fs::recursive_directory_iterator(this->path)){
string path = normalize(entry.path().string()); string path = MoonFS::normalize(entry.path().string());
fileList.push_back(path.substr(path.find(this->path) + this->path.length() + 1)); fileList.push_back(path.substr(path.find(this->path) + this->path.length() + 1));
} }
return fileList; return fileList;
@ -51,7 +53,7 @@ vector<string> StrawFile::entries(){
string StrawFile::read(string file){ string StrawFile::read(string file){
if(isDirectory){ if(isDirectory){
std::ifstream in(joinPath(this->path, file), std::ios::in | std::ios::binary); std::ifstream in(MoonFS::joinPath(this->path, file), std::ios::in | std::ios::binary);
if (in) if (in)
return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>())); return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()));
} }
@ -63,7 +65,7 @@ void StrawFile::read(string file, TextureFileEntry *entry){
if(isDirectory){ if(isDirectory){
char *data; char *data;
long size; long size;
FILE* f = fopen(joinPath(this->path, file).c_str(), "rb"); FILE* f = fopen(MoonFS::joinPath(this->path, file).c_str(), "rb");
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size = ftell(f); size = ftell(f);

View File

@ -5,6 +5,11 @@
#include <vector> #include <vector>
#include <string> #include <string>
namespace MoonFS {
std::string normalize(std::string path);
std::string joinPath(std::string base, std::string file);
}
class StrawFile { class StrawFile {
public: public:
StrawFile(std::string path); StrawFile(std::string path);