Panda3D

virtualFileSimple.cxx

00001 // Filename: virtualFileSimple.cxx
00002 // Created by:  drose (03Aug02)
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 "virtualFileSimple.h"
00016 #include "virtualFileMount.h"
00017 #include "virtualFileList.h"
00018 
00019 TypeHandle VirtualFileSimple::_type_handle;
00020 
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: VirtualFileSimple::get_file_system
00024 //       Access: Published, Virtual
00025 //  Description: Returns the VirtualFileSystem this file is associated
00026 //               with.
00027 ////////////////////////////////////////////////////////////////////
00028 VirtualFileSystem *VirtualFileSimple::
00029 get_file_system() const {
00030   return _mount->get_file_system();
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: VirtualFileSimple::get_filename
00035 //       Access: Published, Virtual
00036 //  Description: Returns the full pathname to this file within the
00037 //               virtual file system.
00038 ////////////////////////////////////////////////////////////////////
00039 Filename VirtualFileSimple::
00040 get_filename() const {
00041   string mount_point = _mount->get_mount_point();
00042   if (_local_filename.empty()) {
00043     if (mount_point.empty()) {
00044       return "/";
00045     } else {
00046       return string("/") + mount_point;
00047     }
00048 
00049   } else {
00050     if (mount_point.empty()) {
00051       return string("/") + _local_filename.get_fullpath();
00052     } else {
00053       return string("/") + mount_point + string("/") + _local_filename.get_fullpath();
00054     }
00055   }
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: VirtualFileSimple::has_file
00060 //       Access: Published, Virtual
00061 //  Description: Returns true if this file exists, false otherwise.
00062 ////////////////////////////////////////////////////////////////////
00063 bool VirtualFileSimple::
00064 has_file() const {
00065   return _mount->has_file(_local_filename);
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: VirtualFileSimple::is_directory
00070 //       Access: Published, Virtual
00071 //  Description: Returns true if this file represents a directory (and
00072 //               scan_directory() may be called), false otherwise.
00073 ////////////////////////////////////////////////////////////////////
00074 bool VirtualFileSimple::
00075 is_directory() const {
00076   return _mount->is_directory(_local_filename);
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: VirtualFileSimple::is_regular_file
00081 //       Access: Published, Virtual
00082 //  Description: Returns true if this file represents a regular file
00083 //               (and read_file() may be called), false otherwise.
00084 ////////////////////////////////////////////////////////////////////
00085 bool VirtualFileSimple::
00086 is_regular_file() const {
00087   return _mount->is_regular_file(_local_filename);
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: VirtualFileSimple::open_read_file
00092 //       Access: Published, Virtual
00093 //  Description: Opens the file for reading.  Returns a newly
00094 //               allocated istream on success (which you should
00095 //               eventually delete when you are done reading).
00096 //               Returns NULL on failure.
00097 //
00098 //               If auto_unwrap is true, an explicitly-named .pz file
00099 //               is automatically decompressed and the decompressed
00100 //               contents are returned.  This is different than
00101 //               vfs-implicit-pz, which will automatically decompress
00102 //               a file if the extension .pz is *not* given.
00103 ////////////////////////////////////////////////////////////////////
00104 istream *VirtualFileSimple::
00105 open_read_file(bool auto_unwrap) const {
00106 
00107   // Will we be automatically unwrapping a .pz file?
00108   bool do_uncompress = (_implicit_pz_file || (auto_unwrap && _local_filename.get_extension() == "pz"));
00109 
00110   Filename local_filename(_local_filename);
00111   if (do_uncompress) {
00112     // .pz files are always binary, of course.
00113     local_filename.set_binary();
00114   }
00115 
00116   return _mount->open_read_file(local_filename, do_uncompress);
00117 }
00118 
00119 ////////////////////////////////////////////////////////////////////
00120 //     Function: VirtualFileSimple::get_file_size
00121 //       Access: Published, Virtual
00122 //  Description: Returns the current size on disk (or wherever it is)
00123 //               of the already-open file.  Pass in the stream that
00124 //               was returned by open_read_file(); some
00125 //               implementations may require this stream to determine
00126 //               the size.
00127 ////////////////////////////////////////////////////////////////////
00128 off_t VirtualFileSimple::
00129 get_file_size(istream *stream) const {
00130   return _mount->get_file_size(_local_filename, stream);
00131 }
00132 
00133 ////////////////////////////////////////////////////////////////////
00134 //     Function: VirtualFileSimple::get_file_size
00135 //       Access: Published, Virtual
00136 //  Description: Returns the current size on disk (or wherever it is)
00137 //               of the file before it has been opened.
00138 ////////////////////////////////////////////////////////////////////
00139 off_t VirtualFileSimple::
00140 get_file_size() const {
00141   return _mount->get_file_size(_local_filename);
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //     Function: VirtualFileSimple::get_timestamp
00146 //       Access: Published, Virtual
00147 //  Description: Returns a time_t value that represents the time the
00148 //               file was last modified, to within whatever precision
00149 //               the operating system records this information (on a
00150 //               Windows95 system, for instance, this may only be
00151 //               accurate to within 2 seconds).
00152 //
00153 //               If the timestamp cannot be determined, either because
00154 //               it is not supported by the operating system or
00155 //               because there is some error (such as file not found),
00156 //               returns 0.
00157 ////////////////////////////////////////////////////////////////////
00158 time_t VirtualFileSimple::
00159 get_timestamp() const {
00160   return _mount->get_timestamp(_local_filename);
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: VirtualFileSimple::read_file
00165 //       Access: Public, Virtual
00166 //  Description: Fills up the indicated pvector with the contents of
00167 //               the file, if it is a regular file.  Returns true on
00168 //               success, false otherwise.
00169 ////////////////////////////////////////////////////////////////////
00170 bool VirtualFileSimple::
00171 read_file(pvector<unsigned char> &result, bool auto_unwrap) const {
00172 
00173   // Will we be automatically unwrapping a .pz file?
00174   bool do_uncompress = (_implicit_pz_file || (auto_unwrap && _local_filename.get_extension() == "pz"));
00175 
00176   Filename local_filename(_local_filename);
00177   if (do_uncompress) {
00178     // .pz files are always binary, of course.
00179     local_filename.set_binary();
00180   }
00181 
00182   return _mount->read_file(local_filename, do_uncompress, result);
00183 }
00184 
00185 ////////////////////////////////////////////////////////////////////
00186 //     Function: VirtualFileSimple::scan_local_directory
00187 //       Access: Protected, Virtual
00188 //  Description: Fills file_list up with the list of files that are
00189 //               within this directory, excluding those whose
00190 //               basenames are listed in mount_points.  Returns true
00191 //               if successful, false if the file is not a directory
00192 //               or the directory cannot be read.
00193 ////////////////////////////////////////////////////////////////////
00194 bool VirtualFileSimple::
00195 scan_local_directory(VirtualFileList *file_list, 
00196                      const ov_set<string> &mount_points) const {
00197   vector_string names;
00198   if (!_mount->scan_directory(names, _local_filename)) {
00199     return false;
00200   }
00201 
00202   // Now the scan above gave us a list of basenames.  Turn these back
00203   // into VirtualFile pointers.
00204 
00205   // Each of the files returned by the mount will be just a simple
00206   // file within the same mount tree, unless it is shadowed by a
00207   // mount point listed in mount_points.
00208 
00209   vector_string::const_iterator ni;
00210   for (ni = names.begin(); ni != names.end(); ++ni) {
00211     const string &basename = (*ni);
00212     if (mount_points.find(basename) == mount_points.end()) {
00213       Filename filename(_local_filename, basename);
00214       VirtualFileSimple *file = new VirtualFileSimple(_mount, filename, false);
00215       file_list->add_file(file);
00216     }
00217   }
00218   
00219   return true;
00220 }
 All Classes Functions Variables Enumerations