aboutsummaryrefslogtreecommitdiff
path: root/src/serializerbzip2.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-07-02 07:27:33 +0000
committerMike Buland <eichlan@xagasoft.com>2006-07-02 07:27:33 +0000
commit264f825549ecd23046ad049733870d0516d59e89 (patch)
treed16fab5f050a69aafac95bd6c134babda6e25c06 /src/serializerbzip2.cpp
parentd121afeeab6c7a52ae0964f5dc66cc6a3d8cf3ca (diff)
downloadlibbu++-264f825549ecd23046ad049733870d0516d59e89.tar.gz
libbu++-264f825549ecd23046ad049733870d0516d59e89.tar.bz2
libbu++-264f825549ecd23046ad049733870d0516d59e89.tar.xz
libbu++-264f825549ecd23046ad049733870d0516d59e89.zip
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.
Diffstat (limited to '')
-rw-r--r--src/serializerbzip2.cpp205
1 files changed, 205 insertions, 0 deletions
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 @@
1#include "serializerbzip2.h"
2#include "serializable.h"
3
4#include <bzlib.h>
5
6SerializerBZip2::SerializerBZip2(FILE *fhFile, bool bLoading):
7 Serializer(bLoading),
8 fhFile(fhFile),
9 bCloseFile(false)
10{
11 if( bLoading )
12 {
13 bzFile = BZ2_bzReadOpen( &bzerror, fhFile, 0, 0, NULL, 0 );
14 checkBZError();
15 }
16 else
17 {
18 bzFile = BZ2_bzWriteOpen( &bzerror, fhFile, 9, 0, 0 );
19 checkBZError();
20 }
21}
22
23SerializerBZip2::SerializerBZip2(char *sFileName, bool bLoading):
24 Serializer(bLoading),
25 bCloseFile(true)
26{
27 if (bLoading)
28 {
29 fhFile = fopen(sFileName, "rb");
30 bzFile = BZ2_bzReadOpen( &bzerror, fhFile, 0, 0, NULL, 0 );
31 checkBZError();
32 }
33 else
34 {
35 fhFile = fopen(sFileName, "wb");
36 bzFile = BZ2_bzWriteOpen( &bzerror, fhFile, 9, 0, 0 );
37 checkBZError();
38 }
39}
40
41SerializerBZip2::~SerializerBZip2()
42{
43 close();
44}
45
46void SerializerBZip2::checkBZError()
47{
48}
49
50void SerializerBZip2::close()
51{
52 if( bzFile != NULL )
53 {
54 if( isLoading() )
55 {
56 BZ2_bzReadClose( &bzerror, bzFile );
57 }
58 else
59 {
60 BZ2_bzWriteClose( &bzerror, bzFile, 0, 0, 0 );
61 }
62 checkBZError();
63 bzFile = NULL;
64 }
65 if( fhFile != NULL && bCloseFile )
66 {
67 fclose(fhFile);
68 fhFile = NULL;
69 }
70}
71
72void SerializerBZip2::write(const void * pData, int32_t nSize)
73{
74 BZ2_bzWrite( &bzerror, bzFile, (void *)pData, nSize );
75 checkBZError();
76}
77
78void SerializerBZip2::read(void * pData, int32_t nSize)
79{
80 BZ2_bzRead( &bzerror, bzFile, pData, nSize );
81 checkBZError();
82}
83
84Serializer &SerializerBZip2::operator<<(bool p)
85{
86 write( &p, sizeof(p) );
87 return *this;
88}
89Serializer &SerializerBZip2::operator<<(int8_t p)
90{
91 write( &p, sizeof(p) );
92 return *this;
93}
94Serializer &SerializerBZip2::operator<<(int16_t p)
95{
96 write (&p, sizeof(p) );
97 return *this;
98}
99Serializer &SerializerBZip2::operator<<(int32_t p)
100{
101 write( &p, sizeof(p) );
102 return *this;
103}
104Serializer &SerializerBZip2::operator<<(int64_t p)
105{
106 write( &p, sizeof(p) );
107 return *this;
108}
109Serializer &SerializerBZip2::operator<<(uint8_t p)
110{
111 write( &p, sizeof(p) );
112 return *this;
113}
114Serializer &SerializerBZip2::operator<<(uint16_t p)
115{
116 write( &p, sizeof(p) );
117 return *this;
118}
119Serializer &SerializerBZip2::operator<<(uint32_t p)
120{
121 write( &p, sizeof(p) );
122 return *this;
123}
124Serializer &SerializerBZip2::operator<<(uint64_t p)
125{
126 write( &p, sizeof(p) );
127 return *this;
128}
129Serializer &SerializerBZip2::operator<<(float p)
130{
131 write( &p, sizeof(p) );
132 return *this;
133}
134Serializer &SerializerBZip2::operator<<(double p)
135{
136 write( &p, sizeof(p) );
137 return *this;
138}
139Serializer &SerializerBZip2::operator<<(long double p)
140{
141 write( &p, sizeof(p) );
142 return *this;
143}
144
145Serializer &SerializerBZip2::operator>>(bool &p)
146{
147 read( &p, sizeof(p) );
148 return *this;
149}
150Serializer &SerializerBZip2::operator>>(int8_t &p)
151{
152 read( &p, sizeof(p) );
153 return *this;
154}
155Serializer &SerializerBZip2::operator>>(int16_t &p)
156{
157 read( &p, sizeof(p) );
158 return *this;
159}
160Serializer &SerializerBZip2::operator>>(int32_t &p)
161{
162 read( &p, sizeof(p) );
163 return *this;
164}
165Serializer &SerializerBZip2::operator>>(int64_t &p)
166{
167 read( &p, sizeof(p) );
168 return *this;
169}
170Serializer &SerializerBZip2::operator>>(uint8_t &p)
171{
172 read( &p, sizeof(p) );
173 return *this;
174}
175Serializer &SerializerBZip2::operator>>(uint16_t &p)
176{
177 read( &p, sizeof(p) );
178 return *this;
179}
180Serializer &SerializerBZip2::operator>>(uint32_t &p)
181{
182 read( &p, sizeof(p) );
183 return *this;
184}
185Serializer &SerializerBZip2::operator>>(uint64_t &p)
186{
187 read( &p, sizeof(p) );
188 return *this;
189}
190Serializer &SerializerBZip2::operator>>(float &p)
191{
192 read( &p, sizeof(p) );
193 return *this;
194}
195Serializer &SerializerBZip2::operator>>(double &p)
196{
197 read( &p, sizeof(p) );
198 return *this;
199}
200Serializer &SerializerBZip2::operator>>(long double &p)
201{
202 read( &p, sizeof(p) );
203 return *this;
204}
205