summaryrefslogtreecommitdiff
path: root/src/xmlfilereader.cpp
blob: dd4bc82cf7eba10440dec3fab90027c2b5077534 (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
#include "xmlfilereader.h"
#include "xmlexception.h"
#include <string.h>

XmlFileReader::XmlFileReader( const char *sFile, bool bStrip )
	: XmlReader( bStrip )
{
	fh = fopen( sFile, "rt" );

	if( fh == NULL )
	{
		throw XmlException("Couldn't open file: %s", sFile );
		//nError = 1;
	}
	else
	{
		char buf[50];
		fgets( buf, 50, fh );

		if( !strcmp( buf, "<?xml version=\"1.0\"?>\n" ) )
		{
			buildDoc();
		}
	}
}

XmlFileReader::~XmlFileReader()
{
}

char XmlFileReader::getChar( int nIndex )
{
	// Make sure we always have a little data left in the buffer
	if( fbDataIn.getLength() <= nIndex+1 && fh )
	{
		int nBytes = fbDataIn.getCapacity()-1;
		char *buf = new char[nBytes];
		int nRead = fread( buf, 1, nBytes, fh );
		fbDataIn.appendData( buf, nRead );
		delete[] buf;

		if( nRead < nBytes )
		{
			fclose( fh );
			fh = NULL;
		}
	}
	if( fbDataIn.getLength() >= nIndex+1 )
	{
		return fbDataIn.getData()[nIndex];
	}
	else
	{
		throw XmlException("End of XML stream read.");
	}
}

void XmlFileReader::usedChar()
{
	if( fbDataIn.getLength() > 0 )
	{
		fbDataIn.usedData( 1 );
	}
}