Panda3D
pathReplace.h
1 // Filename: pathReplace.h
2 // Created by: drose (07Feb03)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PATHREPLACE_H
16 #define PATHREPLACE_H
17 
18 #include "pandatoolbase.h"
19 #include "pathStore.h"
20 #include "referenceCount.h"
21 #include "globPattern.h"
22 #include "filename.h"
23 #include "dSearchPath.h"
24 #include "pvector.h"
25 #include "pmap.h"
26 
27 ////////////////////////////////////////////////////////////////////
28 // Class : PathReplace
29 // Description : This encapsulates the user's command-line request to
30 // replace existing, incorrect pathnames to models and
31 // textures from a file with correct pathnames. It
32 // corresponds to a sequence of -pr command-line
33 // options, as well as the -pp option.
34 //
35 // This can also go the next step, which is to convert a
36 // known file into a suitable form for storing in a
37 // model file. In this capacity, it corresponds to the
38 // -ps and -pd options.
39 ////////////////////////////////////////////////////////////////////
40 class PathReplace : public ReferenceCount {
41 public:
42  PathReplace();
43  ~PathReplace();
44 
45  INLINE void clear_error();
46  INLINE bool had_error() const;
47 
48  INLINE void clear();
49  INLINE void add_pattern(const string &orig_prefix, const string &replacement_prefix);
50 
51  INLINE int get_num_patterns() const;
52  INLINE const string &get_orig_prefix(int n) const;
53  INLINE const string &get_replacement_prefix(int n) const;
54 
55  INLINE bool is_empty() const;
56 
57  Filename match_path(const Filename &orig_filename,
58  const DSearchPath &additional_path = DSearchPath());
59  Filename store_path(const Filename &orig_filename);
60 
61  INLINE Filename convert_path(const Filename &orig_filename,
62  const DSearchPath &additional_path = DSearchPath());
63 
64  void full_convert_path(const Filename &orig_filename,
65  const DSearchPath &additional_path,
66  Filename &resolved_path,
67  Filename &output_path);
68 
69  void write(ostream &out, int indent_level = 0) const;
70 
71 public:
72  // This is used (along with _entries) to support match_path().
73  DSearchPath _path;
74 
75  // These are used to support store_path().
76  PathStore _path_store;
77  Filename _path_directory;
78  bool _copy_files;
79  Filename _copy_into_directory;
80 
81  // If this is this true, then the error flag is set (see had_error()
82  // and clear_error()) if any Filename passed to match_path() or
83  // convert_path(), and unmatched by one of the prefixes, happens to
84  // be an absolute pathname.
85  bool _noabs;
86 
87  // If this is true, then the error flag is set if any Filename
88  // passed to match_path() or convert_path() cannot be found.
89  bool _exists;
90 
91 private:
92  bool copy_this_file(Filename &filename);
93 
94  class Component {
95  public:
96  INLINE Component(const string &component);
97  INLINE Component(const Component &copy);
98  INLINE void operator = (const Component &copy);
99 
100  GlobPattern _orig_prefix;
101  bool _double_star;
102  };
104 
105  class Entry {
106  public:
107  Entry(const string &orig_prefix, const string &replacement_prefix);
108  INLINE Entry(const Entry &copy);
109  INLINE void operator = (const Entry &copy);
110 
111  bool try_match(const Filename &filename, Filename &new_filename) const;
112  size_t r_try_match(const vector_string &components, size_t oi, size_t ci) const;
113 
114  string _orig_prefix;
115  Components _orig_components;
116  bool _is_local;
117  string _replacement_prefix;
118  };
119 
120  typedef pvector<Entry> Entries;
121  Entries _entries;
122 
123  bool _error_flag;
124 
126  Copied _orig_to_target;
127  Copied _target_to_orig;
128 };
129 
130 #include "pathReplace.I"
131 
132 #endif
133 
134 
135 
Filename match_path(const Filename &orig_filename, const DSearchPath &additional_path=DSearchPath())
Looks for a match for the given filename among all the replacement patterns, and returns the first ma...
Definition: pathReplace.cxx:54
const string & get_orig_prefix(int n) const
Returns the original prefix associated with the nth pattern.
Definition: pathReplace.I:81
void full_convert_path(const Filename &orig_filename, const DSearchPath &additional_path, Filename &resolved_path, Filename &output_path)
Converts the input path into two different forms: A resolved path, and an output path.
int get_num_patterns() const
Returns the number of original/replace patterns that have been added.
Definition: pathReplace.I:70
const string & get_replacement_prefix(int n) const
Returns the replacement prefix associated with the nth pattern.
Definition: pathReplace.I:93
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
A base class for all things that want to be reference-counted.
bool is_empty() const
Returns true if the PathReplace object specifies no action, or false if convert_path() may do somethi...
Definition: pathReplace.I:105
void clear()
Removes all the patterns from the specification.
Definition: pathReplace.I:45
This encapsulates the user&#39;s command-line request to replace existing, incorrect pathnames to models ...
Definition: pathReplace.h:40
This class stores a list of directories that can be searched, in order, to locate a particular file...
Definition: dSearchPath.h:32
void add_pattern(const string &orig_prefix, const string &replacement_prefix)
Adds the indicated original/replace pattern to the specification.
Definition: pathReplace.I:59
void clear_error()
Resets the error flag to the no-error state.
Definition: pathReplace.I:24
bool had_error() const
Returns true if an error was detected since the last call to clear_error(), false otherwise...
Definition: pathReplace.I:35
Filename store_path(const Filename &orig_filename)
Given a path to an existing filename, converts it as specified in the _path_store and or _path_direct...
Filename convert_path(const Filename &orig_filename, const DSearchPath &additional_path=DSearchPath())
Calls match_path() followed by store_path(), to replace the initial prefix and then convert the file ...
Definition: pathReplace.I:117
This class can be used to test for string matches against standard Unix-shell filename globbing conve...
Definition: globPattern.h:37