Panda3D
|
00001 // Filename: datagramOutputFile.cxx 00002 // Created by: drose (30Oct00) 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 "datagramOutputFile.h" 00016 #include "streamWriter.h" 00017 #include "zStream.h" 00018 00019 //////////////////////////////////////////////////////////////////// 00020 // Function: DatagramOutputFile::open 00021 // Access: Public 00022 // Description: Opens the indicated filename for reading. Returns 00023 // true if successful, false on failure. 00024 //////////////////////////////////////////////////////////////////// 00025 bool DatagramOutputFile:: 00026 open(Filename filename) { 00027 close(); 00028 00029 // DatagramOutputFiles are always binary. 00030 filename.set_binary(); 00031 00032 _out = &_out_file; 00033 _owns_out = false; 00034 00035 #ifdef HAVE_ZLIB 00036 if (filename.get_extension() == "pz") { 00037 // The filename ends in .pz, which means to automatically 00038 // compress the bam file that we write. 00039 _out = new OCompressStream(_out, _owns_out); 00040 _owns_out = true; 00041 } 00042 #endif // HAVE_ZLIB 00043 00044 return filename.open_write(_out_file); 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: DatagramOutputFile::open 00049 // Access: Public 00050 // Description: Starts writing to the indicated stream. Returns 00051 // true on success, false on failure. The 00052 // DatagramOutputFile does not take ownership of the 00053 // stream; you are responsible for closing or deleting 00054 // it when you are done. 00055 //////////////////////////////////////////////////////////////////// 00056 bool DatagramOutputFile:: 00057 open(ostream &out) { 00058 close(); 00059 00060 _out = &out; 00061 _owns_out = false; 00062 00063 return !_out->fail(); 00064 } 00065 00066 //////////////////////////////////////////////////////////////////// 00067 // Function: DatagramOutputFile::close 00068 // Access: Public 00069 // Description: Closes the file. This is also implicitly done when 00070 // the DatagramOutputFile destructs. 00071 //////////////////////////////////////////////////////////////////// 00072 void DatagramOutputFile:: 00073 close() { 00074 if (_owns_out) { 00075 delete _out; 00076 } 00077 _out_file.close(); 00078 _out = (ostream *)NULL; 00079 _owns_out = false; 00080 00081 _wrote_first_datagram = false; 00082 _error = false; 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: DatagramOutputFile::write_header 00087 // Access: Public 00088 // Description: Writes a sequence of bytes to the beginning of the 00089 // datagram file. This may be called any number of 00090 // times after the file has been opened and before the 00091 // first datagram is written. It may not be called once 00092 // the first datagram is written. 00093 //////////////////////////////////////////////////////////////////// 00094 bool DatagramOutputFile:: 00095 write_header(const string &header) { 00096 nassertr(_out != (ostream *)NULL, false); 00097 nassertr(!_wrote_first_datagram, false); 00098 00099 _out->write(header.data(), header.size()); 00100 thread_consider_yield(); 00101 return !_out->fail(); 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: DatagramOutputFile::put_datagram 00106 // Access: Public, Virtual 00107 // Description: Writes the given datagram to the file. Returns true 00108 // on success, false if there is an error. 00109 //////////////////////////////////////////////////////////////////// 00110 bool DatagramOutputFile:: 00111 put_datagram(const Datagram &data) { 00112 nassertr(_out != (ostream *)NULL, false); 00113 _wrote_first_datagram = true; 00114 00115 // First, write the size of the upcoming datagram. 00116 StreamWriter writer(_out, false); 00117 writer.add_uint32(data.get_length()); 00118 00119 // Now, write the datagram itself. 00120 _out->write((const char *)data.get_data(), data.get_length()); 00121 thread_consider_yield(); 00122 00123 return !_out->fail(); 00124 } 00125 00126 //////////////////////////////////////////////////////////////////// 00127 // Function: DatagramOutputFile::is_error 00128 // Access: Public, Virtual 00129 // Description: Returns true if the file has reached an error 00130 // condition. 00131 //////////////////////////////////////////////////////////////////// 00132 bool DatagramOutputFile:: 00133 is_error() { 00134 if (_out == (ostream *)NULL) { 00135 return true; 00136 } 00137 00138 if (_out->fail()) { 00139 _error = true; 00140 } 00141 return _error; 00142 } 00143 00144 //////////////////////////////////////////////////////////////////// 00145 // Function: DatagramOutputFile::flush 00146 // Access: Public, Virtual 00147 // Description: Ensures that all datagrams previously written will be 00148 // visible in the output file. 00149 //////////////////////////////////////////////////////////////////// 00150 void DatagramOutputFile:: 00151 flush() { 00152 if (_out != (ostream *)NULL) { 00153 _out->flush(); 00154 } 00155 }