Add IXMLWriterUTF8 to allow writing utf8/ansi XML's.

utf8 is usually the default for XML anyway.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@5692 dfc29bdd-3216-0410-991c-e03cc46cb475
master
cutealien 2019-01-25 19:35:53 +00:00
parent f6da59daf3
commit 391dd912bd
15 changed files with 454 additions and 112 deletions

View File

@ -1,5 +1,6 @@
--------------------------
Changes in 1.9 (not yet released)
- Add IXMLWriterUTF8
- IColladaMeshWriter::writeScene got an additional flag to decide if root should be written.
- _IRR_MATERIAL_MAX_TEXTURES_ now set to 8 by default. So we can use now 8 textures per material without recompiling the engine.
Additionally there's a new global variable irr::video::MATERIAL_MAX_TEXTURES_USED which can be set to lower numbers to avoid most of the costs coming with this for people not needing more textures.

View File

@ -24,6 +24,7 @@
#include "irrString.h"
#include "irrArray.h"
#include "IXMLReader.h"
#include "IXMLWriter.h"
#include "EAttributes.h"
#include "path.h"
@ -35,7 +36,6 @@ namespace video
} // end namespace video
namespace io
{
class IXMLWriter;
//! Provides a generic interface for attributes and their values and the possibility to serialize them
class IAttributes : public virtual IReferenceCounted

View File

@ -7,6 +7,7 @@
#include "IReferenceCounted.h"
#include "IXMLReader.h"
#include "IXMLWriter.h"
#include "IFileArchive.h"
namespace irr
@ -21,7 +22,6 @@ namespace io
class IReadFile;
class IWriteFile;
class IFileList;
class IXMLWriter;
class IAttributes;
@ -354,6 +354,20 @@ public:
See IReferenceCounted::drop() for more information. */
virtual IXMLReaderUTF8* createXMLReaderUTF8(IReadFile* file) =0;
//! Creates a XML Writer from a file which will write ASCII/UTF-8 characters (char*).
/** \return 0, if file could not be opened, otherwise a pointer to the created
IXMLWriter is returned. After use, the reader
has to be deleted using its IXMLWriter::drop() method.
See IReferenceCounted::drop() for more information. */
virtual IXMLWriterUTF8* createXMLWriterUTF8(const path& filename) =0;
//! Creates a XML Writer from a file which will write ASCII/UTF-8 characters (char*).
/** \return 0, if file could not be opened, otherwise a pointer to the created
IXMLWriter is returned. After use, the reader
has to be deleted using its IXMLWriter::drop() method.
See IReferenceCounted::drop() for more information. */
virtual IXMLWriterUTF8* createXMLWriterUTF8(IWriteFile* file) =0;
//! Creates a XML Writer from a file.
/** \return 0, if file could not be opened, otherwise a pointer to the created
IXMLWriter is returned. After use, the reader

View File

@ -12,6 +12,7 @@
#include "EFocusFlags.h"
#include "IEventReceiver.h"
#include "IXMLReader.h"
#include "IXMLWriter.h"
#include "path.h"
namespace irr
@ -21,7 +22,6 @@ namespace irr
namespace io
{
class IXMLWriter;
class IReadFile;
class IWriteFile;
class IFileSystem;

View File

@ -19,6 +19,7 @@
#include "SceneParameters.h"
#include "IGeometryCreator.h"
#include "ISkinnedMesh.h"
#include "IXMLWriter.h"
namespace irr
{

View File

@ -6,72 +6,21 @@
#define __I_XML_WRITER_H_INCLUDED__
#include "IReferenceCounted.h"
#include "irrArray.h"
#include "irrString.h"
#include "irrXML.h"
namespace irr
{
namespace io
{
//! An xml writer for wide characters, derived from IReferenceCounted.
/** Call IFileSystem::createXMLReader(). to create an IXMLWriter */
typedef IIrrXMLWriter<wchar_t, IReferenceCounted> IXMLWriter;
//! Interface providing methods for making it easier to write XML files.
/** This XML Writer writes xml files using in the platform dependent
wchar_t format and sets the xml-encoding correspondingly. */
class IXMLWriter : public virtual IReferenceCounted
{
public:
//! Writes an xml 1.0 header.
/** Looks like &lt;?xml version="1.0"?&gt;. This should always
be called before writing anything other, because also the text
file header for Unicode texts is written out with this method. */
virtual void writeXMLHeader() = 0;
//! Writes an xml element with maximal 5 attributes like "<foo />" or
//! &lt;foo optAttr="value" /&gt;.
/** The element can be empty or not.
\param name: Name of the element
\param empty: Specifies if the element should be empty. Like
"<foo />". If You set this to false, something like this is
written instead: "<foo>".
\param attr1Name: 1st attributes name
\param attr1Value: 1st attributes value
\param attr2Name: 2nd attributes name
\param attr2Value: 2nd attributes value
\param attr3Name: 3rd attributes name
\param attr3Value: 3rd attributes value
\param attr4Name: 4th attributes name
\param attr4Value: 4th attributes value
\param attr5Name: 5th attributes name
\param attr5Value: 5th attributes value */
virtual void writeElement(const wchar_t* name, bool empty=false,
const wchar_t* attr1Name = 0, const wchar_t* attr1Value = 0,
const wchar_t* attr2Name = 0, const wchar_t* attr2Value = 0,
const wchar_t* attr3Name = 0, const wchar_t* attr3Value = 0,
const wchar_t* attr4Name = 0, const wchar_t* attr4Value = 0,
const wchar_t* attr5Name = 0, const wchar_t* attr5Value = 0) = 0;
//! Writes an xml element with any number of attributes
virtual void writeElement(const wchar_t* name, bool empty,
core::array<core::stringw> &names, core::array<core::stringw> &values) = 0;
//! Writes a comment into the xml file
virtual void writeComment(const wchar_t* comment) = 0;
//! Writes the closing tag for an element. Like "</foo>"
virtual void writeClosingTag(const wchar_t* name) = 0;
//! Writes a text into the file.
/** All occurrences of special characters such as
& (&amp;), < (&lt;), > (&gt;), and " (&quot;) are automatically
replaced. */
virtual void writeText(const wchar_t* text) = 0;
//! Writes a line break
virtual void writeLineBreak() = 0;
};
//! An xml writer for ASCII or UTF-8 characters, derived from IReferenceCounted.
/** Call IFileSystem::createXMLReaderUtf8(). to create an IXMLWriter */
typedef IIrrXMLWriter<c8, IReferenceCounted> IXMLWriterUTF8;
} // end namespace io
} // end namespace irr
#endif

