tv_rename/filesystem.hpp

123 lines
2.3 KiB
C++
Raw Normal View History

2019-01-03 18:01:43 +00:00
#ifndef FSLIB_H
#define FSLIB_H
#include <string>
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
#include <Windows.h>
using string = std::wstring;
using char_t = wchar_t;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
#else
2019-01-04 19:19:08 +00:00
2020-01-15 17:21:27 +00:00
#include <dirent.h>
2019-02-04 16:39:48 +00:00
using string = std::string;
using char_t = char;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
#endif
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
// windows version stolen from
// http://www.martinbroadhurst.com/list-the-files-in-a-directory-in-c.html
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
namespace FSLib {
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
bool exists( const string &path );
bool isDirectory( const string &path );
bool rename( const string &file_a, const string &file_b );
2019-07-12 21:10:40 +00:00
string canonical( const string &path );
bool createDirectoryFull( const string &path );
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
class Directory {
public:
Directory() = delete;
2020-04-16 11:39:20 +00:00
explicit Directory( const string &path_ );
explicit Directory( const Directory &d ) = default;
explicit Directory( Directory &&d ) = default;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
class Iterator {
public:
2020-04-16 11:23:02 +00:00
explicit Iterator( const Directory &d_ );
~Iterator();
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
2020-04-16 11:39:20 +00:00
explicit Iterator( bool ended_ );
2019-02-04 16:39:48 +00:00
#else
Iterator( const Directory &d_, const struct dirent *current_entry_ );
2019-02-04 16:39:48 +00:00
#endif
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
Iterator() = delete;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
Iterator( const Iterator &i ) = default;
2019-02-04 16:39:48 +00:00
Iterator( Iterator &&i ) = default;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
char_t const *operator*() const;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
Iterator &operator++();
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
bool operator==( const Iterator &i_other ) const;
2019-01-04 19:19:08 +00:00
Iterator operator++( int ) {
Iterator ret( *this );
operator++();
return ret;
}
bool operator!=( const Iterator &i_other ) const {
return !( i_other == *this );
}
2019-01-04 19:19:08 +00:00
private:
2019-02-04 16:39:48 +00:00
#ifndef _WIN32
DIR *d{};
const struct dirent *current_entry{};
2019-02-04 16:39:48 +00:00
#else
HANDLE hFind{};
WIN32_FIND_DATA data{};
2019-02-04 16:39:48 +00:00
bool ended{ false };
#endif
2019-01-04 19:19:08 +00:00
};
2019-02-04 16:39:48 +00:00
using iterator = Iterator;
using const_iterator = Iterator;
iterator end();
2019-02-04 16:39:48 +00:00
const_iterator end() const;
2019-02-04 16:39:48 +00:00
iterator begin() {
return Iterator( *this );
}
2019-02-04 16:39:48 +00:00
const_iterator begin() const {
return Iterator( *this );
}
2019-02-04 16:39:48 +00:00
const_iterator cbegin() const {
return begin();
}
2019-02-04 16:39:48 +00:00
const_iterator cend() const {
return end();
}
const char_t *path() const {
return dir_path.c_str();
}
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
const char_t *validPath() const {
2020-05-11 13:34:40 +00:00
return dir_path.substr( 0, dir_path.length() - 2 ).c_str();
}
#endif
2019-02-04 16:39:48 +00:00
private:
string dir_path;
};
} // namespace FSLib
2019-01-03 18:01:43 +00:00
#endif