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