VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gfiletree.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gfiletree.h
19 ///
20 
21 #ifndef G_FILE_TREE__H
22 #define G_FILE_TREE__H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gdirectorytree.h"
27 #include "gexception.h"
28 #include <list>
29 #include <vector>
30 
31 namespace G
32 {
33  class FileTree ;
34 }
35 
36 /// \class G::FileTree
37 /// A class for walking files in a directory tree, with repositioning. The
38 /// implementation uses G::DirectoryTree, transparently stepping over
39 /// directories.
40 ///
41 /// The iteration model is:
42 /// \code
43 /// while( tree.next() != G::Path() )
44 /// {
45 /// if( fn( tree.current() ) )
46 /// tree.reposition(...) ;
47 /// }
48 /// \endcode
49 ///
51 {
52 public:
53 
54  FileTree() ;
55  ///< Default contructor for an empty tree.
56 
57  explicit FileTree( const G::Path & root , DirectoryTreeCallback * ignore = 0 ) ;
58  ///< Constructor for the tree rooted at the given path.
59  ///< (The optional ignore callback does not need to ignore
60  ///< directories; the FileTree class does that anyways.)
61 
62  void reroot( const G::Path & root ) ;
63  ///< Resets the root, as if newly constructed.
64 
65  G::Path root() const ;
66  ///< Returns the current root, as passed to the constructor
67  ///< or reroot(). Returns the empty string if default
68  ///< constructed.
69 
70  bool moved() const ;
71  ///< Returns true after construction, reroot(), first(), last()
72  ///< or a successful reposition(). The moved() state is
73  ///< reset by next().
74 
75  G::Path current() const ;
76  ///< Returns the current path.
77 
78  G::Path next() ;
79  ///< Moves to the next file in the tree, depth first, and returns
80  ///< the path. Returns the empty path if off the end or if
81  ///< default constructed. However, in the moved() state the
82  ///< first call to next() just returns current() and resets
83  ///< the moved() state. It is okay to call next() if currently
84  ///< off the end.
85 
86  bool reposition( const G::Path & path ) ;
87  ///< Repositions the iterator within the current tree, at or after
88  ///< the given position. The path does not have to represent an
89  ///< existing file or directory.
90  ///<
91  ///< The given path should normally start with the root(). If the
92  ///< root is a relative path then the reposition() path should
93  ///< also be relative.
94  ///<
95  ///< Returns false if there are no files at or after the required
96  ///< position or if the position is outside the tree, without
97  ///< changing the current iterator position.
98 
99  int reposition( const G::Path & path , int ) ;
100  ///< An overload that distinguishes the repositioning failure reason
101  ///< as (1) out-of-tree or (2) off-the-end. Returns zero on success.
102 
103  bool last() ;
104  ///< Repositions to the last position (so that next() will soon go off
105  ///< the end), or to the root() if currently reversed. Returns true
106  ///< if the current path changes. This re-examines the file-system,
107  ///< so the new position may be unexpectedly 'less than' the previous
108  ///< position.
109 
110  bool first() ;
111  ///< Repositions to the root() position, or to the last position if
112  ///< currently reversed(). Returns true if the current path changes.
113  ///< This re-examines the file-system, so the new position
114  ///< may be unexpectedly 'greater than' the previous position.
115 
116  void reverse( bool = true ) ;
117  ///< Iterate in reverse, and reposition() to at-or-before rather than
118  ///< at-or-after.
119 
120  bool reversed() const ;
121  ///< Returns true if currently reverse()d.
122 
123  void swap( FileTree & other ) ;
124  ///< Swaps this and other.
125 
126 private:
127  bool m_moved ;
128  G::Path m_root ;
129  DirectoryTree m_p ;
130  DirectoryTreeCallback * m_callback ;
131 } ;
132 
133 namespace G
134 {
135  inline
136  void swap( FileTree & a , FileTree & b )
137  {
138  a.swap( b ) ;
139  }
140 }
141 
142 #endif
A class for walking files in a directory tree, with repositioning.
Definition: gfiletree.h:50
A directory tree iterator for sorted, depth-first traversal of files and directories.
FileTree()
Default contructor for an empty tree.
Definition: gfiletree.cpp:27
void reroot(const G::Path &root)
Resets the root, as if newly constructed.
Definition: gfiletree.cpp:53
bool reposition(const G::Path &path)
Repositions the iterator within the current tree, at or after the given position. ...
Definition: gfiletree.cpp:92
void reverse(bool=true)
Iterate in reverse, and reposition() to at-or-before rather than at-or-after.
Definition: gfiletree.cpp:164
void swap(FileTree &other)
Swaps this and other.
Definition: gfiletree.cpp:44
bool reversed() const
Returns true if currently reverse()d.
Definition: gfiletree.cpp:169
G::Path current() const
Returns the current path.
Definition: gfiletree.cpp:68
bool moved() const
Returns true after construction, reroot(), first(), last() or a successful reposition().
Definition: gfiletree.cpp:174
bool first()
Repositions to the root() position, or to the last position if currently reversed().
Definition: gfiletree.cpp:150
G::Path next()
Moves to the next file in the tree, depth first, and returns the path.
Definition: gfiletree.cpp:73
G::Path root() const
Returns the current root, as passed to the constructor or reroot().
Definition: gfiletree.cpp:63
A callback interface to allow G::DirectoryTree to ignore paths.
A Path object represents a file system path.
Definition: gpath.h:72
bool last()
Repositions to the last position (so that next() will soon go off the end), or to the root() if curre...
Definition: gfiletree.cpp:134