From caca85ca11aad39198874ea16eefdb698e12372e Mon Sep 17 00:00:00 2001 From: AnxietyWump Date: Wed, 20 May 2020 18:41:04 -0700 Subject: [PATCH] make more consistent. add get files in a dir. --- source/FileManager.cpp | 66 ++++++++++++++++++++++++++++++------------ source/FileManager.hpp | 3 +- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/source/FileManager.cpp b/source/FileManager.cpp index 180c2d9..a8ea996 100755 --- a/source/FileManager.cpp +++ b/source/FileManager.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "FileManager.hpp" @@ -30,18 +31,19 @@ using namespace simpleIniParser; using namespace std; +namespace fs = std::filesystem; -namespace ku +namespace dsu { - std::vector FileManager::readFile(std::string path) + vector FileManager::readFile(string path) { - std::ifstream file; - file.open(path, std::ios::in | std::ios::binary | std::ios::ate); + ifstream file; + file.open(path, ios::in | ios::binary | ios::ate); auto size = file.tellg(); - file.seekg(0, std::ios::beg); + file.seekg(0, ios::beg); - std::vector buffer(size); + vector buffer(size); file.read(buffer.data(), size); file.close(); @@ -69,7 +71,7 @@ namespace ku bool FileManager::deleteFile(string filename) { - if (fileExists(filename)) + if (fs::exists(filename)) { return remove(filename.c_str()) == 0; } @@ -79,19 +81,47 @@ namespace ku bool FileManager::fileExists(string filename) { - FILE *file = fopen(filename.c_str(), "r"); - - if (file) - { - fflush(file); - fsync(fileno(file)); - fclose(file); - return true; - } - + if(fs::exists(filename)) return true; return false; } + vector FileManager::getExistingFiles(string path) + { + DIR *dir; + vector files; + + // First check if the dir even exists. + if(fs::exists(path)) + { + // Then make sure it's actually a directory, and not a file. All before + // iterating all of the files in the directory. + dir = opendir(path.c_str()); + if(dir != NULL) + { + for (const auto & ft : fs::directory_iterator(path)) + { + string file; + string ext = ft.path().extension().string(); + string fname = ft.path().filename().string();; + string fextname = ext + fname; + + if(ext == "") + { + file = fname; + } else + { + file = fextname; + } + files.push_back(file); + } + } + + closedir(dir); + + return files; + } + } + // http://stackoverflow.com/a/11366985 bool FileManager::createSubfolder(string path) { @@ -367,4 +397,4 @@ namespace ku unzCloseCurrentFile(unz); return 0; } -} // namespace ku +} // namespace dsu diff --git a/source/FileManager.hpp b/source/FileManager.hpp index dd0ea20..31c0a05 100755 --- a/source/FileManager.hpp +++ b/source/FileManager.hpp @@ -21,13 +21,14 @@ #include #include -namespace ku { +namespace dsu { class FileManager { public: static std::vector readFile(std::string path); static bool writeFile(std::string filename, std::string data); static bool deleteFile(std::string filename); static bool fileExists(std::string filename); + static std::vector getExistingFiles(std::string path); static bool createSubfolder(std::string path); static bool extract(std::string filename, std::string destination); static void cleanUpFiles();