Panda3D
|
00001 // Filename: virtualFileMount.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 "virtualFileMount.h" 00016 #include "virtualFileSimple.h" 00017 #include "zStream.h" 00018 00019 TypeHandle VirtualFileMount::_type_handle; 00020 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: VirtualFileMount::Destructor 00024 // Access: Public, Virtual 00025 // Description: 00026 //////////////////////////////////////////////////////////////////// 00027 VirtualFileMount:: 00028 ~VirtualFileMount() { 00029 nassertv(_file_system == NULL); 00030 } 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: VirtualFileMount::make_virtual_file 00034 // Access: Public, Virtual 00035 // Description: Constructs and returns a new VirtualFile instance 00036 // that corresponds to the indicated filename within 00037 // this mount point. The returned VirtualFile object 00038 // does not imply that the given file actually exists; 00039 // but if the file does exist, then the handle can be 00040 // used to read it. 00041 //////////////////////////////////////////////////////////////////// 00042 PT(VirtualFile) VirtualFileMount:: 00043 make_virtual_file(const Filename &local_filename, 00044 const Filename &original_filename, bool implicit_pz_file, 00045 bool) { 00046 Filename local(local_filename); 00047 if (original_filename.is_text()) { 00048 local.set_text(); 00049 } 00050 PT(VirtualFileSimple) file = 00051 new VirtualFileSimple(this, local, implicit_pz_file); 00052 file->set_original_filename(original_filename); 00053 00054 return file.p(); 00055 } 00056 00057 //////////////////////////////////////////////////////////////////// 00058 // Function: VirtualFileMount::read_file 00059 // Access: Public, Virtual 00060 // Description: Fills up the indicated pvector with the contents of 00061 // the file, if it is a regular file. Returns true on 00062 // success, false otherwise. 00063 //////////////////////////////////////////////////////////////////// 00064 bool VirtualFileMount:: 00065 read_file(const Filename &file, bool do_uncompress, 00066 pvector<unsigned char> &result) const { 00067 result.clear(); 00068 00069 istream *in = open_read_file(file, do_uncompress); 00070 if (in == (istream *)NULL) { 00071 express_cat.info() 00072 << "Unable to read " << file << "\n"; 00073 return false; 00074 } 00075 00076 off_t file_size = get_file_size(file, in); 00077 if (file_size > 0) { 00078 result.reserve((size_t)file_size); 00079 } 00080 00081 bool okflag = VirtualFile::simple_read_file(in, result); 00082 00083 close_read_file(in); 00084 00085 if (!okflag) { 00086 express_cat.info() 00087 << "Error while reading " << file << "\n"; 00088 } 00089 return okflag; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: VirtualFileMount::open_read_file 00094 // Access: Published, Virtual 00095 // Description: Opens the file for reading. Returns a newly 00096 // allocated istream on success (which you should 00097 // eventually delete when you are done reading). 00098 // Returns NULL on failure. 00099 // 00100 // If do_uncompress is true, the file is also 00101 // decompressed on-the-fly using zlib. 00102 //////////////////////////////////////////////////////////////////// 00103 istream *VirtualFileMount:: 00104 open_read_file(const Filename &file, bool do_uncompress) const { 00105 istream *result = open_read_file(file); 00106 00107 #ifdef HAVE_ZLIB 00108 if (result != (istream *)NULL && do_uncompress) { 00109 // We have to slip in a layer to decompress the file on the fly. 00110 IDecompressStream *wrapper = new IDecompressStream(result, true); 00111 result = wrapper; 00112 } 00113 #endif // HAVE_ZLIB 00114 00115 return result; 00116 } 00117 00118 //////////////////////////////////////////////////////////////////// 00119 // Function: VirtualFileMount::close_read_file 00120 // Access: Public 00121 // Description: Closes a file opened by a previous call to 00122 // open_read_file(). This really just deletes the 00123 // istream pointer, but it is recommended to use this 00124 // interface instead of deleting it explicitly, to help 00125 // work around compiler issues. 00126 //////////////////////////////////////////////////////////////////// 00127 void VirtualFileMount:: 00128 close_read_file(istream *stream) const { 00129 if (stream != (istream *)NULL) { 00130 // For some reason--compiler bug in gcc 3.2?--explicitly deleting 00131 // the stream pointer does not call the appropriate global delete 00132 // function; instead apparently calling the system delete 00133 // function. So we call the delete function by hand instead. 00134 #if !defined(USE_MEMORY_NOWRAPPERS) && defined(REDEFINE_GLOBAL_OPERATOR_NEW) 00135 stream->~istream(); 00136 (*global_operator_delete)(stream); 00137 #else 00138 delete stream; 00139 #endif 00140 } 00141 } 00142 00143 //////////////////////////////////////////////////////////////////// 00144 // Function: VirtualFileMount::output 00145 // Access: Public, Virtual 00146 // Description: 00147 //////////////////////////////////////////////////////////////////// 00148 void VirtualFileMount:: 00149 output(ostream &out) const { 00150 out << get_type(); 00151 } 00152 00153 //////////////////////////////////////////////////////////////////// 00154 // Function: VirtualFileMount::write 00155 // Access: Public, Virtual 00156 // Description: 00157 //////////////////////////////////////////////////////////////////// 00158 void VirtualFileMount:: 00159 write(ostream &out) const { 00160 out << *this << " on /" << get_mount_point() << "\n"; 00161 }