From 2b6106c41f3959d4deb8efc58c9055de0339959e Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 23 Feb 2017 01:06:11 +0300 Subject: Impliment basic VirtFsDir for virtual fs based on directories. Api similar to VirtFs. VirtFsDir unused for now. --- src/fs/files.cpp | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/fs/files.cpp') diff --git a/src/fs/files.cpp b/src/fs/files.cpp index 516283fe6..abb0ee956 100644 --- a/src/fs/files.cpp +++ b/src/fs/files.cpp @@ -30,7 +30,10 @@ #include "fs/virtlist.h" #endif // defined(ANDROID) || defined(__native_client__) +#include "utils/stringutils.h" + #include +#include #include "debug.h" @@ -206,13 +209,8 @@ int Files::copyFile(const std::string &restrict srcName, bool Files::existsLocal(const std::string &path) { - bool flg(false); - std::fstream file; - file.open(path.c_str(), std::ios::in); - if (file.is_open()) - flg = true; - file.close(); - return flg; + struct stat statbuf; + return stat(path.c_str(), &statbuf) == 0; } bool Files::loadTextFileLocal(const std::string &fileName, @@ -266,3 +264,35 @@ void Files::deleteFilesInDirectory(std::string path) closedir(dir); } } + +void Files::enumFiles(StringVect &files, + std::string path, + const bool skipSymlinks) +{ + if (findLast(path, "/") == false) + path += "/"; + const struct dirent *next_file = nullptr; + DIR *const dir = opendir(path.c_str()); + + if (dir) + { + while ((next_file = readdir(dir))) + { + const std::string file = next_file->d_name; + if (file == "." || file == "..") + continue; + if (skipSymlinks == true) + { + struct stat statbuf; + if (lstat(path.c_str(), &statbuf) == 0 && + S_ISLNK(statbuf.st_mode) != 0) + { + continue; + } + } + files.push_back(file); + } + closedir(dir); + } +} + -- cgit v1.2.3-70-g09d2