[DEV] change idea std::u32string to std::string

This commit is contained in:
Edouard DUPIN 2013-11-12 21:58:13 +01:00
parent 2325808997
commit 4d02dd677e
17 changed files with 295 additions and 156 deletions

View File

@ -13,13 +13,25 @@
#undef __class__ #undef __class__
#define __class__ "Attribute" #define __class__ "Attribute"
exml::Attribute::Attribute(const std::u32string& _name, const std::u32string& _value) : exml::Attribute::Attribute(const std::string& _name, const std::string& _value) :
exml::Node(_value), exml::Node(_value),
m_name(_name) { m_name(_name) {
} }
bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { exml::Attribute::Attribute(const std::u32string& _name, const std::u32string& _value) :
exml::Node(_value) {
m_name = to_u8string(_name);
}
void exml::Attribute::setName(const std::u32string& _name) {
m_name = to_u8string(_name);
};
std::u32string exml::Attribute::getUName(void) const {
return to_u32string(m_name);
};
bool exml::Attribute::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) {
EXML_VERBOSE("start parse : 'attribute'"); EXML_VERBOSE("start parse : 'attribute'");
m_pos = _filePos; m_pos = _filePos;
// search end of the comment : // search end of the comment :
@ -35,7 +47,7 @@ bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _c
break; break;
} }
} }
m_name = std::u32string(_data, _pos, lastElementName+1); m_name = std::string(_data, _pos, lastElementName+1);
if (true == _caseSensitive) { if (true == _caseSensitive) {
m_name = to_lower(m_name); m_name = to_lower(m_name);
} }
@ -44,18 +56,18 @@ bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _c
int32_t white = countWhiteChar(_data, lastElementName+1, tmpPos); int32_t white = countWhiteChar(_data, lastElementName+1, tmpPos);
_filePos += tmpPos; _filePos += tmpPos;
if (lastElementName+white+1 >= _data.size()) { if (lastElementName+white+1 >= _data.size()) {
CREATE_ERROR(_doc, _data, lastElementName+white+1, _filePos, U" parse an xml end with an attribute parsing..."); CREATE_ERROR(_doc, _data, lastElementName+white+1, _filePos, " parse an xml end with an attribute parsing...");
return false; return false;
} }
if (_data[lastElementName+white+1] != '=') { if (_data[lastElementName+white+1] != '=') {
CREATE_ERROR(_doc, _data, lastElementName+white+1, _filePos, U" error attribute parsing == > missing '=' ..."); CREATE_ERROR(_doc, _data, lastElementName+white+1, _filePos, " error attribute parsing == > missing '=' ...");
return false; return false;
} }
white += countWhiteChar(_data, lastElementName+white+2, tmpPos); white += countWhiteChar(_data, lastElementName+white+2, tmpPos);
_filePos += tmpPos; _filePos += tmpPos;
if (lastElementName+white+2>=_data.size()) { if (lastElementName+white+2>=_data.size()) {
CREATE_ERROR(_doc, _data, lastElementName+white+2, _filePos, U" parse an xml end with an attribute parsing..."); CREATE_ERROR(_doc, _data, lastElementName+white+2, _filePos, " parse an xml end with an attribute parsing...");
return false; return false;
} }
if (_data[lastElementName+white+2] != '"') { if (_data[lastElementName+white+2] != '"') {
@ -67,7 +79,7 @@ bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _c
drawElementParsed(_data[iii], _filePos); drawElementParsed(_data[iii], _filePos);
#endif #endif
if (_filePos.check(_data[iii]) == true) { if (_filePos.check(_data[iii]) == true) {
CREATE_ERROR(_doc, _data, iii, _filePos, U"unexpected '\\n' in an attribute parsing"); CREATE_ERROR(_doc, _data, iii, _filePos, "unexpected '\\n' in an attribute parsing");
return false; return false;
} }
if( _data[iii] != ' ' if( _data[iii] != ' '
@ -79,7 +91,7 @@ bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _c
break; break;
} }
} }
m_value = std::u32string(_data, lastElementName+white+2, lastAttributePos); m_value = std::string(_data, lastElementName+white+2, lastAttributePos);
EXML_PARSE_ATTRIBUTE(m_pos << " attribute : " << m_name << "=\"" << m_value << "\""); EXML_PARSE_ATTRIBUTE(m_pos << " attribute : " << m_name << "=\"" << m_value << "\"");
@ -98,7 +110,7 @@ bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _c
break; break;
} }
} }
m_value = std::u32string(_data, lastElementName+white+3, lastAttributePos); m_value = std::string(_data, lastElementName+white+3, lastAttributePos);
EXML_PARSE_ATTRIBUTE(m_pos << " attribute : " << m_name << "=\"" << m_value << "\""); EXML_PARSE_ATTRIBUTE(m_pos << " attribute : " << m_name << "=\"" << m_value << "\"");
@ -106,16 +118,16 @@ bool exml::Attribute::iParse(const std::u32string& _data, int32_t& _pos, bool _c
return true; return true;
} }
bool exml::Attribute::iGenerate(std::u32string& _data, int32_t _indent) const { bool exml::Attribute::iGenerate(std::string& _data, int32_t _indent) const {
_data += U" "; _data += " ";
_data += m_name; _data += m_name;
_data += U"=\""; _data += "=\"";
_data += m_value; _data += m_value;
_data += U"\""; _data += "\"";
return true; return true;
} }
void exml::Attribute::clear(void) { void exml::Attribute::clear(void) {
m_name = U""; m_name = "";
} }

View File

@ -24,34 +24,37 @@ namespace exml {
* @param[in] _name Name of the attribute. * @param[in] _name Name of the attribute.
* @param[in] _value Value of the attribute. * @param[in] _value Value of the attribute.
*/ */
Attribute(const std::string& _name, const std::string& _value);
Attribute(const std::u32string& _name, const std::u32string& _value); Attribute(const std::u32string& _name, const std::u32string& _value);
/** /**
* @brief Destructor * @brief Destructor
*/ */
virtual ~Attribute(void) { }; virtual ~Attribute(void) { };
protected: protected:
std::u32string m_name; std::string m_name;
public: public:
/** /**
* @brief set the name of the attribute * @brief set the name of the attribute
* @param[in] _name New name of the attribute * @param[in] _name New name of the attribute
*/ */
virtual void setName(std::u32string _name) { virtual void setName(const std::string& _name) {
m_name = _name; m_name = _name;
}; };
virtual void setName(const std::u32string& _name);
/** /**
* @brief get the current name of the Attribute * @brief get the current name of the Attribute
* @return String of the attribute * @return String of the attribute
*/ */
virtual const std::u32string& getName(void) const { virtual const std::string& getName(void) const {
return m_name; return m_name;
}; };
virtual std::u32string getUName(void) const;
public: // herited function: public: // herited function:
virtual enum nodeType getType(void) const { virtual enum nodeType getType(void) const {
return exml::typeAttribute; return exml::typeAttribute;
}; };
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc);
virtual bool iGenerate(std::u32string& _data, int32_t _indent) const; virtual bool iGenerate(std::string& _data, int32_t _indent) const;
virtual exml::Attribute* toAttribute(void) { virtual exml::Attribute* toAttribute(void) {
return this; return this;
}; };

