From 264f825549ecd23046ad049733870d0516d59e89 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 2 Jul 2006 07:27:33 +0000 Subject: Fixed a bug in the serializer that would close the file it was using even when it shouldn't, and added the serializerbzip2, which lets you read and write to compressed streams. --- src/serializerbzip2.cpp | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 src/serializerbzip2.cpp (limited to 'src/serializerbzip2.cpp') diff --git a/src/serializerbzip2.cpp b/src/serializerbzip2.cpp new file mode 100644 index 0000000..b899384 --- /dev/null +++ b/src/serializerbzip2.cpp @@ -0,0 +1,205 @@ +#include "serializerbzip2.h" +#include "serializable.h" + +#include + +SerializerBZip2::SerializerBZip2(FILE *fhFile, bool bLoading): + Serializer(bLoading), + fhFile(fhFile), + bCloseFile(false) +{ + if( bLoading ) + { + bzFile = BZ2_bzReadOpen( &bzerror, fhFile, 0, 0, NULL, 0 ); + checkBZError(); + } + else + { + bzFile = BZ2_bzWriteOpen( &bzerror, fhFile, 9, 0, 0 ); + checkBZError(); + } +} + +SerializerBZip2::SerializerBZip2(char *sFileName, bool bLoading): + Serializer(bLoading), + bCloseFile(true) +{ + if (bLoading) + { + fhFile = fopen(sFileName, "rb"); + bzFile = BZ2_bzReadOpen( &bzerror, fhFile, 0, 0, NULL, 0 ); + checkBZError(); + } + else + { + fhFile = fopen(sFileName, "wb"); + bzFile = BZ2_bzWriteOpen( &bzerror, fhFile, 9, 0, 0 ); + checkBZError(); + } +} + +SerializerBZip2::~SerializerBZip2() +{ + close(); +} + +void SerializerBZip2::checkBZError() +{ +} + +void SerializerBZip2::close() +{ + if( bzFile != NULL ) + { + if( isLoading() ) + { + BZ2_bzReadClose( &bzerror, bzFile ); + } + else + { + BZ2_bzWriteClose( &bzerror, bzFile, 0, 0, 0 ); + } + checkBZError(); + bzFile = NULL; + } + if( fhFile != NULL && bCloseFile ) + { + fclose(fhFile); + fhFile = NULL; + } +} + +void SerializerBZip2::write(const void * pData, int32_t nSize) +{ + BZ2_bzWrite( &bzerror, bzFile, (void *)pData, nSize ); + checkBZError(); +} + +void SerializerBZip2::read(void * pData, int32_t nSize) +{ + BZ2_bzRead( &bzerror, bzFile, pData, nSize ); + checkBZError(); +} + +Serializer &SerializerBZip2::operator<<(bool p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(int8_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(int16_t p) +{ + write (&p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(int32_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(int64_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(uint8_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(uint16_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(uint32_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(uint64_t p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(float p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(double p) +{ + write( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator<<(long double p) +{ + write( &p, sizeof(p) ); + return *this; +} + +Serializer &SerializerBZip2::operator>>(bool &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(int8_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(int16_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(int32_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(int64_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(uint8_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(uint16_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(uint32_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(uint64_t &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(float &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(double &p) +{ + read( &p, sizeof(p) ); + return *this; +} +Serializer &SerializerBZip2::operator>>(long double &p) +{ + read( &p, sizeof(p) ); + return *this; +} + -- cgit v1.2.3