aboutsummaryrefslogtreecommitdiff
path: root/src/serializerbzip2.cpp
blob: bafabc820360543481a5729cf16939eb758ac85c (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
#include "serializerbzip2.h"
#include "serializable.h"

#include <bzlib.h>

SerializerBZip2::SerializerBZip2(FILE *fhFile, bool bLoading):
	Serializer(bLoading),
	fhFile(fhFile),
	bCloseFile(false)
{
	if( bLoading )
	{
		bzFile = BZ2_bzReadOpen( &bzerror, fhFile, 0, 0, NULL, 0 );
		checkBZError();
	}
	else
	{
		bzFile = BZ2_bzWriteOpen( &bzerror, fhFile, 9, 0, 0 );
		checkBZError();
	}
}

SerializerBZip2::SerializerBZip2(char *sFileName, bool bLoading):
	Serializer(bLoading),
	bCloseFile(true)
{
	if (bLoading)
	{
		fhFile = fopen(sFileName, "rb");
		bzFile = BZ2_bzReadOpen( &bzerror, fhFile, 0, 0, NULL, 0 );
		checkBZError();
	}
	else
	{
		fhFile = fopen(sFileName, "wb");
		bzFile = BZ2_bzWriteOpen( &bzerror, fhFile, 9, 0, 0 );
		checkBZError();
	}
}

SerializerBZip2::~SerializerBZip2()
{
	close();
}

void SerializerBZip2::checkBZError()
{
}

void SerializerBZip2::close()
{
	if( bzFile != NULL )
	{
		if( isLoading() )
		{
			void *unused;
			int nUnused;
			BZ2_bzReadGetUnused( &bzerror, bzFile, &unused, &nUnused );
			BZ2_bzReadClose( &bzerror, bzFile );
			if( nUnused )
				fseek( fhFile, -nUnused, SEEK_CUR );
		}
		else
		{
			BZ2_bzWriteClose( &bzerror, bzFile, 0, 0, 0 );
		}
		checkBZError();
		bzFile = NULL;
	}
	if( fhFile != NULL && bCloseFile )
	{
		fclose(fhFile);
		fhFile = NULL;
	}
}

void SerializerBZip2::write(const void * pData, int32_t nSize)
{
	BZ2_bzWrite( &bzerror, bzFile, (void *)pData, nSize );
	checkBZError();
}

void SerializerBZip2::read(void * pData, int32_t nSize)
{
	BZ2_bzRead( &bzerror, bzFile, pData, nSize );
	checkBZError();
}