Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgraph/bamFile.cxx

Go to the documentation of this file.
00001 // Filename: bamFile.cxx
00002 // Created by:  drose (02Jul00)
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 "bamFile.h"
00020 #include "config_pgraph.h"
00021 
00022 #include "bam.h"
00023 #include "config_util.h"
00024 #include "bamReader.h"
00025 #include "bamWriter.h"
00026 #include "filename.h"
00027 #include "config_express.h"
00028 #include "virtualFileSystem.h"
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: BamFile::Constructor
00032 //       Access: Public
00033 //  Description:
00034 ////////////////////////////////////////////////////////////////////
00035 BamFile::
00036 BamFile() {
00037   _reader = NULL;
00038   _writer = NULL;
00039 }
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //     Function: BamFile::Destructor
00043 //       Access: Public
00044 //  Description:
00045 ////////////////////////////////////////////////////////////////////
00046 BamFile::
00047 ~BamFile() {
00048   close();
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: BamFile::open_read
00053 //       Access: Public
00054 //  Description: Attempts to open the indicated filename for reading.
00055 //               Returns true if successful, false on error.
00056 ////////////////////////////////////////////////////////////////////
00057 bool BamFile::
00058 open_read(const Filename &filename, bool report_errors) {
00059   close();
00060 
00061   Filename bam_filename(filename);
00062 
00063   if (use_vfs) {
00064     VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00065     if (!vfs->exists(bam_filename)) {
00066       if (report_errors) {
00067         loader_cat.error() << "Could not find " << bam_filename << "\n";
00068       }
00069       return false;
00070     }
00071   } else {
00072     if (!bam_filename.exists()) {
00073       if (report_errors) {
00074         loader_cat.error() << "Could not find " << bam_filename << "\n";
00075       }
00076       return false;
00077     }
00078   }
00079 
00080   loader_cat.info() << "Reading " << bam_filename << "\n";
00081 
00082   if (!_din.open(bam_filename)) {
00083     loader_cat.error() << "Could not open " << bam_filename << "\n";
00084     return false;
00085   }
00086 
00087   string head;
00088   if (!_din.read_header(head, _bam_header.size())) {
00089     loader_cat.error() << bam_filename << " is not a valid BAM file.\n";
00090     return false;
00091   }
00092 
00093   if (head != _bam_header) {
00094     loader_cat.error() << bam_filename << " is not a valid BAM file.\n";
00095     return false;
00096   }
00097 
00098   _reader = new BamReader(&_din);
00099   if (!_reader->init()) {
00100     close();
00101     return false;
00102   }
00103 
00104   return true;
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////
00108 //     Function: BamFile::read_object
00109 //       Access: Public
00110 //  Description: Reads and returns the next object from the Bam file,
00111 //               or NULL if the end of the file has been reached, or
00112 //               if there is an error condition.  Use is_eof() to
00113 //               differentiate these two cases.
00114 //
00115 //               The pointers returned by this method will not be
00116 //               valid for use until resolve() is subsequently called.
00117 ////////////////////////////////////////////////////////////////////
00118 TypedWritable *BamFile::
00119 read_object() {
00120   if (_reader == (BamReader *)NULL) {
00121     return NULL;
00122   }
00123 
00124   return _reader->read_object();
00125 }
00126 
00127 ////////////////////////////////////////////////////////////////////
00128 //     Function: BamFile::is_eof
00129 //       Access: Public
00130 //  Description: Returns true if the reader has reached end-of-file,
00131 //               false otherwise.  This call is only valid after a
00132 //               call to read_object().
00133 ////////////////////////////////////////////////////////////////////
00134 bool BamFile::
00135 is_eof() const {
00136   return _reader != (BamReader *)NULL && _reader->is_eof();
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: BamFile::resolve
00141 //       Access: Public
00142 //  Description: This must be called after one or more objects have
00143 //               been read via calls to read_object() in order to
00144 //               resolve all internal pointer references in the
00145 //               objects read and make all the pointers valid.  It
00146 //               returns true if all objects are successfully
00147 //               resolved, or false if some have not been (in which
00148 //               case you must call resolve() again later).
00149 ////////////////////////////////////////////////////////////////////
00150 bool BamFile::
00151 resolve() {
00152   if (_reader == (BamReader *)NULL) {
00153     return false;
00154   }
00155 
00156   return _reader->resolve();
00157 }
00158 
00159 
00160 ////////////////////////////////////////////////////////////////////
00161 //     Function: BamFile::open_write
00162 //       Access: Public
00163 //  Description: Attempts to open the indicated file for writing.  If
00164 //               another file by the same name already exists, it will
00165 //               be silently removed.  Returns true if successful,
00166 //               false otherwise.
00167 ////////////////////////////////////////////////////////////////////
00168 bool BamFile::
00169 open_write(const Filename &filename, bool) {
00170   close();
00171 
00172   loader_cat.info() << "Writing " << filename << "\n";
00173 
00174   filename.unlink();
00175   if (!_dout.open(filename)) {
00176     loader_cat.error() << "Unable to open " << filename << "\n";
00177     return false;
00178   }
00179 
00180   if (!_dout.write_header(_bam_header)) {
00181     loader_cat.error() << "Unable to write to " << filename << "\n";
00182     return false;
00183   }
00184 
00185   _writer = new BamWriter(&_dout);
00186 
00187   if (!_writer->init()) {
00188     close();
00189     return false;
00190   }
00191 
00192   return true;
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: BamFile::write_object
00197 //       Access: Public
00198 //  Description: Writes the indicated object to the Bam file.  Returns
00199 //               true if successful, false on error.
00200 ////////////////////////////////////////////////////////////////////
00201 bool BamFile::
00202 write_object(const TypedWritable *object) {
00203   if (_writer == (BamWriter *)NULL) {
00204     return false;
00205   }
00206 
00207   if (!_writer->write_object(object)) {
00208     close();
00209     return false;
00210   }
00211 
00212   return true;
00213 }
00214 
00215 ////////////////////////////////////////////////////////////////////
00216 //     Function: BamFile::close
00217 //       Access: Public
00218 //  Description: Closes the input or output stream.
00219 ////////////////////////////////////////////////////////////////////
00220 void BamFile::
00221 close() {
00222   if (_reader != (BamReader *)NULL) {
00223     resolve();
00224     delete _reader;
00225     _reader = NULL;
00226   }
00227   if (_writer != (BamWriter *)NULL) {
00228     delete _writer;
00229     _writer = NULL;
00230   }
00231   _din.close();
00232   _dout.close();
00233 }
00234 
00235 
00236 ////////////////////////////////////////////////////////////////////
00237 //     Function: BamFile::get_file_major_ver
00238 //       Access: Public
00239 //  Description: Returns the major version number of the file
00240 //               currently being read, or the system current major
00241 //               version number if no file is currently open for
00242 //               reading.
00243 ////////////////////////////////////////////////////////////////////
00244 int BamFile::
00245 get_file_major_ver() {
00246   if (_reader == (BamReader *)NULL) {
00247     return _bam_major_ver;
00248   }
00249   return _reader->get_file_major_ver();
00250 }
00251 
00252 ////////////////////////////////////////////////////////////////////
00253 //     Function: BamFile::get_file_minor_ver
00254 //       Access: Public
00255 //  Description: Returns the minor version number of the file
00256 //               currently being read, or the system current minor
00257 //               version number if no file is currently open for
00258 //               reading.
00259 ////////////////////////////////////////////////////////////////////
00260 int BamFile::
00261 get_file_minor_ver() {
00262   if (_reader == (BamReader *)NULL) {
00263     return _bam_minor_ver;
00264   }
00265   return _reader->get_file_minor_ver();
00266 }
00267 
00268 ////////////////////////////////////////////////////////////////////
00269 //     Function: BamFile::get_current_major_ver
00270 //       Access: Public
00271 //  Description: Returns the system current major version number.
00272 //               This is the version number that will be assigned to
00273 //               any generated Bam files.
00274 ////////////////////////////////////////////////////////////////////
00275 int BamFile::
00276 get_current_major_ver() {
00277   return _bam_major_ver;
00278 }
00279 
00280 ////////////////////////////////////////////////////////////////////
00281 //     Function: BamFile::get_current_minor_ver
00282 //       Access: Public
00283 //  Description: Returns the system current minor version number.
00284 //               This is the version number that will be assigned to
00285 //               any generated Bam files.
00286 ////////////////////////////////////////////////////////////////////
00287 int BamFile::
00288 get_current_minor_ver() {
00289   return _bam_minor_ver;
00290 }

Generated on Fri May 2 00:41:05 2003 for Panda by doxygen1.3