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