Panda3D

streamWriter.I

00001 // Filename: streamWriter.I
00002 // Created by:  drose (04Aug02)
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 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: StreamWriter::Constructor
00018 //       Access: Public
00019 //  Description: 
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE StreamWriter::
00022 StreamWriter(ostream &out) : 
00023   _out(&out),
00024   _owns_stream(false)
00025 {
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: StreamWriter::Constructor
00030 //       Access: Published
00031 //  Description: 
00032 ////////////////////////////////////////////////////////////////////
00033 INLINE StreamWriter::
00034 StreamWriter(ostream *out, bool owns_stream) : 
00035   _out(out),
00036   _owns_stream(owns_stream)
00037 {
00038 }
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: StreamWriter::Copy Constructor
00042 //       Access: Published
00043 //  Description: The copy constructor does not copy ownership of the
00044 //               stream.
00045 ////////////////////////////////////////////////////////////////////
00046 INLINE StreamWriter::
00047 StreamWriter(const StreamWriter &copy) :
00048   _out(copy._out),
00049   _owns_stream(false)
00050 {
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: StreamWriter::Copy Assignment Operator
00055 //       Access: Published
00056 //  Description: The copy constructor does not copy ownership of the
00057 //               stream.
00058 ////////////////////////////////////////////////////////////////////
00059 INLINE void StreamWriter::
00060 operator = (const StreamWriter &copy) {
00061   if (_owns_stream) {
00062     delete _out;
00063   }
00064   _out = copy._out;
00065   _owns_stream = false;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: StreamWriter::Destructor
00070 //       Access: Published
00071 //  Description:
00072 ////////////////////////////////////////////////////////////////////
00073 INLINE StreamWriter::
00074 ~StreamWriter() {
00075   if (_owns_stream) {
00076     delete _out;
00077   }
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: StreamWriter::get_ostream
00082 //       Access: Published
00083 //  Description: Returns the stream in use.
00084 ////////////////////////////////////////////////////////////////////
00085 INLINE ostream *StreamWriter::
00086 get_ostream() const {
00087   return _out;
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: StreamWriter::add_bool
00092 //       Access: Published
00093 //  Description: Adds a boolean value to the stream.
00094 ////////////////////////////////////////////////////////////////////
00095 INLINE void StreamWriter::
00096 add_bool(bool b) {
00097   add_uint8(b);
00098 }
00099 
00100 ////////////////////////////////////////////////////////////////////
00101 //     Function: StreamWriter::add_int8
00102 //       Access: Published
00103 //  Description: Adds a signed 8-bit integer to the stream.
00104 ////////////////////////////////////////////////////////////////////
00105 INLINE void StreamWriter::
00106 add_int8(PN_int8 value) {
00107   append_data(&value, 1);
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: StreamWriter::add_uint8
00112 //       Access: Published
00113 //  Description: Adds an unsigned 8-bit integer to the stream.
00114 ////////////////////////////////////////////////////////////////////
00115 INLINE void StreamWriter::
00116 add_uint8(PN_uint8 value) {
00117   append_data(&value, 1);
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //     Function: StreamWriter::add_int16
00122 //       Access: Published
00123 //  Description: Adds a signed 16-bit integer to the stream.
00124 ////////////////////////////////////////////////////////////////////
00125 INLINE void StreamWriter::
00126 add_int16(PN_int16 value) {
00127   LittleEndian s(&value, sizeof(value));
00128   append_data(s.get_data(), sizeof(value));
00129 }
00130 
00131 ////////////////////////////////////////////////////////////////////
00132 //     Function: StreamWriter::add_int32
00133 //       Access: Published
00134 //  Description: Adds a signed 32-bit integer to the stream.
00135 ////////////////////////////////////////////////////////////////////
00136 INLINE void StreamWriter::
00137 add_int32(PN_int32 value) {
00138   LittleEndian s(&value, sizeof(value));
00139   append_data(s.get_data(), sizeof(value));
00140 }
00141 
00142 ////////////////////////////////////////////////////////////////////
00143 //     Function: StreamWriter::add_int64
00144 //       Access: Published
00145 //  Description: Adds a signed 64-bit integer to the stream.
00146 ////////////////////////////////////////////////////////////////////
00147 INLINE void StreamWriter::
00148 add_int64(PN_int64 value) {
00149   LittleEndian s(&value, sizeof(value));
00150   append_data(s.get_data(), sizeof(value));
00151 }
00152 
00153 ////////////////////////////////////////////////////////////////////
00154 //     Function: StreamWriter::add_uint16
00155 //       Access: Published
00156 //  Description: Adds an unsigned 16-bit integer to the stream.
00157 ////////////////////////////////////////////////////////////////////
00158 INLINE void StreamWriter::
00159 add_uint16(PN_uint16 value) {
00160   LittleEndian s(&value, sizeof(value));
00161   append_data(s.get_data(), sizeof(value));
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: StreamWriter::add_uint32
00166 //       Access: Published
00167 //  Description: Adds an unsigned 32-bit integer to the stream.
00168 ////////////////////////////////////////////////////////////////////
00169 INLINE void StreamWriter::
00170 add_uint32(PN_uint32 value) {
00171   LittleEndian s(&value, sizeof(value));
00172   append_data(s.get_data(), sizeof(value));
00173 }
00174 
00175 ////////////////////////////////////////////////////////////////////
00176 //     Function: StreamWriter::add_uint64
00177 //       Access: Published
00178 //  Description: Adds an unsigned 64-bit integer to the stream.
00179 ////////////////////////////////////////////////////////////////////
00180 INLINE void StreamWriter::
00181 add_uint64(PN_uint64 value) {
00182   LittleEndian s(&value, sizeof(value));
00183   append_data(s.get_data(), sizeof(value));
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function: StreamWriter::add_float32
00188 //       Access: Published
00189 //  Description: Adds a 32-bit single-precision floating-point number
00190 //               to the stream.  Since this kind of float is not
00191 //               necessarily portable across different architectures,
00192 //               special care is required.
00193 ////////////////////////////////////////////////////////////////////
00194 INLINE void StreamWriter::
00195 add_float32(float value) {
00196   // For now, we assume the float format is portable across all
00197   // architectures we are concerned with.  If we come across one that
00198   // is different, we will have to convert.
00199   nassertv(sizeof(value) == 4);
00200   LittleEndian s(&value, sizeof(value));
00201   append_data(s.get_data(), sizeof(value));
00202 }
00203 
00204 ////////////////////////////////////////////////////////////////////
00205 //     Function: StreamWriter::add_float64
00206 //       Access: Published
00207 //  Description: Adds a 64-bit floating-point number to the stream.
00208 ////////////////////////////////////////////////////////////////////
00209 INLINE void StreamWriter::
00210 add_float64(PN_float64 value) {
00211   LittleEndian s(&value, sizeof(value));
00212   append_data(s.get_data(), sizeof(value));
00213 }
00214 
00215 ////////////////////////////////////////////////////////////////////
00216 //     Function: StreamWriter::add_be_int16
00217 //       Access: Published
00218 //  Description: Adds a signed 16-bit big-endian integer to the
00219 //               streamWriter.
00220 ////////////////////////////////////////////////////////////////////
00221 INLINE void StreamWriter::
00222 add_be_int16(PN_int16 value) {
00223   BigEndian s(&value, sizeof(value));
00224   append_data(s.get_data(), sizeof(value));
00225 }
00226 
00227 ////////////////////////////////////////////////////////////////////
00228 //     Function: StreamWriter::add_be_int32
00229 //       Access: Published
00230 //  Description: Adds a signed 32-bit big-endian integer to the
00231 //               streamWriter.
00232 ////////////////////////////////////////////////////////////////////
00233 INLINE void StreamWriter::
00234 add_be_int32(PN_int32 value) {
00235   BigEndian s(&value, sizeof(value));
00236   append_data(s.get_data(), sizeof(value));
00237 }
00238 
00239 ////////////////////////////////////////////////////////////////////
00240 //     Function: StreamWriter::add_be_int64
00241 //       Access: Published
00242 //  Description: Adds a signed 64-bit big-endian integer to the
00243 //               streamWriter.
00244 ////////////////////////////////////////////////////////////////////
00245 INLINE void StreamWriter::
00246 add_be_int64(PN_int64 value) {
00247   BigEndian s(&value, sizeof(value));
00248   append_data(s.get_data(), sizeof(value));
00249 }
00250 
00251 ////////////////////////////////////////////////////////////////////
00252 //     Function: StreamWriter::add_be_uint16
00253 //       Access: Published
00254 //  Description: Adds an unsigned 16-bit big-endian integer to the
00255 //               streamWriter.
00256 ////////////////////////////////////////////////////////////////////
00257 INLINE void StreamWriter::
00258 add_be_uint16(PN_uint16 value) {
00259   BigEndian s(&value, sizeof(value));
00260   append_data(s.get_data(), sizeof(value));
00261 }
00262 
00263 ////////////////////////////////////////////////////////////////////
00264 //     Function: StreamWriter::add_be_uint32
00265 //       Access: Published
00266 //  Description: Adds an unsigned 32-bit big-endian integer to the
00267 //               streamWriter.
00268 ////////////////////////////////////////////////////////////////////
00269 INLINE void StreamWriter::
00270 add_be_uint32(PN_uint32 value) {
00271   BigEndian s(&value, sizeof(value));
00272   append_data(s.get_data(), sizeof(value));
00273 }
00274 
00275 ////////////////////////////////////////////////////////////////////
00276 //     Function: StreamWriter::add_be_uint64
00277 //       Access: Published
00278 //  Description: Adds an unsigned 64-bit big-endian integer to the
00279 //               streamWriter.
00280 ////////////////////////////////////////////////////////////////////
00281 INLINE void StreamWriter::
00282 add_be_uint64(PN_uint64 value) {
00283   BigEndian s(&value, sizeof(value));
00284   append_data(s.get_data(), sizeof(value));
00285 }
00286 
00287 ////////////////////////////////////////////////////////////////////
00288 //     Function: StreamWriter::add_be_float32
00289 //       Access: Published
00290 //  Description: Adds a 32-bit single-precision big-endian
00291 //               floating-point number to the stream.  Since this
00292 //               kind of float is not necessarily portable across
00293 //               different architectures, special care is required.
00294 ////////////////////////////////////////////////////////////////////
00295 INLINE void StreamWriter::
00296 add_be_float32(float value) {
00297   // For now, we assume the float format is portable across all
00298   // architectures we are concerned with.  If we come across one that
00299   // is different, we will have to convert.
00300   nassertv(sizeof(value) == 4);
00301   BigEndian s(&value, sizeof(value));
00302   append_data(s.get_data(), sizeof(value));
00303 }
00304 
00305 ////////////////////////////////////////////////////////////////////
00306 //     Function: StreamWriter::add_be_float64
00307 //       Access: Published
00308 //  Description: Adds a 64-bit big-endian floating-point number to the
00309 //               streamWriter.
00310 ////////////////////////////////////////////////////////////////////
00311 INLINE void StreamWriter::
00312 add_be_float64(PN_float64 value) {
00313   BigEndian s(&value, sizeof(value));
00314   append_data(s.get_data(), sizeof(value));
00315 }
00316 
00317 ////////////////////////////////////////////////////////////////////
00318 //     Function: StreamWriter::add_string
00319 //       Access: Published
00320 //  Description: Adds a variable-length string to the stream.  This
00321 //               actually adds a count followed by n bytes.
00322 ////////////////////////////////////////////////////////////////////
00323 INLINE void StreamWriter::
00324 add_string(const string &str) {
00325   // The max sendable length for a string is 2^16.
00326   nassertv(str.length() <= (PN_uint16)0xffff);
00327 
00328   // Strings always are preceded by their length
00329   add_uint16(str.length());
00330 
00331   // Add the string
00332   append_data(str);
00333 }
00334 
00335 ////////////////////////////////////////////////////////////////////
00336 //     Function: StreamWriter::add_string32
00337 //       Access: Published
00338 //  Description: Adds a variable-length string to the stream, using a
00339 //               32-bit length field.
00340 ////////////////////////////////////////////////////////////////////
00341 INLINE void StreamWriter::
00342 add_string32(const string &str) {
00343   // Strings always are preceded by their length
00344   add_uint32(str.length());
00345 
00346   // Add the string
00347   append_data(str);
00348 }
00349 
00350 ////////////////////////////////////////////////////////////////////
00351 //     Function: StreamWriter::add_z_string
00352 //       Access: Published
00353 //  Description: Adds a variable-length string to the stream, as a
00354 //               NULL-terminated string.
00355 ////////////////////////////////////////////////////////////////////
00356 INLINE void StreamWriter::
00357 add_z_string(string str) {
00358   // We must not have any nested null characters in the string.
00359   size_t null_pos = str.find('\0');
00360   // Add the string (sans the null character).
00361   append_data(str.substr(0, null_pos));
00362 
00363   // And the null character.
00364   add_uint8('\0');
00365 }
00366 
00367 ////////////////////////////////////////////////////////////////////
00368 //     Function: StreamWriter::add_fixed_string
00369 //       Access: Published
00370 //  Description: Adds a fixed-length string to the stream.  If the
00371 //               string given is less than the requested size, this
00372 //               will pad the string out with zeroes; if it is greater
00373 //               than the requested size, this will silently truncate
00374 //               the string.
00375 ////////////////////////////////////////////////////////////////////
00376 INLINE void StreamWriter::
00377 add_fixed_string(const string &str, size_t size) {
00378   if (str.length() < size) {
00379     append_data(str);
00380     pad_bytes(size - str.length());
00381 
00382   } else { // str.length() >= size
00383     append_data(str.substr(0, size));
00384   }
00385 }
00386 
00387 ////////////////////////////////////////////////////////////////////
00388 //     Function: StreamWriter::append_data
00389 //       Access: Published
00390 //  Description: Appends some more raw data to the end of the
00391 //               streamWriter.
00392 ////////////////////////////////////////////////////////////////////
00393 INLINE void StreamWriter::
00394 append_data(const void *data, size_t size) {
00395   _out->write((const char *)data, size);
00396 }
00397 
00398 ////////////////////////////////////////////////////////////////////
00399 //     Function: StreamWriter::append_data
00400 //       Access: Published
00401 //  Description: Appends some more raw data to the end of the
00402 //               streamWriter.
00403 ////////////////////////////////////////////////////////////////////
00404 INLINE void StreamWriter::
00405 append_data(const string &data) {
00406   append_data(data.data(), data.length());
00407 }
00408 
00409 ////////////////////////////////////////////////////////////////////
00410 //     Function: StreamWriter::flush
00411 //       Access: Published
00412 //  Description: Calls flush() on the underlying stream.
00413 ////////////////////////////////////////////////////////////////////
00414 INLINE void StreamWriter::
00415 flush() {
00416   _out->flush();
00417 }
00418 
00419 ////////////////////////////////////////////////////////////////////
00420 //     Function: StreamWriter::write
00421 //       Access: Published
00422 //  Description: A synonym of append_data().  This is useful when
00423 //               assigning the StreamWriter to sys.stderr and/or
00424 //               sys.stdout in Python.
00425 ////////////////////////////////////////////////////////////////////
00426 INLINE void StreamWriter::
00427 write(const string &data) {
00428   append_data(data.data(), data.length());
00429 }
 All Classes Functions Variables Enumerations