aboutsummaryrefslogtreecommitdiff
path: root/src/old/serializerbinary.cpp
blob: ea7ed9343abc3b3fba945ecc18e196cbc2bbcdae (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
#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)
{
	if( nSize == 0 )
		return;

	fwrite(pData, nSize, 1, fhFile);
}

void SerializerBinary::read(void * pData, int32_t nSize)
{
	if( nSize == 0 )
		return;

	uint32_t nRead = fread(pData, nSize, 1, fhFile);
	if( nRead < 1 )
	{
		throw FileException( excodeEOF, "End of file read");
	}
}