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

SerializerBinary::SerializerBinary(FILE *fhFile, bool bLoading):
	Serializer(bLoading),
	fhFile(fhFile),
	bCloseFile(false)
{	
}

SerializerBinary::SerializerBinary(const char *sFileName, bool bLoading):
	Serializer(bLoading),
	bCloseFile(true)
{
	if (bLoading)
	{
		fhFile = fopen(sFileName, "rb");
		if( fhFile == NULL )
			throw FileException("Unable to open file: %s", sFileName );
	}
	else
	{
		fhFile = fopen(sFileName, "wb");
		if( fhFile == NULL )
			throw FileException("Unable to open file: %s", sFileName );
	}
}

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

void SerializerBinary::close()
{
	if (fhFile != NULL && bCloseFile )
	{
		fclose(fhFile);
		fhFile = NULL;
	}
}

void SerializerBinary::write(const void * pData, int32_t nSize)
{
	fwrite(pData, nSize, 1, fhFile);
}

void SerializerBinary::read(void * pData, int32_t nSize)
{
	if( fread(pData, nSize, 1, fhFile) < 1 )
	{
		throw FileException( excodeEOF, "End of file read");
	}
}