38 size_t pos = _string.find(
"&");
39 if (pos == std::string::npos)
return _string;
41 ret.reserve(_string.size());
43 while (pos != std::string::npos)
45 ret += _string.substr(old, pos - old);
47 size_t end = _string.find(
";", pos + 1);
48 if (end == std::string::npos)
55 std::string tag = _string.substr(pos, end - pos + 1);
56 if (tag ==
"&") ret +=
'&';
57 else if (tag ==
"<") ret +=
'<';
58 else if (tag ==
">") ret +=
'>';
59 else if (tag ==
"'") ret +=
'\'';
60 else if (tag ==
""") ret +=
'\"';
69 pos = _string.find(
"&", old);
71 ret += _string.substr(old, std::string::npos);
80 size_t pos = _string.find_first_of(
"&<>'\"");
81 if (pos == std::string::npos)
return _string;
83 ret.reserve(_string.size() * 2);
85 while (pos != std::string::npos)
87 ret += _string.substr(old, pos - old);
89 if (_string[pos] ==
'&') ret +=
"&";
90 else if (_string[pos] ==
'<') ret +=
"<";
91 else if (_string[pos] ==
'>') ret +=
">";
92 else if (_string[pos] ==
'\'') ret +=
"'";
93 else if (_string[pos] ==
'\"') ret +=
""";
96 pos = _string.find_first_of(
"&<>'\"", old);
98 ret += _string.substr(old, std::string::npos);
108 ElementEnumerator::ElementEnumerator(VectorElement::iterator _begin, VectorElement::iterator _end) :
117 if (m_current == m_end)
125 if (m_current == m_end)
134 if ((*m_current)->getName() == _name)
142 assert(m_current != m_end);
148 assert(m_current != m_end);
155 Element::Element(
const std::string& _name,
ElementPtr _parent,
ElementType _type,
const std::string& _content) :
165 for (VectorElement::iterator iter = mChilds.begin(); iter != mChilds.end(); ++iter)
172 void Element::save(std::ostream& _stream,
size_t _level)
175 for (
size_t tab = 0; tab < _level; ++tab)
188 for (VectorAttributes::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
193 bool empty = mChilds.empty();
195 if (empty && mContent.empty())
210 if (!mContent.empty())
214 for (
size_t tab = 0; tab <= _level; ++tab) _stream <<
" ";
222 for (
size_t child = 0; child < mChilds.size(); child++)
224 mChilds[child]->save(_stream, _level + 1);
229 for (
size_t tab = 0; tab < _level; ++tab)
232 _stream <<
"</" << mName <<
">\n";
239 mChilds.push_back(node);
245 VectorElement::iterator item = std::find(mChilds.begin(), mChilds.end(), _child);
246 if (item != mChilds.end())
255 for (VectorElement::iterator iter = mChilds.begin(); iter != mChilds.end(); ++iter)
delete *iter;
263 for (VectorAttributes::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
265 if ( (*iter).first == _name)
267 _value = (*iter).second;
276 for (VectorAttributes::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
278 if ((*iter).first == _name)
279 return (*iter).second;
291 for (
size_t index = 0; index < mAttributes.size(); ++index)
293 if (mAttributes[index].first == _key)
295 mAttributes.erase(mAttributes.begin() + index);
304 elem->mAttributes = mAttributes;
306 for (VectorElement::iterator iter = mChilds.begin(); iter != mChilds.end(); ++iter)
309 child->mParent = elem;
310 elem->mChilds.push_back(child);
318 for (
size_t index = 0; index < mAttributes.size(); ++index)
320 if (mAttributes[index].first == _key)
322 mAttributes[index].second = _value;
331 if (mContent.empty())
338 mContent += _content;
377 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && !defined(STLPORT)
378 inline void open_stream(std::ofstream& _stream,
const std::wstring& _wide)
380 _stream.open(_wide.c_str());
382 inline void open_stream(std::ifstream& _stream,
const std::wstring& _wide)
384 _stream.open(_wide.c_str());
387 inline void open_stream(std::ofstream& _stream,
const std::wstring& _wide)
389 _stream.open(
UString(_wide).asUTF8_c_str());
391 inline void open_stream(std::ifstream& _stream,
const std::wstring& _wide)
393 _stream.open(UString(_wide).asUTF8_c_str());
417 std::ifstream stream;
418 stream.open(_filename.c_str());
420 if (!stream.is_open())
423 setLastFileError(_filename);
427 bool result =
open(stream);
436 std::ifstream stream;
439 if (!stream.is_open())
442 setLastFileError(_filename);
446 bool result =
open(stream);
456 bool result =
open(data);
465 std::ofstream stream;
466 stream.open(_filename.c_str());
468 if (!stream.is_open())
471 setLastFileError(_filename);
475 bool result =
save(stream);
479 setLastFileError(_filename);
489 std::ofstream stream;
492 if (!stream.is_open())
495 setLastFileError(_filename);
499 bool result =
save(stream);
503 setLastFileError(_filename);
522 while (!_stream->
eof())
528 if (read[read.size() - 1] ==
'\r')
529 read.erase(read.size() - 1, 1);
540 if (!parseLine(line, currentNode))
565 _stream << (char)0xEFu;
566 _stream << (char)0xBBu;
567 _stream << (char)0xBFu;
569 mDeclaration->save(_stream, 0);
571 mRoot->save(_stream, 0);
584 bool Document::parseTag(
ElementPtr& _currentNode, std::string _content)
589 if (_content.empty())
598 _currentNode =
new Element(
"", 0);
601 mRoot = _currentNode;
606 char simbol = _content[0];
607 bool tagDeclaration =
false;
612 if (_currentNode != 0)
619 else if (simbol ==
'?')
621 tagDeclaration =
true;
622 _content.erase(0, 1);
630 if (_currentNode == 0)
640 start = _content.find_first_not_of(
" \t", 1);
641 if (start == _content.npos)
648 end = _content.find_last_not_of(
" \t");
649 _content = _content.substr(start, end - start + 1);
652 if (_currentNode->
getName() != _content)
658 _currentNode = _currentNode->
getParent();
663 std::string cut = _content;
664 start = _content.find_first_of(
" \t/?", 1);
665 if (start != _content.npos)
667 cut = _content.substr(0, start);
668 _content = _content.substr(start);
690 mDeclaration = _currentNode;
701 mRoot = _currentNode;
706 start = _content.find_last_not_of(
" \t");
707 if (start == _content.npos)
712 if ((_content[start] ==
'/') || (_content[start] ==
'?'))
716 _content[start] =
' ';
718 start = _content.find_last_not_of(
" \t");
719 if (start == _content.npos)
722 _currentNode = _currentNode->
getParent();
731 start = _content.find(
'=');
732 if (start == _content.npos)
738 end = _content.find_first_of(
"\"\'", start + 1);
739 if (end == _content.npos)
744 end = _content.find_first_of(
"\"\'", end + 1);
745 if (end == _content.npos)
751 std::string key = _content.substr(0, start);
752 std::string value = _content.substr(start + 1, end - start);
755 if (! checkPair(key, value))
765 _content = _content.substr(end + 1);
768 start = _content.find_first_not_of(
" \t");
769 if (start == _content.npos)
779 _currentNode = _currentNode->
getParent();
786 bool Document::checkPair(std::string& _key, std::string& _value)
792 size_t start = _key.find_first_of(
" \t\"\'&");
793 if (start != _key.npos)
798 if (_value.size() < 2)
800 if (((_value[0] !=
'"') || (_value[_value.length() - 1] !=
'"')) &&
801 ((_value[0] !=
'\'') || (_value[_value.length() - 1] !=
'\'')))
809 size_t Document::find(
const std::string& _text,
char _char,
size_t _start)
815 char buff[16] =
"\"_\0";
822 pos = _text.find_first_of(buff, pos);
825 if (pos == _text.npos)
830 else if (_text[pos] ==
'"')
850 void Document::clearDeclaration()
859 void Document::clearRoot()
884 bool Document::parseLine(std::string& _line,
ElementPtr& _element)
890 size_t start = find(_line,
'<');
891 if (start == _line.npos)
893 size_t end = _line.npos;
896 if ((start + 3 < _line.size()) && (_line[start + 1] ==
'!') && (_line[start + 2] ==
'-') && (_line[start + 3] ==
'-'))
898 end = _line.find(
"-->", start + 4);
899 if (end == _line.npos)
905 end = find(_line,
'>', start + 1);
906 if (end == _line.npos)
910 size_t body = _line.find_first_not_of(
" \t<");
913 std::string body_str = _line.substr(0, start);
929 if (!parseTag(_element, _line.substr(start + 1, end - start - 1)))
934 _line = _line.substr(end + 1);
941 const std::string& error = mLastError.
print();
944 return MyGUI::utility::toString(
"'", error,
"' , file='", mLastErrorFile,
"' , line=", mLine,
" , col=", mCol);
967 void Document::setLastFileError(
const std::string& _filename)
969 mLastErrorFile = _filename;
972 void Document::setLastFileError(
const std::wstring& _filename)