aboutsummaryrefslogtreecommitdiff
path: root/src/tests/streamstack.cpp
blob: ea9ec0b2e3f4349c7bc9c0e372a3c6db2a433c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "bu/streamstack.h"

#include "bu/file.h"
#include "bu/base64.h"
#include "bu/bzip2.h"

#include "bu/sio.h"

#include <time.h>

using namespace Bu;

class DoStuff
{
public:
	DoStuff( Bu::Stream &rStream ) :
		rStream( rStream )
	{
	}

	virtual ~DoStuff()
	{
	}

	void write()
	{
		Bu::String s;
		time_t tNow = time( NULL );
		s = ctime( &tNow );
		long lSize = s.getSize()-1;
		rStream.write( &lSize, sizeof(long) );
		rStream.write( s.getStr(), lSize );
	}

	void read()
	{
		Bu::String s;
		long lSize;
		rStream.read( &lSize, sizeof(long) );
		s.setSize( lSize );
		rStream.read( s.getStr(), lSize );
		sio << "Read str(" << lSize << ") = '" << s << "'" << sio.nl;
	}

private:
	Bu::Stream &rStream;
};

int main()
{
	Bu::StreamStack ss;

	DoStuff ds( ss );

	try
	{
		ds.write();
		sio << "This shouldn't have worked." << sio.nl;
	}
	catch( Bu::ExceptionBase &e )
	{
		sio << "Got exception, this is good:  " << e.what() << sio.nl;
	}

	ss.setStream( new Bu::File("Hello.test", Bu::File::WriteNew ) );

	ds.write();

	ss.pushFilter<Bu::Base64>();

	ds.write();

	ss.pushFilter<Bu::BZip2>();

	ds.write();

	ss.clear();

	ss.setStream( new Bu::File("Hello.test", Bu::File::Read ) );

	ds.read();

	ss.pushFilter<Bu::Base64>();

	ds.read();

	ss.pushFilter<Bu::BZip2>();

	ds.read();

	return 0;
}