aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/irrXML.cpp
blob: 06f87d717e0521bd05ba2045f7b0d10ffd70432c (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
// For conditions of distribution and use, see copyright notice in irrlicht.h and/or irrXML.h

#include "irrXML.h"
#include "irrString.h"
#include "irrArray.h"
#include "fast_atof.h"
#include "CXMLReaderImpl.h"

namespace irr
{
namespace io
{

//! Implementation of the file read callback for ordinary files
class CFileReadCallBack : public IFileReadCallBack
{
public:

	//! construct from filename
	CFileReadCallBack(const char* filename)
		: File(0), Size(-1), Close(true)
	{
		// open file
		File = fopen(filename, "rb");

		if (File)
			getFileSize();
	}

	//! construct from FILE pointer
	CFileReadCallBack(FILE* file)
		: File(file), Size(-1), Close(false)
	{
		if (File)
			getFileSize();
	}

	//! destructor
	virtual ~CFileReadCallBack()
	{
		if (Close && File)
			fclose(File);
	}

	//! Reads an amount of bytes from the file.
	virtual int read(void* buffer, int sizeToRead)
	{
		if (!File)
			return 0;

		return (int)fread(buffer, 1, sizeToRead, File);
	}

	//! Returns size of file in bytes
	virtual long getSize() const
	{
		return Size;
	}

private:

	//! retrieves the file size of the open file
	void getFileSize()
	{
		fseek(File, 0, SEEK_END);
		Size = ftell(File);
		fseek(File, 0, SEEK_SET);
	}

	FILE* File;
	long Size;
	bool Close;

}; // end class CFileReadCallBack



// FACTORY FUNCTIONS:


//! Creates an instance of an UFT-8 or ASCII character xml parser. 
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(const char* filename)
{
	return createIrrXMLReader(new CFileReadCallBack(filename), true); 
}


//! Creates an instance of an UFT-8 or ASCII character xml parser. 
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(FILE* file)
{
	return createIrrXMLReader(new CFileReadCallBack(file), true); 
}


//! Creates an instance of an UFT-8 or ASCII character xml parser. 
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(IFileReadCallBack* callback,
														  bool deleteCallback)
{
	if (callback && (callback->getSize() >= 0))
	{
		return new CXMLReaderImpl<char, IXMLBase>(callback, deleteCallback); 
	}
	else
	{
		if(callback && deleteCallback)
			delete callback;

		return 0;
	}
}


//! Creates an instance of an UTF-16 xml parser. 
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(const char* filename)
{
	return createIrrXMLReaderUTF16(new CFileReadCallBack(filename), true); 
}


//! Creates an instance of an UTF-16 xml parser. 
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(FILE* file)
{
	return createIrrXMLReaderUTF16(new CFileReadCallBack(file), true); 
}


//! Creates an instance of an UTF-16 xml parser. 
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(IFileReadCallBack* callback,
																	bool deleteCallback)
{
	if (callback && (callback->getSize() >= 0))
	{
		return new CXMLReaderImpl<char16, IXMLBase>(callback, deleteCallback); 
	}
	else
	{
		if(callback && deleteCallback)
			delete callback;

		return 0;
	}
}


//! Creates an instance of an UTF-32 xml parser. 
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(const char* filename)
{
	return createIrrXMLReaderUTF32(new CFileReadCallBack(filename), true); 
}


//! Creates an instance of an UTF-32 xml parser. 
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(FILE* file)
{
	return createIrrXMLReaderUTF32(new CFileReadCallBack(file), true); 
}


//! Creates an instance of an UTF-32 xml parser. 
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(
		IFileReadCallBack* callback, bool deleteCallback)
{
	if (callback && (callback->getSize() >= 0))
	{
		return new CXMLReaderImpl<char32, IXMLBase>(callback, deleteCallback); 
	}
	else
	{
		if(callback && deleteCallback)
			delete callback;

		return 0;
	}
}


} // end namespace io
} // end namespace irr