Panda3D

pathReplace.h

00001 // Filename: pathReplace.h
00002 // Created by:  drose (07Feb03)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef PATHREPLACE_H
00016 #define PATHREPLACE_H
00017 
00018 #include "pandatoolbase.h"
00019 #include "pathStore.h"
00020 #include "referenceCount.h"
00021 #include "globPattern.h"
00022 #include "filename.h"
00023 #include "dSearchPath.h"
00024 #include "pvector.h"
00025 #include "pmap.h"
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //       Class : PathReplace
00029 // Description : This encapsulates the user's command-line request to
00030 //               replace existing, incorrect pathnames to models and
00031 //               textures from a file with correct pathnames.  It
00032 //               corresponds to a sequence of -pr command-line
00033 //               options, as well as the -pp option.
00034 //
00035 //               This can also go the next step, which is to convert a
00036 //               known file into a suitable form for storing in a
00037 //               model file.  In this capacity, it corresponds to the
00038 //               -ps and -pd options.
00039 ////////////////////////////////////////////////////////////////////
00040 class PathReplace : public ReferenceCount {
00041 public:
00042   PathReplace();
00043   ~PathReplace();
00044 
00045   INLINE void clear_error();
00046   INLINE bool had_error() const;
00047 
00048   INLINE void clear();
00049   INLINE void add_pattern(const string &orig_prefix, const string &replacement_prefix);
00050 
00051   INLINE int get_num_patterns() const;
00052   INLINE const string &get_orig_prefix(int n) const;
00053   INLINE const string &get_replacement_prefix(int n) const;
00054 
00055   INLINE bool is_empty() const;
00056 
00057   Filename match_path(const Filename &orig_filename, 
00058                       const DSearchPath &additional_path = DSearchPath());
00059   Filename store_path(const Filename &orig_filename);
00060 
00061   INLINE Filename convert_path(const Filename &orig_filename,
00062                                const DSearchPath &additional_path = DSearchPath());
00063   
00064   void full_convert_path(const Filename &orig_filename,
00065                          const DSearchPath &additional_path,
00066                          Filename &resolved_path,
00067                          Filename &output_path);
00068 
00069   void write(ostream &out, int indent_level = 0) const;
00070 
00071 public:
00072   // This is used (along with _entries) to support match_path().
00073   DSearchPath _path;
00074 
00075   // These are used to support store_path().
00076   PathStore _path_store;
00077   Filename _path_directory;
00078   bool _copy_files;
00079   Filename _copy_into_directory;
00080 
00081   // If this is this true, then the error flag is set (see had_error()
00082   // and clear_error()) if any Filename passed to match_path() or
00083   // convert_path(), and unmatched by one of the prefixes, happens to
00084   // be an absolute pathname.
00085   bool _noabs;
00086 
00087   // If this is true, then the error flag is set if any Filename
00088   // passed to match_path() or convert_path() cannot be found.
00089   bool _exists;
00090 
00091 private:
00092   bool copy_this_file(Filename &filename);
00093 
00094   class Component {
00095   public:
00096     INLINE Component(const string &component);
00097     INLINE Component(const Component &copy);
00098     INLINE void operator = (const Component &copy);
00099 
00100     GlobPattern _orig_prefix;
00101     bool _double_star;
00102   };
00103   typedef pvector<Component> Components;
00104 
00105   class Entry {
00106   public:
00107     Entry(const string &orig_prefix, const string &replacement_prefix);
00108     INLINE Entry(const Entry &copy);
00109     INLINE void operator = (const Entry &copy);
00110 
00111     bool try_match(const Filename &filename, Filename &new_filename) const;
00112     size_t r_try_match(const vector_string &components, size_t oi, size_t ci) const;
00113 
00114     string _orig_prefix;
00115     Components _orig_components;
00116     bool _is_local;
00117     string _replacement_prefix;
00118   };
00119 
00120   typedef pvector<Entry> Entries;
00121   Entries _entries;
00122 
00123   bool _error_flag;
00124 
00125   typedef pmap<Filename, Filename> Copied;
00126   Copied _orig_to_target;
00127   Copied _target_to_orig;
00128 };
00129 
00130 #include "pathReplace.I"
00131 
00132 #endif
00133 
00134   
00135   
 All Classes Functions Variables Enumerations