directory_tree.h

Go to the documentation of this file.
00001 #ifndef DIRECTORY_TREE_CLASS
00002 #define DIRECTORY_TREE_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : directory_tree                                                    *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2004-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "directory.h"
00019 #include "file_info.h"
00020 
00021 #include <basis/byte_array.h>
00022 #include <basis/contracts.h>
00023 #include <basis/outcome.h>
00024 #include <structures/string_array.h>
00025 
00026 namespace filesystem {
00027 
00028 // forward declarations.
00029 class dir_tree_iterator;
00030 class filename;
00031 class filename_list;
00032 class filename_tree;
00033 class fname_tree_creator;
00034 
00036 
00037 class directory_tree : public virtual basis::packable
00038 {
00039 public:
00040   directory_tree();  
00041 
00042   directory_tree(const basis::astring &path, const char *pattern = "*",
00043           bool ignore_files = false);
00045 
00055   ~directory_tree();
00056 
00057   DEFINE_CLASS_NAME("directory_tree");
00058 
00059   bool good() const { return _scanned_okay; }
00061 
00062   const basis::astring &path() const;
00064 
00065   bool reset(const basis::astring &path, const char *pattern = "*");
00067 
00071   filename_tree *seek(const basis::astring &dir_name, bool ignore_initial) const;
00073 
00078   virtual int packed_size() const;
00080   virtual void pack(basis::byte_array &packed_form) const;
00082   virtual bool unpack(basis::byte_array &packed_form);
00084 
00085   bool calculate(bool just_size);
00087 
00090   bool calculate(filename_tree *start, bool just_size);
00092 
00093   basis::outcome add_path(const basis::astring &new_item, bool just_size = false);
00095 
00101   basis::outcome remove_path(const basis::astring &zap_item);
00103 
00107   static bool compare_trees(const directory_tree &source,
00108           const directory_tree &target, filename_list &differences,
00109           file_info::file_similarity how_to_compare);
00111 
00120   static bool compare_trees(const directory_tree &source,
00121           const basis::astring &source_start, const directory_tree &target,
00122           const basis::astring &target_start, filename_list &differences,
00123           file_info::file_similarity how_to_compare);
00124     // compares the trees but not at their roots.  the location on the source
00125     // side is specified by "source_start", which must be a path found under
00126     // the "source" tree.  similarly, the "target_start" will be the location
00127     // compared with the "source" + "source_start".  the "diffs" will still
00128     // be valid with respect to "source" rather than "source_start".
00129 
00130   void text_form(basis::astring &tree_dump, bool show_files = true);
00132 
00135   // Note on the iterator functions: the iterator becomes invalid if the
00136   // directory tree is reset.  the only valid operation on the iterator
00137   // at that point is to call throw_out().
00138 
00139   enum traversal_types {
00140     prefix,  
00141     infix,  
00142     postfix  
00143   };
00144 
00145   dir_tree_iterator *start(traversal_types type) const;
00147 
00148   dir_tree_iterator *start_at(filename_tree *start,
00149           traversal_types type) const;
00151 
00152   static bool jump_to(dir_tree_iterator &scanning, const basis::astring &sub_path);
00154 
00157   static bool current_dir(dir_tree_iterator &scanning, filename &dir_name);
00159 
00160   static bool current(dir_tree_iterator &scanning, filename &dir_name,
00161           structures::string_array &to_fill);
00163 
00168   static bool current(dir_tree_iterator &scanning, filename &dir_name,
00169           filename_list &to_fill);
00171 
00172   static filename_list *access(dir_tree_iterator &scanning);
00174 
00177   static bool depth(dir_tree_iterator &scanning, int &depth);
00179 
00181   static bool children(dir_tree_iterator &scanning, int &children);
00183 
00184   static bool next(dir_tree_iterator &scanning);
00186 
00188   static void throw_out(dir_tree_iterator * &to_whack);
00190 
00191 private:
00192   bool _scanned_okay;  
00193   basis::astring *_path;  
00194   basis::astring *_pattern;  
00195   filename_tree *_real_tree;  
00196   bool _ignore_files;  
00197   fname_tree_creator *_creator;  
00198 
00199   static filename_tree *goto_current(dir_tree_iterator &scanning);
00201 
00203   void traverse(const basis::astring &path, const char *pattern,
00204           filename_tree &add_to);
00206 
00210   basis::outcome find_common_root(const basis::astring &path, bool exists,
00211           filename_tree * &common_root, basis::astring &common_path,
00212           structures::string_array &pieces, int &match_place);
00214 
00219 };
00220 
00221 } //namespace.
00222 
00223 #endif
00224 
Generated on Sat Jan 28 04:22:20 2012 for hoople2 project by  doxygen 1.6.3