tibia-client/src/framework/otml/otmlemitter.cpp

93 lines
3.1 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2017-01-13 11:47:07 +01:00
* Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-07-13 23:12:36 +02:00
#include "otmlemitter.h"
2011-08-14 04:09:11 +02:00
#include "otmldocument.h"
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
std::string OTMLEmitter::emitNode(const OTMLNodePtr& node, int currentDepth)
2011-07-13 23:12:36 +02:00
{
2011-08-14 04:09:11 +02:00
std::stringstream ss;
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
// emit nodes
if(currentDepth >= 0) {
// fill spaces for current depth
for(int i=0;i<currentDepth;++i)
ss << " ";
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
// emit node tag
if(node->hasTag()) {
ss << node->tag();
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
// add ':' to if the node is unique or has value
if(node->hasValue() || node->isUnique() || node->isNull())
2011-08-14 04:09:11 +02:00
ss << ":";
} else
ss << "-";
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
// emit node value
if(node->isNull())
ss << " ~";
else if(node->hasValue()) {
2011-08-14 04:09:11 +02:00
ss << " ";
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
std::string value = node->value();
// emit multiline values
if(value.find("\n") != std::string::npos) {
if(value[value.length()-1] == '\n' && value[value.length()-2] == '\n')
ss << "|+";
else if(value[value.length()-1] == '\n')
ss << "|";
else
ss << "|-";
// multilines
for(std::size_t pos = 0; pos < value.length(); ++pos) {
ss << "\n";
// fill spaces for multiline depth
for(int i=0;i<currentDepth+1;++i)
ss << " ";
// fill until a new line
while(pos < value.length()) {
if(value[pos] == '\n')
break;
ss << value[pos++];
}
}
// emit inline values
} else
ss << value;
}
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
// emit children
for(int i=0;i<node->size();++i) {
if(currentDepth >= 0 || i != 0)
ss << "\n";
ss << emitNode(node->atIndex(i), currentDepth+1);
2011-08-14 04:09:11 +02:00
}
2011-07-13 23:12:36 +02:00
return ss.str();
}