23 size_t pos = _string.find(
"&");
24 if (pos == std::string::npos)
return _string;
26 ret.reserve(_string.size());
28 while (pos != std::string::npos)
30 ret += _string.substr(old, pos - old);
32 size_t end = _string.find(
";", pos + 1);
33 if (end == std::string::npos)
40 std::string tag = _string.substr(pos, end - pos + 1);
41 if (tag ==
"&") ret +=
'&';
42 else if (tag ==
"<") ret +=
'<';
43 else if (tag ==
">") ret +=
'>';
44 else if (tag ==
"'") ret +=
'\'';
45 else if (tag ==
""") ret +=
'\"';
54 pos = _string.find(
"&", old);
56 ret += _string.substr(old, std::string::npos);
65 size_t pos = _string.find_first_of(
"&<>'\"");
66 if (pos == std::string::npos)
return _string;
68 ret.reserve(_string.size() * 2);
70 while (pos != std::string::npos)
72 ret += _string.substr(old, pos - old);
74 if (_string[pos] ==
'&') ret +=
"&";
75 else if (_string[pos] ==
'<') ret +=
"<";
76 else if (_string[pos] ==
'>') ret +=
">";
77 else if (_string[pos] ==
'\'') ret +=
"'";
78 else if (_string[pos] ==
'\"') ret +=
""";
81 pos = _string.find_first_of(
"&<>'\"", old);
83 ret += _string.substr(old, std::string::npos);
93 ElementEnumerator::ElementEnumerator(VectorElement::iterator _begin, VectorElement::iterator _end) :
102 if (m_current == m_end)
110 if (m_current == m_end)
119 if ((*m_current)->getName() == _name)
127 assert(m_current != m_end);
133 assert(m_current != m_end);
140 Element::Element(
const std::string& _name,
ElementPtr _parent,
ElementType _type,
const std::string& _content) :
150 for (VectorElement::iterator iter = mChilds.begin(); iter != mChilds.end(); ++iter)
157 void Element::save(std::ostream& _stream,
size_t _level)
160 for (
size_t tab = 0; tab < _level; ++tab)
173 for (VectorAttributes::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
178 bool empty = mChilds.empty();
180 if (empty && mContent.empty())
195 if (!mContent.empty())
199 for (
size_t tab = 0; tab <= _level; ++tab) _stream <<
" ";
207 for (
size_t child = 0; child < mChilds.size(); child++)
209 mChilds[child]->save(_stream, _level + 1);
214 for (
size_t tab = 0; tab < _level; ++tab)
217 _stream <<
"</" << mName <<
">\n";
224 mChilds.push_back(node);
230 VectorElement::iterator item = std::find(mChilds.begin(), mChilds.end(), _child);
231 if (item != mChilds.end())
240 for (VectorElement::iterator iter = mChilds.begin(); iter != mChilds.end(); ++iter)
delete *iter;
248 for (VectorAttributes::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
250 if ( (*iter).first == _name)
252 _value = (*iter).second;
261 for (VectorAttributes::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
263 if ((*iter).first == _name)
264 return (*iter).second;
276 for (
size_t index = 0; index < mAttributes.size(); ++index)
278 if (mAttributes[index].first == _key)
280 mAttributes.erase(mAttributes.begin() + index);
289 elem->mAttributes = mAttributes;
291 for (VectorElement::iterator iter = mChilds.begin(); iter != mChilds.end(); ++iter)
294 child->mParent = elem;
295 elem->mChilds.push_back(child);
303 for (
size_t index = 0; index < mAttributes.size(); ++index)
305 if (mAttributes[index].first == _key)
307 mAttributes[index].second = _value;
316 if (mContent.empty())
323 mContent += _content;
362 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && !defined(STLPORT)
363 inline void open_stream(std::ofstream& _stream,
const std::wstring& _wide)
365 _stream.open(_wide.c_str());
367 inline void open_stream(std::ifstream& _stream,
const std::wstring& _wide)
369 _stream.open(_wide.c_str());
372 inline void open_stream(std::ofstream& _stream,
const std::wstring& _wide)
374 _stream.open(
UString(_wide).asUTF8_c_str());
376 inline void open_stream(std::ifstream& _stream,
const std::wstring& _wide)
378 _stream.open(UString(_wide).asUTF8_c_str());
402 std::ifstream stream;
403 stream.open(_filename.c_str());
405 if (!stream.is_open())
408 setLastFileError(_filename);
412 bool result =
open(stream);
421 std::ifstream stream;
424 if (!stream.is_open())
427 setLastFileError(_filename);
431 bool result =
open(stream);
441 bool result =
open(data);
450 std::ofstream stream;
451 stream.open(_filename.c_str());
453 if (!stream.is_open())
456 setLastFileError(_filename);
460 bool result =
save(stream);
464 setLastFileError(_filename);
474 std::ofstream stream;
477 if (!stream.is_open())
480 setLastFileError(_filename);
484 bool result =
save(stream);
488 setLastFileError(_filename);
507 while (!_stream->
eof())
513 if (read[read.size() - 1] ==
'\r')
514 read.erase(read.size() - 1, 1);
525 if (!parseLine(line, currentNode))
550 _stream << (char)0xEFu;
551 _stream << (char)0xBBu;
552 _stream << (char)0xBFu;
554 mDeclaration->save(_stream, 0);
556 mRoot->save(_stream, 0);
569 bool Document::parseTag(
ElementPtr& _currentNode, std::string _content)
574 if (_content.empty())
583 _currentNode =
new Element(
"", 0);
586 mRoot = _currentNode;
591 char simbol = _content[0];
592 bool tagDeclaration =
false;
597 if (_currentNode != 0)
604 else if (simbol ==
'?')
606 tagDeclaration =
true;
607 _content.erase(0, 1);
615 if (_currentNode == 0)
625 start = _content.find_first_not_of(
" \t", 1);
626 if (start == _content.npos)
633 end = _content.find_last_not_of(
" \t");
634 _content = _content.substr(start, end - start + 1);
637 if (_currentNode->
getName() != _content)
643 _currentNode = _currentNode->
getParent();
648 std::string cut = _content;
649 start = _content.find_first_of(
" \t/?", 1);
650 if (start != _content.npos)
652 cut = _content.substr(0, start);
653 _content = _content.substr(start);
675 mDeclaration = _currentNode;
686 mRoot = _currentNode;
691 start = _content.find_last_not_of(
" \t");
692 if (start == _content.npos)
697 if ((_content[start] ==
'/') || (_content[start] ==
'?'))
701 _content[start] =
' ';
703 start = _content.find_last_not_of(
" \t");
704 if (start == _content.npos)
707 _currentNode = _currentNode->
getParent();
716 start = _content.find(
'=');
717 if (start == _content.npos)
723 end = _content.find_first_of(
"\"\'", start + 1);
724 if (end == _content.npos)
729 end = _content.find_first_of(
"\"\'", end + 1);
730 if (end == _content.npos)
736 std::string key = _content.substr(0, start);
737 std::string value = _content.substr(start + 1, end - start);
740 if (! checkPair(key, value))
750 _content = _content.substr(end + 1);
753 start = _content.find_first_not_of(
" \t");
754 if (start == _content.npos)
764 _currentNode = _currentNode->
getParent();
771 bool Document::checkPair(std::string& _key, std::string& _value)
777 size_t start = _key.find_first_of(
" \t\"\'&");
778 if (start != _key.npos)
783 if (_value.size() < 2)
785 if (((_value[0] !=
'"') || (_value[_value.length() - 1] !=
'"')) &&
786 ((_value[0] !=
'\'') || (_value[_value.length() - 1] !=
'\'')))
794 size_t Document::find(
const std::string& _text,
char _char,
size_t _start)
800 char buff[16] =
"\"_\0";
807 pos = _text.find_first_of(buff, pos);
810 if (pos == _text.npos)
815 else if (_text[pos] ==
'"')
835 void Document::clearDeclaration()
844 void Document::clearRoot()
869 bool Document::parseLine(std::string& _line,
ElementPtr& _element)
875 size_t start = find(_line,
'<');
876 if (start == _line.npos)
878 size_t end = _line.npos;
881 if ((start + 3 < _line.size()) && (_line[start + 1] ==
'!') && (_line[start + 2] ==
'-') && (_line[start + 3] ==
'-'))
883 end = _line.find(
"-->", start + 4);
884 if (end == _line.npos)
890 end = find(_line,
'>', start + 1);
891 if (end == _line.npos)
895 size_t body = _line.find_first_not_of(
" \t<");
898 std::string body_str = _line.substr(0, start);
914 if (!parseTag(_element, _line.substr(start + 1, end - start - 1)))
919 _line = _line.substr(end + 1);
926 const std::string& error = mLastError.
print();
929 return MyGUI::utility::toString(
"'", error,
"' , file='", mLastErrorFile,
"' , line=", mLine,
" , col=", mCol);
952 void Document::setLastFileError(
const std::string& _filename)
954 mLastErrorFile = _filename;
957 void Document::setLastFileError(
const std::wstring& _filename)