aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-06-07 19:56:20 +0000
committerMike Buland <eichlan@xagasoft.com>2007-06-07 19:56:20 +0000
commit9e8a4944e50fab432012878c66e1bdac20649f76 (patch)
tree412b0d49ae4086bb330045f9956513397b866807
parentc2e3879b965d297604804f03271ac71c8c5c81f3 (diff)
downloadlibbu++-9e8a4944e50fab432012878c66e1bdac20649f76.tar.gz
libbu++-9e8a4944e50fab432012878c66e1bdac20649f76.tar.bz2
libbu++-9e8a4944e50fab432012878c66e1bdac20649f76.tar.xz
libbu++-9e8a4944e50fab432012878c66e1bdac20649f76.zip
The Stream Filter archetecture is finished, it's actually much cooler than I
had anticipated, and much cleaner. I'll have to add some documentation to it, because it's not really obvious how any of it fits together from the outset, although I have to say that the bzip2 test program is the easiest general bzip2 compression program I've ever made...it just goes :) Decompression in Bu::BZip2 isn't finished yet, but that's ok, it's coming soon.
Diffstat (limited to '')
-rw-r--r--build.conf2
-rw-r--r--src/bzip2.cpp123
-rw-r--r--src/bzip2.h35
-rw-r--r--src/entities/bu-class4
-rw-r--r--src/filter.cpp77
-rw-r--r--src/filter.h59
-rw-r--r--src/fstring.h6
-rw-r--r--src/old/paramproc.h153
-rw-r--r--src/paramproc.cpp (renamed from src/old/paramproc.cpp)98
-rw-r--r--src/paramproc.h156
-rw-r--r--src/stream.h3
-rw-r--r--src/tests/bzip2.cpp23
12 files changed, 533 insertions, 206 deletions
diff --git a/build.conf b/build.conf
index 35738e3..f493d67 100644
--- a/build.conf
+++ b/build.conf
@@ -61,6 +61,8 @@ filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"):
61 61
62"tests/plugin": set "LDFLAGS" += "-ldl" 62"tests/plugin": set "LDFLAGS" += "-ldl"
63 63
64"tests/bzip2": set "LDFLAGS" += "-lbz2"
65
64rule "exe": 66rule "exe":
65 matches regexp("(.*)\\.o$"), 67 matches regexp("(.*)\\.o$"),
66 aggregate toString(" "), 68 aggregate toString(" "),
diff --git a/src/bzip2.cpp b/src/bzip2.cpp
new file mode 100644
index 0000000..b8c0f74
--- /dev/null
+++ b/src/bzip2.cpp
@@ -0,0 +1,123 @@
1#include "bu/bzip2.h"
2#include "bu/exceptions.h"
3
4using namespace Bu;
5
6Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) :
7 Bu::Filter( rNext ),
8 nCompression( nCompression )
9{
10 start();
11}
12
13Bu::BZip2::~BZip2()
14{
15 printf("-> Bu::BZip2::~BZip2()\n");
16 stop();
17}
18
19void Bu::BZip2::start()
20{
21 printf("-> Bu::BZip2::start()\n");
22 printf("Hey, it's starting...\n");
23 bzState.state = NULL;
24 bzState.bzalloc = NULL;
25 bzState.bzfree = NULL;
26 bzState.opaque = NULL;
27
28 nBufSize = 50000;
29 pBuf = new char[nBufSize];
30}
31
32void Bu::BZip2::stop()
33{
34 printf("-> Bu::BZip2::stop()\n");
35 if( bzState.state )
36 {
37 if( bReading )
38 {
39 }
40 else
41 {
42 for(;;)
43 {
44 bzState.next_in = NULL;
45 bzState.avail_in = 0;
46 bzState.avail_out = nBufSize;
47 bzState.next_out = pBuf;
48 int res = BZ2_bzCompress( &bzState, BZ_FINISH );
49 if( bzState.avail_out < nBufSize )
50 {
51 rNext.write( pBuf, nBufSize-bzState.avail_out );
52 }
53 if( res == BZ_STREAM_END )
54 break;
55 }
56 BZ2_bzCompressEnd( &bzState );
57 }
58 }
59}
60
61void Bu::BZip2::bzError( int code )
62{
63 switch( code )
64 {
65 case BZ_OK:
66 return;
67
68 case BZ_CONFIG_ERROR:
69 throw ExceptionBase("The bzip2 library has been miscompiled.");
70
71 case BZ_PARAM_ERROR:
72 throw ExceptionBase("bzip2 parameter error.");
73
74 case BZ_MEM_ERROR:
75 throw ExceptionBase("Not enough memory available for bzip2.");
76 }
77}
78
79size_t Bu::BZip2::read( void *pData, size_t nBytes )
80{
81 if( !bzState.state )
82 {
83 bReading = true;
84 }
85 if( bReading == false )
86 throw ExceptionBase("This bzip2 filter is in writing mode, you can't read.");
87 //bzState.next_in = pData;
88 //bzState.avail_in = nSizeIn;
89
90 //printf("%db at [%08X] (%db)\n", bzState.avail_in, (uint32_t)bzState.next_in, bzState.total_in_lo32 );
91 return 0;
92}
93
94size_t Bu::BZip2::write( const void *pData, size_t nBytes )
95{
96 if( !bzState.state )
97 {
98 bReading = false;
99 BZ2_bzCompressInit( &bzState, nCompression, 0, 30 );
100 }
101 if( bReading == true )
102 throw ExceptionBase("This bzip2 filter is in reading mode, you can't write.");
103
104 bzState.next_in = (char *)pData;
105 bzState.avail_in = nBytes;
106 for(;;)
107 {
108 bzState.avail_out = nBufSize;
109 bzState.next_out = pBuf;
110
111 BZ2_bzCompress( &bzState, BZ_RUN );
112
113 if( bzState.avail_out < nBufSize )
114 {
115 rNext.write( pBuf, nBufSize-bzState.avail_out );
116 }
117 if( bzState.avail_in == 0 )
118 break;
119 }
120
121 return 0;
122}
123
diff --git a/src/bzip2.h b/src/bzip2.h
new file mode 100644
index 0000000..056f336
--- /dev/null
+++ b/src/bzip2.h
@@ -0,0 +1,35 @@
1#ifndef B_ZIP2_H
2#define B_ZIP2_H
3
4#include <stdint.h>
5#include <bzlib.h>
6
7#include "bu/filter.h"
8
9namespace Bu
10{
11 /**
12 *
13 */
14 class BZip2 : public Bu::Filter
15 {
16 public:
17 BZip2( Bu::Stream &rNext, int nCompression=9 );
18 virtual ~BZip2();
19
20 virtual void start();
21 virtual void stop();
22 virtual size_t read( void *pBuf, size_t nBytes );
23 virtual size_t write( const void *pBuf, size_t nBytes );
24
25 private:
26 void bzError( int code );
27 bz_stream bzState;
28 bool bReading;
29 int nCompression;
30 char *pBuf;
31 uint32_t nBufSize;
32 };
33}
34
35#endif
diff --git a/src/entities/bu-class b/src/entities/bu-class
index 81e3d25..7b25291 100644
--- a/src/entities/bu-class
+++ b/src/entities/bu-class
@@ -10,7 +10,7 @@
10 10
11#include &lt;stdint.h&gt; 11#include &lt;stdint.h&gt;
12 12
13{?parent:"#include \"{=parent:%tolower}.h\" 13{?parent:"#include \"bu/{=parent:%tolower}.h\"
14 14
15"}namespace Bu 15"}namespace Bu
16{ 16{
@@ -35,6 +35,8 @@
35 filename="{=name:%tolower}.cpp" 35 filename="{=name:%tolower}.cpp"
36>#include "bu/{=name:%tolower}.h" 36>#include "bu/{=name:%tolower}.h"
37 37
38using namespace Bu;
39
38Bu::{=name}::{=name}() 40Bu::{=name}::{=name}()
39{ 41{
40} 42}
diff --git a/src/filter.cpp b/src/filter.cpp
new file mode 100644
index 0000000..d3faa00
--- /dev/null
+++ b/src/filter.cpp
@@ -0,0 +1,77 @@
1#include "bu/filter.h"
2
3Bu::Filter::Filter( Bu::Stream &rNext ) :
4 rNext( rNext )
5{
6}
7
8Bu::Filter::~Filter()
9{
10 printf("-> Bu::Filter::~Filter()\n");
11}
12/*
13void Bu::Filter::start()
14{
15 printf("-> Bu::Filter::start()\n");
16}
17
18void Bu::Filter::stop()
19{
20}*/
21
22void Bu::Filter::close()
23{
24 stop();
25 rNext.close();
26}
27
28long Bu::Filter::tell()
29{
30 return rNext.tell();
31}
32
33void Bu::Filter::seek( long offset )
34{
35 rNext.seek( offset );
36}
37
38void Bu::Filter::setPos( long pos )
39{
40 rNext.setPos( pos );
41}
42
43void Bu::Filter::setPosEnd( long pos )
44{
45 rNext.setPosEnd( pos );
46}
47
48bool Bu::Filter::isEOS()
49{
50 return rNext.isEOS();
51}
52
53bool Bu::Filter::canRead()
54{
55 return rNext.canRead();
56}
57
58bool Bu::Filter::canWrite()
59{
60 return rNext.canWrite();
61}
62
63bool Bu::Filter::canSeek()
64{
65 return rNext.canSeek();
66}
67
68bool Bu::Filter::isBlocking()
69{
70 return rNext.isBlocking();
71}
72
73void Bu::Filter::setBlocking( bool bBlocking )
74{
75 rNext.setBlocking( bBlocking );
76}
77
diff --git a/src/filter.h b/src/filter.h
new file mode 100644
index 0000000..b068206
--- /dev/null
+++ b/src/filter.h
@@ -0,0 +1,59 @@
1#ifndef FILTER_H
2#define FILTER_H
3
4#include <stdint.h>
5
6#include "bu/stream.h"
7
8namespace Bu
9{
10 /**
11 * Data filter base class. Each data filter should contain a read and write
12 * section. Effectively, the write applies the filter, the read un-applies
13 * the filter, if possible. For example, BZip2 is a filter that compresses
14 * on write and decompresses on read. All bi-directional filters should
15 * follow: x == read( write( x ) ) (byte-for-byte comparison)
16 *
17 * Also, all returned buffers should be owned by the filter, and deleted
18 * when the filter is deleted. This means that the output of a read or
19 * write operation must be used before the next call to read or write or the
20 * data will be destroyed. Also, the internal buffer may be changed or
21 * recreated between calls, so always get a new pointer from a call to
22 * read or write.
23 *
24 * The close function can also return data, so make sure to check for it,
25 * many filters such as compression filters will buffer data until they have
26 * enough to create a compression block, in these cases the leftover data
27 * will be returned by close.
28 */
29 class Filter : public Bu::Stream
30 {
31 public:
32 Filter( Bu::Stream &rNext );
33 virtual ~Filter();
34
35 virtual void start()=0;
36 virtual void stop()=0;
37 virtual void close();
38 virtual long tell();
39 virtual void seek( long offset );
40 virtual void setPos( long pos );
41 virtual void setPosEnd( long pos );
42 virtual bool isEOS();
43
44 virtual bool canRead();
45 virtual bool canWrite();
46 virtual bool canSeek();
47
48 virtual bool isBlocking();
49 virtual void setBlocking( bool bBlocking=true );
50
51 protected:
52 Bu::Stream &rNext;
53
54 private:
55
56 };
57}
58
59#endif
diff --git a/src/fstring.h b/src/fstring.h
index 43033b8..d0307b5 100644
--- a/src/fstring.h
+++ b/src/fstring.h
@@ -3,9 +3,9 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include <memory> 5#include <memory>
6#include "archival.h" 6#include "bu/archival.h"
7#include "archive.h" 7#include "bu/archive.h"
8#include "hash.h" 8#include "bu/hash.h"
9 9
10#define min( a, b ) ((a<b)?(a):(b)) 10#define min( a, b ) ((a<b)?(a):(b))
11 11
diff --git a/src/old/paramproc.h b/src/old/paramproc.h
deleted file mode 100644
index d857193..0000000
--- a/src/old/paramproc.h
+++ /dev/null
@@ -1,153 +0,0 @@
1#ifndef PARAM_PROC_H
2#define PARAM_PROC_H
3
4#include <stdint.h>
5#include <string>
6#include <list>
7#include "staticstring.h"
8
9class ParamProc
10{
11public:
12 class ParamPtr
13 {
14 public:
15 ParamPtr();
16 ParamPtr( std::string *str );
17 ParamPtr( uint64_t *uint64 );
18 ParamPtr( uint32_t *uint32 );
19 ParamPtr( uint16_t *uint16 );
20 ParamPtr( uint8_t *uint8 );
21 ParamPtr( int64_t *int64 );
22 ParamPtr( int32_t *int32 );
23 ParamPtr( int16_t *int16 );
24 ParamPtr( int8_t *int8 );
25 ParamPtr( float *float32 );
26 ParamPtr( double *float64 );
27 ParamPtr( long double *float96 );
28 ParamPtr( bool *bln );
29
30 enum
31 {
32 vtunset,
33 vtstr,
34 vtuint64,
35 vtuint32,
36 vtuint16,
37 vtuint8,
38 vtint64,
39 vtint32,
40 vtint16,
41 vtint8,
42 vtfloat32,
43 vtfloat64,
44 vtfloat96,
45 vtbln,
46 };
47 ParamPtr &operator=( ParamPtr &ptr );
48 ParamPtr &operator=( const char *str );
49
50 bool isSet();
51
52 private:
53 int type;
54 union
55 {
56 std::string *str;
57 uint64_t *uint64;
58 uint32_t *uint32;
59 uint16_t *uint16;
60 uint8_t *uint8;
61 int64_t *int64;
62 int32_t *int32;
63 int16_t *int16;
64 int8_t *int8;
65 float *float32;
66 double *float64;
67 long double *float96;
68 bool *bln;
69 } val;
70 };
71
72 typedef int (ParamProc::*Proc)( int, char *[] );
73
74 typedef struct ArgSpec
75 {
76 uint8_t nFlags;
77 StaticString sWord;
78 char cChar;
79 Proc proc;
80 ParamProc::ParamPtr val;
81 StaticString sExtra;
82 StaticString sDesc;
83 StaticString sValue;
84 } ArgSpec;
85
86public:
87 ParamProc();
88 virtual ~ParamProc();
89
90 void addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val,
91 const char *lpDesc=NULL, const char *lpExtra=NULL,
92 const char *lpValue=NULL
93 );
94 void addParam( const char *lpWord, char cChar, Proc proc,
95 const char *lpDesc=NULL, const char *lpExtra=NULL,
96 const char *lpValue=NULL
97 );
98 void addParam( const char *lpWord, char cChar, ParamPtr val,
99 const char *lpDesc=NULL, const char *lpExtra=NULL,
100 const char *lpValue=NULL
101 );
102
103 void addParam( const char *lpWord, Proc proc, ParamPtr val,
104 const char *lpDesc=NULL, const char *lpExtra=NULL,
105 const char *lpValue=NULL
106 );
107 void addParam( const char *lpWord, Proc proc,
108 const char *lpDesc=NULL, const char *lpExtra=NULL,
109 const char *lpValue=NULL
110 );
111 void addParam( const char *lpWord, ParamPtr val,
112 const char *lpDesc=NULL, const char *lpExtra=NULL,
113 const char *lpValue=NULL
114 );
115
116 void addParam( char cChar, Proc proc, ParamPtr val,
117 const char *lpDesc=NULL, const char *lpExtra=NULL,
118 const char *lpValue=NULL
119 );
120 void addParam( char cChar, Proc proc,
121 const char *lpDesc=NULL, const char *lpExtra=NULL,
122 const char *lpValue=NULL
123 );
124 void addParam( char cChar, ParamPtr val,
125 const char *lpDesc=NULL, const char *lpExtra=NULL,
126 const char *lpValue=NULL
127 );
128
129 void process( int argc, char *argv[] );
130 void addHelpBanner( const char *sHelpBanner );
131
132private:
133 ArgSpec *checkWord( const char *arg );
134 ArgSpec *checkLetr( const char arg );
135
136public:
137 virtual int cmdParam( int argc, char *argv[] );
138 virtual int unknownParam( int argc, char *argv[] );
139 virtual int help( int argc, char *argv[] );
140
141private:
142 typedef struct Banner
143 {
144 StaticString sBanner;
145 ArgSpec *pBefore;
146 } Banner;
147 std::list<Banner *> lBan;
148 std::list<ArgSpec *> lArg;
149};
150
151#define mkproc( cls ) static_cast<int (ParamProc::*)( int, char *[])>(&cls)
152
153#endif
diff --git a/src/old/paramproc.cpp b/src/paramproc.cpp
index a352e66..34e973e 100644
--- a/src/old/paramproc.cpp
+++ b/src/paramproc.cpp
@@ -2,10 +2,10 @@
2#include <stdio.h> 2#include <stdio.h>
3 3
4#define ptrtype( iitype, iiname ) \ 4#define ptrtype( iitype, iiname ) \
5 ParamProc::ParamPtr::ParamPtr( iitype *iiname ) : \ 5 Bu::ParamProc::ParamPtr::ParamPtr( iitype *iiname ) : \
6 type( vt ##iiname ) { val.iiname = iiname; } 6 type( vt ##iiname ) { val.iiname = iiname; }
7 7
8ParamProc::ParamPtr::ParamPtr() 8Bu::ParamProc::ParamPtr::ParamPtr()
9{ 9{
10 val.str = NULL; 10 val.str = NULL;
11 type = vtunset; 11 type = vtunset;
@@ -25,7 +25,7 @@ ptrtype( double, float64 );
25ptrtype( long double, float96 ); 25ptrtype( long double, float96 );
26ptrtype( bool, bln ); 26ptrtype( bool, bln );
27 27
28ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr ) 28Bu::ParamProc::ParamPtr &Bu::ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr )
29{ 29{
30 val = ptr.val; 30 val = ptr.val;
31 type = ptr.type; 31 type = ptr.type;
@@ -33,12 +33,12 @@ ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( ParamProc::ParamPtr &ptr )
33 return *this; 33 return *this;
34} 34}
35 35
36bool ParamProc::ParamPtr::isSet() 36bool Bu::ParamProc::ParamPtr::isSet()
37{ 37{
38 return type != vtunset; 38 return type != vtunset;
39} 39}
40 40
41ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( const char *str ) 41Bu::ParamProc::ParamPtr &Bu::ParamProc::ParamPtr::operator=( const char *str )
42{ 42{
43 if( !isSet() ) return *this; 43 if( !isSet() ) return *this;
44 switch( type ) 44 switch( type )
@@ -107,11 +107,11 @@ ParamProc::ParamPtr &ParamProc::ParamPtr::operator=( const char *str )
107 return *this; 107 return *this;
108} 108}
109 109
110ParamProc::ParamProc() 110Bu::ParamProc::ParamProc()
111{ 111{
112} 112}
113 113
114ParamProc::~ParamProc() 114Bu::ParamProc::~ParamProc()
115{ 115{
116 for( std::list<ArgSpec *>::iterator i = lArg.begin(); 116 for( std::list<ArgSpec *>::iterator i = lArg.begin();
117 i != lArg.end(); i++ ) 117 i != lArg.end(); i++ )
@@ -127,14 +127,14 @@ ParamProc::~ParamProc()
127 127
128} 128}
129/* 129/*
130void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val ) 130void Bu::ParamProc::addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val )
131{ 131{
132 printf("Calling callback...\n"); 132 printf("Calling callback...\n");
133 val = "Hello there, this is set in the ParamProc"; 133 val = "Hello there, this is set in the ParamProc";
134 (this->*proc)(); 134 (this->*proc)();
135}*/ 135}*/
136 136
137void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, 137void Bu::ParamProc::addParam( const char *lpWord, char cChar, Proc proc,
138 ParamPtr val, const char *lpDesc, const char *lpExtra, 138 ParamPtr val, const char *lpDesc, const char *lpExtra,
139 const char *lpValue ) 139 const char *lpValue )
140{ 140{
@@ -161,63 +161,63 @@ void ParamProc::addParam( const char *lpWord, char cChar, Proc proc,
161 } 161 }
162} 162}
163 163
164void ParamProc::addParam( const char *lpWord, char cChar, Proc proc, 164void Bu::ParamProc::addParam( const char *lpWord, char cChar, Proc proc,
165 const char *lpDesc, const char *lpExtra, 165 const char *lpDesc, const char *lpExtra,
166 const char *lpValue ) 166 const char *lpValue )
167{ 167{
168 addParam( lpWord, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); 168 addParam( lpWord, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue );
169} 169}
170 170
171void ParamProc::addParam( const char *lpWord, char cChar, ParamPtr val, 171void Bu::ParamProc::addParam( const char *lpWord, char cChar, ParamPtr val,
172 const char *lpDesc, const char *lpExtra, 172 const char *lpDesc, const char *lpExtra,
173 const char *lpValue ) 173 const char *lpValue )
174{ 174{
175 addParam( lpWord, cChar, NULL, val, lpDesc, lpExtra, lpValue ); 175 addParam( lpWord, cChar, NULL, val, lpDesc, lpExtra, lpValue );
176} 176}
177 177
178void ParamProc::addParam( const char *lpWord, Proc proc, ParamPtr val, 178void Bu::ParamProc::addParam( const char *lpWord, Proc proc, ParamPtr val,
179 const char *lpDesc, const char *lpExtra, 179 const char *lpDesc, const char *lpExtra,
180 const char *lpValue ) 180 const char *lpValue )
181{ 181{
182 addParam( lpWord, '\0', proc, val, lpDesc, lpExtra, lpValue ); 182 addParam( lpWord, '\0', proc, val, lpDesc, lpExtra, lpValue );
183} 183}
184 184
185void ParamProc::addParam( const char *lpWord, Proc proc, 185void Bu::ParamProc::addParam( const char *lpWord, Proc proc,
186 const char *lpDesc, const char *lpExtra, 186 const char *lpDesc, const char *lpExtra,
187 const char *lpValue ) 187 const char *lpValue )
188{ 188{
189 addParam( lpWord, '\0', proc, ParamPtr(), lpDesc, lpExtra, lpValue ); 189 addParam( lpWord, '\0', proc, ParamPtr(), lpDesc, lpExtra, lpValue );
190} 190}
191 191
192void ParamProc::addParam( const char *lpWord, ParamPtr val, 192void Bu::ParamProc::addParam( const char *lpWord, ParamPtr val,
193 const char *lpDesc, const char *lpExtra, 193 const char *lpDesc, const char *lpExtra,
194 const char *lpValue ) 194 const char *lpValue )
195{ 195{
196 addParam( lpWord, '\0', NULL, val, lpDesc, lpExtra, lpValue ); 196 addParam( lpWord, '\0', NULL, val, lpDesc, lpExtra, lpValue );
197} 197}
198 198
199void ParamProc::addParam( char cChar, Proc proc, ParamPtr val, 199void Bu::ParamProc::addParam( char cChar, Proc proc, ParamPtr val,
200 const char *lpDesc, const char *lpExtra, 200 const char *lpDesc, const char *lpExtra,
201 const char *lpValue ) 201 const char *lpValue )
202{ 202{
203 addParam( NULL, cChar, proc, val, lpDesc, lpExtra, lpValue ); 203 addParam( NULL, cChar, proc, val, lpDesc, lpExtra, lpValue );
204} 204}
205 205
206void ParamProc::addParam( char cChar, Proc proc, 206void Bu::ParamProc::addParam( char cChar, Proc proc,
207 const char *lpDesc, const char *lpExtra, 207 const char *lpDesc, const char *lpExtra,
208 const char *lpValue ) 208 const char *lpValue )
209{ 209{
210 addParam( NULL, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue ); 210 addParam( NULL, cChar, proc, ParamPtr(), lpDesc, lpExtra, lpValue );
211} 211}
212 212
213void ParamProc::addParam( char cChar, ParamPtr val, 213void Bu::ParamProc::addParam( char cChar, ParamPtr val,
214 const char *lpDesc, const char *lpExtra, 214 const char *lpDesc, const char *lpExtra,
215 const char *lpValue ) 215 const char *lpValue )
216{ 216{
217 addParam( NULL, cChar, NULL, val, lpDesc, lpExtra, lpValue ); 217 addParam( NULL, cChar, NULL, val, lpDesc, lpExtra, lpValue );
218} 218}
219 219
220void ParamProc::process( int argc, char *argv[] ) 220void Bu::ParamProc::process( int argc, char *argv[] )
221{ 221{
222 for( int arg = 1; arg < argc; arg++ ) 222 for( int arg = 1; arg < argc; arg++ )
223 { 223 {
@@ -229,23 +229,23 @@ void ParamProc::process( int argc, char *argv[] )
229 ArgSpec *s = checkWord( argv[arg]+2 ); 229 ArgSpec *s = checkWord( argv[arg]+2 );
230 if( s ) 230 if( s )
231 { 231 {
232 if( argv[arg][s->sWord.getLength()+2] == '=' ) 232 if( argv[arg][s->sWord.getSize()+2] == '=' )
233 { 233 {
234 if( s->val.isSet() ) 234 if( s->val.isSet() )
235 { 235 {
236 if( s->sValue.getString() == NULL ) 236 if( s->sValue.getStr() == NULL )
237 { 237 {
238 s->val = argv[arg]+s->sWord.getLength()+3; 238 s->val = argv[arg]+s->sWord.getSize()+3;
239 } 239 }
240 else 240 else
241 { 241 {
242 s->val = s->sValue.getString(); 242 s->val = s->sValue.getStr();
243 } 243 }
244 } 244 }
245 if( s->proc ) 245 if( s->proc )
246 { 246 {
247 char **tmp = new char*[argc-arg]; 247 char **tmp = new char*[argc-arg];
248 tmp[0] = argv[arg]+s->sWord.getLength()+3; 248 tmp[0] = argv[arg]+s->sWord.getSize()+3;
249 for( int k = 1; k < argc-arg; k++ ) 249 for( int k = 1; k < argc-arg; k++ )
250 tmp[k] = argv[arg+k]; 250 tmp[k] = argv[arg+k];
251 int ret = (this->*s->proc)( argc-arg, tmp ); 251 int ret = (this->*s->proc)( argc-arg, tmp );
@@ -261,7 +261,7 @@ void ParamProc::process( int argc, char *argv[] )
261 int add = 0; 261 int add = 0;
262 if( s->val.isSet() ) 262 if( s->val.isSet() )
263 { 263 {
264 if( s->sValue.getString() == NULL ) 264 if( s->sValue.getStr() == NULL )
265 { 265 {
266 if( arg+1 >= argc ) 266 if( arg+1 >= argc )
267 { 267 {
@@ -272,7 +272,7 @@ void ParamProc::process( int argc, char *argv[] )
272 } 272 }
273 else 273 else
274 { 274 {
275 s->val = s->sValue.getString(); 275 s->val = s->sValue.getStr();
276 } 276 }
277 } 277 }
278 if( s->proc ) 278 if( s->proc )
@@ -307,14 +307,14 @@ void ParamProc::process( int argc, char *argv[] )
307 bool bUsed = false; 307 bool bUsed = false;
308 if( s->val.isSet() ) 308 if( s->val.isSet() )
309 { 309 {
310 if( s->sValue.getString() == NULL ) 310 if( s->sValue.getStr() == NULL )
311 { 311 {
312 s->val = argv[arg]+chr+1; 312 s->val = argv[arg]+chr+1;
313 bUsed = true; 313 bUsed = true;
314 } 314 }
315 else 315 else
316 { 316 {
317 s->val = s->sValue.getString(); 317 s->val = s->sValue.getStr();
318 } 318 }
319 } 319 }
320 if( s->proc ) 320 if( s->proc )
@@ -342,14 +342,14 @@ void ParamProc::process( int argc, char *argv[] )
342 bool bUsed = false; 342 bool bUsed = false;
343 if( s->val.isSet() ) 343 if( s->val.isSet() )
344 { 344 {
345 if( s->sValue.getString() == NULL ) 345 if( s->sValue.getStr() == NULL )
346 { 346 {
347 s->val = argv[arg+1]; 347 s->val = argv[arg+1];
348 bUsed = true; 348 bUsed = true;
349 } 349 }
350 else 350 else
351 { 351 {
352 s->val = s->sValue.getString(); 352 s->val = s->sValue.getStr();
353 } 353 }
354 } 354 }
355 if( s->proc ) 355 if( s->proc )
@@ -384,22 +384,22 @@ void ParamProc::process( int argc, char *argv[] )
384 } 384 }
385} 385}
386 386
387ParamProc::ArgSpec *ParamProc::checkWord( const char *arg ) 387Bu::ParamProc::ArgSpec *Bu::ParamProc::checkWord( const char *arg )
388{ 388{
389 //printf("Checking \"%s\"...\n", arg ); 389 //printf("Checking \"%s\"...\n", arg );
390 std::list<ArgSpec *>::const_iterator i; 390 std::list<ArgSpec *>::const_iterator i;
391 for( i = lArg.begin(); i != lArg.end(); i++ ) 391 for( i = lArg.begin(); i != lArg.end(); i++ )
392 { 392 {
393 if( (*i)->sWord.getString() == NULL ) 393 if( (*i)->sWord.getStr() == NULL )
394 continue; 394 continue;
395 395
396 if( !strcmp( (*i)->sWord, arg ) ) 396 if( !strcmp( (*i)->sWord.getStr(), arg ) )
397 return *i; 397 return *i;
398 398
399 if( (*i)->val.isSet() ) 399 if( (*i)->val.isSet() )
400 { 400 {
401 if( !strncmp( (*i)->sWord, arg, (*i)->sWord.getLength() ) && 401 if( !strncmp( (*i)->sWord.getStr(), arg, (*i)->sWord.getSize() ) &&
402 arg[(*i)->sWord.getLength()] == '=' ) 402 arg[(*i)->sWord.getSize()] == '=' )
403 { 403 {
404 return *i; 404 return *i;
405 } 405 }
@@ -409,7 +409,7 @@ ParamProc::ArgSpec *ParamProc::checkWord( const char *arg )
409 return NULL; 409 return NULL;
410} 410}
411 411
412ParamProc::ArgSpec *ParamProc::checkLetr( const char arg ) 412Bu::ParamProc::ArgSpec *Bu::ParamProc::checkLetr( const char arg )
413{ 413{
414 //printf("Checking \'%c\'...\n", arg ); 414 //printf("Checking \'%c\'...\n", arg );
415 std::list<ArgSpec *>::const_iterator i; 415 std::list<ArgSpec *>::const_iterator i;
@@ -427,27 +427,27 @@ ParamProc::ArgSpec *ParamProc::checkLetr( const char arg )
427 return NULL; 427 return NULL;
428} 428}
429 429
430int ParamProc::cmdParam( int argc, char *argv[] ) 430int Bu::ParamProc::cmdParam( int argc, char *argv[] )
431{ 431{
432 printf("Unhandled command parameter \"%s\" found!\n", argv[0] ); 432 printf("Unhandled command parameter \"%s\" found!\n", argv[0] );
433 return 0; 433 return 0;
434} 434}
435 435
436int ParamProc::unknownParam( int argc, char *argv[] ) 436int Bu::ParamProc::unknownParam( int argc, char *argv[] )
437{ 437{
438 printf("Unknown parameter \"%s\" found!\n", argv[0] ); 438 printf("Unknown parameter \"%s\" found!\n", argv[0] );
439 return 0; 439 return 0;
440} 440}
441 441
442int ParamProc::help( int argc, char *argv[] ) 442int Bu::ParamProc::help( int argc, char *argv[] )
443{ 443{
444 std::list<Banner *>::const_iterator b = lBan.begin(); 444 std::list<Banner *>::const_iterator b = lBan.begin();
445 std::list<ArgSpec *>::const_iterator i; 445 std::list<ArgSpec *>::const_iterator i;
446 int len=0; 446 int len=0;
447 for( i = lArg.begin(); i != lArg.end(); i++ ) 447 for( i = lArg.begin(); i != lArg.end(); i++ )
448 { 448 {
449 if( len < (*i)->sWord.getLength() + (*i)->sExtra.getLength() ) 449 if( len < (*i)->sWord.getSize() + (*i)->sExtra.getSize() )
450 len = (*i)->sWord.getLength() + (*i)->sExtra.getLength(); 450 len = (*i)->sWord.getSize() + (*i)->sExtra.getSize();
451 } 451 }
452 char fmt[10]; 452 char fmt[10];
453 sprintf( fmt, "%%-%ds ", len ); 453 sprintf( fmt, "%%-%ds ", len );
@@ -458,14 +458,14 @@ int ParamProc::help( int argc, char *argv[] )
458 { 458 {
459 if( (*b)->pBefore == (*i) ) 459 if( (*b)->pBefore == (*i) )
460 { 460 {
461 printf( (*b)->sBanner.getString() ); 461 printf( (*b)->sBanner.getStr() );
462 b++; 462 b++;
463 } 463 }
464 } 464 }
465 printf(" "); 465 printf(" ");
466 if( (*i)->cChar ) 466 if( (*i)->cChar )
467 { 467 {
468 if( (*i)->sWord.getString() ) 468 if( (*i)->sWord.getStr() )
469 { 469 {
470 printf("-%c, ", (*i)->cChar ); 470 printf("-%c, ", (*i)->cChar );
471 } 471 }
@@ -478,12 +478,12 @@ int ParamProc::help( int argc, char *argv[] )
478 { 478 {
479 printf(" "); 479 printf(" ");
480 } 480 }
481 if( (*i)->sWord.getString() ) 481 if( (*i)->sWord.getStr() )
482 { 482 {
483 printf("--"); 483 printf("--");
484 std::string sTmp = (*i)->sWord.getString(); 484 std::string sTmp = (*i)->sWord.getStr();
485 if( (*i)->sExtra.getString() ) 485 if( (*i)->sExtra.getStr() )
486 sTmp += (*i)->sExtra.getString(); 486 sTmp += (*i)->sExtra.getStr();
487 printf( fmt, sTmp.c_str() ); 487 printf( fmt, sTmp.c_str() );
488 } 488 }
489 else 489 else
@@ -491,20 +491,20 @@ int ParamProc::help( int argc, char *argv[] )
491 printf(" "); 491 printf(" ");
492 printf(fmt, "" ); 492 printf(fmt, "" );
493 } 493 }
494 printf("%s\n", (*i)->sDesc.getString() ); 494 printf("%s\n", (*i)->sDesc.getStr() );
495 } 495 }
496 if( b != lBan.end() ) 496 if( b != lBan.end() )
497 { 497 {
498 if( (*b)->pBefore == NULL ) 498 if( (*b)->pBefore == NULL )
499 { 499 {
500 printf( (*b)->sBanner.getString() ); 500 printf( (*b)->sBanner.getStr() );
501 } 501 }
502 } 502 }
503 503
504 exit( 0 ); 504 exit( 0 );
505} 505}
506 506
507void ParamProc::addHelpBanner( const char *sHelpBanner ) 507void Bu::ParamProc::addHelpBanner( const char *sHelpBanner )
508{ 508{
509 Banner *pBan = new Banner; 509 Banner *pBan = new Banner;
510 pBan->sBanner = sHelpBanner; 510 pBan->sBanner = sHelpBanner;
diff --git a/src/paramproc.h b/src/paramproc.h
new file mode 100644
index 0000000..beee4a0
--- /dev/null
+++ b/src/paramproc.h
@@ -0,0 +1,156 @@
1#ifndef PARAM_PROC_H
2#define PARAM_PROC_H
3
4#include <stdint.h>
5#include <string>
6#include <list>
7#include "bu/fstring.h"
8
9namespace Bu
10{
11 class ParamProc
12 {
13 public:
14 class ParamPtr
15 {
16 public:
17 ParamPtr();
18 ParamPtr( std::string *str );
19 ParamPtr( uint64_t *uint64 );
20 ParamPtr( uint32_t *uint32 );
21 ParamPtr( uint16_t *uint16 );
22 ParamPtr( uint8_t *uint8 );
23 ParamPtr( int64_t *int64 );
24 ParamPtr( int32_t *int32 );
25 ParamPtr( int16_t *int16 );
26 ParamPtr( int8_t *int8 );
27 ParamPtr( float *float32 );
28 ParamPtr( double *float64 );
29 ParamPtr( long double *float96 );
30 ParamPtr( bool *bln );
31
32 enum
33 {
34 vtunset,
35 vtstr,
36 vtuint64,
37 vtuint32,
38 vtuint16,
39 vtuint8,
40 vtint64,
41 vtint32,
42 vtint16,
43 vtint8,
44 vtfloat32,
45 vtfloat64,
46 vtfloat96,
47 vtbln,
48 };
49 ParamPtr &operator=( ParamPtr &ptr );
50 ParamPtr &operator=( const char *str );
51
52 bool isSet();
53
54 private:
55 int type;
56 union
57 {
58 std::string *str;
59 uint64_t *uint64;
60 uint32_t *uint32;
61 uint16_t *uint16;
62 uint8_t *uint8;
63 int64_t *int64;
64 int32_t *int32;
65 int16_t *int16;
66 int8_t *int8;
67 float *float32;
68 double *float64;
69 long double *float96;
70 bool *bln;
71 } val;
72 };
73
74 typedef int (ParamProc::*Proc)( int, char *[] );
75
76 typedef struct ArgSpec
77 {
78 uint8_t nFlags;
79 Bu::FString sWord;
80 char cChar;
81 Proc proc;
82 ParamProc::ParamPtr val;
83 Bu::FString sExtra;
84 Bu::FString sDesc;
85 Bu::FString sValue;
86 } ArgSpec;
87
88 public:
89 ParamProc();
90 virtual ~ParamProc();
91
92 void addParam( const char *lpWord, char cChar, Proc proc, ParamPtr val,
93 const char *lpDesc=NULL, const char *lpExtra=NULL,
94 const char *lpValue=NULL
95 );
96 void addParam( const char *lpWord, char cChar, Proc proc,
97 const char *lpDesc=NULL, const char *lpExtra=NULL,
98 const char *lpValue=NULL
99 );
100 void addParam( const char *lpWord, char cChar, ParamPtr val,
101 const char *lpDesc=NULL, const char *lpExtra=NULL,
102 const char *lpValue=NULL
103 );
104
105 void addParam( const char *lpWord, Proc proc, ParamPtr val,
106 const char *lpDesc=NULL, const char *lpExtra=NULL,
107 const char *lpValue=NULL
108 );
109 void addParam( const char *lpWord, Proc proc,
110 const char *lpDesc=NULL, const char *lpExtra=NULL,
111 const char *lpValue=NULL
112 );
113 void addParam( const char *lpWord, ParamPtr val,
114 const char *lpDesc=NULL, const char *lpExtra=NULL,
115 const char *lpValue=NULL
116 );
117
118 void addParam( char cChar, Proc proc, ParamPtr val,
119 const char *lpDesc=NULL, const char *lpExtra=NULL,
120 const char *lpValue=NULL
121 );
122 void addParam( char cChar, Proc proc,
123 const char *lpDesc=NULL, const char *lpExtra=NULL,
124 const char *lpValue=NULL
125 );
126 void addParam( char cChar, ParamPtr val,
127 const char *lpDesc=NULL, const char *lpExtra=NULL,
128 const char *lpValue=NULL
129 );
130
131 void process( int argc, char *argv[] );
132 void addHelpBanner( const char *sHelpBanner );
133
134 private:
135 ArgSpec *checkWord( const char *arg );
136 ArgSpec *checkLetr( const char arg );
137
138 public:
139 virtual int cmdParam( int argc, char *argv[] );
140 virtual int unknownParam( int argc, char *argv[] );
141 virtual int help( int argc, char *argv[] );
142
143 private:
144 typedef struct Banner
145 {
146 Bu::FString sBanner;
147 ArgSpec *pBefore;
148 } Banner;
149 std::list<Banner *> lBan;
150 std::list<ArgSpec *> lArg;
151 };
152}
153
154#define mkproc( cls ) static_cast<int (Bu::ParamProc::*)( int, char *[])>(&cls)
155
156#endif
diff --git a/src/stream.h b/src/stream.h
index 5f586e6..fa0a606 100644
--- a/src/stream.h
+++ b/src/stream.h
@@ -39,6 +39,9 @@ namespace Bu
39 virtual bool isBlocking() = 0; 39 virtual bool isBlocking() = 0;
40 virtual void setBlocking( bool bBlocking=true ) = 0; 40 virtual void setBlocking( bool bBlocking=true ) = 0;
41 41
42 public: // Filters
43
44
42 private: 45 private:
43 46
44 }; 47 };
diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp
new file mode 100644
index 0000000..683d3d7
--- /dev/null
+++ b/src/tests/bzip2.cpp
@@ -0,0 +1,23 @@
1#include "bu/bzip2.h"
2#include "bu/file.h"
3
4int main( int argc, char *argv[] )
5{
6 char buf[1024];
7 size_t nRead;
8
9 Bu::File f( "test.bz2", "wb" );
10 Bu::BZip2 bz2( f );
11
12 Bu::File fin( argv[1], "rb");
13
14 for(;;)
15 {
16 nRead = fin.read( buf, 1024 );
17 if( nRead > 0 )
18 bz2.write( buf, nRead );
19 if( fin.isEOS() )
20 break;
21 }
22}
23