Panda3D
 All Classes Functions Variables Enumerations
filenameUnifier.cxx
1 // Filename: filenameUnifier.cxx
2 // Created by: drose (05Dec00)
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 #include "filenameUnifier.h"
16 
17 #include "executionEnvironment.h"
18 
19 Filename FilenameUnifier::_txa_filename;
20 Filename FilenameUnifier::_txa_dir;
21 Filename FilenameUnifier::_rel_dirname;
22 
23 FilenameUnifier::CanonicalFilenames FilenameUnifier::_canonical_filenames;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: FilenameUnifier::set_txa_filename
27 // Access: Public, Static
28 // Description: Notes the filename the .txa file was found in. This
29 // may have come from the command line, or it may have
30 // been implicitly located. This has other implications
31 // for the FilenameUnifier, particularly in locating the bam
32 // file that saves the filenameUnifier state from last
33 // session.
34 ////////////////////////////////////////////////////////////////////
36 set_txa_filename(const Filename &txa_filename) {
37  _txa_filename = txa_filename;
38  _txa_dir = txa_filename.get_dirname();
39  if (_txa_dir.empty()) {
40  _txa_dir = ".";
41  }
42  make_canonical(_txa_dir);
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: FilenameUnifier::set_rel_dirname
47 // Access: Public, Static
48 // Description: Sets the name of the directory that texture filenames
49 // will be written relative to, when generating egg
50 // files. This is not the directory the textures are
51 // actually written to (see set_map_dirname()), but
52 // rather is the name of some directory above that,
53 // which will be the starting point for the pathnames
54 // written to the egg files. If this is empty, the full
55 // pathnames will be written to the egg files.
56 ////////////////////////////////////////////////////////////////////
58 set_rel_dirname(const Filename &rel_dirname) {
59  _rel_dirname = rel_dirname;
60  if (!_rel_dirname.empty()) {
61  make_canonical(_rel_dirname);
62  }
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: FilenameUnifier::make_bam_filename
67 // Access: Public, Static
68 // Description: Returns a new filename that's made relative to the
69 // bam file itself, suitable for writing to the bam file.
70 ////////////////////////////////////////////////////////////////////
73  make_canonical(filename);
74  filename.make_relative_to(_txa_dir);
75  return filename;
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: FilenameUnifier::get_bam_filename
80 // Access: Public, Static
81 // Description: Returns an absolute pathname based on the given
82 // relative pathname, presumably read from the bam file
83 // and relative to the bam file.
84 ////////////////////////////////////////////////////////////////////
87  if (!filename.empty()) {
88  filename.make_absolute(_txa_dir);
89  }
90  return filename;
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: FilenameUnifier::make_egg_filename
95 // Access: Public, Static
96 // Description: Returns a new filename that's made relative to the
97 // rel_directory, suitable for writing out within egg
98 // files.
99 ////////////////////////////////////////////////////////////////////
102  if (!filename.empty()) {
103  make_canonical(filename);
104  filename.make_relative_to(_rel_dirname);
105  }
106  return filename;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: FilenameUnifier::make_user_filename
111 // Access: Public, Static
112 // Description: Returns a new filename that's made relative to the
113 // current directory, suitable for reporting to the
114 // user.
115 ////////////////////////////////////////////////////////////////////
118  if (!filename.empty()) {
119  make_canonical(filename);
121  }
122  return filename;
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: FilenameUnifier::make_canonical
127 // Access: Public, Static
128 // Description: Does the same thing as Filename::make_canonical()--it
129 // converts the filename to its canonical form--but
130 // caches the operation so that repeated calls to
131 // filenames in the same directory will tend to be
132 // faster.
133 ////////////////////////////////////////////////////////////////////
136  if (filename.empty()) {
137  return;
138  }
139 
140  Filename orig_dirname = filename.get_dirname();
141 
142  CanonicalFilenames::iterator fi;
143  fi = _canonical_filenames.find(orig_dirname);
144  if (fi != _canonical_filenames.end()) {
145  filename.set_dirname((*fi).second);
146  return;
147  }
148 
149  Filename new_dirname = orig_dirname;
150  if (new_dirname.empty()) {
151  new_dirname = ".";
152  }
153  new_dirname.make_canonical();
154  filename.set_dirname(new_dirname);
155 
156  _canonical_filenames.insert(CanonicalFilenames::value_type(orig_dirname, new_dirname));
157 }
static Filename make_bam_filename(Filename filename)
Returns a new filename that's made relative to the bam file itself, suitable for writing to the bam f...
static void set_txa_filename(const Filename &txa_filename)
Notes the filename the .txa file was found in.
bool make_canonical()
Converts this filename to a canonical name by replacing the directory part with the fully-qualified d...
Definition: filename.cxx:1072
string get_dirname() const
Returns the directory part of the filename.
Definition: filename.I:424
static Filename make_user_filename(Filename filename)
Returns a new filename that's made relative to the current directory, suitable for reporting to the u...
static Filename make_egg_filename(Filename filename)
Returns a new filename that's made relative to the rel_directory, suitable for writing out within egg...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
static Filename get_bam_filename(Filename filename)
Returns an absolute pathname based on the given relative pathname, presumably read from the bam file ...
static void make_canonical(Filename &filename)
Does the same thing as Filename::make_canonical()–it converts the filename to its canonical form–but ...
void set_dirname(const string &s)
Replaces the directory part of the filename.
Definition: filename.cxx:726
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).
Definition: filename.cxx:1766
void make_absolute()
Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname)...
Definition: filename.cxx:1019
static Filename get_cwd()
Returns the name of the current working directory.
static void set_rel_dirname(const Filename &rel_dirname)
Sets the name of the directory that texture filenames will be written relative to, when generating egg files.