15 #include "pathReplace.h" 16 #include "config_util.h" 17 #include "config_pandatoolbase.h" 19 #include "virtualFileSystem.h" 28 _path_store = PS_keep;
57 bool got_match =
false;
61 Entries::const_iterator ei;
62 for (ei = _entries.begin(); ei != _entries.end(); ++ei) {
63 const Entry &entry = (*ei);
65 if (entry.try_match(orig_filename, new_filename)) {
74 if (vfs->
exists(new_filename)) {
85 if (_path_store == PS_keep) {
105 pandatoolbase_cat.error()
106 <<
"File does not exist: " << match <<
"\n";
107 }
else if (pandatoolbase_cat.is_debug()) {
108 pandatoolbase_cat.debug()
109 <<
"File does not exist: " << match <<
"\n";
120 pandatoolbase_cat.error()
121 <<
"Absolute pathname: " << orig_filename <<
"\n";
122 }
else if (pandatoolbase_cat.is_debug()) {
123 pandatoolbase_cat.debug()
124 <<
"Absolute pathname: " << orig_filename <<
"\n";
130 if (_path_store != PS_keep) {
131 Filename new_filename = orig_filename;
144 pandatoolbase_cat.error()
145 <<
"File does not exist: " << orig_filename <<
"\n";
146 }
else if (pandatoolbase_cat.is_debug()) {
147 pandatoolbase_cat.debug()
148 <<
"File does not exist: " << orig_filename <<
"\n";
150 return orig_filename;
163 if (orig_filename.empty()) {
164 return orig_filename;
173 copy_this_file(filename);
176 switch (_path_store) {
224 bool got_match =
false;
228 Entries::const_iterator ei;
229 for (ei = _entries.begin(); ei != _entries.end(); ++ei) {
230 const Entry &entry = (*ei);
232 if (entry.try_match(orig_filename, new_filename)) {
236 match = new_filename;
241 if (vfs->
exists(new_filename)) {
242 resolved_path = new_filename;
243 goto calculate_output_path;
253 resolved_path = new_filename;
254 goto calculate_output_path;
267 pandatoolbase_cat.error()
268 <<
"File does not exist: " << match <<
"\n";
269 }
else if (pandatoolbase_cat.is_debug()) {
270 pandatoolbase_cat.debug()
271 <<
"File does not exist: " << match <<
"\n";
274 resolved_path = match;
275 goto calculate_output_path;
283 pandatoolbase_cat.error()
284 <<
"Absolute pathname: " << orig_filename <<
"\n";
285 }
else if (pandatoolbase_cat.is_debug()) {
286 pandatoolbase_cat.debug()
287 <<
"Absolute pathname: " << orig_filename <<
"\n";
294 Filename new_filename = orig_filename;
299 match = orig_filename;
300 resolved_path = new_filename;
301 goto calculate_output_path;
309 pandatoolbase_cat.error()
310 <<
"File does not exist: " << orig_filename <<
"\n";
311 }
else if (pandatoolbase_cat.is_debug()) {
312 pandatoolbase_cat.debug()
313 <<
"File does not exist: " << orig_filename <<
"\n";
315 match = orig_filename;
316 resolved_path = orig_filename;
321 calculate_output_path:
324 if (copy_this_file(resolved_path)) {
325 match = resolved_path;
329 switch (_path_store) {
331 if (resolved_path.empty())
332 output_path = resolved_path;
334 output_path = resolved_path;
341 if (resolved_path.empty())
342 output_path = resolved_path;
344 output_path = resolved_path;
350 if (resolved_path.empty())
351 output_path = resolved_path;
353 output_path = resolved_path;
379 write(ostream &out,
int indent_level)
const {
380 Entries::const_iterator ei;
381 for (ei = _entries.begin(); ei != _entries.end(); ++ei) {
382 indent(out, indent_level)
383 <<
"-pr " << (*ei)._orig_prefix <<
"=" 384 << (*ei)._replacement_prefix <<
"\n";
387 for (
int i = 0; i < num_directories; i++) {
388 indent(out, indent_level)
391 indent(out, indent_level)
392 <<
"-ps " << _path_store <<
"\n";
395 switch (_path_store) {
398 indent(out, indent_level)
399 <<
"-pd " << _path_directory <<
"\n";
406 indent(out, indent_level)
407 <<
"-pc " << _copy_into_directory <<
"\n";
411 indent(out, indent_level)
425 copy_this_file(
Filename &filename) {
426 if (_copy_into_directory.
is_local()) {
427 _copy_into_directory =
Filename(_path_directory, _copy_into_directory);
430 Copied::iterator ci = _orig_to_target.find(filename);
431 if (ci != _orig_to_target.end()) {
434 if (filename != (*ci).second) {
435 filename = (*ci).second;
442 ci = _target_to_orig.find(target_filename);
443 if (ci != _target_to_orig.end()) {
444 if ((*ci).second != filename) {
446 pandatoolbase_cat.error()
447 <<
"Filename conflict! Both " << (*ci).second <<
" and " 448 << filename <<
" map to " << target_filename <<
"\n";
452 _orig_to_target[filename] = filename;
456 _orig_to_target[filename] = target_filename;
457 _target_to_orig[target_filename] = filename;
462 if (!vfs->
copy_file(filename, target_filename)) {
464 pandatoolbase_cat.error()
465 <<
"Cannot copy file from " << filename <<
" to " << target_filename
467 _orig_to_target[filename] = filename;
471 filename = target_filename;
481 Entry(
const string &orig_prefix,
const string &replacement_prefix) :
482 _orig_prefix(orig_prefix),
483 _replacement_prefix(replacement_prefix)
486 if (_orig_prefix.length() > 1 &&
487 _orig_prefix[_orig_prefix.length() - 1] ==
'/') {
488 _orig_prefix = _orig_prefix.substr(0, _orig_prefix.length() - 1);
490 if (_replacement_prefix.length() > 1 &&
491 _replacement_prefix[_replacement_prefix.length() - 1] ==
'/') {
492 _replacement_prefix = _replacement_prefix.substr(0, _replacement_prefix.length() - 1);
498 vector_string components;
500 vector_string::const_iterator ci;
501 for (ci = components.begin(); ci != components.end(); ++ci) {
502 _orig_components.push_back(Component(*ci));
514 bool PathReplace::Entry::
516 if (_is_local != filename.
is_local()) {
519 vector_string components;
521 size_t mi = r_try_match(components, 0, 0);
528 string result = _replacement_prefix;
529 while (mi < components.size()) {
530 if (!result.empty()) {
533 result += components[mi];
536 new_filename = result;
552 size_t PathReplace::Entry::
553 r_try_match(
const vector_string &components,
size_t oi,
size_t ci)
const {
554 if (oi >= _orig_components.size()) {
558 if (ci >= components.size()) {
567 const Component &orig_component = _orig_components[oi];
568 if (orig_component._double_star) {
571 size_t mi = r_try_match(components, oi, ci + 1);
577 return r_try_match(components, oi + 1, ci);
582 if (orig_component._orig_prefix.matches(components[ci])) {
584 return r_try_match(components, oi + 1, ci + 1);
bool copy_file(const Filename &orig_filename, const Filename &new_filename)
Attempts to copy the contents of the indicated file to the indicated file.
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...
string get_basename() const
Returns the basename part of the filename.
bool resolve_filename(Filename &filename, const DSearchPath &searchpath, const string &default_extension=string()) const
Searches the given search path for the filename.
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS's file system.
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.
bool make_directory_full(const Filename &filename)
Attempts to create a directory within the file system.
bool is_fully_qualified() const
Returns true if the filename is fully qualified, e.g.
int get_num_directories() const
Returns the number of directories on the search list.
const Filename & get_directory(int n) const
Returns the nth directory on the search list.
bool exists(const Filename &filename) const
Convenience function; returns true if the named file exists.
The name of a file, such as a texture file or an Egg file.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
void extract_components(vector_string &components) const
Extracts out the individual directory components of the path into a series of strings.
bool is_local() const
Returns true if the filename is local, e.g.
bool make_relative_to(Filename directory, bool allow_backups=true)
Adjusts this filename, which must be a fully-specified pathname beginning with a slash, to make it a relative filename, relative to the fully-specified directory indicated (which must also begin with, and may or may not end with, a slash–a terminating slash is ignored).
void make_absolute()
Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname)...
This class stores a list of directories that can be searched, in order, to locate a particular file...
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...