View File

@ -50,8 +50,8 @@ void exml::AttributeList::appendAttribute(exml::Attribute* _attr) {
m_listAttribute.push_back(_attr); m_listAttribute.push_back(_attr);
} }
const std::u32string& exml::AttributeList::getAttribute(const std::u32string& _name) const { const std::string& exml::AttributeList::getAttribute(const std::string& _name) const {
static const std::u32string errorReturn(U""); static const std::string errorReturn("");
if (_name.size() == 0) { if (_name.size() == 0) {
return errorReturn; return errorReturn;
} }
@ -64,7 +64,21 @@ const std::u32string& exml::AttributeList::getAttribute(const std::u32string& _n
return errorReturn; return errorReturn;
} }
bool exml::AttributeList::existAttribute(const std::u32string& _name) const { std::u32string exml::AttributeList::getAttribute(const std::u32string& _name) const {
static const std::u32string errorReturn(U"");
if (_name.size() == 0) {
return errorReturn;
}
for (int32_t iii=0; iii<m_listAttribute.size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->getUName() == _name) {
return m_listAttribute[iii]->getUValue();
}
}
return errorReturn;
}
bool exml::AttributeList::existAttribute(const std::string& _name) const {
if (_name.size() == 0) { if (_name.size() == 0) {
return false; return false;
} }
@ -77,7 +91,20 @@ bool exml::AttributeList::existAttribute(const std::u32string& _name) const {
return false; return false;
} }
void exml::AttributeList::setAttribute(const std::u32string& _name, const std::u32string& _value) { bool exml::AttributeList::existAttribute(const std::u32string& _name) const {
if (_name.size() == 0) {
return false;
}
for (int32_t iii=0; iii<m_listAttribute.size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->getUName() == _name) {
return true;
}
}
return false;
}
void exml::AttributeList::setAttribute(const std::string& _name, const std::string& _value) {
// check if attribute already det : // check if attribute already det :
for (int32_t iii=0; iii<m_listAttribute.size(); iii++) { for (int32_t iii=0; iii<m_listAttribute.size(); iii++) {
if( NULL != m_listAttribute[iii] if( NULL != m_listAttribute[iii]
@ -94,7 +121,24 @@ void exml::AttributeList::setAttribute(const std::u32string& _name, const std::u
m_listAttribute.push_back(attr); m_listAttribute.push_back(attr);
} }
bool exml::AttributeList::iGenerate(std::u32string& _data, int32_t _indent) const { void exml::AttributeList::setAttribute(const std::u32string& _name, const std::u32string& _value) {
// check if attribute already det :
for (int32_t iii=0; iii<m_listAttribute.size(); iii++) {
if( NULL != m_listAttribute[iii]
&& m_listAttribute[iii]->getUName() == _name) {
// update the value :
m_listAttribute[iii]->setValue(_value);
return;
}
}
exml::Attribute* attr = new exml::Attribute(_name, _value);
if (NULL == attr) {
EXML_ERROR("memory allocation error...");
}
m_listAttribute.push_back(attr);
}
bool exml::AttributeList::iGenerate(std::string& _data, int32_t _indent) const {
for (int32_t iii=0; iii<m_listAttribute.size(); iii++) { for (int32_t iii=0; iii<m_listAttribute.size(); iii++) {
if (NULL!=m_listAttribute[iii]) { if (NULL!=m_listAttribute[iii]) {
m_listAttribute[iii]->iGenerate(_data, _indent); m_listAttribute[iii]->iGenerate(_data, _indent);

View File

@ -24,6 +24,10 @@ namespace exml {
* @brief Constructor * @brief Constructor
* @param[in] _value Node value; * @param[in] _value Node value;
*/ */
AttributeList(const std::string& _value) :
exml::Node(_value) {
};
AttributeList(const std::u32string& _value) : AttributeList(const std::u32string& _value) :
exml::Node(_value) { exml::Node(_value) {
@ -59,21 +63,24 @@ namespace exml {
* @param[in] _name Attribute Name. * @param[in] _name Attribute Name.
* @return Value of the attribute or no data in the string * @return Value of the attribute or no data in the string
*/ */
const std::u32string& getAttribute(const std::u32string& _name) const; const std::string& getAttribute(const std::string& _name) const;
std::u32string getAttribute(const std::u32string& _name) const;
/** /**
* @brief check if an attribute exist or not with his name. * @brief check if an attribute exist or not with his name.
* @param[in] _name Attribute Name. * @param[in] _name Attribute Name.
* @return true if the attribute exist or False * @return true if the attribute exist or False
*/ */
bool existAttribute(const std::string& _name) const;
bool existAttribute(const std::u32string& _name) const; bool existAttribute(const std::u32string& _name) const;
/** /**
* @brief Sen A new attribute or replace data of the previous one * @brief Sen A new attribute or replace data of the previous one
* @param[in] _name Name of the attribute * @param[in] _name Name of the attribute
* @param[in] _value Value of the attribute * @param[in] _value Value of the attribute
*/ */
void setAttribute(const std::string& _name, const std::string& _value);
void setAttribute(const std::u32string& _name, const std::u32string& _value); void setAttribute(const std::u32string& _name, const std::u32string& _value);
public: // herited function: public: // herited function:
bool iGenerate(std::u32string& _data, int32_t _indent) const; bool iGenerate(std::string& _data, int32_t _indent) const;
virtual void clear(void); virtual void clear(void);
}; };
}; };

View File

@ -13,7 +13,7 @@
#undef __class__ #undef __class__
#define __class__ "Comment" #define __class__ "Comment"
bool exml::Comment::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { bool exml::Comment::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) {
EXML_VERBOSE("start parse : 'comment'"); EXML_VERBOSE("start parse : 'comment'");
m_pos = _filePos; m_pos = _filePos;
exml::filePos tmpPos; exml::filePos tmpPos;
@ -41,21 +41,21 @@ bool exml::Comment::iParse(const std::u32string& _data, int32_t& _pos, bool _cas
} }
} }
// find end of value: // find end of value:
m_value = std::u32string(_data, _pos+white, newEnd); m_value = std::string(_data, _pos+white, newEnd);
EXML_VERBOSE(" find comment '" << m_value << "'"); EXML_VERBOSE(" find comment '" << m_value << "'");
_pos = iii+2; _pos = iii+2;
return true; return true;
} }
} }
_pos = _data.size(); _pos = _data.size();
CREATE_ERROR(_doc, _data, _pos, _filePos, U"comment got end of file without finding end node"); CREATE_ERROR(_doc, _data, _pos, _filePos, "comment got end of file without finding end node");
return false; return false;
} }
bool exml::Comment::iGenerate(std::u32string& _data, int32_t _indent) const { bool exml::Comment::iGenerate(std::string& _data, int32_t _indent) const {
addIndent(_data, _indent); addIndent(_data, _indent);
_data += U"<!--"; _data += "<!--";
_data += m_value; _data += m_value;
_data += U"-->\n"; _data += "-->\n";
return true; return true;
} }

View File

@ -23,6 +23,10 @@ namespace exml {
* @brief Constructor * @brief Constructor
* @param[in] _value comment value * @param[in] _value comment value
*/ */
Comment(const std::string& _value) :
exml::Node(_value) {
};
Comment(const std::u32string& _value) : Comment(const std::u32string& _value) :
exml::Node(_value) { exml::Node(_value) {
@ -35,8 +39,8 @@ namespace exml {
virtual enum nodeType getType(void) const { virtual enum nodeType getType(void) const {
return typeAttribute; return typeAttribute;
}; };
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc);
virtual bool iGenerate(std::u32string& _data, int32_t _indent) const; virtual bool iGenerate(std::string& _data, int32_t _indent) const;
virtual exml::Comment* toComment(void) { virtual exml::Comment* toComment(void) {
return this; return this;
}; };

View File

@ -20,34 +20,52 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
*/ */
exml::DeclarationXML::DeclarationXML(const std::u32string& _version, enum unicode::charset _format, bool _standalone) : exml::DeclarationXML::DeclarationXML(const std::string& _version, enum unicode::charset _format, bool _standalone) :
exml::Declaration(U"xml") { exml::Declaration("xml") {
if (_version.size()!=0) { if (_version.size()!=0) {
setAttribute(U"version", _version); setAttribute("version", _version);
} }
if (_format!=unicode::charsetUTF8) { if (_format!=unicode::charsetUTF8) {
setAttribute(U"encoding", U"UTF-8"); setAttribute("encoding", "UTF-8");
} else { } else {
EXML_ERROR("Actually does not supported other charset than UTF8"); EXML_ERROR("Actually does not supported other charset than UTF8");
setAttribute(U"encoding", U"UTF-8"); setAttribute("encoding", "UTF-8");
} }
if (_standalone == true) { if (_standalone == true) {
setAttribute(U"standalone", U"true"); setAttribute("standalone", "true");
} else { } else {
setAttribute(U"standalone", U"true"); setAttribute("standalone", "true");
} }
} }
bool exml::Declaration::iGenerate(std::u32string& _data, int32_t _indent) const { exml::DeclarationXML::DeclarationXML(const std::u32string& _version, enum unicode::charset _format, bool _standalone) :
exml::Declaration("xml") {
if (_version.size()!=0) {
setAttribute("version", to_u8string(_version));
}
if (_format!=unicode::charsetUTF8) {
setAttribute("encoding", "UTF-8");
} else {
EXML_ERROR("Actually does not supported other charset than UTF8");
setAttribute("encoding", "UTF-8");
}
if (_standalone == true) {
setAttribute("standalone", "true");
} else {
setAttribute("standalone", "true");
}
}
bool exml::Declaration::iGenerate(std::string& _data, int32_t _indent) const {
addIndent(_data, _indent); addIndent(_data, _indent);
_data += U"<?"; _data += "<?";
_data += m_value; _data += m_value;
exml::AttributeList::iGenerate(_data, _indent); exml::AttributeList::iGenerate(_data, _indent);
_data += U"?>\n"; _data += "?>\n";
return true; return true;
} }
bool exml::Declaration::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { bool exml::Declaration::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) {
EXML_VERBOSE("start parse : 'declaration' : '" << m_value << "'"); EXML_VERBOSE("start parse : 'declaration' : '" << m_value << "'");
m_pos = _filePos; m_pos = _filePos;
// search end of the comment : // search end of the comment :
@ -61,7 +79,7 @@ bool exml::Declaration::iParse(const std::u32string& _data, int32_t& _pos, bool
if( _data[iii] == '>' if( _data[iii] == '>'
|| _data[iii] == '<') { || _data[iii] == '<') {
// an error occured : // an error occured :
CREATE_ERROR(_doc, _data, _pos, _filePos, U" find '>' or '<' instead of '?>'"); CREATE_ERROR(_doc, _data, _pos, _filePos, " find '>' or '<' instead of '?>'");
return false; return false;
} }
if( _data[iii] == '?' if( _data[iii] == '?'
@ -75,7 +93,7 @@ bool exml::Declaration::iParse(const std::u32string& _data, int32_t& _pos, bool
// we find an attibute == > create a new and parse it : // we find an attibute == > create a new and parse it :
exml::Attribute* attribute = new exml::Attribute(); exml::Attribute* attribute = new exml::Attribute();
if (NULL == attribute) { if (NULL == attribute) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U" Allocation error ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, " Allocation error ...");
return false; return false;
} }
_pos = iii; _pos = iii;
@ -88,7 +106,7 @@ bool exml::Declaration::iParse(const std::u32string& _data, int32_t& _pos, bool
continue; continue;
} }
} }
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Text got end of file without finding end node"); CREATE_ERROR(_doc, _data, _pos, _filePos, "Text got end of file without finding end node");
_pos = _data.size(); _pos = _data.size();
return false; return false;
} }

View File

@ -23,6 +23,10 @@ namespace exml {
* @brief Constructor * @brief Constructor
* @param[in] _name name of the declaration (xml, xml:xxxx ...) * @param[in] _name name of the declaration (xml, xml:xxxx ...)
*/ */
Declaration(const std::string& _name) :
exml::AttributeList(_name) {
};
Declaration(const std::u32string& _name) : Declaration(const std::u32string& _name) :
exml::AttributeList(_name) { exml::AttributeList(_name) {
@ -35,8 +39,8 @@ namespace exml {
virtual enum nodeType getType(void) const { virtual enum nodeType getType(void) const {
return typeAttribute; return typeAttribute;
}; };
virtual bool iGenerate(std::u32string& _data, int32_t _indent) const; virtual bool iGenerate(std::string& _data, int32_t _indent) const;
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc);
virtual exml::Declaration* toDeclaration(void) { virtual exml::Declaration* toDeclaration(void) {
return this; return this;
}; };
@ -52,6 +56,7 @@ namespace exml {
* @param[in] _format charset of the XML * @param[in] _format charset of the XML
* @param[in] _standalone this document is standalone * @param[in] _standalone this document is standalone
*/ */
DeclarationXML(const std::string& _version, enum unicode::charset _format=unicode::charsetUTF8, bool _standalone=true);
DeclarationXML(const std::u32string& _version, enum unicode::charset _format=unicode::charsetUTF8, bool _standalone=true); DeclarationXML(const std::u32string& _version, enum unicode::charset _format=unicode::charsetUTF8, bool _standalone=true);
/** /**
* @brief Destructor * @brief Destructor

View File

@ -17,14 +17,14 @@ exml::Document::Document(void) :
m_charset(unicode::charsetUTF8), m_charset(unicode::charsetUTF8),
m_caseSensitive(false), m_caseSensitive(false),
m_writeErrorWhenDetexted(true), m_writeErrorWhenDetexted(true),
m_comment(U""), m_comment(""),
m_Line(U""), m_Line(""),
m_filePos(0,0) { m_filePos(0,0) {
} }
bool exml::Document::iGenerate(std::u32string& _data, int32_t _indent) const { bool exml::Document::iGenerate(std::string& _data, int32_t _indent) const {
for (int32_t iii=0; iii<m_listSub.size(); iii++) { for (int32_t iii=0; iii<m_listSub.size(); iii++) {
if (NULL!=m_listSub[iii]) { if (NULL!=m_listSub[iii]) {
m_listSub[iii]->iGenerate(_data, _indent); m_listSub[iii]->iGenerate(_data, _indent);
@ -34,6 +34,9 @@ bool exml::Document::iGenerate(std::u32string& _data, int32_t _indent) const {
} }
bool exml::Document::parse(const std::u32string& _data) { bool exml::Document::parse(const std::u32string& _data) {
return parse(to_u8string(_data));
}
bool exml::Document::parse(const std::string& _data) {
EXML_VERBOSE("Start parsing document (type: string) size=" << _data.size()); EXML_VERBOSE("Start parsing document (type: string) size=" << _data.size());
clear(); clear();
// came from char == > force in utf8 ... // came from char == > force in utf8 ...
@ -45,11 +48,20 @@ bool exml::Document::parse(const std::u32string& _data) {
} }
bool exml::Document::generate(std::u32string& _data) { bool exml::Document::generate(std::u32string& _data) {
_data = U""; std::string data;
bool ret = generate(data);
_data = to_u32string(data);
return ret;
}
bool exml::Document::generate(std::string& _data) {
_data = "";
return iGenerate(_data,0); return iGenerate(_data,0);
} }
bool exml::Document::load(const std::u32string& _file) { bool exml::Document::load(const std::u32string& _data) {
return load(to_u8string(_data));
}
bool exml::Document::load(const std::string& _file) {
// Start loading the XML : // Start loading the XML :
EXML_VERBOSE("open file (xml) \"" << _file << "\""); EXML_VERBOSE("open file (xml) \"" << _file << "\"");
clear(); clear();
@ -81,7 +93,7 @@ bool exml::Document::load(const std::u32string& _file) {
tmpFile.fileClose(); tmpFile.fileClose();
// convert in UTF8 : // convert in UTF8 :
std::u32string tmpDataUnicode(to_u32string(fileBuffer)); std::string tmpDataUnicode(fileBuffer);
// remove temporary buffer: // remove temporary buffer:
delete(fileBuffer); delete(fileBuffer);
// parse the data : // parse the data :
@ -90,8 +102,11 @@ bool exml::Document::load(const std::u32string& _file) {
return ret; return ret;
} }
bool exml::Document::store(const std::u32string& _file) { bool exml::Document::store(const std::u32string& _data) {
std::u32string createData; return store(to_u8string(_data));
}
bool exml::Document::store(const std::string& _file) {
std::string createData;
if (false == generate(createData)) { if (false == generate(createData)) {
EXML_ERROR("Error while creating the XML : " << _file); EXML_ERROR("Error while creating the XML : " << _file);
return false; return false;
@ -101,8 +116,7 @@ bool exml::Document::store(const std::u32string& _file) {
EXML_ERROR("Can not open (w) the file : " << _file); EXML_ERROR("Can not open (w) the file : " << _file);
return false; return false;
} }
std::string endTable = to_u8string(createData); if (tmpFile.fileWrite((char*)createData.c_str(), sizeof(char), createData.size()) != createData.size()) {
if (tmpFile.fileWrite((char*)endTable.c_str(), sizeof(char), endTable.size()) != endTable.size()) {
EXML_ERROR("Error while writing output XML file : " << _file); EXML_ERROR("Error while writing output XML file : " << _file);
tmpFile.fileClose(); tmpFile.fileClose();
return false; return false;
@ -112,25 +126,25 @@ bool exml::Document::store(const std::u32string& _file) {
} }
void exml::Document::display(void) { void exml::Document::display(void) {
std::u32string tmpp; std::string tmpp;
iGenerate(tmpp, 0); iGenerate(tmpp, 0);
EXML_INFO("Generated XML : \n" << tmpp); EXML_INFO("Generated XML : \n" << tmpp);
} }
std::u32string createPosPointer(const std::u32string& _line, int32_t _pos) { std::string createPosPointer(const std::string& _line, int32_t _pos) {
std::u32string out; std::string out;
int32_t iii; int32_t iii;
for (iii=0; iii<_pos && iii<_line.size(); iii++) { for (iii=0; iii<_pos && iii<_line.size(); iii++) {
if (_line[iii] == '\t') { if (_line[iii] == '\t') {
out += U"\t"; out += "\t";
} else { } else {
out += U" "; out += " ";
} }
} }
for (; iii<_pos; iii++) { for (; iii<_pos; iii++) {
out += U" "; out += " ";
} }
out += U"^"; out += "^";
return out; return out;
} }
@ -147,7 +161,7 @@ void exml::Document::displayError(void) {
#endif #endif
} }
void exml::Document::createError(const std::u32string& _data, int32_t _pos, const exml::filePos& _filePos, const std::u32string& _comment) { void exml::Document::createError(const std::string& _data, int32_t _pos, const exml::filePos& _filePos, const std::string& _comment) {
m_comment = _comment; m_comment = _comment;
m_Line = extract_line(_data, _pos); m_Line = extract_line(_data, _pos);
m_filePos = _filePos; m_filePos = _filePos;

View File

@ -65,6 +65,7 @@ namespace exml {
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool parse(const std::string& _data);
bool parse(const std::u32string& _data); bool parse(const std::u32string& _data);
/** /**
* @brief generate a string that contain the created XML * @brief generate a string that contain the created XML
@ -72,6 +73,7 @@ namespace exml {
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool generate(std::string& _data);
bool generate(std::u32string& _data); bool generate(std::u32string& _data);
/** /**
* @brief Load the file that might contain the xml * @brief Load the file that might contain the xml
@ -79,6 +81,7 @@ namespace exml {
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool load(const std::string& _file);
bool load(const std::u32string& _file); bool load(const std::u32string& _file);
/** /**
* @brief Store the Xml in the file * @brief Store the Xml in the file
@ -86,6 +89,7 @@ namespace exml {
* @return false : An error occured * @return false : An error occured
* @return true : Parsing is OK * @return true : Parsing is OK
*/ */
bool store(const std::string& _file);
bool store(const std::u32string& _file); bool store(const std::u32string& _file);
/** /**
* @brief Display the Document on console * @brief Display the Document on console
@ -93,8 +97,8 @@ namespace exml {
void display(void); void display(void);
private: private:
bool m_writeErrorWhenDetexted; bool m_writeErrorWhenDetexted;
std::u32string m_comment; std::string m_comment;
std::u32string m_Line; std::string m_Line;
exml::filePos m_filePos; exml::filePos m_filePos;
public: public:
void displayErrorWhenDetected(void) { void displayErrorWhenDetected(void) {
@ -104,13 +108,13 @@ namespace exml {
m_writeErrorWhenDetexted = false; m_writeErrorWhenDetexted = false;
}; };
void createError(const std::u32string& _data, int32_t _pos, const exml::filePos& _filePos, const std::u32string& _comment); void createError(const std::string& _data, int32_t _pos, const exml::filePos& _filePos, const std::string& _comment);
void displayError(void); void displayError(void);
public: // herited function: public: // herited function:
virtual enum nodeType getType(void) const { virtual enum nodeType getType(void) const {
return typeDocument; return typeDocument;
}; };
bool iGenerate(std::u32string& _data, int32_t _indent) const; bool iGenerate(std::string& _data, int32_t _indent) const;
virtual exml::Document* toDocument(void) { virtual exml::Document* toDocument(void) {
return this; return this;
}; };

View File

@ -74,7 +74,7 @@ const exml::Element* exml::Element::getElement(int32_t _id) const {
return tmpp->toElement(); return tmpp->toElement();
} }
exml::Element* exml::Element::getNamed(const std::u32string& _name) { exml::Element* exml::Element::getNamed(const std::string& _name) {
if (_name.size() == 0) { if (_name.size() == 0) {
return NULL; return NULL;
} }
@ -90,8 +90,11 @@ exml::Element* exml::Element::getNamed(const std::u32string& _name) {
} }
return NULL; return NULL;
} }
exml::Element* exml::Element::getNamed(const std::u32string& _name) {
return getNamed(to_u8string(_name));
}
const exml::Element* exml::Element::getNamed(const std::u32string& _name) const { const exml::Element* exml::Element::getNamed(const std::string& _name) const {
if (_name.size() == 0) { if (_name.size() == 0) {
return NULL; return NULL;
} }
@ -107,6 +110,9 @@ const exml::Element* exml::Element::getNamed(const std::u32string& _name) const
} }
return NULL; return NULL;
} }
const exml::Element* exml::Element::getNamed(const std::u32string& _name) const {
return getNamed(to_u8string(_name));
}
void exml::Element::append(exml::Node* _node) { void exml::Element::append(exml::Node* _node) {
if (_node == NULL) { if (_node == NULL) {
@ -126,9 +132,9 @@ void exml::Element::append(exml::Node* _node) {
m_listSub.push_back(_node); m_listSub.push_back(_node);
} }
std::u32string exml::Element::getText(void) { std::string exml::Element::getText(void) {
// TODO : add more capabilities ... // TODO : add more capabilities ...
std::u32string res; std::string res;
for (int32_t iii=0; iii<m_listSub.size(); iii++) { for (int32_t iii=0; iii<m_listSub.size(); iii++) {
if (NULL!=m_listSub[iii]) { if (NULL!=m_listSub[iii]) {
m_listSub[iii]->iGenerate(res, 0); m_listSub[iii]->iGenerate(res, 0);
@ -137,9 +143,9 @@ std::u32string exml::Element::getText(void) {
return res; return res;
} }
bool exml::Element::iGenerate(std::u32string& _data, int32_t _indent) const { bool exml::Element::iGenerate(std::string& _data, int32_t _indent) const {
addIndent(_data, _indent); addIndent(_data, _indent);
_data += U"<"; _data += "<";
_data += m_value; _data += m_value;
exml::AttributeList::iGenerate(_data, _indent); exml::AttributeList::iGenerate(_data, _indent);
@ -148,10 +154,10 @@ bool exml::Element::iGenerate(std::u32string& _data, int32_t _indent) const {
&& m_listSub[0] != NULL && m_listSub[0] != NULL
&& m_listSub[0]->getType() == exml::typeText && m_listSub[0]->getType() == exml::typeText
&& static_cast<exml::Text*>(m_listSub[0])->countLines() == 1) { && static_cast<exml::Text*>(m_listSub[0])->countLines() == 1) {
_data += U">"; _data += ">";
m_listSub[0]->iGenerate(_data,0); m_listSub[0]->iGenerate(_data,0);
} else { } else {
_data += U">\n"; _data += ">\n";
for (int32_t iii=0; iii<m_listSub.size(); iii++) { for (int32_t iii=0; iii<m_listSub.size(); iii++) {
if (NULL!=m_listSub[iii]) { if (NULL!=m_listSub[iii]) {
@ -160,17 +166,17 @@ bool exml::Element::iGenerate(std::u32string& _data, int32_t _indent) const {
} }
addIndent(_data, _indent); addIndent(_data, _indent);
} }
_data += U"</"; _data += "</";
_data += m_value; _data += m_value;
_data += U">\n"; _data += ">\n";
} else { } else {
_data += U"/>\n"; _data += "/>\n";
} }
return true; return true;
} }
bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc, bool _mainNode) { bool exml::Element::subParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc, bool _mainNode) {
EXML_PARSE_ELEMENT(" start subParse ... " << _pos << " " << _filePos); EXML_PARSE_ELEMENT(" start subParse ... " << _pos << " " << _filePos);
for (int32_t iii=_pos; iii<_data.size(); iii++) { for (int32_t iii=_pos; iii<_data.size(); iii++) {
_filePos.check(_data[iii]); _filePos.check(_data[iii]);
@ -182,14 +188,14 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
int32_t white = countWhiteChar(_data, iii+1, tmpPos); int32_t white = countWhiteChar(_data, iii+1, tmpPos);
if (iii+white+1>=_data.size()) { if (iii+white+1>=_data.size()) {
_filePos+=tmpPos; _filePos+=tmpPos;
CREATE_ERROR(_doc, _data, _pos, _filePos, U"End file with '<' char == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, "End file with '<' char == > invalide XML");
_pos = iii+white; _pos = iii+white;
return false; return false;
} }
// Detect type of the element: // Detect type of the element:
if(_data[iii+white+1] == '>') { if(_data[iii+white+1] == '>') {
_filePos+=tmpPos; _filePos+=tmpPos;
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Find '>' with no element in the element..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Find '>' with no element in the element...");
_pos = iii+white+1; _pos = iii+white+1;
return false; return false;
} }
@ -197,7 +203,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
++tmpPos; ++tmpPos;
// TODO : white space ... // TODO : white space ...
if( false == checkAvaillable(_data[iii+white+2], true) ) { if( false == checkAvaillable(_data[iii+white+2], true) ) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Find unavaillable name in the Declaration node..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Find unavaillable name in the Declaration node...");
_pos = iii+white+1; _pos = iii+white+1;
return false; return false;
} }
@ -213,14 +219,14 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
} }
tmpPos.check(_data[jjj]); tmpPos.check(_data[jjj]);
} }
std::u32string tmpname = std::u32string(_data, iii+white+2, endPosName+1); std::string tmpname = std::string(_data, iii+white+2, endPosName+1);
if (true == _caseSensitive) { if (true == _caseSensitive) {
tmpname = to_lower(tmpname); tmpname = to_lower(tmpname);
} }
// Find declaration balise // Find declaration balise
exml::Declaration* declaration = new exml::Declaration(tmpname); exml::Declaration* declaration = new exml::Declaration(tmpname);
if (NULL == declaration) { if (NULL == declaration) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Allocation Error..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Allocation Error...");
return false; return false;
} }
_filePos += tmpPos; _filePos += tmpPos;
@ -237,24 +243,24 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
++tmpPos; ++tmpPos;
// Find special block element // Find special block element
if (iii+white+2>=_data.size()) { if (iii+white+2>=_data.size()) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"End file with '<!' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, "End file with '<!' chars == > invalide XML");
return false; return false;
} }
if(_data[iii+white+2] == '-') { if(_data[iii+white+2] == '-') {
++tmpPos; ++tmpPos;
if (iii+white+3>=_data.size()) { if (iii+white+3>=_data.size()) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"End file with '<!-' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, "End file with '<!-' chars == > invalide XML");
return false; return false;
} }
if(_data[iii+white+3] != '-') { if(_data[iii+white+3] != '-') {
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"Element parse with '<!-") + _data[iii+3] + U"' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("Element parse with '<!-") + _data[iii+3] + "' chars == > invalide XML");
return false; return false;
} }
++tmpPos; ++tmpPos;
// find comment: // find comment:
exml::Comment* comment = new exml::Comment(); exml::Comment* comment = new exml::Comment();
if (NULL == comment) { if (NULL == comment) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Allocation error ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Allocation error ...");
return false; return false;
} }
_pos = iii+white+4; _pos = iii+white+4;
@ -268,7 +274,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
} else if (_data[iii+white+2] == '[') { } else if (_data[iii+white+2] == '[') {
++tmpPos; ++tmpPos;
if (iii+white+8>=_data.size()) { if (iii+white+8>=_data.size()) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"End file with '<![' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, "End file with '<![' chars == > invalide XML");
return false; return false;
} }
if( _data[iii+white+3] != 'C' if( _data[iii+white+3] != 'C'
@ -277,14 +283,14 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
|| _data[iii+white+6] != 'T' || _data[iii+white+6] != 'T'
|| _data[iii+white+7] != 'A' || _data[iii+white+7] != 'A'
|| _data[iii+white+8] != '[') { || _data[iii+white+8] != '[') {
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"Element parse with '<![") + _data[iii+white+3] + _data[iii+white+4] + _data[iii+white+5] + _data[iii+white+6] + _data[iii+white+7] + _data[iii+white+8] + U"' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("Element parse with '<![") + _data[iii+white+3] + _data[iii+white+4] + _data[iii+white+5] + _data[iii+white+6] + _data[iii+white+7] + _data[iii+white+8] + "' chars == > invalide XML");
return false; return false;
} }
tmpPos+=6; tmpPos+=6;
// find text: // find text:
exml::TextCDATA* text = new exml::TextCDATA(); exml::TextCDATA* text = new exml::TextCDATA();
if (NULL == text) { if (NULL == text) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Allocation error ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Allocation error ...");
return false; return false;
} }
_pos = iii+9+white; _pos = iii+9+white;
@ -296,7 +302,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
iii = _pos; iii = _pos;
m_listSub.push_back(text); m_listSub.push_back(text);
} else { } else {
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"End file with '<!") + _data[iii+white+2] + U"' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("End file with '<!") + _data[iii+white+2] + "' chars == > invalide XML");
return false; return false;
} }
@ -316,7 +322,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
} }
tmpPos.check(_data[jjj]); tmpPos.check(_data[jjj]);
} }
std::u32string tmpname = std::u32string(_data, iii+white+2, endPosName+1); std::string tmpname = std::string(_data, iii+white+2, endPosName+1);
if (true == _caseSensitive) { if (true == _caseSensitive) {
tmpname = to_lower(tmpname); tmpname = to_lower(tmpname);
} }
@ -338,18 +344,18 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
&& _data[jjj] != ' ' && _data[jjj] != ' '
&& _data[jjj] != '\t') { && _data[jjj] != '\t') {
_filePos += tmpPos; _filePos += tmpPos;
CREATE_ERROR(_doc, _data, jjj, _filePos, std::u32string(U"End node error : have data inside end node other than [ \\n\\t\\r] ") + m_value + U"'"); CREATE_ERROR(_doc, _data, jjj, _filePos, std::string("End node error : have data inside end node other than [ \\n\\t\\r] ") + m_value + "'");
return false; return false;
} }
} }
} else { } else {
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"End node error : '") + tmpname + U"' != '" + m_value + U"'"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("End node error : '") + tmpname + "' != '" + m_value + "'");
return false; return false;
} }
} }
if (_data[iii+white+1] == '>') { if (_data[iii+white+1] == '>') {
// end of something == > this is really bad // end of something == > this is really bad
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Find '>' chars == > invalide XML"); CREATE_ERROR(_doc, _data, _pos, _filePos, "Find '>' chars == > invalide XML");
return false; return false;
} }
@ -367,7 +373,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
} }
tmpPos.check(_data[jjj]); tmpPos.check(_data[jjj]);
} }
std::u32string tmpname = std::u32string(_data, iii+white+1, endPosName+1); std::string tmpname = std::string(_data, iii+white+1, endPosName+1);
if (true == _caseSensitive) { if (true == _caseSensitive) {
to_lower(tmpname); to_lower(tmpname);
} }
@ -375,7 +381,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
// find text: // find text:
exml::Element* element = new exml::Element(tmpname); exml::Element* element = new exml::Element(tmpname);
if (NULL == element) { if (NULL == element) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Allocation error ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Allocation error ...");
return false; return false;
} }
_pos = endPosName+1; _pos = endPosName+1;
@ -390,11 +396,11 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
} }
_filePos+=tmpPos; _filePos+=tmpPos;
// here we have an error : // here we have an error :
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"Find an ununderstanding element : '") + _data[iii+white+1] + U"'"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("Find an ununderstanding element : '") + _data[iii+white+1] + "'");
return false; return false;
} else { } else {
if (_data[iii] == '>') { if (_data[iii] == '>') {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Find elemement '>' == > no reason to be here ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Find elemement '>' == > no reason to be here ...");
return false; return false;
} }
// might to be data text ... // might to be data text ...
@ -407,7 +413,7 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
// find data == > parse it... // find data == > parse it...
exml::Text* text = new exml::Text(); exml::Text* text = new exml::Text();
if (NULL == text) { if (NULL == text) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Allocation error ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Allocation error ...");
return false; return false;
} }
_pos = iii; _pos = iii;
@ -424,11 +430,11 @@ bool exml::Element::subParse(const std::u32string& _data, int32_t& _pos, bool _c
if (_mainNode == true) { if (_mainNode == true) {
return true; return true;
} }
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"Did not find end of the exml::Element : '") + m_value + U"'"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("Did not find end of the exml::Element : '") + m_value + "'");
return false; return false;
} }
bool exml::Element::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { bool exml::Element::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) {
EXML_PARSE_ELEMENT("start parse : 'element' named='" << m_value << "'"); EXML_PARSE_ELEMENT("start parse : 'element' named='" << m_value << "'");
// note : When start parsing the upper element must have set the value of the element and set the position after this one // note : When start parsing the upper element must have set the value of the element and set the position after this one
m_pos=_filePos; m_pos=_filePos;
@ -446,7 +452,7 @@ bool exml::Element::iParse(const std::u32string& _data, int32_t& _pos, bool _cas
if (_data[iii] == '/') { if (_data[iii] == '/') {
// standalone node or error... // standalone node or error...
if (iii+1>=_data.size()) { if (iii+1>=_data.size()) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Find end of files ... == > bad case"); CREATE_ERROR(_doc, _data, _pos, _filePos, "Find end of files ... == > bad case");
return false; return false;
} }
// TODO : Can have white spaces .... // TODO : Can have white spaces ....
@ -455,14 +461,14 @@ bool exml::Element::iParse(const std::u32string& _data, int32_t& _pos, bool _cas
return true; return true;
} }
// error // error
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Find / without > char ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Find / without > char ...");
return false; return false;
} }
if (true == checkAvaillable(_data[iii], true)) { if (true == checkAvaillable(_data[iii], true)) {
// we find an attibute == > create a new and parse it : // we find an attibute == > create a new and parse it :
exml::Attribute* attribute = new exml::Attribute(); exml::Attribute* attribute = new exml::Attribute();
if (NULL == attribute) { if (NULL == attribute) {
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Allocation error ..."); CREATE_ERROR(_doc, _data, _pos, _filePos, "Allocation error ...");
return false; return false;
} }
_pos = iii; _pos = iii;
@ -475,11 +481,11 @@ bool exml::Element::iParse(const std::u32string& _data, int32_t& _pos, bool _cas
continue; continue;
} }
if (false == etk::isWhiteChar(_data[iii])) { if (false == etk::isWhiteChar(_data[iii])) {
CREATE_ERROR(_doc, _data, iii, _filePos, std::u32string(U"Find an unknow element : '") + _data[iii] + U"'"); CREATE_ERROR(_doc, _data, iii, _filePos, std::string("Find an unknow element : '") + _data[iii] + "'");
return false; return false;
} }
} }
CREATE_ERROR(_doc, _data, _pos, _filePos, std::u32string(U"Unexpecting end of parsing exml::Element : '") + m_value + U"' == > check if the '/>' is set or the end of element"); CREATE_ERROR(_doc, _data, _pos, _filePos, std::string("Unexpecting end of parsing exml::Element : '") + m_value + "' == > check if the '/>' is set or the end of element");
return false; return false;
} }

View File

@ -24,6 +24,10 @@ namespace exml {
* @brief Constructor * @brief Constructor
* @param[in] _value Element name; * @param[in] _value Element name;
*/ */
Element(const std::string& _value) :
exml::AttributeList(_value) {
};
Element(const std::u32string& _value) : Element(const std::u32string& _value) :
exml::AttributeList(_value) { exml::AttributeList(_value) {
@ -73,21 +77,23 @@ namespace exml {
* @param[in] _name Name of the element that is requested * @param[in] _name Name of the element that is requested
* @return Pointer on the element or NULL. * @return Pointer on the element or NULL.
*/ */
Element* getNamed(const std::string& _name);
const Element* getNamed(const std::string& _name) const;
Element* getNamed(const std::u32string& _name); Element* getNamed(const std::u32string& _name);
const Element* getNamed(const std::u32string& _name) const; const Element* getNamed(const std::u32string& _name) const;
/** /**
* @brief get the internal data of the element (if the element has some sub node thay are converted in xml string == > like this it is not needed to use <![CDATA[...]]> * @brief get the internal data of the element (if the element has some sub node thay are converted in xml string == > like this it is not needed to use <![CDATA[...]]>
* @return the curent data string. * @return the curent data string.
*/ */
std::u32string getText(void); std::string getText(void);
protected: protected:
bool subParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc, bool _mainNode=false); bool subParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc, bool _mainNode=false);
public: // herited function: public: // herited function:
virtual enum nodeType getType(void) const { virtual enum nodeType getType(void) const {
return typeElement; return typeElement;
}; };
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc);
virtual bool iGenerate(std::u32string& _data, int32_t _indent) const; virtual bool iGenerate(std::string& _data, int32_t _indent) const;
virtual exml::Element* toElement(void) { virtual exml::Element* toElement(void) {
return this; return this;
}; };

View File

@ -14,23 +14,28 @@
etk::CCout& exml::operator <<(etk::CCout& _os, const exml::filePos& _obj) { etk::CCout& exml::operator <<(etk::CCout& _os, const exml::filePos& _obj) {
_os << U"(l="; _os << "(l=";
_os << _obj.getLine(); _os << _obj.getLine();
_os << U",c="; _os << ",c=";
_os << _obj.getCol(); _os << _obj.getCol();
_os << U")"; _os << ")";
return _os; return _os;
} }
exml::Node::Node(const std::u32string& _value) : exml::Node::Node(const std::string& _value) :
m_pos(0,0), m_pos(0,0),
m_value(_value) { m_value(_value) {
// nothing to do. // nothing to do.
} }
exml::Node::Node(const std::u32string& _value) :
m_pos(0,0) {
m_value = to_u8string(_value);
}
void exml::Node::addIndent(std::u32string& _data, int32_t _indent) const {
void exml::Node::addIndent(std::string& _data, int32_t _indent) const {
for (int32_t iii=0; iii<_indent; iii++) { for (int32_t iii=0; iii<_indent; iii++) {
_data += U"\t"; _data += "\t";
} }
} }
@ -91,7 +96,7 @@ bool exml::Node::checkAvaillable(char32_t _val, bool _firstChar) const {
} }
int32_t exml::Node::countWhiteChar(const std::u32string& _data, int32_t _pos, exml::filePos& _filePos) const { int32_t exml::Node::countWhiteChar(const std::string& _data, int32_t _pos, exml::filePos& _filePos) const {
_filePos.clear(); _filePos.clear();
int32_t white=0; int32_t white=0;
for (int32_t iii=_pos; iii<_data.size(); iii++) { for (int32_t iii=_pos; iii<_data.size(); iii++) {
@ -107,6 +112,14 @@ int32_t exml::Node::countWhiteChar(const std::u32string& _data, int32_t _pos, ex
} }
void exml::Node::clear(void) { void exml::Node::clear(void) {
m_value = U""; m_value = "";
m_pos.clear(); m_pos.clear();
} }
void exml::Node::setValue(std::u32string _value) {
m_value = to_u8string(_value);
}
std::u32string exml::Node::getUValue(void) const {
return to_u32string(m_value);
}

View File

@ -132,6 +132,7 @@ namespace exml
* @brief basic element of a xml structure * @brief basic element of a xml structure
* @param[in] value of the node * @param[in] value of the node
*/ */
Node(const std::string& _value);
Node(const std::u32string& _value); Node(const std::u32string& _value);
/** /**
* @brief destructor * @brief destructor
@ -146,14 +147,14 @@ namespace exml
* @param[in,out] file parsing position (line x col x) * @param[in,out] file parsing position (line x col x)
* @return false if an error occured. * @return false if an error occured.
*/ */
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) = 0; virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) = 0;
/** /**
* @brief generate a string with the tree of the xml * @brief generate a string with the tree of the xml
* @param[in,out] _data string where to add the elements * @param[in,out] _data string where to add the elements
* @param[in] current indentation of the file * @param[in] current indentation of the file
* @return false if an error occured. * @return false if an error occured.
*/ */
virtual bool iGenerate(std::u32string& _data, int32_t _indent) const { virtual bool iGenerate(std::string& _data, int32_t _indent) const {
return true; return true;
}; };
protected: protected:
@ -166,22 +167,24 @@ namespace exml
return m_pos; return m_pos;
}; };
protected: protected:
std::u32string m_value; //!< value of the node (for element this is the name, for text it is the inside text ...) std::string m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
public: public:
/** /**
* @brief set the value of the node. * @brief set the value of the node.
* @param[in] _value New value of the node. * @param[in] _value New value of the node.
*/ */
virtual void setValue(std::u32string _value) { virtual void setValue(std::string _value) {
m_value = _value; m_value = _value;
}; };
virtual void setValue(std::u32string _value);
/** /**
* @brief get the current element Value. * @brief get the current element Value.
* @return the reference of the string value. * @return the reference of the string value.
*/ */
virtual const std::u32string& getValue(void) const { virtual const std::string& getValue(void) const {
return m_value; return m_value;
}; };
virtual std::u32string getUValue(void) const;
public: public:
/** /**
* @brief get the node type. * @brief get the node type.
@ -196,7 +199,7 @@ namespace exml
* @param[in,out] _data String where the indentation is done. * @param[in,out] _data String where the indentation is done.
* @param[in] _indent Number of tab to add at the string. * @param[in] _indent Number of tab to add at the string.
*/ */
void addIndent(std::u32string& _data, int32_t _indent) const; void addIndent(std::string& _data, int32_t _indent) const;
/** /**
* @brief Display the cuurent element that is curently parse. * @brief Display the cuurent element that is curently parse.
* @param[in] _val Char that is parsed. * @param[in] _val Char that is parsed.
@ -216,7 +219,7 @@ namespace exml
* @param[out] _filePos new poistion of te file to add. * @param[out] _filePos new poistion of te file to add.
* @return number of white element. * @return number of white element.
*/ */
int32_t countWhiteChar(const std::u32string& _data, int32_t _pos, exml::filePos& _filePos) const; int32_t countWhiteChar(const std::string& _data, int32_t _pos, exml::filePos& _filePos) const;
public: public:
/** /**
* @brief Cast the element in a Document if it is possible. * @brief Cast the element in a Document if it is possible.

View File

@ -13,7 +13,7 @@
#undef __class__ #undef __class__
#define __class__ "Text" #define __class__ "Text"
bool exml::Text::iGenerate(std::u32string& _data, int32_t _indent) const { bool exml::Text::iGenerate(std::string& _data, int32_t _indent) const {
_data += m_value; _data += m_value;
return true; return true;
} }
@ -28,7 +28,7 @@ int32_t exml::Text::countLines(void) const {
return count; return count;
} }
bool exml::Text::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { bool exml::Text::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) {
EXML_VERBOSE("start parse : 'text'"); EXML_VERBOSE("start parse : 'text'");
m_pos = _filePos; m_pos = _filePos;
// search end of the comment : // search end of the comment :
@ -51,18 +51,18 @@ bool exml::Text::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSe
} }
} }
// find end of value: // find end of value:
m_value = std::u32string(_data, _pos, newEnd); m_value = std::string(_data, _pos, newEnd);
EXML_VERBOSE(" find text '" << m_value << "'"); EXML_VERBOSE(" find text '" << m_value << "'");
_pos = iii-1; _pos = iii-1;
return true; return true;
} }
} }
CREATE_ERROR(_doc, _data, _pos, _filePos, U"Text got end of file without finding end node"); CREATE_ERROR(_doc, _data, _pos, _filePos, "Text got end of file without finding end node");
_pos = _data.size(); _pos = _data.size();
return false; return false;
} }
bool exml::TextCDATA::iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) { bool exml::TextCDATA::iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc) {
EXML_VERBOSE("start parse : 'text::CDATA'"); EXML_VERBOSE("start parse : 'text::CDATA'");
m_pos = _filePos; m_pos = _filePos;
// search end of the comment : // search end of the comment :
@ -78,13 +78,13 @@ bool exml::TextCDATA::iParse(const std::u32string& _data, int32_t& _pos, bool _c
&& _data[iii+2] == '>') { && _data[iii+2] == '>') {
// find end of value: // find end of value:
_filePos += 2; _filePos += 2;
m_value = std::u32string(_data, _pos, iii); m_value = std::string(_data, _pos, iii);
EXML_VERBOSE(" find text CDATA '" << m_value << "'"); EXML_VERBOSE(" find text CDATA '" << m_value << "'");
_pos = iii+2; _pos = iii+2;
return true; return true;
} }
} }
CREATE_ERROR(_doc, _data, _pos, _filePos, U"text CDATA got end of file without finding end node"); CREATE_ERROR(_doc, _data, _pos, _filePos, "text CDATA got end of file without finding end node");
_pos = _data.size(); _pos = _data.size();
return false; return false;
} }

View File

@ -23,7 +23,7 @@ namespace exml {
* @brief Constructor * @brief Constructor
* @param[in] _data String data of the current Text * @param[in] _data String data of the current Text
*/ */
Text(const std::u32string& _data) : exml::Node(_data) { }; Text(const std::string& _data) : exml::Node(_data) { };
/** /**
* @brief Destructor * @brief Destructor
*/ */
@ -37,8 +37,8 @@ namespace exml {
virtual enum nodeType getType(void) const { virtual enum nodeType getType(void) const {
return typeText; return typeText;
}; };
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc);
virtual bool iGenerate(std::u32string& _data, int32_t _indent) const; virtual bool iGenerate(std::string& _data, int32_t _indent) const;
virtual exml::Text* toText(void) { virtual exml::Text* toText(void) {
return this; return this;
}; };
@ -57,7 +57,7 @@ namespace exml {
*/ */
virtual ~TextCDATA(void) { }; virtual ~TextCDATA(void) { };
public: // herited function: public: // herited function:
virtual bool iParse(const std::u32string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc); virtual bool iParse(const std::string& _data, int32_t& _pos, bool _caseSensitive, exml::filePos& _filePos, exml::Document& _doc);
}; };
}; };

View File

@ -16,11 +16,11 @@
class testCheck { class testCheck {
public: public:
std::u32string m_ref; std::string m_ref;
std::u32string m_input; std::string m_input;
int32_t m_errorPos; // -1 : no error , 1 : parsing error, 2 generation error, 3 comparaison error ???? int32_t m_errorPos; // -1 : no error , 1 : parsing error, 2 generation error, 3 comparaison error ????
testCheck(void) {}; testCheck(void) {};
void set(const std::u32string& _ref, int32_t _pos, const std::u32string& _input) { void set(const std::string& _ref, int32_t _pos, const std::string& _input) {
m_ref = _ref; m_ref = _ref;
m_input = _input; m_input = _input;
m_errorPos = _pos; m_errorPos = _pos;
@ -30,8 +30,8 @@ class testCheck {
std::vector<testCheck> l_list; std::vector<testCheck> l_list;
void init(void) { void init(void) {
std::u32string reference; std::string reference;
std::u32string input; std::string input;
testCheck check; testCheck check;
// == ==================================================== // == ====================================================
@ -296,7 +296,7 @@ int main(int argc, const char *argv[]) {
} }
sectionID++; sectionID++;
exml::Document doc; exml::Document doc;
std::u32string out(""); std::string out("");
//EXML_DEBUG("parse : \n" << l_list[iii].m_input); //EXML_DEBUG("parse : \n" << l_list[iii].m_input);
if (false == doc.parse(l_list[iii].m_input)) { if (false == doc.parse(l_list[iii].m_input)) {
if (l_list[iii].m_errorPos == 1) { if (l_list[iii].m_errorPos == 1) {