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/serializerbinary.cpp | 2 +- src/serializerbzip2.cpp | 205 +++++++++++++++++++++++++++++++++++++++++++++++ src/serializerbzip2.h | 53 ++++++++++++ 3 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 src/serializerbzip2.cpp create mode 100644 src/serializerbzip2.h diff --git a/src/serializerbinary.cpp b/src/serializerbinary.cpp index 4b741ae..f2d8371 100644 --- a/src/serializerbinary.cpp +++ b/src/serializerbinary.cpp @@ -29,7 +29,7 @@ SerializerBinary::~SerializerBinary() void SerializerBinary::close() { - if (fhFile != NULL) + if (fhFile != NULL && bCloseFile ) { fclose(fhFile); fhFile = NULL; 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; +} + diff --git a/src/serializerbzip2.h b/src/serializerbzip2.h new file mode 100644 index 0000000..2d646f2 --- /dev/null +++ b/src/serializerbzip2.h @@ -0,0 +1,53 @@ +#ifndef SERIALIZER_BINARY_H +#define SERIALIZER_BINARY_H + +#include "serializer.h" +#include + +class SerializerBZip2 : public Serializer +{ +public: + SerializerBZip2(FILE *fhFile, bool bLoading); + SerializerBZip2(char *sFileName, bool bLoading); + ~SerializerBZip2(); + + virtual void close(); + + virtual void write(const void *, int32_t); + virtual void read(void *, int32_t); + + virtual Serializer &operator<<(bool); + virtual Serializer &operator<<(int8_t); + virtual Serializer &operator<<(int16_t); + virtual Serializer &operator<<(int32_t); + virtual Serializer &operator<<(int64_t); + virtual Serializer &operator<<(uint8_t); + virtual Serializer &operator<<(uint16_t); + virtual Serializer &operator<<(uint32_t); + virtual Serializer &operator<<(uint64_t); + virtual Serializer &operator<<(float); + virtual Serializer &operator<<(double); + virtual Serializer &operator<<(long double); + + virtual Serializer &operator>>(bool &); + virtual Serializer &operator>>(int8_t &); + virtual Serializer &operator>>(int16_t &); + virtual Serializer &operator>>(int32_t &); + virtual Serializer &operator>>(int64_t &); + virtual Serializer &operator>>(uint8_t &); + virtual Serializer &operator>>(uint16_t &); + virtual Serializer &operator>>(uint32_t &); + virtual Serializer &operator>>(uint64_t &); + virtual Serializer &operator>>(float &); + virtual Serializer &operator>>(double &); + virtual Serializer &operator>>(long double &); + +private: + void checkBZError(); + FILE *fhFile; + void *bzFile; + bool bCloseFile; + int bzerror; +}; + +#endif -- cgit v1.2.3