diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-07-02 07:27:33 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-07-02 07:27:33 +0000 |
commit | 264f825549ecd23046ad049733870d0516d59e89 (patch) | |
tree | d16fab5f050a69aafac95bd6c134babda6e25c06 /src/serializerbzip2.cpp | |
parent | d121afeeab6c7a52ae0964f5dc66cc6a3d8cf3ca (diff) | |
download | libbu++-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.cpp | 205 |
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 | |||
6 | SerializerBZip2::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 | |||
23 | SerializerBZip2::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 | |||
41 | SerializerBZip2::~SerializerBZip2() | ||
42 | { | ||
43 | close(); | ||
44 | } | ||
45 | |||
46 | void SerializerBZip2::checkBZError() | ||
47 | { | ||
48 | } | ||
49 | |||
50 | void 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 | |||
72 | void SerializerBZip2::write(const void * pData, int32_t nSize) | ||
73 | { | ||
74 | BZ2_bzWrite( &bzerror, bzFile, (void *)pData, nSize ); | ||
75 | checkBZError(); | ||
76 | } | ||
77 | |||
78 | void SerializerBZip2::read(void * pData, int32_t nSize) | ||
79 | { | ||
80 | BZ2_bzRead( &bzerror, bzFile, pData, nSize ); | ||
81 | checkBZError(); | ||
82 | } | ||
83 | |||
84 | Serializer &SerializerBZip2::operator<<(bool p) | ||
85 | { | ||
86 | write( &p, sizeof(p) ); | ||
87 | return *this; | ||
88 | } | ||
89 | Serializer &SerializerBZip2::operator<<(int8_t p) | ||
90 | { | ||
91 | write( &p, sizeof(p) ); | ||
92 | return *this; | ||
93 | } | ||
94 | Serializer &SerializerBZip2::operator<<(int16_t p) | ||
95 | { | ||
96 | write (&p, sizeof(p) ); | ||
97 | return *this; | ||
98 | } | ||
99 | Serializer &SerializerBZip2::operator<<(int32_t p) | ||
100 | { | ||
101 | write( &p, sizeof(p) ); | ||
102 | return *this; | ||
103 | } | ||
104 | Serializer &SerializerBZip2::operator<<(int64_t p) | ||
105 | { | ||
106 | write( &p, sizeof(p) ); | ||
107 | return *this; | ||
108 | } | ||
109 | Serializer &SerializerBZip2::operator<<(uint8_t p) | ||
110 | { | ||
111 | write( &p, sizeof(p) ); | ||
112 | return *this; | ||
113 | } | ||
114 | Serializer &SerializerBZip2::operator<<(uint16_t p) | ||
115 | { | ||
116 | write( &p, sizeof(p) ); | ||
117 | return *this; | ||
118 | } | ||
119 | Serializer &SerializerBZip2::operator<<(uint32_t p) | ||
120 | { | ||
121 | write( &p, sizeof(p) ); | ||
122 | return *this; | ||
123 | } | ||
124 | Serializer &SerializerBZip2::operator<<(uint64_t p) | ||
125 | { | ||
126 | write( &p, sizeof(p) ); | ||
127 | return *this; | ||
128 | } | ||
129 | Serializer &SerializerBZip2::operator<<(float p) | ||
130 | { | ||
131 | write( &p, sizeof(p) ); | ||
132 | return *this; | ||
133 | } | ||
134 | Serializer &SerializerBZip2::operator<<(double p) | ||
135 | { | ||
136 | write( &p, sizeof(p) ); | ||
137 | return *this; | ||
138 | } | ||
139 | Serializer &SerializerBZip2::operator<<(long double p) | ||
140 | { | ||
141 | write( &p, sizeof(p) ); | ||
142 | return *this; | ||
143 | } | ||
144 | |||
145 | Serializer &SerializerBZip2::operator>>(bool &p) | ||
146 | { | ||
147 | read( &p, sizeof(p) ); | ||
148 | return *this; | ||
149 | } | ||
150 | Serializer &SerializerBZip2::operator>>(int8_t &p) | ||
151 | { | ||
152 | read( &p, sizeof(p) ); | ||
153 | return *this; | ||
154 | } | ||
155 | Serializer &SerializerBZip2::operator>>(int16_t &p) | ||
156 | { | ||
157 | read( &p, sizeof(p) ); | ||
158 | return *this; | ||
159 | } | ||
160 | Serializer &SerializerBZip2::operator>>(int32_t &p) | ||
161 | { | ||
162 | read( &p, sizeof(p) ); | ||
163 | return *this; | ||
164 | } | ||
165 | Serializer &SerializerBZip2::operator>>(int64_t &p) | ||
166 | { | ||
167 | read( &p, sizeof(p) ); | ||
168 | return *this; | ||
169 | } | ||
170 | Serializer &SerializerBZip2::operator>>(uint8_t &p) | ||
171 | { | ||
172 | read( &p, sizeof(p) ); | ||
173 | return *this; | ||
174 | } | ||
175 | Serializer &SerializerBZip2::operator>>(uint16_t &p) | ||
176 | { | ||
177 | read( &p, sizeof(p) ); | ||
178 | return *this; | ||
179 | } | ||
180 | Serializer &SerializerBZip2::operator>>(uint32_t &p) | ||
181 | { | ||
182 | read( &p, sizeof(p) ); | ||
183 | return *this; | ||
184 | } | ||
185 | Serializer &SerializerBZip2::operator>>(uint64_t &p) | ||
186 | { | ||
187 | read( &p, sizeof(p) ); | ||
188 | return *this; | ||
189 | } | ||
190 | Serializer &SerializerBZip2::operator>>(float &p) | ||
191 | { | ||
192 | read( &p, sizeof(p) ); | ||
193 | return *this; | ||
194 | } | ||
195 | Serializer &SerializerBZip2::operator>>(double &p) | ||
196 | { | ||
197 | read( &p, sizeof(p) ); | ||
198 | return *this; | ||
199 | } | ||
200 | Serializer &SerializerBZip2::operator>>(long double &p) | ||
201 | { | ||
202 | read( &p, sizeof(p) ); | ||
203 | return *this; | ||
204 | } | ||
205 | |||