feisty meow concerns codebase  2.140
directory_tree.h
Go to the documentation of this file.
1 #ifndef DIRECTORY_TREE_CLASS
2 #define DIRECTORY_TREE_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : directory_tree *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 2004-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "directory.h"
19 #include "file_info.h"
20 
21 #include <basis/byte_array.h>
22 #include <basis/contracts.h>
23 #include <basis/outcome.h>
25 
26 namespace filesystem {
27 
28 // forward declarations.
29 class dir_tree_iterator;
30 class filename;
31 class filename_list;
32 class filename_tree;
33 class fname_tree_creator;
34 
36 
37 class directory_tree : public virtual basis::packable
38 {
39 public:
40  directory_tree();
41 
42  directory_tree(const basis::astring &path, const char *pattern = "*",
43  bool ignore_files = false);
45 
56 
57  DEFINE_CLASS_NAME("directory_tree");
58 
59  bool good() const { return _scanned_okay; }
61 
62  const basis::astring &path() const;
64 
65  bool reset(const basis::astring &path, const char *pattern = "*");
67 
71  filename_tree *seek(const basis::astring &dir_name, bool ignore_initial) const;
73 
78  virtual int packed_size() const;
80  virtual void pack(basis::byte_array &packed_form) const;
82  virtual bool unpack(basis::byte_array &packed_form);
84 
85  bool calculate(bool just_size);
87 
90  bool calculate(filename_tree *start, bool just_size);
92 
93  basis::outcome add_path(const basis::astring &new_item, bool just_size = false);
95 
101  basis::outcome remove_path(const basis::astring &zap_item);
103 
109 
110  static bool compare_trees(const directory_tree &source,
111  const directory_tree &target, filename_list &differences,
112  file_info::file_similarity how_to_compare);
114 
123  static bool compare_trees(const directory_tree &source,
124  const basis::astring &source_start, const directory_tree &target,
125  const basis::astring &target_start, filename_list &differences,
126  file_info::file_similarity how_to_compare);
127  // compares the trees but not at their roots. the location on the source
128  // side is specified by "source_start", which must be a path found under
129  // the "source" tree. similarly, the "target_start" will be the location
130  // compared with the "source" + "source_start". the "diffs" will still
131  // be valid with respect to "source" rather than "source_start".
132 
133  void text_form(basis::astring &tree_dump, bool show_files = true);
135 
138  // Note on the iterator functions: the iterator becomes invalid if the
139  // directory tree is reset. the only valid operation on the iterator
140  // at that point is to call throw_out().
141 
145  postfix
146  };
147 
148  dir_tree_iterator *start(traversal_types type) const;
150 
151  dir_tree_iterator *start_at(filename_tree *start,
152  traversal_types type) const;
154 
155  static bool jump_to(dir_tree_iterator &scanning, const basis::astring &sub_path);
157 
160  static bool current_dir(dir_tree_iterator &scanning, filename &dir_name);
162 
163  static bool current(dir_tree_iterator &scanning, filename &dir_name,
164  structures::string_array &to_fill);
166 
171  static bool current(dir_tree_iterator &scanning, filename &dir_name,
172  filename_list &to_fill);
174 
175  static filename_list *access(dir_tree_iterator &scanning);
177 
180  static bool depth(dir_tree_iterator &scanning, int &depth);
182 
184  static bool children(dir_tree_iterator &scanning, int &children);
186 
187  static bool next(dir_tree_iterator &scanning);
189 
191  static void throw_out(dir_tree_iterator * &to_whack);
193 
194 private:
195  bool _scanned_okay;
196  basis::astring *_path;
197  basis::astring *_pattern;
198  filename_tree *_real_tree;
199  bool _ignore_files;
200  fname_tree_creator *_creator;
201 
202  static filename_tree *goto_current(dir_tree_iterator &scanning);
204 
206  void traverse(const basis::astring &path, const char *pattern,
207  filename_tree &add_to);
209 
213  basis::outcome find_common_root(const basis::astring &path, bool exists,
214  filename_tree * &common_root, basis::astring &common_path,
215  structures::string_array &pieces, int &match_place);
217 
222 };
223 
224 } //namespace.
225 
226 #endif
227 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
An object that traverses directory trees and provides a view of all files.
static bool current(dir_tree_iterator &scanning, filename &dir_name, structures::string_array &to_fill)
retrieves the information for the iterator's current location.
static bool compare_trees(const directory_tree &source, const directory_tree &target, filename_list &differences, file_info::file_similarity how_to_compare)
compares the tree in "source" with the tree in "target".
void text_form(basis::astring &tree_dump, bool show_files=true)
provides a visual representation of the tree in "tree_dump".
filename_tree * seek(const basis::astring &dir_name, bool ignore_initial) const
finds the "dir_name" in our tree.
basis::outcome add_path(const basis::astring &new_item, bool just_size=false)
adds a "new_item" into the tree.
dir_tree_iterator * start_at(filename_tree *start, traversal_types type) const
starts the iterator at a specific "start" node.
static filename_list * access(dir_tree_iterator &scanning)
more dangerous operation that lets the actual list be manipulated.
static bool depth(dir_tree_iterator &scanning, int &depth)
returns the current depth of the iterator.
@ postfix
postfix means that subnodes are traversed first (depth first).
@ prefix
prefix means that subnodes are processed after their parent.
@ infix
infix (for binary trees) goes 1) left, 2) current, 3) right.
basis::outcome remove_path(const basis::astring &zap_item)
removes the "zap_item" from the tree.
virtual bool unpack(basis::byte_array &packed_form)
unpacks the directory_tree from a byte_array.
static void throw_out(dir_tree_iterator *&to_whack)
cleans up an iterator that was previously opened with start().
bool calculate(bool just_size)
visits each file in the directory_tree and calculates its attributes.
virtual int packed_size() const
reports the size after packing up the tree.
DEFINE_CLASS_NAME("directory_tree")
static bool children(dir_tree_iterator &scanning, int &children)
returns the number of children for the current node.
const basis::astring & path() const
returns the root of the directory tree that we manage.
basis::outcome make_directories(const basis::astring new_root)
creates all of the directories in this object, but start at the "new_root".
directory_tree()
constructs an empty tree.
static bool next(dir_tree_iterator &scanning)
goes to the next filename in the "scanning" iterator.
virtual void pack(basis::byte_array &packed_form) const
packs the directory_tree into a byte_array.
dir_tree_iterator * start(traversal_types type) const
starts an iterator on the directory tree.
bool good() const
returns true if the directory existed and we read its contents.
bool reset(const basis::astring &path, const char *pattern="*")
gets rid of any current files and rescans the directory at "path".
static bool jump_to(dir_tree_iterator &scanning, const basis::astring &sub_path)
seeks to a "sub_path" below the iterator's current position.
static bool current_dir(dir_tree_iterator &scanning, filename &dir_name)
sets "dir_name" to the directory name at the "scanning" location.
file_similarity
this enum encapsulates how files may be compared.
Definition: file_info.h:32
Provides operations commonly needed on file names.
Definition: filename.h:64
this is the tree factory used in the recursive_unpack.
Definition: filename_tree.h:53
An array of strings with some additional helpful methods.
Definition: string_array.h:32
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37