tv_rename/filesystem.hpp

145 lines
3.1 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
#ifndef GUI
bool exists( const string &path );
string canonical( const string &path );
#endif
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
bool isDirectory( const string &path );
bool rename( const string &file_a, const string &file_b );
2019-01-04 19:19:08 +00:00
2019-02-04 16:39:48 +00:00
class Directory {
public:
Directory( const string &path_ ) : dir_path( path_ ) {
#ifdef _WIN32
// need to append \\* for windows to search files in directory
dir_path.append( L"\\*" );
#endif
}
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:
#ifdef _WIN32
Iterator( const Directory &d_ ) {
hFind = FindFirstFileW( d_.path(), &data );
if ( hFind != INVALID_HANDLE_VALUE ) {
if ( !wcscmp( data.cFileName, L"." ) ||
!wcscmp( data.cFileName, L".." ) ) {
++( *this );
}
} else {
ended = true;
}
}
// this is definitely not a good way to create the "end" iterator
// but it was the only way I thought of with my limited knowledge of
// windows.h
Iterator( bool ended_ ) : ended( ended_ ) {}
~Iterator() {
if ( hFind )
FindClose( hFind );
}
#else
Iterator( const Directory &d_ ) : d( opendir( d_.path() ) ) {
current_entry = readdir( d );
if ( current_entry != nullptr &&
( !strcmp( current_entry->d_name, "." ) ||
!strcmp( current_entry->d_name, ".." ) ) )
++( *this );
}
Iterator( const Directory &d_, const struct dirent *current_entry_ )
: d( opendir( d_.path() ) ), current_entry( current_entry_ ) {}
~Iterator() {
closedir( d );
}
#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
Iterator operator++( int );
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
2019-02-04 16:39:48 +00:00
bool operator!=( const Iterator &i_other ) const;
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 begin();
const_iterator begin() const;
const_iterator cbegin() const;
iterator end();
const_iterator end() const;
const_iterator cend() const;
const char_t *path() const;
private:
string dir_path;
};
} // namespace FSLib
2019-01-03 18:01:43 +00:00
#endif