View File

@ -7,6 +7,8 @@
#include <stdio.h>
#include "IrrCompileConfig.h"
#include "irrArray.h"
#include "irrString.h"
/** \mainpage irrXML 1.2 API documentation
<div align="center"><img src="logobig.png" ></div>
@ -370,6 +372,65 @@ namespace io
virtual ETEXT_FORMAT getParserFormat() const = 0;
};
//! Interface providing methods for making it easier to write XML files.
template<class char_type, class super_class>
class IIrrXMLWriter : public super_class
{
public:
//! Destructor
virtual ~IIrrXMLWriter() {}
//! Writes an xml 1.0 header.
/** Looks like &lt;?xml version="1.0"?&gt;. This should always
be called before writing anything other, because also the text
file header for Unicode texts is written out with this method. */
virtual void writeXMLHeader() = 0;
//! Writes an xml element with maximal 5 attributes like "<foo />" or
//! &lt;foo optAttr="value" /&gt;.
/** The element can be empty or not.
\param name: Name of the element
\param empty: Specifies if the element should be empty. Like
"<foo />". If You set this to false, something like this is
written instead: "<foo>".
\param attr1Name: 1st attributes name
\param attr1Value: 1st attributes value
\param attr2Name: 2nd attributes name
\param attr2Value: 2nd attributes value
\param attr3Name: 3rd attributes name
\param attr3Value: 3rd attributes value
\param attr4Name: 4th attributes name
\param attr4Value: 4th attributes value
\param attr5Name: 5th attributes name
\param attr5Value: 5th attributes value */
virtual void writeElement(const char_type* name, bool empty=false,
const char_type* attr1Name = 0, const char_type* attr1Value = 0,
const char_type* attr2Name = 0, const char_type* attr2Value = 0,
const char_type* attr3Name = 0, const char_type* attr3Value = 0,
const char_type* attr4Name = 0, const char_type* attr4Value = 0,
const char_type* attr5Name = 0, const char_type* attr5Value = 0) = 0;
//! Writes an xml element with any number of attributes
virtual void writeElement(const char_type* name, bool empty,
core::array<core::string<char_type> > &names, core::array<core::string<char_type> > &values) = 0;
//! Writes a comment into the xml file
virtual void writeComment(const char_type* comment) = 0;
//! Writes the closing tag for an element. Like "</foo>"
virtual void writeClosingTag(const char_type* name) = 0;
//! Writes a text into the file.
/** All occurrences of special characters such as
& (&amp;), < (&lt;), > (&gt;), and " (&quot;) are automatically
replaced. */
virtual void writeText(const char_type* text) = 0;
//! Writes a line break
virtual void writeLineBreak() = 0;
};
template <typename T>
struct xmlChar

