Panda3D
 All Classes Functions Variables Enumerations
softFilename.cxx
00001 // Filename: softFilename.cxx
00002 // Created by:  drose (10Nov00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "softFilename.h"
00016 
00017 #include "pnotify.h"
00018 
00019 ////////////////////////////////////////////////////////////////////
00020 //     Function: SoftFilename::Constructor
00021 //       Access: Public
00022 //  Description:
00023 ////////////////////////////////////////////////////////////////////
00024 SoftFilename::
00025 SoftFilename(const string &dirname, const string &filename) :
00026   _dirname(dirname),
00027   _filename(filename)
00028 {
00029   _has_version = false;
00030   _major = 0;
00031   _minor = 0;
00032   _in_cvs = false;
00033   _wants_cvs = false;
00034   _use_count = 0;
00035 
00036   _base = _filename;
00037 
00038   // Scan for a version number and an optional extension after each
00039   // dot in the filename.
00040   size_t dot = _filename.find('.');
00041   while (dot != string::npos) {
00042     size_t m = dot + 1;
00043     const char *fstr = _filename.c_str();
00044     char *endptr;
00045     // Check for a numeric version number.
00046     int major = strtol(fstr + m , &endptr, 10);
00047     if (endptr != fstr + m && *endptr == '-') {
00048       // We got a major number, is there a minor number?
00049       m = (endptr - fstr) + 1;
00050       int minor = strtol(fstr + m, &endptr, 10);
00051       if (endptr != fstr + m && (*endptr == '.' || *endptr == '\0')) {
00052         // We got a minor number too!
00053         _has_version = true;
00054         _base = _filename.substr(0, dot + 1);
00055         _major = major;
00056         _minor = minor;
00057         _ext = endptr;
00058         return;
00059       }
00060     }
00061 
00062     // That wasn't a version number.  Is there more?
00063     dot = _filename.find('.', dot + 1);
00064   }
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: SoftFilename::Copy Constructor
00069 //       Access: Public
00070 //  Description:
00071 ////////////////////////////////////////////////////////////////////
00072 SoftFilename::
00073 SoftFilename(const SoftFilename &copy) :
00074   _dirname(copy._dirname),
00075   _filename(copy._filename),
00076   _has_version(copy._has_version),
00077   _base(copy._base),
00078   _major(copy._major),
00079   _minor(copy._minor),
00080   _ext(copy._ext),
00081   _in_cvs(copy._in_cvs),
00082   _wants_cvs(copy._wants_cvs),
00083   _use_count(copy._use_count)
00084 {
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: SoftFilename::Copy Assignment operator
00089 //       Access: Public
00090 //  Description:
00091 ////////////////////////////////////////////////////////////////////
00092 void SoftFilename::
00093 operator = (const SoftFilename &copy) {
00094   _dirname = copy._dirname;
00095   _filename = copy._filename;
00096   _has_version = copy._has_version;
00097   _base = copy._base;
00098   _major = copy._major;
00099   _minor = copy._minor;
00100   _ext = copy._ext;
00101   _in_cvs = copy._in_cvs;
00102   _wants_cvs = copy._wants_cvs;
00103   _use_count = copy._use_count;
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: SoftFilename::get_dirname
00108 //       Access: Public
00109 //  Description: Returns the name of the directory this file was
00110 //               found in.
00111 ////////////////////////////////////////////////////////////////////
00112 const string &SoftFilename::
00113 get_dirname() const {
00114   return _dirname;
00115 }
00116 
00117 ////////////////////////////////////////////////////////////////////
00118 //     Function: SoftFilename::get_filename
00119 //       Access: Public
00120 //  Description: Returns the actual filename as found in the
00121 //               directory.
00122 ////////////////////////////////////////////////////////////////////
00123 const string &SoftFilename::
00124 get_filename() const {
00125   return _filename;
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: SoftFilename::has_version
00130 //       Access: Public
00131 //  Description: Returns true if the filename had a version number,
00132 //               false otherwise.
00133 ////////////////////////////////////////////////////////////////////
00134 bool SoftFilename::
00135 has_version() const {
00136   return _has_version;
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: SoftFilename::get_1_0_filename
00141 //       Access: Public
00142 //  Description: Returns what the filename would be if it were version
00143 //               1-0.
00144 ////////////////////////////////////////////////////////////////////
00145 string SoftFilename::
00146 get_1_0_filename() const {
00147   nassertr(_has_version, string());
00148   return _base + "1-0" + _ext;
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: SoftFilename::get_base
00153 //       Access: Public
00154 //  Description: Returns the base part of the filename.  This is
00155 //               everything before the version number.
00156 ////////////////////////////////////////////////////////////////////
00157 const string &SoftFilename::
00158 get_base() const {
00159   nassertr(_has_version, _filename);
00160   return _base;
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: SoftFilename::get_major
00165 //       Access: Public
00166 //  Description: Returns the major version number.
00167 ////////////////////////////////////////////////////////////////////
00168 int SoftFilename::
00169 get_major() const {
00170   nassertr(_has_version, 0);
00171   return _major;
00172 }
00173 
00174 ////////////////////////////////////////////////////////////////////
00175 //     Function: SoftFilename::get_minor
00176 //       Access: Public
00177 //  Description: Returns the minor version number.
00178 ////////////////////////////////////////////////////////////////////
00179 int SoftFilename::
00180 get_minor() const {
00181   nassertr(_has_version, 0);
00182   return _minor;
00183 }
00184 
00185 ////////////////////////////////////////////////////////////////////
00186 //     Function: SoftFilename::get_extension
00187 //       Access: Public
00188 //  Description: Returns the extension part of the filename.  This is
00189 //               everything after the version number.
00190 ////////////////////////////////////////////////////////////////////
00191 const string &SoftFilename::
00192 get_extension() const {
00193   nassertr(_has_version, _ext);
00194   return _ext;
00195 }
00196 
00197 ////////////////////////////////////////////////////////////////////
00198 //     Function: SoftFilename::get_non_extension
00199 //       Access: Public
00200 //  Description: Returns the filename part, without the extension.
00201 ////////////////////////////////////////////////////////////////////
00202 string SoftFilename::
00203 get_non_extension() const {
00204   nassertr(_has_version, _filename);
00205   nassertr(_ext.length() < _filename.length(), _filename);
00206   return _filename.substr(0, _filename.length() - _ext.length());
00207 }
00208 
00209 ////////////////////////////////////////////////////////////////////
00210 //     Function: SoftFilename::is_1_0
00211 //       Access: Public
00212 //  Description: Returns true if this is a version 1_0 filename, false
00213 //               otherwise.
00214 ////////////////////////////////////////////////////////////////////
00215 bool SoftFilename::
00216 is_1_0() const {
00217   nassertr(_has_version, false);
00218   return (_major == 1 && _minor == 0);
00219 }
00220 
00221 ////////////////////////////////////////////////////////////////////
00222 //     Function: SoftFilename::make_1_0
00223 //       Access: Public
00224 //  Description: Makes this a 1_0 filename.
00225 ////////////////////////////////////////////////////////////////////
00226 void SoftFilename::
00227 make_1_0() {
00228   _has_version = true;
00229   _major = 1;
00230   _minor = 0;
00231   _filename = get_1_0_filename();
00232 }
00233 
00234 ////////////////////////////////////////////////////////////////////
00235 //     Function: SoftFilename::is_same_file
00236 //       Access: Public
00237 //  Description: Returns true if this file has the same base and
00238 //               extension as the other, disregarding the version
00239 //               number; false otherwise.
00240 ////////////////////////////////////////////////////////////////////
00241 bool SoftFilename::
00242 is_same_file(const SoftFilename &other) const {
00243   return _base == other._base && _ext == other._ext;
00244 }
00245 
00246 ////////////////////////////////////////////////////////////////////
00247 //     Function: SoftFilename::Ordering operator
00248 //       Access: Public
00249 //  Description: Puts filenames in order such that the files with the
00250 //               same base are sorted together, ignoring extension;
00251 //               and within files with the same base, files are sorted
00252 //               in decreasing version number order so that the most
00253 //               recent version appears first.
00254 ////////////////////////////////////////////////////////////////////
00255 bool SoftFilename::
00256 operator < (const SoftFilename &other) const {
00257   if (_base != other._base) {
00258     return _base < other._base;
00259   }
00260 
00261   if (_has_version != other._has_version) {
00262     // If one has a version and the other one doesn't, the one without
00263     // a version comes first.
00264     return _has_version < other._has_version;
00265   }
00266 
00267   if (_has_version) {
00268     if (_major != other._major) {
00269       return _major > other._major;
00270     }
00271     if (_minor != other._minor) {
00272       return _minor > other._minor;
00273     }
00274   }
00275 
00276   return false;
00277 }
00278 
00279 ////////////////////////////////////////////////////////////////////
00280 //     Function: SoftFilename::set_in_cvs
00281 //       Access: Public
00282 //  Description: Sets the flag that indicates whether this file is
00283 //               known to be entered into the CVS database.
00284 ////////////////////////////////////////////////////////////////////
00285 void SoftFilename::
00286 set_in_cvs(bool in_cvs) {
00287   _in_cvs = in_cvs;
00288 }
00289 
00290 ////////////////////////////////////////////////////////////////////
00291 //     Function: SoftFilename::get_in_cvs
00292 //       Access: Public
00293 //  Description: Returns true if this file is known to be entered in
00294 //               the CVS database, false if it is not.
00295 ////////////////////////////////////////////////////////////////////
00296 bool SoftFilename::
00297 get_in_cvs() const {
00298   return _in_cvs;
00299 }
00300 
00301 ////////////////////////////////////////////////////////////////////
00302 //     Function: SoftFilename::set_wants_cvs
00303 //       Access: Public
00304 //  Description: Sets the flag that indicates whether this file 
00305 //               should be entered into the CVS database.
00306 ////////////////////////////////////////////////////////////////////
00307 void SoftFilename::
00308 set_wants_cvs(bool wants_cvs) {
00309   _wants_cvs = wants_cvs;
00310 }
00311 
00312 ////////////////////////////////////////////////////////////////////
00313 //     Function: SoftFilename::get_wants_cvs
00314 //       Access: Public
00315 //  Description: Returns true if this file should be entered into the
00316 //               CVS database, false otherwise.
00317 ////////////////////////////////////////////////////////////////////
00318 bool SoftFilename::
00319 get_wants_cvs() const {
00320   return _wants_cvs;
00321 }
00322 
00323 ////////////////////////////////////////////////////////////////////
00324 //     Function: SoftFilename::increment_use_count
00325 //       Access: Public
00326 //  Description: Indicates that this filename is referenced by one
00327 //               more scene file.
00328 ////////////////////////////////////////////////////////////////////
00329 void SoftFilename::
00330 increment_use_count() {
00331   _use_count++;
00332 }
00333 
00334 ////////////////////////////////////////////////////////////////////
00335 //     Function: SoftFilename::get_use_count
00336 //       Access: Public
00337 //  Description: Returns the number of scene files that referenced
00338 //               this filename.
00339 ////////////////////////////////////////////////////////////////////
00340 int SoftFilename::
00341 get_use_count() const {
00342   return _use_count;
00343 }
 All Classes Functions Variables Enumerations