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