00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_binaryfileserializer.hpp"
00012
00013 #include <string.h>
00014
00015 namespace Xrb
00016 {
00017
00018 BinaryFileSerializer::BinaryFileSerializer ()
00019 :
00020 Serializer(),
00021 m_cache(MAX_SUPPORTED_STRING_BUFFER_SIZE, Endian::SERIALIZER)
00022 {
00023 }
00024
00025 BinaryFileSerializer::~BinaryFileSerializer ()
00026 {
00027 }
00028
00029 bool BinaryFileSerializer::IsAtEnd () const
00030 {
00031 return m_cache.IsAtEnd();
00032 }
00033
00034 bool BinaryFileSerializer::HasFewerThan8BitsLeft () const
00035 {
00036 return m_cache.HasFewerThan8BitsLeft();
00037 }
00038
00039 void BinaryFileSerializer::Open (
00040 char const *const path,
00041 char const *const mode)
00042 {
00043 ASSERT1(strchr(mode, 't') == NULL &&
00044 "You cannot specify mode 't' for a binary file");
00045
00046
00047 std::string modified_mode(mode);
00048 if (strchr(modified_mode.c_str(), 'b') == NULL)
00049 modified_mode += 'b';
00050
00051 m_cache.Open(path, modified_mode.c_str());
00052 SetIsOpen(m_cache.IsOpen());
00053 SetIODirection(m_cache.GetIODirection());
00054 SetError(m_cache.Error());
00055 }
00056
00057 void BinaryFileSerializer::Close ()
00058 {
00059 m_cache.Close();
00060 SetIsOpen(false);
00061 SetIODirection(IOD_NONE);
00062 SetError(m_cache.Error());
00063 }
00064
00065 void BinaryFileSerializer::FlushWriteCache ()
00066 {
00067 ASSERT1(IsOpen());
00068 ASSERT1(GetIODirection() == IOD_WRITE);
00069 m_cache.FlushWriteCache();
00070 SetError(m_cache.Error());
00071 }
00072
00073 Sint32 BinaryFileSerializer::ReadSignedBits (Uint32 const bit_count)
00074 {
00075 Sint32 retval = m_cache.ReadSignedBits(bit_count);
00076 SetError(m_cache.Error());
00077 return retval;
00078 }
00079
00080 void BinaryFileSerializer::WriteSignedBits (
00081 Sint32 const value,
00082 Uint32 const bit_count)
00083 {
00084 m_cache.WriteSignedBits(value, bit_count);
00085 SetError(m_cache.Error());
00086 }
00087
00088 Uint32 BinaryFileSerializer::ReadUnsignedBits (Uint32 const bit_count)
00089 {
00090 Uint32 retval = m_cache.ReadUnsignedBits(bit_count);
00091 SetError(m_cache.Error());
00092 return retval;
00093 }
00094
00095 void BinaryFileSerializer::WriteUnsignedBits (
00096 Uint32 const value,
00097 Uint32 const bit_count)
00098 {
00099 m_cache.WriteUnsignedBits(value, bit_count);
00100 SetError(m_cache.Error());
00101 }
00102
00103 bool BinaryFileSerializer::ReadBool ()
00104 {
00105 bool retval = m_cache.ReadBool();
00106 SetError(m_cache.Error());
00107 return retval;
00108 }
00109
00110 void BinaryFileSerializer::WriteBool (bool const value)
00111 {
00112 m_cache.WriteBool(value);
00113 SetError(m_cache.Error());
00114 }
00115
00116 Sint8 BinaryFileSerializer::ReadSint8 ()
00117 {
00118 Sint8 retval;
00119 m_cache.Read1ByteWordFromCache(&retval);
00120 SetError(m_cache.Error());
00121 return retval;
00122 }
00123
00124 void BinaryFileSerializer::WriteSint8 (Sint8 value)
00125 {
00126 m_cache.Write1ByteWordToCache(&value);
00127 SetError(m_cache.Error());
00128 }
00129
00130 Uint8 BinaryFileSerializer::ReadUint8 ()
00131 {
00132 Uint8 retval;
00133 m_cache.Read1ByteWordFromCache(&retval);
00134 SetError(m_cache.Error());
00135 return retval;
00136 }
00137
00138 void BinaryFileSerializer::WriteUint8 (Uint8 value)
00139 {
00140 m_cache.Write1ByteWordToCache(&value);
00141 SetError(m_cache.Error());
00142 }
00143
00144 Sint16 BinaryFileSerializer::ReadSint16 ()
00145 {
00146 Sint16 retval;
00147 m_cache.Read2ByteWordFromCache(&retval);
00148 SetError(m_cache.Error());
00149 return retval;
00150 }
00151
00152 void BinaryFileSerializer::WriteSint16 (Sint16 value)
00153 {
00154 m_cache.Write2ByteWordToCache(&value);
00155 SetError(m_cache.Error());
00156 }
00157
00158 Uint16 BinaryFileSerializer::ReadUint16 ()
00159 {
00160 Uint16 retval;
00161 m_cache.Read2ByteWordFromCache(&retval);
00162 SetError(m_cache.Error());
00163 return retval;
00164 }
00165
00166 void BinaryFileSerializer::WriteUint16 (Uint16 value)
00167 {
00168 m_cache.Write2ByteWordToCache(&value);
00169 SetError(m_cache.Error());
00170 }
00171
00172 Sint32 BinaryFileSerializer::ReadSint32 ()
00173 {
00174 Sint32 retval;
00175 m_cache.Read4ByteWordFromCache(&retval);
00176 SetError(m_cache.Error());
00177 return retval;
00178 }
00179
00180 void BinaryFileSerializer::WriteSint32 (Sint32 value)
00181 {
00182 m_cache.Write4ByteWordToCache(&value);
00183 SetError(m_cache.Error());
00184 }
00185
00186 Uint32 BinaryFileSerializer::ReadUint32 ()
00187 {
00188 Uint32 retval;
00189 m_cache.Read4ByteWordFromCache(&retval);
00190 SetError(m_cache.Error());
00191 return retval;
00192 }
00193
00194 void BinaryFileSerializer::WriteUint32 (Uint32 value)
00195 {
00196 m_cache.Write4ByteWordToCache(&value);
00197 SetError(m_cache.Error());
00198 }
00199
00200 void BinaryFileSerializer::ReadFloat (float *const destination)
00201 {
00202 m_cache.Read4ByteWordFromCache(destination);
00203 SetError(m_cache.Error());
00204 }
00205
00206 void BinaryFileSerializer::WriteFloat (float value)
00207 {
00208 m_cache.Write4ByteWordToCache(&value);
00209 SetError(m_cache.Error());
00210 }
00211
00212 void BinaryFileSerializer::ReadFloat (double *const destination)
00213 {
00214 m_cache.Read8ByteWordFromCache(destination);
00215 SetError(m_cache.Error());
00216 }
00217
00218 void BinaryFileSerializer::WriteFloat (double value)
00219 {
00220 m_cache.Write8ByteWordToCache(&value);
00221 SetError(m_cache.Error());
00222 }
00223
00224 inline Uint32 BinaryFileSerializer::ReadBufferString (
00225 char *const destination,
00226 Uint32 const destination_length)
00227 {
00228 Uint32 retval = m_cache.ReadBufferString(destination, destination_length);
00229 SetError(m_cache.Error());
00230 return retval;
00231 }
00232
00233 inline Uint32 BinaryFileSerializer::WriteBufferString (
00234 char const *const source,
00235 Uint32 const source_length)
00236 {
00237 Uint32 retval = m_cache.WriteBufferString(source, source_length);
00238 SetError(m_cache.Error());
00239 return retval;
00240 }
00241
00242 void BinaryFileSerializer::ReadColor (Color *const destination)
00243 {
00244 ASSERT1(destination != NULL);
00245
00246 BinaryFileSerializer::ReadFloat(&(*destination)[Dim::R]);
00247 if (Error() != IOE_NONE)
00248 return;
00249
00250 BinaryFileSerializer::ReadFloat(&(*destination)[Dim::G]);
00251 if (Error() != IOE_NONE)
00252 return;
00253
00254 BinaryFileSerializer::ReadFloat(&(*destination)[Dim::B]);
00255 if (Error() != IOE_NONE)
00256 return;
00257
00258 BinaryFileSerializer::ReadFloat(&(*destination)[Dim::A]);
00259 }
00260
00261 void BinaryFileSerializer::WriteColor (Color const &value)
00262 {
00263 BinaryFileSerializer::WriteFloat(value[Dim::R]);
00264 if (Error() != IOE_NONE)
00265 return;
00266
00267 BinaryFileSerializer::WriteFloat(value[Dim::G]);
00268 if (Error() != IOE_NONE)
00269 return;
00270
00271 BinaryFileSerializer::WriteFloat(value[Dim::B]);
00272 if (Error() != IOE_NONE)
00273 return;
00274
00275 BinaryFileSerializer::WriteFloat(value[Dim::A]);
00276 }
00277
00278 void BinaryFileSerializer::ReadScreenCoordVector2 (ScreenCoordVector2 *destination)
00279 {
00280 ASSERT1(destination != NULL);
00281
00282 (*destination)[Dim::X] = BinaryFileSerializer::ReadScreenCoord();
00283 if (Error() != IOE_NONE)
00284 return;
00285
00286 (*destination)[Dim::Y] = BinaryFileSerializer::ReadScreenCoord();
00287 }
00288
00289 void BinaryFileSerializer::WriteScreenCoordVector2 (ScreenCoordVector2 const &value)
00290 {
00291 BinaryFileSerializer::WriteScreenCoord(value[Dim::X]);
00292 if (Error() != IOE_NONE)
00293 return;
00294
00295 BinaryFileSerializer::WriteScreenCoord(value[Dim::Y]);
00296 }
00297
00298 void BinaryFileSerializer::ReadFloatVector2 (FloatVector2 *const destination)
00299 {
00300 ASSERT1(destination != NULL);
00301
00302 BinaryFileSerializer::ReadFloat(&(*destination)[Dim::X]);
00303 if (Error() != IOE_NONE)
00304 return;
00305
00306 BinaryFileSerializer::ReadFloat(&(*destination)[Dim::Y]);
00307 }
00308
00309 void BinaryFileSerializer::WriteFloatVector2 (FloatVector2 const &value)
00310 {
00311 BinaryFileSerializer::WriteFloat(value[Dim::X]);
00312 if (Error() != IOE_NONE)
00313 return;
00314
00315 BinaryFileSerializer::WriteFloat(value[Dim::Y]);
00316 }
00317
00318 void BinaryFileSerializer::ReadFloatSimpleTransform2 (
00319 FloatSimpleTransform2 *const destination)
00320 {
00321 ASSERT1(destination != NULL);
00322
00323 BinaryFileSerializer::ReadFloat(&(*destination).m_scale_factors[Dim::X]);
00324 if (Error() != IOE_NONE)
00325 return;
00326
00327 BinaryFileSerializer::ReadFloat(&(*destination).m_scale_factors[Dim::Y]);
00328 if (Error() != IOE_NONE)
00329 return;
00330
00331 BinaryFileSerializer::ReadFloat(&(*destination).m_translation[Dim::X]);
00332 if (Error() != IOE_NONE)
00333 return;
00334
00335 BinaryFileSerializer::ReadFloat(&(*destination).m_translation[Dim::Y]);
00336 }
00337
00338 void BinaryFileSerializer::WriteFloatSimpleTransform2 (
00339 FloatSimpleTransform2 const &value)
00340 {
00341 BinaryFileSerializer::WriteFloat(value.m_scale_factors[Dim::X]);
00342 if (Error() != IOE_NONE)
00343 return;
00344
00345 BinaryFileSerializer::WriteFloat(value.m_scale_factors[Dim::Y]);
00346 if (Error() != IOE_NONE)
00347 return;
00348
00349 BinaryFileSerializer::WriteFloat(value.m_translation[Dim::X]);
00350 if (Error() != IOE_NONE)
00351 return;
00352
00353 BinaryFileSerializer::WriteFloat(value.m_translation[Dim::Y]);
00354 }
00355
00356 void BinaryFileSerializer::ReadFloatMatrix2 (FloatMatrix2 *const destination)
00357 {
00358 ASSERT1(destination != NULL);
00359
00360 BinaryFileSerializer::ReadFloat(&(*destination)[FloatMatrix2::A]);
00361 if (Error() != IOE_NONE)
00362 return;
00363
00364 BinaryFileSerializer::ReadFloat(&(*destination)[FloatMatrix2::B]);
00365 if (Error() != IOE_NONE)
00366 return;
00367
00368 BinaryFileSerializer::ReadFloat(&(*destination)[FloatMatrix2::X]);
00369 if (Error() != IOE_NONE)
00370 return;
00371
00372 BinaryFileSerializer::ReadFloat(&(*destination)[FloatMatrix2::C]);
00373 if (Error() != IOE_NONE)
00374 return;
00375
00376 BinaryFileSerializer::ReadFloat(&(*destination)[FloatMatrix2::D]);
00377 if (Error() != IOE_NONE)
00378 return;
00379
00380 BinaryFileSerializer::ReadFloat(&(*destination)[FloatMatrix2::Y]);
00381 }
00382
00383 void BinaryFileSerializer::WriteFloatMatrix2 (FloatMatrix2 const &value)
00384 {
00385 BinaryFileSerializer::WriteFloat(value[FloatMatrix2::A]);
00386 if (Error() != IOE_NONE)
00387 return;
00388
00389 BinaryFileSerializer::WriteFloat(value[FloatMatrix2::B]);
00390 if (Error() != IOE_NONE)
00391 return;
00392
00393 BinaryFileSerializer::WriteFloat(value[FloatMatrix2::X]);
00394 if (Error() != IOE_NONE)
00395 return;
00396
00397 BinaryFileSerializer::WriteFloat(value[FloatMatrix2::C]);
00398 if (Error() != IOE_NONE)
00399 return;
00400
00401 BinaryFileSerializer::WriteFloat(value[FloatMatrix2::D]);
00402 if (Error() != IOE_NONE)
00403 return;
00404
00405 BinaryFileSerializer::WriteFloat(value[FloatMatrix2::Y]);
00406 }
00407
00408 void BinaryFileSerializer::ReadFloatTransform2 (
00409 FloatTransform2 *const destination)
00410 {
00411 ASSERT1(destination != NULL);
00412
00413 {
00414 FloatVector2 translation;
00415 BinaryFileSerializer::ReadFloatVector2(&translation);
00416 if (Error() != IOE_NONE)
00417 return;
00418 destination->SetTranslation(translation);
00419 }
00420
00421 {
00422 FloatVector2 scale_factors;
00423 BinaryFileSerializer::ReadFloatVector2(&scale_factors);
00424 if (Error() != IOE_NONE)
00425 return;
00426 destination->SetScaleFactors(scale_factors);
00427 }
00428
00429 {
00430 Float angle;
00431 BinaryFileSerializer::ReadFloat(&angle);
00432 if (Error() != IOE_NONE)
00433 return;
00434 destination->SetAngle(angle);
00435 }
00436
00437 {
00438 bool post_translate = BinaryFileSerializer::ReadBool();
00439 if (Error() != IOE_NONE)
00440 return;
00441 destination->SetPostTranslate(post_translate);
00442 }
00443 }
00444
00445 void BinaryFileSerializer::WriteFloatTransform2 (FloatTransform2 const &value)
00446 {
00447 BinaryFileSerializer::WriteFloatVector2(value.Translation());
00448 if (Error() != IOE_NONE)
00449 return;
00450
00451 BinaryFileSerializer::WriteFloatVector2(value.ScaleFactors());
00452 if (Error() != IOE_NONE)
00453 return;
00454
00455 BinaryFileSerializer::WriteFloat(value.Angle());
00456 if (Error() != IOE_NONE)
00457 return;
00458
00459 BinaryFileSerializer::WriteBool(value.PostTranslate());
00460 }
00461
00462 }