2016-06-03 20:31:34 +02:00
|
|
|
#ifndef _SQXML_DOCUMENT_HPP_
|
|
|
|
#define _SQXML_DOCUMENT_HPP_
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-03 20:31:34 +02:00
|
|
|
#include "Handle/Document.hpp"
|
|
|
|
#include "Wrapper/ParseResult.hpp"
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Class that can read/write and alter the contents of XML files.
|
|
|
|
*/
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
typedef xml_document Type;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor. (disabled)
|
|
|
|
*/
|
|
|
|
Document(const Document & o);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator. (disabled)
|
|
|
|
*/
|
|
|
|
Document & operator = (const Document & o);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See if the document is allowed to overwrite its contents and throw an error if not.
|
|
|
|
*/
|
2016-02-28 16:39:26 +01:00
|
|
|
void CanLoad() const;
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
2016-04-02 10:04:28 +02:00
|
|
|
DocumentRef m_Doc; // The main xml document instance.
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
Document()
|
2016-04-02 10:04:28 +02:00
|
|
|
: m_Doc(nullptr)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~Document()
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to compare two instances of this type.
|
|
|
|
*/
|
|
|
|
Int32 Cmp(const Document & o) const
|
|
|
|
{
|
|
|
|
if (m_Doc && !o.m_Doc)
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return 1;
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
else if (!m_Doc && o.m_Doc)
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return -1;
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
else if (!m_Doc && !o.m_Doc)
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return 0;
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
else if (*m_Doc == *o.m_Doc)
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return 0;
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
else if (*m_Doc > *o.m_Doc)
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return 1;
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
else
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return -1;
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to convert an instance of this type to a string.
|
|
|
|
*/
|
|
|
|
CSStr ToString() const
|
|
|
|
{
|
2016-04-02 10:04:28 +02:00
|
|
|
// Do we manage a valid document?
|
2016-02-27 10:57:10 +01:00
|
|
|
if (m_Doc)
|
2016-04-02 10:04:28 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return m_Doc->name();
|
2016-04-02 10:04:28 +02:00
|
|
|
}
|
|
|
|
// Default to an empty name
|
2016-02-27 10:57:10 +01:00
|
|
|
return _SC("");
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:39:26 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to retrieve the name from instances of this type.
|
|
|
|
*/
|
|
|
|
static SQInteger Typename(HSQUIRRELVM vm);
|
|
|
|
|
2016-02-27 10:57:10 +01:00
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* See whether this instance references a valid xml document.
|
|
|
|
*/
|
|
|
|
bool IsValid() const
|
|
|
|
{
|
|
|
|
return m_Doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Return the number of active references to this document instance.
|
|
|
|
*/
|
|
|
|
Uint32 GetRefCount() const
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
return m_Doc.Count();
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Removes all nodes, leaving the empty document.
|
|
|
|
*/
|
|
|
|
void Reset()
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Validate the document handle
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc.Validate();
|
2016-02-28 16:39:26 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Doc->reset();
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Removes all nodes, then copies the entire contents of the specified document.
|
|
|
|
*/
|
|
|
|
void Reset(const Document & doc)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Validate the document handles
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc.Validate();
|
|
|
|
doc.m_Doc.Validate();
|
2016-02-28 16:39:26 +01:00
|
|
|
// Perform the requested operation
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc->reset(*(doc.m_Doc));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Load document from zero-terminated string. (LoadString collides with the windows api)
|
|
|
|
*/
|
|
|
|
ParseResult LoadData(CSStr source)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Make sure that we are allowed to load in data
|
|
|
|
CanLoad();
|
|
|
|
// Perform the requested operation and return the result
|
|
|
|
return ParseResult(m_Doc, m_Doc->load_string(source));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Load document from zero-terminated string. (LoadString collides with the windows api)
|
|
|
|
*/
|
|
|
|
ParseResult LoadData(CSStr source, Uint32 options)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Make sure that we are allowed to load in data
|
|
|
|
CanLoad();
|
|
|
|
// Perform the requested operation and return the result
|
|
|
|
return ParseResult(m_Doc, m_Doc->load_string(source, options));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Load document from file on disk.
|
|
|
|
*/
|
|
|
|
ParseResult LoadFile(CSStr filepath)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Make sure that we are allowed to load in data
|
|
|
|
CanLoad();
|
|
|
|
// Perform the requested operation and return the result
|
|
|
|
return ParseResult(m_Doc, m_Doc->load_file(filepath));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Load document from file on disk.
|
|
|
|
*/
|
|
|
|
ParseResult LoadFile(CSStr filepath, Uint32 options)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Make sure that we are allowed to load in data
|
|
|
|
CanLoad();
|
|
|
|
// Perform the requested operation and return the result
|
|
|
|
return ParseResult(m_Doc, m_Doc->load_file(filepath, options));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Load document from file on disk.
|
|
|
|
*/
|
|
|
|
ParseResult LoadFile(CSStr filepath, Uint32 options, Int32 encoding)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Make sure that we are allowed to load in data
|
|
|
|
CanLoad();
|
|
|
|
// Perform the requested operation and return the result
|
2016-04-02 10:04:28 +02:00
|
|
|
return ParseResult(m_Doc, m_Doc->load_file(filepath, options,
|
|
|
|
static_cast< xml_encoding >(encoding)));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Save XML to file on disk.
|
|
|
|
*/
|
|
|
|
void SaveFile(CSStr filepath)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Validate the document handle
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc.Validate();
|
2016-02-28 16:39:26 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Doc->save_file(filepath);
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Save XML to file on disk.
|
|
|
|
*/
|
|
|
|
void SaveFile(CSStr filepath, CSStr indent)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Validate the document handle
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc.Validate();
|
2016-02-28 16:39:26 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Doc->save_file(filepath, indent);
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Save XML to file on disk.
|
|
|
|
*/
|
|
|
|
void SaveFile(CSStr filepath, CSStr indent, Uint32 format)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Validate the document handle
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc.Validate();
|
2016-02-28 16:39:26 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Doc->save_file(filepath, indent, format);
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Save XML to file on disk.
|
|
|
|
*/
|
|
|
|
void SaveFile(CSStr filepath, CSStr indent, Uint32 format, Int32 encoding)
|
|
|
|
{
|
2016-02-28 16:39:26 +01:00
|
|
|
// Validate the document handle
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc.Validate();
|
2016-02-28 16:39:26 +01:00
|
|
|
// Perform the requested operation
|
2016-04-02 10:04:28 +02:00
|
|
|
m_Doc->save_file(filepath, indent, format, static_cast< xml_encoding >(encoding));
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the document root node.
|
|
|
|
*/
|
|
|
|
Node GetNode() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
2016-06-03 20:31:34 +02:00
|
|
|
#endif // _SQXML_DOCUMENT_HPP_
|