aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-10-23 07:43:50 +0000
committerMike Buland <eichlan@xagasoft.com>2011-10-23 07:43:50 +0000
commitda1e0ef0772b078bd295301bd675afdee00d40e9 (patch)
tree7d1703bbb5c2d76e6e6300e51f0ed1e09704af4f /src
parent208b983734d7431699f4bd3534e08321e42ada86 (diff)
downloadlibbu++-da1e0ef0772b078bd295301bd675afdee00d40e9.tar.gz
libbu++-da1e0ef0772b078bd295301bd675afdee00d40e9.tar.bz2
libbu++-da1e0ef0772b078bd295301bd675afdee00d40e9.tar.xz
libbu++-da1e0ef0772b078bd295301bd675afdee00d40e9.zip
Switched ito* to synchro*, except the server, I'm thinking of takeing the core
in a different direction anyway. Added the Deflate class, it uses zlib, and can do raw (headerless) deflate streams, zlib format, or gzip format. It's easy to use and quite versitile.
Diffstat (limited to 'src')
-rw-r--r--src/bzip2.cpp2
-rw-r--r--src/conduit.h8
-rw-r--r--src/deflate.cpp242
-rw-r--r--src/deflate.h63
-rw-r--r--src/itoserver.h6
-rw-r--r--src/synchroatom.h (renamed from src/itoatom.h)17
-rw-r--r--src/synchrocounter.cpp (renamed from src/itocounter.cpp)2
-rw-r--r--src/synchrocounter.h (renamed from src/itocounter.h)12
-rw-r--r--src/synchroheap.cpp (renamed from src/itoheap.cpp)2
-rw-r--r--src/synchroheap.h (renamed from src/itoheap.h)21
-rw-r--r--src/synchroqueue.h (renamed from src/itoqueue.h)16
-rw-r--r--src/tests/deflate.cpp53
12 files changed, 399 insertions, 45 deletions
diff --git a/src/bzip2.cpp b/src/bzip2.cpp
index 5c35a26..0ff5444 100644
--- a/src/bzip2.cpp
+++ b/src/bzip2.cpp
@@ -33,7 +33,7 @@ void Bu::BZip2::start()
33 bzState.bzfree = NULL; 33 bzState.bzfree = NULL;
34 bzState.opaque = NULL; 34 bzState.opaque = NULL;
35 35
36 nBufSize = 50000; 36 nBufSize = 64*1024;
37 pBuf = new char[nBufSize]; 37 pBuf = new char[nBufSize];
38} 38}
39 39
diff --git a/src/conduit.h b/src/conduit.h
index 72b8d52..9babaaf 100644
--- a/src/conduit.h
+++ b/src/conduit.h
@@ -11,8 +11,8 @@
11#include "bu/stream.h" 11#include "bu/stream.h"
12#include "bu/string.h" 12#include "bu/string.h"
13#include "bu/queuebuf.h" 13#include "bu/queuebuf.h"
14#include "bu/itomutex.h" 14#include "bu/mutex.h"
15#include "bu/itocondition.h" 15#include "bu/condition.h"
16 16
17namespace Bu 17namespace Bu
18{ 18{
@@ -54,8 +54,8 @@ namespace Bu
54 54
55 private: 55 private:
56 QueueBuf qb; 56 QueueBuf qb;
57 mutable ItoMutex im; 57 mutable Mutex im;
58 ItoCondition cBlock; 58 Condition cBlock;
59 bool bBlocking; 59 bool bBlocking;
60 bool bOpen; 60 bool bOpen;
61 }; 61 };
diff --git a/src/deflate.cpp b/src/deflate.cpp
new file mode 100644
index 0000000..aec2a18
--- /dev/null
+++ b/src/deflate.cpp
@@ -0,0 +1,242 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/deflate.h"
9#include "bu/trace.h"
10
11using namespace Bu;
12
13Bu::Deflate::Deflate( Bu::Stream &rNext, int nCompression, Format eFmt ) :
14 Bu::Filter( rNext ),
15 nCompression( nCompression ),
16 sTotalOut( 0 ),
17 eFmt( eFmt ),
18 bEos( false )
19{
20 TRACE( nCompression );
21 start();
22}
23
24Bu::Deflate::~Deflate()
25{
26 TRACE();
27 stop();
28}
29
30void Bu::Deflate::start()
31{
32 TRACE();
33 zState.zalloc = NULL;
34 zState.zfree = NULL;
35 zState.opaque = NULL;
36 zState.state = NULL;
37
38 nBufSize = 64*1024;
39 pBuf = new char[nBufSize];
40}
41
42Bu::size Bu::Deflate::stop()
43{
44 TRACE();
45 if( zState.state )
46 {
47 if( bReading )
48 {
49 inflateEnd( &zState );
50 delete[] pBuf;
51 pBuf = NULL;
52 return 0;
53 }
54 else
55 {
56 for(;;)
57 {
58 zState.next_in = NULL;
59 zState.avail_in = 0;
60 zState.avail_out = nBufSize;
61 zState.next_out = (Bytef *)pBuf;
62 int res = deflate( &zState, Z_FINISH );
63 if( zState.avail_out < nBufSize )
64 {
65 sTotalOut += rNext.write( pBuf, nBufSize-zState.avail_out );
66 }
67 if( res == Z_STREAM_END )
68 break;
69 }
70 deflateEnd( &zState );
71 delete[] pBuf;
72 pBuf = NULL;
73 return sTotalOut;
74 }
75 }
76 return 0;
77}
78
79void Bu::Deflate::zError( int code )
80{
81 TRACE( code );
82 switch( code )
83 {
84 case Z_OK:
85 case Z_STREAM_END:
86 case Z_NEED_DICT:
87 return;
88
89 case Z_ERRNO:
90 throw ExceptionBase("Deflate: Errno - %s", zState.msg );
91
92 case Z_STREAM_ERROR:
93 throw ExceptionBase("Deflate: Stream Error - %s", zState.msg );
94
95 case Z_DATA_ERROR:
96 throw ExceptionBase("Deflate: Data Error - %s", zState.msg );
97
98 case Z_MEM_ERROR:
99 throw ExceptionBase("Deflate: Mem Error - %s", zState.msg );
100
101 case Z_BUF_ERROR:
102 throw ExceptionBase("Deflate: Buf Error - %s", zState.msg );
103
104 case Z_VERSION_ERROR:
105 throw ExceptionBase("Deflate: Version Error - %s", zState.msg );
106
107 default:
108 throw ExceptionBase("Deflate: Unknown error encountered - %s.", zState.msg );
109
110 }
111}
112
113Bu::size Bu::Deflate::read( void *pData, Bu::size nBytes )
114{
115 TRACE( pData, nBytes );
116 if( !zState.state )
117 {
118 bReading = true;
119 if( eFmt&AutoDetect )
120 inflateInit2( &zState, 32+15 ); // Auto-detect, large window
121 else if( eFmt == Raw )
122 inflateInit2( &zState, -15 ); // Raw
123 else if( eFmt == Zlib )
124 inflateInit2( &zState, 15 ); // Zlib
125 else if( eFmt == Gzip )
126 inflateInit2( &zState, 16+15 ); // GZip
127 else
128 throw Bu::ExceptionBase("Format mode for deflate read.");
129 zState.next_in = (Bytef *)pBuf;
130 zState.avail_in = 0;
131 }
132 if( bReading == false )
133 throw ExceptionBase("This deflate filter is in writing mode, you can't read.");
134
135 int nRead = 0;
136 int nReadTotal = zState.total_out;
137 zState.next_out = (Bytef *)pData;
138 zState.avail_out = nBytes;
139 for(;;)
140 {
141 int ret = inflate( &zState, Z_NO_FLUSH );
142 printf("inflate returned %d; avail in=%d, out=%d\n", ret,
143 zState.avail_in, zState.avail_out );
144
145 nReadTotal += nRead-zState.avail_out;
146
147 if( ret == Z_STREAM_END )
148 {
149 bEos = true;
150 if( zState.avail_in > 0 )
151 {
152 if( rNext.isSeekable() )
153 {
154 rNext.seek( -zState.avail_in );
155 }
156 }
157 return nBytes-zState.avail_out;
158 }
159 if( ret != Z_BUF_ERROR )
160 zError( ret );
161
162 if( zState.avail_out )
163 {
164 if( zState.avail_in == 0 )
165 {
166 nRead = rNext.read( pBuf, nBufSize );
167 if( nRead == 0 && rNext.isEos() )
168 {
169 throw Bu::ExceptionBase("Premature end of underlying "
170 "stream found reading deflate stream.");
171 }
172 zState.next_in = (Bytef *)pBuf;
173 zState.avail_in = nRead;
174 }
175 }
176 else
177 {
178 return nBytes-zState.avail_out;
179 }
180 }
181 return 0;
182}
183
184Bu::size Bu::Deflate::write( const void *pData, Bu::size nBytes )
185{
186 TRACE( pData, nBytes );
187 if( !zState.state )
188 {
189 bReading = false;
190 int iFmt = eFmt&Gzip;
191 if( iFmt == Raw )
192 deflateInit2( &zState, nCompression, Z_DEFLATED, -15, 9,
193 Z_DEFAULT_STRATEGY );
194 else if( iFmt == Zlib )
195 deflateInit2( &zState, nCompression, Z_DEFLATED, 15, 9,
196 Z_DEFAULT_STRATEGY );
197 else if( iFmt == Gzip )
198 deflateInit2( &zState, nCompression, Z_DEFLATED, 16+15, 9,
199 Z_DEFAULT_STRATEGY );
200 else
201 throw Bu::ExceptionBase("Invalid format for deflate.");
202 }
203 if( bReading == true )
204 throw ExceptionBase("This deflate filter is in reading mode, you can't write.");
205
206 zState.next_in = (Bytef *)pData;
207 zState.avail_in = nBytes;
208 for(;;)
209 {
210 zState.avail_out = nBufSize;
211 zState.next_out = (Bytef *)pBuf;
212
213 zError( deflate( &zState, Z_NO_FLUSH ) );
214
215 if( zState.avail_out < nBufSize )
216 {
217 sTotalOut += rNext.write( pBuf, nBufSize-zState.avail_out );
218 }
219 if( zState.avail_in == 0 )
220 break;
221 }
222
223 return nBytes;
224}
225
226bool Bu::Deflate::isOpen()
227{
228 TRACE();
229 return (zState.state != NULL);
230}
231
232bool Bu::Deflate::isEos()
233{
234 TRACE();
235 return bEos;
236}
237
238Bu::size Bu::Deflate::getCompressedSize()
239{
240 return sTotalOut;
241}
242
diff --git a/src/deflate.h b/src/deflate.h
new file mode 100644
index 0000000..cab9b51
--- /dev/null
+++ b/src/deflate.h
@@ -0,0 +1,63 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_DEFLATE_H
9#define BU_DEFLATE_H
10
11#include <stdint.h>
12#include <zlib.h>
13
14#include "bu/filter.h"
15
16namespace Bu
17{
18 /**
19 *
20 *@ingroup Streams
21 */
22 class Deflate : public Bu::Filter
23 {
24 public:
25 enum Format
26 {
27 Raw = 0x01,
28 Zlib = 0x02,
29 Gzip = 0x03,
30 AutoDetect = 0x04,
31
32 AutoRaw = 0x04|0x01,
33 AutoZlib = 0x04|0x02,
34 AutoGzip = 0x04|0x03
35 };
36
37 Deflate( Bu::Stream &rNext, int nCompression=9, Format eFmt=AutoRaw );
38 virtual ~Deflate();
39
40 virtual void start();
41 virtual Bu::size stop();
42 virtual Bu::size read( void *pBuf, Bu::size nBytes );
43 virtual Bu::size write( const void *pBuf, Bu::size nBytes );
44
45 virtual bool isOpen();
46 virtual bool isEos();
47
48 Bu::size getCompressedSize();
49
50 private:
51 void zError( int code );
52 z_stream zState;
53 bool bReading;
54 int nCompression;
55 char *pBuf;
56 uint32_t nBufSize;
57 Bu::size sTotalOut;
58 Format eFmt;
59 bool bEos;
60 };
61}
62
63#endif
diff --git a/src/itoserver.h b/src/itoserver.h
index 75b3349..b1f5479 100644
--- a/src/itoserver.h
+++ b/src/itoserver.h
@@ -18,7 +18,7 @@
18#include "bu/list.h" 18#include "bu/list.h"
19#include "bu/thread.h" 19#include "bu/thread.h"
20#include "bu/mutex.h" 20#include "bu/mutex.h"
21#include "bu/itoqueue.h" 21#include "bu/synchroqueue.h"
22#include "bu/set.h" 22#include "bu/set.h"
23 23
24#include "bu/clientlink.h" 24#include "bu/clientlink.h"
@@ -82,7 +82,7 @@ namespace Bu
82 int nTimeoutSec, int nTimeoutUSec ); 82 int nTimeoutSec, int nTimeoutUSec );
83 virtual ~ItoClient(); 83 virtual ~ItoClient();
84 84
85 typedef ItoQueue<Bu::String *> StringQueue; 85 typedef SynchroQueue<Bu::String *> StringQueue;
86 StringQueue qMsg; 86 StringQueue qMsg;
87 87
88 protected: 88 protected:
@@ -129,7 +129,7 @@ namespace Bu
129 typedef Hash<int,TcpServerSocket *> ServerHash; 129 typedef Hash<int,TcpServerSocket *> ServerHash;
130 ServerHash hServers; 130 ServerHash hServers;
131 typedef Hash<int,ItoClient *> ClientHash; 131 typedef Hash<int,ItoClient *> ClientHash;
132 typedef ItoQueue<ItoClient *> ClientQueue; 132 typedef SynchroQueue<ItoClient *> ClientQueue;
133 ClientHash hClients; 133 ClientHash hClients;
134 ClientQueue qClientCleanup; 134 ClientQueue qClientCleanup;
135 Mutex imClients; 135 Mutex imClients;
diff --git a/src/itoatom.h b/src/synchroatom.h
index 3659f4e..fb02054 100644
--- a/src/itoatom.h
+++ b/src/synchroatom.h
@@ -5,13 +5,12 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_ATOM_H 8#ifndef BU_SYNCHRO_ATOM_H
9#define BU_ITO_ATOM_H 9#define BU_SYNCHRO_ATOM_H
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
13#include "itomutex.h" 13#include "bu/mutex.h"
14#include "itocondition.h"
15 14
16namespace Bu 15namespace Bu
17{ 16{
@@ -20,22 +19,22 @@ namespace Bu
20 *@ingroup Threading 19 *@ingroup Threading
21 */ 20 */
22 template <class T> 21 template <class T>
23 class ItoAtom 22 class SynchroAtom
24 { 23 {
25 public: 24 public:
26 /** 25 /**
27 * Construct an empty queue. 26 * Construct an empty queue.
28 */ 27 */
29 ItoAtom() 28 SynchroAtom()
30 { 29 {
31 } 30 }
32 31
33 ItoAtom( const T &src ) : 32 SynchroAtom( const T &src ) :
34 data( src ) 33 data( src )
35 { 34 {
36 } 35 }
37 36
38 ~ItoAtom() 37 ~SynchroAtom()
39 { 38 {
40 } 39 }
41 40
@@ -57,7 +56,7 @@ namespace Bu
57 private: 56 private:
58 T data; 57 T data;
59 58
60 ItoMutex mOperate; /**< The master mutex, used on all operations. */ 59 Mutex mOperate; /**< The master mutex, used on all operations. */
61 }; 60 };
62}; 61};
63 62
diff --git a/src/itocounter.cpp b/src/synchrocounter.cpp
index 0c6e06c..48bbe21 100644
--- a/src/itocounter.cpp
+++ b/src/synchrocounter.cpp
@@ -5,4 +5,4 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#include "bu/itocounter.h" 8#include "bu/synchrocounter.h"
diff --git a/src/itocounter.h b/src/synchrocounter.h
index 10126a5..d201bee 100644
--- a/src/itocounter.h
+++ b/src/synchrocounter.h
@@ -5,10 +5,10 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_COUNTER_H 8#ifndef BU_SYNCHRO_COUNTER_H
9#define BU_ITO_COUNTER_H 9#define BU_SYNCHRO_COUNTER_H
10 10
11#include "mutex.h" 11#include "bu/mutex.h"
12 12
13namespace Bu 13namespace Bu
14{ 14{
@@ -18,15 +18,15 @@ namespace Bu
18 *@ingroup Threading Containers 18 *@ingroup Threading Containers
19 */ 19 */
20 template <class T> 20 template <class T>
21 class ItoCounter 21 class SynchroCounter
22 { 22 {
23 public: 23 public:
24 ItoCounter() : 24 SynchroCounter() :
25 tCounter( 0 ) 25 tCounter( 0 )
26 { 26 {
27 } 27 }
28 28
29 virtual ~ItoCounter() 29 virtual ~SynchroCounter()
30 { 30 {
31 } 31 }
32 32
diff --git a/src/itoheap.cpp b/src/synchroheap.cpp
index 21ccef8..5dcce33 100644
--- a/src/itoheap.cpp
+++ b/src/synchroheap.cpp
@@ -5,5 +5,5 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#include "bu/itoheap.h" 8#include "bu/synchroheap.h"
9 9
diff --git a/src/itoheap.h b/src/synchroheap.h
index a5aad05..4dd898d 100644
--- a/src/itoheap.h
+++ b/src/synchroheap.h
@@ -5,28 +5,25 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_HEAP_H 8#ifndef BU_SYNCHRO_HEAP_H
9#define BU_ITO_HEAP_H 9#define BU_SYNCHRO_HEAP_H
10 10
11#include "bu/heap.h" 11#include "bu/heap.h"
12#include "bu/itomutex.h" 12#include "bu/mutex.h"
13#include "bu/itocondition.h" 13#include "bu/condition.h"
14 14
15namespace Bu 15namespace Bu
16{ 16{
17 class ItoMutex;
18 class ItoCondition;
19
20 template<typename item, typename cmpfunc=__basicLTCmp<item>, 17 template<typename item, typename cmpfunc=__basicLTCmp<item>,
21 typename itemalloc=std::allocator<item> > 18 typename itemalloc=std::allocator<item> >
22 class ItoHeap 19 class SynchroHeap
23 { 20 {
24 public: 21 public:
25 ItoHeap() 22 SynchroHeap()
26 { 23 {
27 } 24 }
28 25
29 virtual ~ItoHeap() 26 virtual ~SynchroHeap()
30 { 27 {
31 } 28 }
32 29
@@ -145,8 +142,8 @@ namespace Bu
145 142
146 private: 143 private:
147 Heap< item, cmpfunc, itemalloc > hData; 144 Heap< item, cmpfunc, itemalloc > hData;
148 ItoMutex imData; 145 Mutex imData;
149 ItoCondition icBlock; 146 Condition icBlock;
150 }; 147 };
151}; 148};
152 149
diff --git a/src/itoqueue.h b/src/synchroqueue.h
index 039e09c..79d5e49 100644
--- a/src/itoqueue.h
+++ b/src/synchroqueue.h
@@ -5,19 +5,19 @@
5 * terms of the license contained in the file LICENSE. 5 * terms of the license contained in the file LICENSE.
6 */ 6 */
7 7
8#ifndef BU_ITO_QUEUE_H 8#ifndef BU_SYNCHRO_QUEUE_H
9#define BU_ITO_QUEUE_H 9#define BU_SYNCHRO_QUEUE_H
10 10
11#include <pthread.h> 11#include <pthread.h>
12 12
13#include "mutex.h" 13#include "bu/mutex.h"
14#include "condition.h" 14#include "bu/condition.h"
15 15
16namespace Bu 16namespace Bu
17{ 17{
18 /** 18 /**
19 * A thread-safe queue class. This class is a very simple queue with some 19 * A thread-safe queue class. This class is a very simple queue with some
20 * cool extra functionality for use with the Ito system. The main extra 20 * cool extra functionality for use with the Synchro system. The main extra
21 * that it provides is the option to either dequeue without blocking, with 21 * that it provides is the option to either dequeue without blocking, with
22 * infinite blocking, or with timed blocking, which will return a value if 22 * infinite blocking, or with timed blocking, which will return a value if
23 * something is enqueued within the specified time limit, or NULL if the 23 * something is enqueued within the specified time limit, or NULL if the
@@ -25,7 +25,7 @@ namespace Bu
25 *@ingroup Threading Containers 25 *@ingroup Threading Containers
26 */ 26 */
27 template <class T> 27 template <class T>
28 class ItoQueue 28 class SynchroQueue
29 { 29 {
30 private: 30 private:
31 /** 31 /**
@@ -41,7 +41,7 @@ namespace Bu
41 /** 41 /**
42 * Construct an empty queue. 42 * Construct an empty queue.
43 */ 43 */
44 ItoQueue() : 44 SynchroQueue() :
45 pStart( NULL ), 45 pStart( NULL ),
46 pEnd( NULL ), 46 pEnd( NULL ),
47 nSize( 0 ) 47 nSize( 0 )
@@ -54,7 +54,7 @@ namespace Bu
54 * pointers without cleaning up the memory they pointed to. Make sure 54 * pointers without cleaning up the memory they pointed to. Make sure
55 * you're queue is empty before allowing it to be destroyed! 55 * you're queue is empty before allowing it to be destroyed!
56 */ 56 */
57 ~ItoQueue() 57 ~SynchroQueue()
58 { 58 {
59 Item *pCur = pStart; 59 Item *pCur = pStart;
60 while( pCur ) 60 while( pCur )
diff --git a/src/tests/deflate.cpp b/src/tests/deflate.cpp
new file mode 100644
index 0000000..9796408
--- /dev/null
+++ b/src/tests/deflate.cpp
@@ -0,0 +1,53 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/deflate.h"
9#include "bu/file.h"
10
11int main( int argc, char *argv[] )
12{
13 if( argc < 3 )
14 {
15 printf("usage: %s <in> <out>\n", argv[0] );
16 return -1;
17 }
18
19 char buf[1024];
20 size_t nRead;
21
22 /*
23 Bu::File fin( argv[1], Bu::File::Read );
24 fin.seek( 4 );
25 Bu::Deflate def( fin );
26
27 Bu::File f( argv[2], Bu::File::WriteNew );
28
29 for(;;)
30 {
31 nRead = def.read( buf, 1024 );
32 if( nRead > 0 )
33 f.write( buf, nRead );
34 if( def.isEos() )
35 break;
36 }
37 */
38
39 Bu::File fin( argv[1], Bu::File::Read );
40
41 Bu::File f( argv[2], Bu::File::WriteNew );
42 Bu::Deflate def( f, 9, Bu::Deflate::Gzip );
43
44 for(;;)
45 {
46 nRead = fin.read( buf, 1024 );
47 if( nRead > 0 )
48 def.write( buf, nRead );
49 if( fin.isEos() )
50 break;
51 }
52}
53