2022-06-21 12:50:39 +00:00
|
|
|
#ifndef FSLIB_H
|
|
|
|
#define FSLIB_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
using string = std::wstring;
|
|
|
|
using char_t = wchar_t;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
using string = std::string;
|
|
|
|
using char_t = char;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// windows version stolen from
|
|
|
|
// http://www.martinbroadhurst.com/list-the-files-in-a-directory-in-c.html
|
|
|
|
|
|
|
|
namespace FSLib {
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool exists(const string &path);
|
|
|
|
bool isDirectory(const string &path);
|
|
|
|
bool rename(const string &file_a, const string &file_b);
|
|
|
|
bool deleteFile(const string &file);
|
|
|
|
string canonical(const string &path);
|
|
|
|
bool createDirectoryFull(const string &path);
|
|
|
|
string getContainingDirectory(const string &path);
|
|
|
|
string getFileName(const string &path);
|
|
|
|
string getFileExtension(const string &path);
|
2022-06-21 12:50:39 +00:00
|
|
|
extern char dir_divisor;
|
|
|
|
|
|
|
|
class Directory {
|
|
|
|
public:
|
|
|
|
Directory() = delete;
|
2022-07-21 18:17:24 +00:00
|
|
|
explicit Directory(const string &path_);
|
|
|
|
explicit Directory(const Directory &d) = default;
|
|
|
|
explicit Directory(Directory &&d) = default;
|
2022-06-21 12:50:39 +00:00
|
|
|
|
|
|
|
class Iterator {
|
|
|
|
public:
|
2022-07-21 18:17:24 +00:00
|
|
|
explicit Iterator(const Directory &d_);
|
2022-06-21 12:50:39 +00:00
|
|
|
~Iterator();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2022-07-21 18:17:24 +00:00
|
|
|
explicit Iterator(bool ended_);
|
2022-06-21 12:50:39 +00:00
|
|
|
#else
|
2022-07-21 18:17:24 +00:00
|
|
|
Iterator(const Directory &d_, const struct dirent *current_entry_);
|
2022-06-21 12:50:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Iterator() = delete;
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
Iterator(const Iterator &i) = default;
|
2022-06-21 12:50:39 +00:00
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
Iterator(Iterator &&i) = default;
|
2022-06-21 12:50:39 +00:00
|
|
|
|
|
|
|
char_t const *operator*() const;
|
|
|
|
|
|
|
|
Iterator &operator++();
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool operator==(const Iterator &i_other) const;
|
2022-06-21 12:50:39 +00:00
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
Iterator operator++(int) {
|
|
|
|
Iterator ret(*this);
|
2022-06-21 12:50:39 +00:00
|
|
|
operator++();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
bool operator!=(const Iterator &i_other) const {
|
|
|
|
return !(i_other == *this);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
#ifndef _WIN32
|
|
|
|
DIR *d{};
|
|
|
|
const struct dirent *current_entry{};
|
|
|
|
#else
|
|
|
|
HANDLE hFind{};
|
|
|
|
WIN32_FIND_DATA data{};
|
|
|
|
bool ended{ false };
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
using iterator = Iterator;
|
|
|
|
using const_iterator = Iterator;
|
|
|
|
|
|
|
|
iterator end();
|
|
|
|
|
|
|
|
const_iterator end() const;
|
|
|
|
|
|
|
|
iterator begin() {
|
2022-07-21 18:17:24 +00:00
|
|
|
return Iterator(*this);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
2022-07-21 18:17:24 +00:00
|
|
|
return Iterator(*this);
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator cbegin() const {
|
|
|
|
return begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator cend() const {
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char_t *path() const {
|
|
|
|
return dir_path.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
const char_t *validPath() const {
|
2022-07-21 18:17:24 +00:00
|
|
|
return dir_path.substr(0, dir_path.length() - 2).c_str();
|
2022-06-21 12:50:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
string dir_path;
|
|
|
|
};
|
|
|
|
} // namespace FSLib
|
|
|
|
|
|
|
|
#endif
|