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

panda/src/putil/datagramInputFile.cxx

Go to the documentation of this file.
00001 // Filename: datagramInputFile.cxx
00002 // Created by:  drose (30Oct00)
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 
00020 #include "numeric_types.h"
00021 #include "datagramIterator.h"
00022 #include "profileTimer.h"
00023 #include "config_util.h"
00024 #include "config_express.h"
00025 #include "virtualFileSystem.h"
00026 
00027 #include "datagramInputFile.h"
00028 
00029 //#define SKYLER_TIMER 1
00030 #ifdef SKYLER_TIMER //[
00031   EXPCL_PANDAEXPRESS ProfileTimer Skyler_timer_file;
00032 #endif //]
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //     Function: DatagramInputFile::open
00036 //       Access: Public
00037 //  Description: Opens the indicated filename for reading.  Returns
00038 //               true if successful, false on failure.
00039 ////////////////////////////////////////////////////////////////////
00040 bool DatagramInputFile::
00041 open(Filename filename) {
00042   // DatagramInputFiles are always binary.
00043   _read_first_datagram = false;
00044   _error = false;
00045   filename.set_binary();
00046 
00047   if (use_vfs) {
00048     VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00049     PT(VirtualFile) file = vfs->get_file(filename);
00050     if (file == (VirtualFile *)NULL) {
00051       // No such file.
00052       return false;
00053     }
00054     _in = file->open_read_file();
00055     _owns_in = (_in != (istream *)NULL);
00056     return _owns_in && !_in->fail();
00057     
00058   } else {
00059     _in = &_in_file;
00060     _owns_in = false;
00061     return filename.open_read(_in_file);
00062   }
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: DatagramInputFile::read_header
00067 //       Access: Public
00068 //  Description: Reads a sequence of bytes from the beginning of the
00069 //               datagram file.  This may be called any number of
00070 //               times after the file has been opened and before the
00071 //               first datagram is read.  It may not be called once
00072 //               the first datagram has been read.
00073 ////////////////////////////////////////////////////////////////////
00074 bool DatagramInputFile::
00075 read_header(string &header, size_t num_bytes) {
00076   nassertr(!_read_first_datagram, false);
00077 
00078   char *buffer = (char *)alloca(num_bytes);
00079   nassertr(buffer != (char *)NULL, false);
00080 
00081   _in->read(buffer, num_bytes);
00082   if (_in->fail() || _in->eof()) {
00083     return false;
00084   }
00085 
00086   header = string(buffer, num_bytes);
00087   return true;
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: DatagramInputFile::get_datagram
00092 //       Access: Public, Virtual
00093 //  Description: Reads the next datagram from the file.  Returns true
00094 //               on success, false if there is an error or end of
00095 //               file.
00096 ////////////////////////////////////////////////////////////////////
00097 bool DatagramInputFile::
00098 get_datagram(Datagram &data) {
00099   #ifdef SKYLER_TIMER //[
00100     Skyler_timer_file.on();
00101   #endif //]
00102   _read_first_datagram = true;
00103 
00104   // First, get the size of the upcoming datagram.  We do this with
00105   // the help of a second datagram.
00106   char sizebuf[sizeof(PN_uint32)];
00107   _in->read(sizebuf, sizeof(PN_uint32));
00108   if (_in->fail() || _in->eof()) {
00109     #ifdef SKYLER_TIMER //[
00110       Skyler_timer_file.off("DatagramInputFile::get_datagram");
00111     #endif //]
00112     return false;
00113   }
00114 
00115   Datagram size(sizebuf, sizeof(PN_uint32));
00116   DatagramIterator di(size);
00117   PN_uint32 num_bytes = di.get_uint32();
00118 
00119   // Now, read the datagram itself.
00120   char *buffer = new char[num_bytes];
00121   nassertr(buffer != (char *)NULL, false);
00122 
00123   _in->read(buffer, num_bytes);
00124   if (_in->fail() || _in->eof()) {
00125     _error = true;
00126     delete[] buffer;
00127     #ifdef SKYLER_TIMER //[
00128       Skyler_timer_file.off("DatagramInputFile::get_datagram");
00129     #endif //]
00130     return false;
00131   }
00132 
00133   data = Datagram(buffer, num_bytes);
00134   delete[] buffer;
00135   #ifdef SKYLER_TIMER //[
00136     Skyler_timer_file.off("DatagramInputFile::get_datagram");
00137   #endif //]
00138   return true;
00139 }
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: DatagramInputFile::is_eof
00143 //       Access: Public, Virtual
00144 //  Description: Returns true if the file has reached the end-of-file.
00145 //               This test may only be made after a call to
00146 //               read_header() or get_datagram() has failed.
00147 ////////////////////////////////////////////////////////////////////
00148 bool DatagramInputFile::
00149 is_eof() {
00150   return _in->eof();
00151 }
00152 
00153 ////////////////////////////////////////////////////////////////////
00154 //     Function: DatagramInputFile::is_error
00155 //       Access: Public, Virtual
00156 //  Description: Returns true if the file has reached an error
00157 //               condition.
00158 ////////////////////////////////////////////////////////////////////
00159 bool DatagramInputFile::
00160 is_error() {
00161   if (_in->fail()) {
00162     _error = true;
00163   }
00164   return _error;
00165 }

Generated on Fri May 2 00:43:36 2003 for Panda by doxygen1.3