00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 INLINE StreamWriter::
00022 StreamWriter(ostream &out) :
00023 _out(&out),
00024 _owns_stream(false)
00025 {
00026 }
00027
00028
00029
00030
00031
00032
00033 INLINE StreamWriter::
00034 StreamWriter(ostream *out, bool owns_stream) :
00035 _out(out),
00036 _owns_stream(owns_stream)
00037 {
00038 }
00039
00040
00041
00042
00043
00044
00045
00046 INLINE StreamWriter::
00047 StreamWriter(const StreamWriter ©) :
00048 _out(copy._out),
00049 _owns_stream(false)
00050 {
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 INLINE void StreamWriter::
00060 operator = (const StreamWriter ©) {
00061 if (_owns_stream) {
00062 delete _out;
00063 }
00064 _out = copy._out;
00065 _owns_stream = false;
00066 }
00067
00068
00069
00070
00071
00072
00073 INLINE StreamWriter::
00074 ~StreamWriter() {
00075 if (_owns_stream) {
00076 delete _out;
00077 }
00078 }
00079
00080
00081
00082
00083
00084
00085 INLINE ostream *StreamWriter::
00086 get_ostream() const {
00087 return _out;
00088 }
00089
00090
00091
00092
00093
00094
00095 INLINE void StreamWriter::
00096 add_bool(bool b) {
00097 add_uint8(b);
00098 }
00099
00100
00101
00102
00103
00104
00105 INLINE void StreamWriter::
00106 add_int8(PN_int8 value) {
00107 append_data(&value, 1);
00108 }
00109
00110
00111
00112
00113
00114
00115 INLINE void StreamWriter::
00116 add_uint8(PN_uint8 value) {
00117 append_data(&value, 1);
00118 }
00119
00120
00121
00122
00123
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
00133
00134
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
00144
00145
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
00155
00156
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
00166
00167
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
00177
00178
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
00188
00189
00190
00191
00192
00193
00194 INLINE void StreamWriter::
00195 add_float32(float value) {
00196
00197
00198
00199 nassertv(sizeof(value) == 4);
00200 LittleEndian s(&value, sizeof(value));
00201 append_data(s.get_data(), sizeof(value));
00202 }
00203
00204
00205
00206
00207
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
00217
00218
00219
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
00229
00230
00231
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
00241
00242
00243
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
00253
00254
00255
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
00265
00266
00267
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
00277
00278
00279
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
00289
00290
00291
00292
00293
00294
00295 INLINE void StreamWriter::
00296 add_be_float32(float value) {
00297
00298
00299
00300 nassertv(sizeof(value) == 4);
00301 BigEndian s(&value, sizeof(value));
00302 append_data(s.get_data(), sizeof(value));
00303 }
00304
00305
00306
00307
00308
00309
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
00319
00320
00321
00322
00323 INLINE void StreamWriter::
00324 add_string(const string &str) {
00325
00326 nassertv(str.length() <= (PN_uint16)0xffff);
00327
00328
00329 add_uint16(str.length());
00330
00331
00332 append_data(str);
00333 }
00334
00335
00336
00337
00338
00339
00340
00341 INLINE void StreamWriter::
00342 add_string32(const string &str) {
00343
00344 add_uint32(str.length());
00345
00346
00347 append_data(str);
00348 }
00349
00350
00351
00352
00353
00354
00355
00356 INLINE void StreamWriter::
00357 add_z_string(string str) {
00358
00359 size_t null_pos = str.find('\0');
00360
00361 append_data(str.substr(0, null_pos));
00362
00363
00364 add_uint8('\0');
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
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 {
00383 append_data(str.substr(0, size));
00384 }
00385 }
00386
00387
00388
00389
00390
00391
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
00400
00401
00402
00403
00404 INLINE void StreamWriter::
00405 append_data(const string &data) {
00406 append_data(data.data(), data.length());
00407 }
00408
00409
00410
00411
00412
00413
00414 INLINE void StreamWriter::
00415 flush() {
00416 _out->flush();
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426 INLINE void StreamWriter::
00427 write(const string &data) {
00428 append_data(data.data(), data.length());
00429 }