From 3db839f163913c594d0453b86d835c68cf1dbe1f Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 23 May 2021 05:56:39 -0500 Subject: [PATCH] Moved fs utils into MoonFS --- src/moon/zip/straw.cpp | 34 ++++++++++++++++++---------------- src/moon/zip/straw.h | 5 +++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/moon/zip/straw.cpp b/src/moon/zip/straw.cpp index 8fa70e62..ae6d5243 100644 --- a/src/moon/zip/straw.cpp +++ b/src/moon/zip/straw.cpp @@ -6,26 +6,28 @@ #include #include -namespace fs = std::filesystem; using namespace std; using namespace miniz_cpp; +namespace fs = std::filesystem; bool isDirectory; - -string normalize(string path){ - replace(path.begin(), path.end(), '\\', '/'); - return path; -} - -string joinPath(string base, string file){ - return normalize((fs::path(base) / fs::path(file)).string()); -} +zip_file zipFile; StrawFile::StrawFile(string path){ - this->path = normalize(path); + this->path = MoonFS::normalize(path); +} + +namespace MoonFS { + string normalize(string path){ + replace(path.begin(), path.end(), '\\', '/'); + return path; + } + + string joinPath(string base, string file){ + return normalize((fs::path(base) / fs::path(file)).string()); + } } -zip_file zipFile; void StrawFile::open(){ if(!(isDirectory = fs::is_directory(this->path))) @@ -33,14 +35,14 @@ void StrawFile::open(){ } 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 StrawFile::entries(){ if(isDirectory) { vector fileList; 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)); } return fileList; @@ -51,7 +53,7 @@ vector StrawFile::entries(){ string StrawFile::read(string file){ 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) return(std::string((std::istreambuf_iterator(in)), std::istreambuf_iterator())); } @@ -63,7 +65,7 @@ void StrawFile::read(string file, TextureFileEntry *entry){ if(isDirectory){ char *data; 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); size = ftell(f); diff --git a/src/moon/zip/straw.h b/src/moon/zip/straw.h index e05808cf..2e9bf3b4 100644 --- a/src/moon/zip/straw.h +++ b/src/moon/zip/straw.h @@ -5,6 +5,11 @@ #include #include +namespace MoonFS { + std::string normalize(std::string path); + std::string joinPath(std::string base, std::string file); +} + class StrawFile { public: StrawFile(std::string path);