00001 // Filename: streamReader.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: StreamReader::Constructor 00022 // Access: Public 00023 // Description: 00024 //////////////////////////////////////////////////////////////////// 00025 INLINE StreamReader:: 00026 StreamReader(istream &in) : 00027 _in(&in), 00028 _owns_stream(false) 00029 { 00030 } 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: StreamReader::Constructor 00034 // Access: Published 00035 // Description: If owns_stream is true, the stream pointer will be 00036 // deleted when the StreamReader destructs. 00037 //////////////////////////////////////////////////////////////////// 00038 INLINE StreamReader:: 00039 StreamReader(istream *in, bool owns_stream) : 00040 _in(in), 00041 _owns_stream(owns_stream) 00042 { 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: StreamReader::Copy Constructor 00047 // Access: Published 00048 // Description: The copy constructor does not copy ownership of the 00049 // stream. 00050 //////////////////////////////////////////////////////////////////// 00051 INLINE StreamReader:: 00052 StreamReader(const StreamReader ©) : 00053 _in(copy._in), 00054 _owns_stream(false) 00055 { 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: StreamReader::Copy Assignment Operator 00060 // Access: Published 00061 // Description: The copy constructor does not copy ownership of the 00062 // stream. 00063 //////////////////////////////////////////////////////////////////// 00064 INLINE void StreamReader:: 00065 operator = (const StreamReader ©) { 00066 _in = copy._in; 00067 _owns_stream = false; 00068 } 00069 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: StreamReader::Destructor 00072 // Access: Published 00073 // Description: 00074 //////////////////////////////////////////////////////////////////// 00075 INLINE StreamReader:: 00076 ~StreamReader() { 00077 if (_owns_stream) { 00078 delete _in; 00079 } 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: StreamReader::get_istream 00084 // Access: Published 00085 // Description: Returns the stream in use. 00086 //////////////////////////////////////////////////////////////////// 00087 INLINE istream *StreamReader:: 00088 get_istream() const { 00089 return _in; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: StreamReader::get_bool 00094 // Access: Published 00095 // Description: Extracts a boolean value. 00096 //////////////////////////////////////////////////////////////////// 00097 INLINE bool StreamReader:: 00098 get_bool() { 00099 return (get_uint8() != 0); 00100 } 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: StreamReader::get_int8 00104 // Access: Published 00105 // Description: Extracts a signed 8-bit integer. 00106 //////////////////////////////////////////////////////////////////// 00107 INLINE PN_int8 StreamReader:: 00108 get_int8() { 00109 return (PN_int8)_in->get(); 00110 } 00111 00112 //////////////////////////////////////////////////////////////////// 00113 // Function: StreamReader::get_uint8 00114 // Access: Published 00115 // Description: Extracts an unsigned 8-bit integer. 00116 //////////////////////////////////////////////////////////////////// 00117 INLINE PN_uint8 StreamReader:: 00118 get_uint8() { 00119 return (PN_uint8)_in->get(); 00120 } 00121 00122 //////////////////////////////////////////////////////////////////// 00123 // Function: StreamReader::get_int16 00124 // Access: Published 00125 // Description: Extracts a signed 16-bit integer. 00126 //////////////////////////////////////////////////////////////////// 00127 INLINE PN_int16 StreamReader:: 00128 get_int16() { 00129 PN_int16 readval, retval; 00130 _in->read((char *)&readval, sizeof(readval)); 00131 LittleEndian s(&readval, 0, sizeof(readval)); 00132 s.store_value(&retval, sizeof(retval)); 00133 return retval; 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function: StreamReader::get_int32 00138 // Access: Published 00139 // Description: Extracts a signed 32-bit integer. 00140 //////////////////////////////////////////////////////////////////// 00141 INLINE PN_int32 StreamReader:: 00142 get_int32() { 00143 PN_int32 readval, retval; 00144 _in->read((char *)&readval, sizeof(readval)); 00145 LittleEndian s(&readval, 0, sizeof(readval)); 00146 s.store_value(&retval, sizeof(retval)); 00147 return retval; 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: StreamReader::get_int64 00152 // Access: Published 00153 // Description: Extracts a signed 64-bit integer. 00154 //////////////////////////////////////////////////////////////////// 00155 INLINE PN_int64 StreamReader:: 00156 get_int64() { 00157 PN_int64 readval, retval; 00158 _in->read((char *)&readval, sizeof(readval)); 00159 LittleEndian s(&readval, 0, sizeof(readval)); 00160 s.store_value(&retval, sizeof(retval)); 00161 return retval; 00162 } 00163 00164 //////////////////////////////////////////////////////////////////// 00165 // Function: StreamReader::get_uint16 00166 // Access: Published 00167 // Description: Extracts an unsigned 16-bit integer. 00168 //////////////////////////////////////////////////////////////////// 00169 INLINE PN_uint16 StreamReader:: 00170 get_uint16() { 00171 PN_uint16 readval, retval; 00172 _in->read((char *)&readval, sizeof(readval)); 00173 LittleEndian s(&readval, 0, sizeof(readval)); 00174 s.store_value(&retval, sizeof(retval)); 00175 return retval; 00176 } 00177 00178 //////////////////////////////////////////////////////////////////// 00179 // Function: StreamReader::get_uint32 00180 // Access: Published 00181 // Description: Extracts an unsigned 32-bit integer. 00182 //////////////////////////////////////////////////////////////////// 00183 INLINE PN_uint32 StreamReader:: 00184 get_uint32() { 00185 PN_uint32 readval, retval; 00186 _in->read((char *)&readval, sizeof(readval)); 00187 LittleEndian s(&readval, 0, sizeof(readval)); 00188 s.store_value(&retval, sizeof(retval)); 00189 return retval; 00190 } 00191 00192 //////////////////////////////////////////////////////////////////// 00193 // Function: StreamReader::get_uint64 00194 // Access: Published 00195 // Description: Extracts an unsigned 64-bit integer. 00196 //////////////////////////////////////////////////////////////////// 00197 INLINE PN_uint64 StreamReader:: 00198 get_uint64() { 00199 PN_uint64 readval, retval; 00200 _in->read((char *)&readval, sizeof(readval)); 00201 LittleEndian s(&readval, 0, sizeof(readval)); 00202 s.store_value(&retval, sizeof(retval)); 00203 return retval; 00204 } 00205 00206 //////////////////////////////////////////////////////////////////// 00207 // Function: StreamReader::get_float32 00208 // Access: Published 00209 // Description: Extracts a 32-bit single-precision floating-point 00210 // number. Since this kind of float is not necessarily 00211 // portable across different architectures, special care 00212 // is required. 00213 //////////////////////////////////////////////////////////////////// 00214 INLINE float StreamReader:: 00215 get_float32() { 00216 // For now, we assume the float format is portable across all 00217 // architectures we are concerned with. If we come across one that 00218 // is different, we will have to convert. 00219 nassertr(sizeof(float) == 4, 0.0f); 00220 00221 float readval, retval; 00222 _in->read((char *)&readval, sizeof(readval)); 00223 LittleEndian s(&readval, 0, sizeof(readval)); 00224 s.store_value(&retval, sizeof(retval)); 00225 return retval; 00226 } 00227 00228 //////////////////////////////////////////////////////////////////// 00229 // Function: StreamReader::get_float64 00230 // Access: Published 00231 // Description: Extracts a 64-bit floating-point number. 00232 //////////////////////////////////////////////////////////////////// 00233 INLINE PN_float64 StreamReader:: 00234 get_float64() { 00235 PN_float64 readval, retval; 00236 _in->read((char *)&readval, sizeof(readval)); 00237 LittleEndian s(&readval, 0, sizeof(readval)); 00238 s.store_value(&retval, sizeof(retval)); 00239 return retval; 00240 } 00241 00242 //////////////////////////////////////////////////////////////////// 00243 // Function: StreamReader::get_be_int16 00244 // Access: Published 00245 // Description: Extracts a signed big-endian 16-bit integer. 00246 //////////////////////////////////////////////////////////////////// 00247 INLINE PN_int16 StreamReader:: 00248 get_be_int16() { 00249 PN_int16 readval, retval; 00250 _in->read((char *)&readval, sizeof(readval)); 00251 BigEndian s(&readval, 0, sizeof(readval)); 00252 s.store_value(&retval, sizeof(retval)); 00253 return retval; 00254 } 00255 00256 //////////////////////////////////////////////////////////////////// 00257 // Function: StreamReader::get_be_int32 00258 // Access: Published 00259 // Description: Extracts a signed big-endian 32-bit integer. 00260 //////////////////////////////////////////////////////////////////// 00261 INLINE PN_int32 StreamReader:: 00262 get_be_int32() { 00263 PN_int32 readval, retval; 00264 _in->read((char *)&readval, sizeof(readval)); 00265 BigEndian s(&readval, 0, sizeof(readval)); 00266 s.store_value(&retval, sizeof(retval)); 00267 return retval; 00268 } 00269 00270 //////////////////////////////////////////////////////////////////// 00271 // Function: StreamReader::get_be_int64 00272 // Access: Published 00273 // Description: Extracts a signed big-endian 64-bit integer. 00274 //////////////////////////////////////////////////////////////////// 00275 INLINE PN_int64 StreamReader:: 00276 get_be_int64() { 00277 PN_int64 readval, retval; 00278 _in->read((char *)&readval, sizeof(readval)); 00279 BigEndian s(&readval, 0, sizeof(readval)); 00280 s.store_value(&retval, sizeof(retval)); 00281 return retval; 00282 } 00283 00284 //////////////////////////////////////////////////////////////////// 00285 // Function: StreamReader::get_be_uint16 00286 // Access: Published 00287 // Description: Extracts an unsigned big-endian 16-bit integer. 00288 //////////////////////////////////////////////////////////////////// 00289 INLINE PN_uint16 StreamReader:: 00290 get_be_uint16() { 00291 PN_uint16 readval, retval; 00292 _in->read((char *)&readval, sizeof(readval)); 00293 BigEndian s(&readval, 0, sizeof(readval)); 00294 s.store_value(&retval, sizeof(retval)); 00295 return retval; 00296 } 00297 00298 //////////////////////////////////////////////////////////////////// 00299 // Function: StreamReader::get_be_uint32 00300 // Access: Published 00301 // Description: Extracts an unsigned big-endian 32-bit integer. 00302 //////////////////////////////////////////////////////////////////// 00303 INLINE PN_uint32 StreamReader:: 00304 get_be_uint32() { 00305 PN_uint32 readval, retval; 00306 _in->read((char *)&readval, sizeof(readval)); 00307 BigEndian s(&readval, 0, sizeof(readval)); 00308 s.store_value(&retval, sizeof(retval)); 00309 return retval; 00310 } 00311 00312 //////////////////////////////////////////////////////////////////// 00313 // Function: StreamReader::get_be_uint64 00314 // Access: Published 00315 // Description: Extracts an unsigned big-endian 64-bit integer. 00316 //////////////////////////////////////////////////////////////////// 00317 INLINE PN_uint64 StreamReader:: 00318 get_be_uint64() { 00319 PN_uint64 readval, retval; 00320 _in->read((char *)&readval, sizeof(readval)); 00321 BigEndian s(&readval, 0, sizeof(readval)); 00322 s.store_value(&retval, sizeof(retval)); 00323 return retval; 00324 } 00325 00326 //////////////////////////////////////////////////////////////////// 00327 // Function: StreamReader::get_be_float32 00328 // Access: Published 00329 // Description: Extracts a 32-bit single-precision big-endian 00330 // floating-point number. Since this kind of float is 00331 // not necessarily portable across different 00332 // architectures, special care is required. 00333 //////////////////////////////////////////////////////////////////// 00334 INLINE float StreamReader:: 00335 get_be_float32() { 00336 // For now, we assume the float format is portable across all 00337 // architectures we are concerned with. If we come across one that 00338 // is different, we will have to convert. 00339 nassertr(sizeof(float) == 4, 0.0f); 00340 00341 float readval, retval; 00342 _in->read((char *)&readval, sizeof(readval)); 00343 BigEndian s(&readval, 0, sizeof(readval)); 00344 s.store_value(&retval, sizeof(retval)); 00345 return retval; 00346 } 00347 00348 //////////////////////////////////////////////////////////////////// 00349 // Function: StreamReader::get_be_float64 00350 // Access: Published 00351 // Description: Extracts a 64-bit big-endian floating-point number. 00352 //////////////////////////////////////////////////////////////////// 00353 INLINE PN_float64 StreamReader:: 00354 get_be_float64() { 00355 PN_float64 readval, retval; 00356 _in->read((char *)&readval, sizeof(readval)); 00357 BigEndian s(&readval, 0, sizeof(readval)); 00358 s.store_value(&retval, sizeof(retval)); 00359 return retval; 00360 }