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 | |
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 'src')
-rw-r--r-- | src/serializerbinary.cpp | 2 | ||||
-rw-r--r-- | src/serializerbzip2.cpp | 205 | ||||
-rw-r--r-- | src/serializerbzip2.h | 53 |
3 files changed, 259 insertions, 1 deletions
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() | |||
29 | 29 | ||
30 | void SerializerBinary::close() | 30 | void SerializerBinary::close() |
31 | { | 31 | { |
32 | if (fhFile != NULL) | 32 | if (fhFile != NULL && bCloseFile ) |
33 | { | 33 | { |
34 | fclose(fhFile); | 34 | fclose(fhFile); |
35 | fhFile = NULL; | 35 | 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 @@ | |||
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 | |||
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 @@ | |||
1 | #ifndef SERIALIZER_BINARY_H | ||
2 | #define SERIALIZER_BINARY_H | ||
3 | |||
4 | #include "serializer.h" | ||
5 | #include <stdio.h> | ||
6 | |||
7 | class SerializerBZip2 : public Serializer | ||
8 | { | ||
9 | public: | ||
10 | SerializerBZip2(FILE *fhFile, bool bLoading); | ||
11 | SerializerBZip2(char *sFileName, bool bLoading); | ||
12 | ~SerializerBZip2(); | ||
13 | |||
14 | virtual void close(); | ||
15 | |||
16 | virtual void write(const void *, int32_t); | ||
17 | virtual void read(void *, int32_t); | ||
18 | |||
19 | virtual Serializer &operator<<(bool); | ||
20 | virtual Serializer &operator<<(int8_t); | ||
21 | virtual Serializer &operator<<(int16_t); | ||
22 | virtual Serializer &operator<<(int32_t); | ||
23 | virtual Serializer &operator<<(int64_t); | ||
24 | virtual Serializer &operator<<(uint8_t); | ||
25 | virtual Serializer &operator<<(uint16_t); | ||
26 | virtual Serializer &operator<<(uint32_t); | ||
27 | virtual Serializer &operator<<(uint64_t); | ||
28 | virtual Serializer &operator<<(float); | ||
29 | virtual Serializer &operator<<(double); | ||
30 | virtual Serializer &operator<<(long double); | ||
31 | |||
32 | virtual Serializer &operator>>(bool &); | ||
33 | virtual Serializer &operator>>(int8_t &); | ||
34 | virtual Serializer &operator>>(int16_t &); | ||
35 | virtual Serializer &operator>>(int32_t &); | ||
36 | virtual Serializer &operator>>(int64_t &); | ||
37 | virtual Serializer &operator>>(uint8_t &); | ||
38 | virtual Serializer &operator>>(uint16_t &); | ||
39 | virtual Serializer &operator>>(uint32_t &); | ||
40 | virtual Serializer &operator>>(uint64_t &); | ||
41 | virtual Serializer &operator>>(float &); | ||
42 | virtual Serializer &operator>>(double &); | ||
43 | virtual Serializer &operator>>(long double &); | ||
44 | |||
45 | private: | ||
46 | void checkBZError(); | ||
47 | FILE *fhFile; | ||
48 | void *bzFile; | ||
49 | bool bCloseFile; | ||
50 | int bzerror; | ||
51 | }; | ||
52 | |||
53 | #endif | ||