tv_rename/filesystem.hpp

124 lines
2.2 KiB
C++
Raw Normal View History

2019-01-03 18:01:43 +00:00
#ifndef FSLIB_H
#define FSLIB_H
2019-02-04 16:39:48 +00:00
#include <string.h>
2019-01-03 18:01:43 +00:00
#include <string>
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
#include <Windows.h>
#else
2019-01-03 18:01:43 +00:00
#include <dirent.h>
2019-01-23 19:46:03 +00:00
#endif
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
// set apropriate data types for each operating system
#ifdef _WIN32
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
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
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 );
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
class Directory {
public:
Directory( const string &path_ );
2019-02-04 16:39:48 +00:00
Directory() = delete;
Directory( const Directory &d ) = default;
Directory( Directory &&d ) = default;
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
class Iterator {
public:
Iterator( const Directory &d_ );
~Iterator();
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
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;
#else
HANDLE hFind;
WIN32_FIND_DATA data;
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
private:
string dir_path;
};
} // namespace FSLib
2019-01-03 18:01:43 +00:00
#endif