std::experimental fix for android
This commit is contained in:
115
data/File.h
Normal file
115
data/File.h
Normal file
@@ -0,0 +1,115 @@
|
||||
#ifndef FS_FILE_H
|
||||
#define FS_FILE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
||||
|
||||
#if __cplusplus > 201103L
|
||||
|
||||
// use new cross-platform std::experimental stuff
|
||||
#include <experimental/filesystem>
|
||||
|
||||
namespace FS {
|
||||
|
||||
class File {
|
||||
|
||||
std::experimental::filesystem::path path;
|
||||
|
||||
public:
|
||||
|
||||
File(const std::string& fileOrFolder) : path(fileOrFolder) {
|
||||
|
||||
}
|
||||
|
||||
bool exists() const {
|
||||
return std::experimental::filesystem::exists(path);
|
||||
}
|
||||
|
||||
std::vector<File> listFiles() const {
|
||||
std::vector<File> res;
|
||||
for (std::experimental::filesystem::directory_entry entry : std::experimental::filesystem::directory_iterator(path)) {
|
||||
const std::string abs = entry.path().string();
|
||||
res.push_back(File(abs));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string getPath() const {
|
||||
return path.string();
|
||||
}
|
||||
|
||||
std::string getFilename() const {
|
||||
return path.filename().string();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// use linux-only fallback
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace FS {
|
||||
|
||||
class File {
|
||||
|
||||
std::string path;
|
||||
|
||||
public:
|
||||
|
||||
File(const std::string& fileOrFolder) : path(fileOrFolder) {
|
||||
|
||||
}
|
||||
|
||||
bool exists() const {
|
||||
struct stat st;
|
||||
int res = stat(path.c_str(), &st );
|
||||
return res == 0;
|
||||
}
|
||||
|
||||
/** list of all files/folders within the current folder */
|
||||
std::vector<File> listFiles() const {
|
||||
|
||||
std::vector<File> res;
|
||||
|
||||
DIR* dir;
|
||||
|
||||
dir = opendir(path.c_str());
|
||||
if (!dir) {throw Exception("failed to open folder " + path);}
|
||||
|
||||
// fetch all entries
|
||||
while(true) {
|
||||
dirent* entry = readdir(dir);
|
||||
if (!entry) {break;}
|
||||
const std::string abs = path + "/" + entry->d_name;
|
||||
res.push_back(File(abs));
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
const std::string& getPath() const {
|
||||
return path;
|
||||
}
|
||||
|
||||
const std::string getFilename() const {
|
||||
const size_t lastSlash = path.find_last_of("/");
|
||||
if (std::string::npos == lastSlash) {return path;}
|
||||
std::string name = path.substr(lastSlash+1);
|
||||
return name;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // FS_FILE_H
|
||||
Reference in New Issue
Block a user