Panda3D
 All Classes Functions Variables Enumerations
softFilename.cxx
1 // Filename: softFilename.cxx
2 // Created by: drose (10Nov00)
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 "softFilename.h"
16 
17 #include "pnotify.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: SoftFilename::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 SoftFilename::
25 SoftFilename(const string &dirname, const string &filename) :
26  _dirname(dirname),
27  _filename(filename)
28 {
29  _has_version = false;
30  _major = 0;
31  _minor = 0;
32  _in_cvs = false;
33  _wants_cvs = false;
34  _use_count = 0;
35 
36  _base = _filename;
37 
38  // Scan for a version number and an optional extension after each
39  // dot in the filename.
40  size_t dot = _filename.find('.');
41  while (dot != string::npos) {
42  size_t m = dot + 1;
43  const char *fstr = _filename.c_str();
44  char *endptr;
45  // Check for a numeric version number.
46  int major = strtol(fstr + m , &endptr, 10);
47  if (endptr != fstr + m && *endptr == '-') {
48  // We got a major number, is there a minor number?
49  m = (endptr - fstr) + 1;
50  int minor = strtol(fstr + m, &endptr, 10);
51  if (endptr != fstr + m && (*endptr == '.' || *endptr == '\0')) {
52  // We got a minor number too!
53  _has_version = true;
54  _base = _filename.substr(0, dot + 1);
55  _major = major;
56  _minor = minor;
57  _ext = endptr;
58  return;
59  }
60  }
61 
62  // That wasn't a version number. Is there more?
63  dot = _filename.find('.', dot + 1);
64  }
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: SoftFilename::Copy Constructor
69 // Access: Public
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 SoftFilename::
73 SoftFilename(const SoftFilename &copy) :
74  _dirname(copy._dirname),
75  _filename(copy._filename),
76  _has_version(copy._has_version),
77  _base(copy._base),
78  _major(copy._major),
79  _minor(copy._minor),
80  _ext(copy._ext),
81  _in_cvs(copy._in_cvs),
82  _wants_cvs(copy._wants_cvs),
83  _use_count(copy._use_count)
84 {
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: SoftFilename::Copy Assignment operator
89 // Access: Public
90 // Description:
91 ////////////////////////////////////////////////////////////////////
92 void SoftFilename::
93 operator = (const SoftFilename &copy) {
94  _dirname = copy._dirname;
95  _filename = copy._filename;
96  _has_version = copy._has_version;
97  _base = copy._base;
98  _major = copy._major;
99  _minor = copy._minor;
100  _ext = copy._ext;
101  _in_cvs = copy._in_cvs;
102  _wants_cvs = copy._wants_cvs;
103  _use_count = copy._use_count;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: SoftFilename::get_dirname
108 // Access: Public
109 // Description: Returns the name of the directory this file was
110 // found in.
111 ////////////////////////////////////////////////////////////////////
112 const string &SoftFilename::
113 get_dirname() const {
114  return _dirname;
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: SoftFilename::get_filename
119 // Access: Public
120 // Description: Returns the actual filename as found in the
121 // directory.
122 ////////////////////////////////////////////////////////////////////
123 const string &SoftFilename::
124 get_filename() const {
125  return _filename;
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: SoftFilename::has_version
130 // Access: Public
131 // Description: Returns true if the filename had a version number,
132 // false otherwise.
133 ////////////////////////////////////////////////////////////////////
134 bool SoftFilename::
135 has_version() const {
136  return _has_version;
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: SoftFilename::get_1_0_filename
141 // Access: Public
142 // Description: Returns what the filename would be if it were version
143 // 1-0.
144 ////////////////////////////////////////////////////////////////////
145 string SoftFilename::
147  nassertr(_has_version, string());
148  return _base + "1-0" + _ext;
149 }
150 
151 ////////////////////////////////////////////////////////////////////
152 // Function: SoftFilename::get_base
153 // Access: Public
154 // Description: Returns the base part of the filename. This is
155 // everything before the version number.
156 ////////////////////////////////////////////////////////////////////
157 const string &SoftFilename::
158 get_base() const {
159  nassertr(_has_version, _filename);
160  return _base;
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function: SoftFilename::get_major
165 // Access: Public
166 // Description: Returns the major version number.
167 ////////////////////////////////////////////////////////////////////
168 int SoftFilename::
169 get_major() const {
170  nassertr(_has_version, 0);
171  return _major;
172 }
173 
174 ////////////////////////////////////////////////////////////////////
175 // Function: SoftFilename::get_minor
176 // Access: Public
177 // Description: Returns the minor version number.
178 ////////////////////////////////////////////////////////////////////
179 int SoftFilename::
180 get_minor() const {
181  nassertr(_has_version, 0);
182  return _minor;
183 }
184 
185 ////////////////////////////////////////////////////////////////////
186 // Function: SoftFilename::get_extension
187 // Access: Public
188 // Description: Returns the extension part of the filename. This is
189 // everything after the version number.
190 ////////////////////////////////////////////////////////////////////
191 const string &SoftFilename::
192 get_extension() const {
193  nassertr(_has_version, _ext);
194  return _ext;
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: SoftFilename::get_non_extension
199 // Access: Public
200 // Description: Returns the filename part, without the extension.
201 ////////////////////////////////////////////////////////////////////
202 string SoftFilename::
204  nassertr(_has_version, _filename);
205  nassertr(_ext.length() < _filename.length(), _filename);
206  return _filename.substr(0, _filename.length() - _ext.length());
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: SoftFilename::is_1_0
211 // Access: Public
212 // Description: Returns true if this is a version 1_0 filename, false
213 // otherwise.
214 ////////////////////////////////////////////////////////////////////
215 bool SoftFilename::
216 is_1_0() const {
217  nassertr(_has_version, false);
218  return (_major == 1 && _minor == 0);
219 }
220 
221 ////////////////////////////////////////////////////////////////////
222 // Function: SoftFilename::make_1_0
223 // Access: Public
224 // Description: Makes this a 1_0 filename.
225 ////////////////////////////////////////////////////////////////////
226 void SoftFilename::
228  _has_version = true;
229  _major = 1;
230  _minor = 0;
231  _filename = get_1_0_filename();
232 }
233 
234 ////////////////////////////////////////////////////////////////////
235 // Function: SoftFilename::is_same_file
236 // Access: Public
237 // Description: Returns true if this file has the same base and
238 // extension as the other, disregarding the version
239 // number; false otherwise.
240 ////////////////////////////////////////////////////////////////////
241 bool SoftFilename::
242 is_same_file(const SoftFilename &other) const {
243  return _base == other._base && _ext == other._ext;
244 }
245 
246 ////////////////////////////////////////////////////////////////////
247 // Function: SoftFilename::Ordering operator
248 // Access: Public
249 // Description: Puts filenames in order such that the files with the
250 // same base are sorted together, ignoring extension;
251 // and within files with the same base, files are sorted
252 // in decreasing version number order so that the most
253 // recent version appears first.
254 ////////////////////////////////////////////////////////////////////
255 bool SoftFilename::
256 operator < (const SoftFilename &other) const {
257  if (_base != other._base) {
258  return _base < other._base;
259  }
260 
261  if (_has_version != other._has_version) {
262  // If one has a version and the other one doesn't, the one without
263  // a version comes first.
264  return _has_version < other._has_version;
265  }
266 
267  if (_has_version) {
268  if (_major != other._major) {
269  return _major > other._major;
270  }
271  if (_minor != other._minor) {
272  return _minor > other._minor;
273  }
274  }
275 
276  return false;
277 }
278 
279 ////////////////////////////////////////////////////////////////////
280 // Function: SoftFilename::set_in_cvs
281 // Access: Public
282 // Description: Sets the flag that indicates whether this file is
283 // known to be entered into the CVS database.
284 ////////////////////////////////////////////////////////////////////
285 void SoftFilename::
286 set_in_cvs(bool in_cvs) {
287  _in_cvs = in_cvs;
288 }
289 
290 ////////////////////////////////////////////////////////////////////
291 // Function: SoftFilename::get_in_cvs
292 // Access: Public
293 // Description: Returns true if this file is known to be entered in
294 // the CVS database, false if it is not.
295 ////////////////////////////////////////////////////////////////////
296 bool SoftFilename::
297 get_in_cvs() const {
298  return _in_cvs;
299 }
300 
301 ////////////////////////////////////////////////////////////////////
302 // Function: SoftFilename::set_wants_cvs
303 // Access: Public
304 // Description: Sets the flag that indicates whether this file
305 // should be entered into the CVS database.
306 ////////////////////////////////////////////////////////////////////
307 void SoftFilename::
308 set_wants_cvs(bool wants_cvs) {
309  _wants_cvs = wants_cvs;
310 }
311 
312 ////////////////////////////////////////////////////////////////////
313 // Function: SoftFilename::get_wants_cvs
314 // Access: Public
315 // Description: Returns true if this file should be entered into the
316 // CVS database, false otherwise.
317 ////////////////////////////////////////////////////////////////////
318 bool SoftFilename::
319 get_wants_cvs() const {
320  return _wants_cvs;
321 }
322 
323 ////////////////////////////////////////////////////////////////////
324 // Function: SoftFilename::increment_use_count
325 // Access: Public
326 // Description: Indicates that this filename is referenced by one
327 // more scene file.
328 ////////////////////////////////////////////////////////////////////
329 void SoftFilename::
331  _use_count++;
332 }
333 
334 ////////////////////////////////////////////////////////////////////
335 // Function: SoftFilename::get_use_count
336 // Access: Public
337 // Description: Returns the number of scene files that referenced
338 // this filename.
339 ////////////////////////////////////////////////////////////////////
340 int SoftFilename::
341 get_use_count() const {
342  return _use_count;
343 }
bool get_in_cvs() const
Returns true if this file is known to be entered in the CVS database, false if it is not...
bool has_version() const
Returns true if the filename had a version number, false otherwise.
bool get_wants_cvs() const
Returns true if this file should be entered into the CVS database, false otherwise.
bool is_same_file(const SoftFilename &other) const
Returns true if this file has the same base and extension as the other, disregarding the version numb...
const string & get_extension() const
Returns the extension part of the filename.
const string & get_base() const
Returns the base part of the filename.
void increment_use_count()
Indicates that this filename is referenced by one more scene file.
int get_minor() const
Returns the minor version number.
int get_major() const
Returns the major version number.
bool is_1_0() const
Returns true if this is a version 1_0 filename, false otherwise.
string get_non_extension() const
Returns the filename part, without the extension.
const string & get_dirname() const
Returns the name of the directory this file was found in.
This encapsulates a SoftImage versioned filename, of the form base.v-v.ext: it consists of a director...
Definition: softFilename.h:31
void set_in_cvs(bool in_cvs)
Sets the flag that indicates whether this file is known to be entered into the CVS database...
void set_wants_cvs(bool wants_cvs)
Sets the flag that indicates whether this file should be entered into the CVS database.
int get_use_count() const
Returns the number of scene files that referenced this filename.
void make_1_0()
Makes this a 1_0 filename.
bool operator<(const SoftFilename &other) const
Puts filenames in order such that the files with the same base are sorted together, ignoring extension; and within files with the same base, files are sorted in decreasing version number order so that the most recent version appears first.
const string & get_filename() const
Returns the actual filename as found in the directory.
string get_1_0_filename() const
Returns what the filename would be if it were version 1-0.