summaryrefslogtreecommitdiff
path: root/src/serializerbinary.cpp
blob: cb3c8bf97f12f163bcf3fcacd2afd013fa0525d1 (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
#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)
{
	uint32_t nWrite = fwrite(pData, nSize, 1, fhFile);
	printf("fwrite( 0x%08X, %d, 1, fhFile ) = %u;\n", pData, nSize, nWrite );
}

void SerializerBinary::read(void * pData, int32_t nSize)
{
	uint32_t nRead = fread(pData, nSize, 1, fhFile);
	printf("fread( 0x%08X, %d, 1, fhFile ) = %u;\n", pData, nSize, nRead );
	if( nRead < 1 )
	{
		throw FileException( excodeEOF, "End of file read");
	}
}