#include "irrXML.h"
#include "irrString.h"
#include "irrArray.h"
#include "fast_atof.h"
#include "CXMLReaderImpl.h"
namespace irr
{
namespace io
{
class CFileReadCallBack : public IFileReadCallBack
{
public:
CFileReadCallBack(const char* filename)
: File(0), Size(-1), Close(true)
{
File = fopen(filename, "rb");
if (File)
getFileSize();
}
CFileReadCallBack(FILE* file)
: File(file), Size(-1), Close(false)
{
if (File)
getFileSize();
}
virtual ~CFileReadCallBack()
{
if (Close && File)
fclose(File);
}
virtual int read(void* buffer, int sizeToRead)
{
if (!File)
return 0;
return (int)fread(buffer, 1, sizeToRead, File);
}
virtual long getSize() const
{
return Size;
}
private:
void getFileSize()
{
fseek(File, 0, SEEK_END);
Size = ftell(File);
fseek(File, 0, SEEK_SET);
}
FILE* File;
long Size;
bool Close;
};
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(const char* filename)
{
return createIrrXMLReader(new CFileReadCallBack(filename), true);
}
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(FILE* file)
{
return createIrrXMLReader(new CFileReadCallBack(file), true);
}
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;
}
}
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(const char* filename)
{
return createIrrXMLReaderUTF16(new CFileReadCallBack(filename), true);
}
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(FILE* file)
{
return createIrrXMLReaderUTF16(new CFileReadCallBack(file), true);
}
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;
}
}
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(const char* filename)
{
return createIrrXMLReaderUTF32(new CFileReadCallBack(filename), true);
}
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(FILE* file)
{
return createIrrXMLReaderUTF32(new CFileReadCallBack(file), true);
}
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;
}
}
}
}