Panda3D

virtualFileMountSystem.cxx

00001 // Filename: virtualFileMountSystem.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 "virtualFileMountSystem.h"
00016 
00017 TypeHandle VirtualFileMountSystem::_type_handle;
00018 
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: VirtualFileMountSystem::has_file
00022 //       Access: Public, Virtual
00023 //  Description: Returns true if the indicated file exists within the
00024 //               mount system.
00025 ////////////////////////////////////////////////////////////////////
00026 bool VirtualFileMountSystem::
00027 has_file(const Filename &file) const {
00028   Filename pathname(_physical_filename, file);
00029 #ifdef WIN32
00030   if (VirtualFileSystem::get_global_ptr()->vfs_case_sensitive) {
00031     Filename case_pathname = pathname;
00032     if (!case_pathname.make_true_case()) {
00033       return false;
00034     }
00035     if (case_pathname != pathname) {
00036       express_cat.warning()
00037         << "Filename is incorrect case: " << pathname
00038         << " instead of " << case_pathname << "\n";
00039       return false;
00040     }
00041   }
00042 #endif  // WIN32
00043   return pathname.exists();
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: VirtualFileMountSystem::is_directory
00048 //       Access: Public, Virtual
00049 //  Description: Returns true if the indicated file exists within the
00050 //               mount system and is a directory.
00051 ////////////////////////////////////////////////////////////////////
00052 bool VirtualFileMountSystem::
00053 is_directory(const Filename &file) const {
00054 #ifdef WIN32
00055   // First ensure that the file exists to validate its case.
00056   if (VirtualFileSystem::get_global_ptr()->vfs_case_sensitive) {
00057     if (!has_file(file)) {
00058       return false;
00059     }
00060   }
00061 #endif  // WIN32
00062   Filename pathname(_physical_filename, file);
00063   return pathname.is_directory();
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////
00067 //     Function: VirtualFileMountSystem::is_regular_file
00068 //       Access: Public, Virtual
00069 //  Description: Returns true if the indicated file exists within the
00070 //               mount system and is a regular file.
00071 ////////////////////////////////////////////////////////////////////
00072 bool VirtualFileMountSystem::
00073 is_regular_file(const Filename &file) const {
00074 #ifdef WIN32
00075   // First ensure that the file exists to validate its case.
00076   if (VirtualFileSystem::get_global_ptr()->vfs_case_sensitive) {
00077     if (!has_file(file)) {
00078       return false;
00079     }
00080   }
00081 #endif  // WIN32
00082   Filename pathname(_physical_filename, file);
00083   return pathname.is_regular_file();
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: VirtualFileMountSystem::open_read_file
00088 //       Access: Public, Virtual
00089 //  Description: Opens the file for reading, if it exists.  Returns a
00090 //               newly allocated istream on success (which you should
00091 //               eventually delete when you are done reading).
00092 //               Returns NULL on failure.
00093 ////////////////////////////////////////////////////////////////////
00094 istream *VirtualFileMountSystem::
00095 open_read_file(const Filename &file) const {
00096 #ifdef WIN32
00097   // First ensure that the file exists to validate its case.
00098   if (VirtualFileSystem::get_global_ptr()->vfs_case_sensitive) {
00099     if (!has_file(file)) {
00100       return NULL;
00101     }
00102   }
00103 #endif  // WIN32
00104   Filename pathname(_physical_filename, file);
00105   if (file.is_text()) {
00106     pathname.set_text();
00107   } else {
00108     pathname.set_binary();
00109   }
00110   pifstream *stream = new pifstream;
00111   if (!pathname.open_read(*stream)) {
00112     // Couldn't open the file for some reason.
00113     close_read_file(stream);
00114     return NULL;
00115   }
00116 
00117   return stream;
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //     Function: VirtualFileMountSystem::get_file_size
00122 //       Access: Published, Virtual
00123 //  Description: Returns the current size on disk (or wherever it is)
00124 //               of the already-open file.  Pass in the stream that
00125 //               was returned by open_read_file(); some
00126 //               implementations may require this stream to determine
00127 //               the size.
00128 ////////////////////////////////////////////////////////////////////
00129 off_t VirtualFileMountSystem::
00130 get_file_size(const Filename &file, istream *stream) const {
00131   // First, save the original stream position.
00132   streampos orig = stream->tellg();
00133 
00134   // Seek to the end and get the stream position there.
00135   stream->seekg(0, ios::end);
00136   if (stream->fail()) {
00137     // Seeking not supported.
00138     stream->clear();
00139     return get_file_size(file);
00140   }
00141   streampos size = stream->tellg();
00142 
00143   // Then return to the original point.
00144   stream->seekg(orig, ios::beg);
00145 
00146   // Make sure there are no error flags set as a result of the seek.
00147   stream->clear();
00148 
00149   return size;
00150 }
00151 
00152 ////////////////////////////////////////////////////////////////////
00153 //     Function: VirtualFileMountSystem::get_file_size
00154 //       Access: Published, Virtual
00155 //  Description: Returns the current size on disk (or wherever it is)
00156 //               of the file before it has been opened.
00157 ////////////////////////////////////////////////////////////////////
00158 off_t VirtualFileMountSystem::
00159 get_file_size(const Filename &file) const {
00160   Filename pathname(_physical_filename, file);
00161   return pathname.get_file_size();
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: VirtualFileMountSystem::get_timestamp
00166 //       Access: Published, Virtual
00167 //  Description: Returns a time_t value that represents the time the
00168 //               file was last modified, to within whatever precision
00169 //               the operating system records this information (on a
00170 //               Windows95 system, for instance, this may only be
00171 //               accurate to within 2 seconds).
00172 //
00173 //               If the timestamp cannot be determined, either because
00174 //               it is not supported by the operating system or
00175 //               because there is some error (such as file not found),
00176 //               returns 0.
00177 ////////////////////////////////////////////////////////////////////
00178 time_t VirtualFileMountSystem::
00179 get_timestamp(const Filename &file) const {
00180   Filename pathname(_physical_filename, file);
00181   return pathname.get_timestamp();
00182 }
00183 
00184 ////////////////////////////////////////////////////////////////////
00185 //     Function: VirtualFileMountSystem::scan_directory
00186 //       Access: Public, Virtual
00187 //  Description: Fills the given vector up with the list of filenames
00188 //               that are local to this directory, if the filename is
00189 //               a directory.  Returns true if successful, or false if
00190 //               the file is not a directory or cannot be read.
00191 ////////////////////////////////////////////////////////////////////
00192 bool VirtualFileMountSystem::
00193 scan_directory(vector_string &contents, const Filename &dir) const {
00194 #ifdef WIN32
00195   // First ensure that the file exists to validate its case.
00196   if (VirtualFileSystem::get_global_ptr()->vfs_case_sensitive) {
00197     if (!has_file(dir)) {
00198       return false;
00199     }
00200   }
00201 #endif  // WIN32
00202   Filename pathname(_physical_filename, dir);
00203   return pathname.scan_directory(contents);
00204 }
00205 
00206 
00207 ////////////////////////////////////////////////////////////////////
00208 //     Function: VirtualFileMountSystem::output
00209 //       Access: Public, Virtual
00210 //  Description: 
00211 ////////////////////////////////////////////////////////////////////
00212 void VirtualFileMountSystem::
00213 output(ostream &out) const {
00214   out << get_physical_filename();
00215 }
 All Classes Functions Variables Enumerations