View File

@ -9,12 +9,12 @@
#include "S3DVertex.h"
#include "irrMap.h"
#include "IVideoDriver.h"
#include "IXMLWriter.h"
namespace irr
{
namespace io
{
class IXMLWriter;
class IFileSystem;
}

View File

@ -1070,7 +1070,37 @@ IXMLWriter* CFileSystem::createXMLWriter(const io::path& filename)
IXMLWriter* CFileSystem::createXMLWriter(IWriteFile* file)
{
#ifdef _IRR_COMPILE_WITH_XML_
return new CXMLWriter(file);
return createIXMLWriter(file);
#else
noXML();
return 0;
#endif
}
//! Creates a XML Writer from a file.
IXMLWriterUTF8* CFileSystem::createXMLWriterUTF8(const io::path& filename)
{
#ifdef _IRR_COMPILE_WITH_XML_
IWriteFile* file = createAndWriteFile(filename);
IXMLWriterUTF8* writer = 0;
if (file)
{
writer = createXMLWriterUTF8(file);
file->drop();
}
return writer;
#else
noXML();
return 0;
#endif
}
//! Creates a XML Writer from a file.
IXMLWriterUTF8* CFileSystem::createXMLWriterUTF8(IWriteFile* file)
{
#ifdef _IRR_COMPILE_WITH_XML_
return createIXMLWriterUTF8(file);
#else
noXML();
return 0;

View File

@ -145,6 +145,12 @@ public:
//! Creates a XML Writer from a file.
virtual IXMLWriter* createXMLWriter(IWriteFile* file) _IRR_OVERRIDE_;
//! Creates a XML Writer from a file which will write ASCII/UTF-8 characters (char*).
virtual IXMLWriterUTF8* createXMLWriterUTF8(const path& filename) _IRR_OVERRIDE_;
//! Creates a XML Writer from a file which will write ASCII/UTF-8 characters (char*).
virtual IXMLWriterUTF8* createXMLWriterUTF8(IWriteFile* file) _IRR_OVERRIDE_;
//! Creates a new empty collection of attributes, usable for serialization and more.
virtual IAttributes* createEmptyAttributes(video::IVideoDriver* driver) _IRR_OVERRIDE_;

View File

@ -16,10 +16,7 @@
namespace irr
{
namespace io
{
class IXMLWriter;
}
namespace gui
{

View File

@ -9,13 +9,11 @@
#include "S3DVertex.h"
#include "IVideoDriver.h"
#include "IFileSystem.h"
#include "IXMLWriter.h"
namespace irr
{
namespace io
{
class IXMLWriter;
}
namespace scene
{
class IMeshBuffer;

View File

@ -18,7 +18,6 @@ namespace irr
{
namespace io
{
class IXMLWriter;
class IFileSystem;
}
namespace scene

View File

@ -15,30 +15,28 @@ namespace irr
namespace io
{
//! creates an IXMLReader
IXMLWriter* createIXMLWriter(IWriteFile* file)
{
return new CXMLWriter(file);
}
//! creates an IXMLReader
IXMLWriterUTF8* createIXMLWriterUTF8(IWriteFile* file)
{
return new CXMLWriterUTF8(file);
}
//! Constructor
CXMLWriter::CXMLWriter(IWriteFile* file)
: File(file), Tabs(0), TextWrittenLast(false)
: CXMLWriterCommon(file)
{
#ifdef _DEBUG
setDebugName("CXMLWriter");
#endif
if (File)
File->grab();
}
//! Destructor
CXMLWriter::~CXMLWriter()
{
if (File)
File->drop();
}
//! Writes a xml 1.0 header like <?xml version="1.0"?>
void CXMLWriter::writeXMLHeader()
{
@ -189,18 +187,6 @@ void CXMLWriter::writeClosingTag(const wchar_t* name)
TextWrittenLast = false;
}
const CXMLWriter::XMLSpecialCharacters XMLWSChar[] =
{
{ L'&', L"&amp;" },
{ L'<', L"&lt;" },
{ L'>', L"&gt;" },
{ L'"', L"&quot;" },
{ L'\0', 0 }
};
//! Writes a text into the file. All occurrences of special characters like
//! & (&amp;), < (&lt;), > (&gt;), and " (&quot;) are automatically replaced.
void CXMLWriter::writeText(const wchar_t* text)
@ -208,6 +194,15 @@ void CXMLWriter::writeText(const wchar_t* text)
if (!File || !text)
return;
static const CXMLWriter::XMLSpecialCharacters XMLWSChar[] =
{
{ L'&', L"&amp;" },
{ L'<', L"&lt;" },
{ L'>', L"&gt;" },
{ L'"', L"&quot;" },
{ L'\0', 0 }
};
// TODO: we have to get rid of that reserve call as well as it slows down xml-writing seriously.
// Making a member-variable would work, but a lot of memory would stay around after writing.
// So the correct solution is probably using fixed block here and always write when that is full.
@ -255,6 +250,222 @@ void CXMLWriter::writeLineBreak()
}
//! Constructor
CXMLWriterUTF8::CXMLWriterUTF8(IWriteFile* file)
: CXMLWriterCommon(file)
{
#ifdef _DEBUG
setDebugName("CXMLWriter");
#endif
}
//! Writes a xml 1.0 header like <?xml version="1.0"?>
void CXMLWriterUTF8::writeXMLHeader()
{
if (!File)
return;
// No BOM as it's not necessarily utf8
const c8* const p = "<?xml version=\"1.0\"?>";
File->write(p, strlen(p) * sizeof(c8));
writeLineBreak();
TextWrittenLast = false;
}
//! Writes an xml element with maximal 5 attributes
void CXMLWriterUTF8::writeElement(const c8* name, bool empty,
const c8* attr1Name, const c8* attr1Value,
const c8* attr2Name, const c8* attr2Value,
const c8* attr3Name, const c8* attr3Value,
const c8* attr4Name, const c8* attr4Value,
const c8* attr5Name, const c8* attr5Value)
{
if (!File || !name)
return;
if (Tabs > 0)
{
for (int i=0; i<Tabs; ++i)
File->write("\t", sizeof(c8));
}
// write name
File->write("<", sizeof(c8));
File->write(name, strlen(name)*sizeof(c8));
// write attributes
writeAttribute(attr1Name, attr1Value);
writeAttribute(attr2Name, attr2Value);
writeAttribute(attr3Name, attr3Value);
writeAttribute(attr4Name, attr4Value);
writeAttribute(attr5Name, attr5Value);
// write closing tag
if (empty)
File->write(" />", 3*sizeof(c8));
else
{
File->write(">", sizeof(c8));
++Tabs;
}
TextWrittenLast = false;
}
//! Writes an xml element with any number of attributes
void CXMLWriterUTF8::writeElement(const c8* name, bool empty,
core::array<core::stringc> &names,
core::array<core::stringc> &values)
{
if (!File || !name)
return;
if (Tabs > 0)
{
for (int i=0; i<Tabs; ++i)
File->write("\t", sizeof(c8));
}
// write name
File->write("<", sizeof(c8));
File->write(name, strlen(name)*sizeof(c8));
// write attributes
u32 i=0;
for (; i < names.size() && i < values.size(); ++i)
writeAttribute(names[i].c_str(), values[i].c_str());
// write closing tag
if (empty)
File->write(" />", 3*sizeof(c8));
else
{
File->write(">", sizeof(c8));
++Tabs;
}
TextWrittenLast = false;
}
void CXMLWriterUTF8::writeAttribute(const c8* name, const c8* value)
{
if (!name || !value)
return;
File->write(" ", sizeof(c8));
File->write(name, strlen(name)*sizeof(c8));
File->write("=\"", 2*sizeof(c8));
writeText(value);
File->write("\"", sizeof(c8));
}
//! Writes a comment into the xml file
void CXMLWriterUTF8::writeComment(const c8* comment)
{
if (!File || !comment)
return;
File->write("<!--", 4*sizeof(c8));
writeText(comment);
File->write("-->", 3*sizeof(c8));
}
//! Writes the closing tag for an element. Like </foo>
void CXMLWriterUTF8::writeClosingTag(const c8* name)
{
if (!File || !name)
return;
--Tabs;
if (Tabs > 0 && !TextWrittenLast)
{
for (int i=0; i<Tabs; ++i)
File->write("\t", sizeof(c8));
}
File->write("</", 2*sizeof(c8));
File->write(name, strlen(name)*sizeof(c8));
File->write(">", sizeof(c8));
TextWrittenLast = false;
}
//! Writes a text into the file. All occurrences of special characters like
//! & (&amp;), < (&lt;), > (&gt;), and " (&quot;) are automatically replaced.
void CXMLWriterUTF8::writeText(const c8* text)
{
if (!File || !text)
return;
static const CXMLWriterUTF8::XMLSpecialCharacters XMLWSChar[] =
{
{ '&', "&amp;" },
{ '<', "&lt;" },
{ '>', "&gt;" },
{ '"', "&quot;" },
{ '\0', 0 }
};
// TODO: we have to get rid of that reserve call as well as it slows down xml-writing seriously.
// Making a member-variable would work, but a lot of memory would stay around after writing.
// So the correct solution is probably using fixed block here and always write when that is full.
core::stringc s;
s.reserve(strlen(text)+1);
const c8* p = text;
while(*p)
{
// check if it is matching
bool found = false;
for (s32 i=0; XMLWSChar[i].Character != '\0'; ++i)
if (*p == XMLWSChar[i].Character)
{
s.append(XMLWSChar[i].Symbol);
found = true;
break;
}
if (!found)
s.append(*p);
++p;
}
// write new string
File->write(s.c_str(), s.size()*sizeof(c8));
TextWrittenLast = true;
}
//! Writes a line break
void CXMLWriterUTF8::writeLineBreak()
{
if (!File)
return;
#if defined(_IRR_OSX_PLATFORM_)
File->write("\r", sizeof(c8));
#elif defined(_IRR_WINDOWS_API_)
File->write("\r\n", 2*sizeof(c8));
#else
File->write("\n", sizeof(c8));
#endif
}
} // end namespace irr
} // end namespace io

View File

@ -17,8 +17,49 @@ namespace irr
namespace io
{
//! Interface providing methods for making it easier to write XML files.
class CXMLWriter : public IXMLWriter
//! creates an IXMLReader
IXMLWriter* createIXMLWriter(IWriteFile* file);
//! creates an IXMLReader
IXMLWriterUTF8* createIXMLWriterUTF8(IWriteFile* file);
// Stuff needed by implementations for all character types
// TODO: With some more work it could maybe become a pure template based thing like CXMLReaderImpl
// and replace the type based writer implementations. Sorry, too lazy for now :-/
template<class char_type>
class CXMLWriterCommon
{
public:
//! Constructor
CXMLWriterCommon(IWriteFile* file): File(file), Tabs(0), TextWrittenLast(false)
{
if (File)
File->grab();
}
//! Destructor
virtual ~CXMLWriterCommon()
{
if (File)
File->drop();
}
struct XMLSpecialCharacters
{
char_type Character;
const char_type* Symbol;
};
protected:
IWriteFile* File;
s32 Tabs;
bool TextWrittenLast;
};
//! Implementation providing methods for making it easier to write XML files.
class CXMLWriter : public IXMLWriter, public CXMLWriterCommon<wchar_t>
{
public:
@ -26,7 +67,7 @@ namespace io
CXMLWriter(IWriteFile* file);
//! Destructor
virtual ~CXMLWriter();
virtual ~CXMLWriter() {}
//! Writes a xml 1.0 header like <?xml version="1.0"?>
virtual void writeXMLHeader() _IRR_OVERRIDE_;
@ -56,22 +97,56 @@ namespace io
//! Writes a line break
virtual void writeLineBreak() _IRR_OVERRIDE_;
struct XMLSpecialCharacters
{
wchar_t Character;
const wchar_t* Symbol;
};
private:
void writeAttribute(const wchar_t* att, const wchar_t* name);
IWriteFile* File;
s32 Tabs;
bool TextWrittenLast;
};
//! Implementation providing methods for making it easier to write XML files.
class CXMLWriterUTF8 : public IXMLWriterUTF8, public CXMLWriterCommon<c8>
{
public:
//! Constructor
CXMLWriterUTF8(IWriteFile* file);
//! Destructor
virtual ~CXMLWriterUTF8() {}
//! Writes a xml 1.0 header like <?xml version="1.0"?>
virtual void writeXMLHeader() _IRR_OVERRIDE_;
//! Writes an xml element with maximal 5 attributes
virtual void writeElement(const c8* name, bool empty=false,
const c8* attr1Name = 0, const c8* attr1Value = 0,
const c8* attr2Name = 0, const c8* attr2Value = 0,
const c8* attr3Name = 0, const c8* attr3Value = 0,
const c8* attr4Name = 0, const c8* attr4Value = 0,
const c8* attr5Name = 0, const c8* attr5Value = 0) _IRR_OVERRIDE_;
//! Writes an xml element with any number of attributes
virtual void writeElement(const c8* name, bool empty,
core::array<core::stringc> &names, core::array<core::stringc> &values) _IRR_OVERRIDE_;
//! Writes a comment into the xml file
virtual void writeComment(const c8* comment) _IRR_OVERRIDE_;
//! Writes the closing tag for an element. Like </foo>
virtual void writeClosingTag(const c8* name) _IRR_OVERRIDE_;
//! Writes a text into the file. All occurrences of special characters like
//! & (&amp;), < (&lt;), > (&gt;), and " (&quot;) are automatically replaced.
virtual void writeText(const c8* text) _IRR_OVERRIDE_;
//! Writes a line break
virtual void writeLineBreak() _IRR_OVERRIDE_;
private:
void writeAttribute(const c8* att, const c8* name);
};
} // end namespace irr
} // end namespace io