| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_FS_FILESYSTEM_H ) |
| 23 |
#define INCLUDED_FS_FILESYSTEM_H |
| 24 |
|
| 25 |
#include "string/string.h" |
| 26 |
#include "os/path.h" |
| 27 |
|
| 28 |
#include <map> |
| 29 |
|
| 30 |
inline unsigned int path_get_depth( const char* path ){ |
| 31 |
unsigned int depth = 0; |
| 32 |
while ( path != 0 && path[0] != '\0' ) |
| 33 |
{ |
| 34 |
path = strchr( path, '/' ); |
| 35 |
if ( path != 0 ) { |
| 36 |
++path; |
| 37 |
} |
| 38 |
++depth; |
| 39 |
} |
| 40 |
return depth; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
template<typename file_type> |
| 47 |
class GenericFileSystem |
| 48 |
{ |
| 49 |
class Path |
| 50 |
{ |
| 51 |
CopiedString m_path; |
| 52 |
unsigned int m_depth; |
| 53 |
public: |
| 54 |
Path( const char* path ) |
| 55 |
: m_path( path ), m_depth( path_get_depth( c_str() ) ){ |
| 56 |
} |
| 57 |
Path( StringRange range ) |
| 58 |
: m_path( range ), m_depth( path_get_depth( c_str() ) ){ |
| 59 |
} |
| 60 |
bool operator<( const Path& other ) const { |
| 61 |
return string_less_nocase( c_str(), other.c_str() ); |
| 62 |
} |
| 63 |
unsigned int depth() const { |
| 64 |
return m_depth; |
| 65 |
} |
| 66 |
const char* c_str() const { |
| 67 |
return m_path.c_str(); |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
class Entry |
| 72 |
{ |
| 73 |
file_type* m_file; |
| 74 |
public: |
| 75 |
Entry() : m_file( 0 ){ |
| 76 |
} |
| 77 |
Entry( file_type* file ) : m_file( file ){ |
| 78 |
} |
| 79 |
file_type* file() const { |
| 80 |
return m_file; |
| 81 |
} |
| 82 |
bool is_directory() const { |
| 83 |
return file() == 0; |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
typedef std::map<Path, Entry> Entries; |
| 88 |
Entries m_entries; |
| 89 |
|
| 90 |
public: |
| 91 |
typedef typename Entries::iterator iterator; |
| 92 |
typedef typename Entries::value_type value_type; |
| 93 |
typedef Entry entry_type; |
| 94 |
|
| 95 |
iterator begin(){ |
| 96 |
return m_entries.begin(); |
| 97 |
} |
| 98 |
iterator end(){ |
| 99 |
return m_entries.end(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
entry_type& operator[]( const Path& path ){ |
| 106 |
{ |
| 107 |
const char* end = path_remove_directory( path.c_str() ); |
| 108 |
while ( end[0] != '\0' ) |
| 109 |
{ |
| 110 |
Path dir( StringRange( path.c_str(), end ) ); |
| 111 |
m_entries.insert( value_type( dir, Entry( 0 ) ) ); |
| 112 |
end = path_remove_directory( end ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return m_entries[path]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
iterator find( const Path& path ){ |
| 121 |
return m_entries.find( path ); |
| 122 |
} |
| 123 |
|
| 124 |
iterator begin( const char* root ){ |
| 125 |
if ( root[0] == '\0' ) { |
| 126 |
return m_entries.begin(); |
| 127 |
} |
| 128 |
iterator i = m_entries.find( root ); |
| 129 |
if ( i == m_entries.end() ) { |
| 130 |
return i; |
| 131 |
} |
| 132 |
return ++i; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
template<typename visitor_type> |
| 140 |
void traverse( visitor_type visitor, const char* root ){ |
| 141 |
unsigned int start_depth = path_get_depth( root ); |
| 142 |
unsigned int skip_depth = 0; |
| 143 |
for ( iterator i = begin( root ); i != end() && i->first.depth() > start_depth; ++i ) |
| 144 |
{ |
| 145 |
if ( i->first.depth() == skip_depth ) { |
| 146 |
skip_depth = 0; |
| 147 |
} |
| 148 |
if ( skip_depth == 0 ) { |
| 149 |
if ( !i->second.is_directory() ) { |
| 150 |
visitor.file( i->first.c_str() ); |
| 151 |
} |
| 152 |
else if ( visitor.directory( i->first.c_str(), i->first.depth() - start_depth ) ) { |
| 153 |
skip_depth = i->first.depth(); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
#endif |