many changes and refactoring

This commit is contained in:
Eduardo Bart
2011-07-13 18:12:36 -03:00
parent 6c05ee0e82
commit 8ef1b28546
120 changed files with 1545 additions and 1273 deletions

View File

@@ -0,0 +1,9 @@
#ifndef OTML_H
#define OTML_H
#include "otmlnode.h"
#include "otmlemitter.h"
#include "otmlparser.h"
#endif // OTML_H

View File

@@ -0,0 +1,63 @@
#include "otmlemitter.h"
#include "otmlnode.h"
#include <boost/algorithm/string.hpp>
OTMLEmitter::OTMLEmitter() :
m_rootNode(0)
{
}
OTMLEmitter::~OTMLEmitter()
{
if(m_rootNode)
delete m_rootNode;
}
OTMLNode* OTMLEmitter::createDocument()
{
m_rootNode = new OTMLNode;
return m_rootNode;
}
std::string OTMLEmitter::emitDocument()
{
if(m_rootNode)
return emitNode(m_rootNode, 0);
return std::string();
}
std::string OTMLEmitter::emitNodeValue(OTMLNode* node)
{
std::string value = node->value();
if(!value.empty() && (value[0] == '"' || *value.rbegin() == '"'||
value[0] == ' ' || *value.rbegin() == ' '||
value[0] == '-' || value[0] == '{' || value[0] == '[' || value[0] == '|' ||
value.find("\n") != std::string::npos)) {
boost::replace_all(value, "\\", "\\\\");
boost::replace_all(value, "\"", "\\\"");
boost::replace_all(value, "\n", "\\n");
value.append("\"");
value.insert(0, "\"");
}
return value;
}
std::string OTMLEmitter::emitNode(OTMLNode* node, int currentDepth)
{
std::stringstream ss;
for(int i=1;i<currentDepth;++i)
ss << " ";
if(currentDepth > 0) {
if(node->hasTag())
ss << node->tag();
if(node->hasValue())
ss << (node->hasTag() ? ": " : "- ") << emitNodeValue(node);
if(!node->hasTag() && !node->hasValue())
ss << "-";
ss << std::endl;
}
for(int i=0;i<node->size();++i)
ss << emitNode(node->at(i), currentDepth+1);
return ss.str();
}

View File

@@ -0,0 +1,24 @@
#ifndef OTMLEMITTER_H
#define OTMLEMITTER_H
#include <string>
class OTMLNode;
class OTMLEmitter
{
public:
OTMLEmitter();
~OTMLEmitter();
OTMLNode* createDocument();
std::string emitDocument();
static std::string emitNodeValue(OTMLNode* node);
static std::string emitNode(OTMLNode* node, int currentDepth = 0);
private:
OTMLNode* m_rootNode;
};
#endif // OTMLEMITTER_H

View File

@@ -0,0 +1,93 @@
#include "otmlnode.h"
#include <boost/algorithm/string.hpp>
OTMLNode::OTMLNode(std::string what) :
m_parent(0), m_line(0), m_what(what)
{
}
OTMLNode::~OTMLNode()
{
for(int i=0;i<size();++i)
delete at(i);
}
OTMLNode* OTMLNode::at(const std::string& childTag) const {
int i=0;
while(i<size() && at(i)->tag()!=childTag)
++i;
return at(i);
}
OTMLNode* OTMLNode::at(int pos) const
{
if(pos < (int)m_children.size() && pos >= 0)
return m_children[pos];
return 0;
}
OTMLNode* OTMLNode::atPath(const std::string& path) const
{
std::vector<std::string> nodeTags;
OTMLNode* node = const_cast<OTMLNode*>(this);
std::string shortcutKey;
boost::split(nodeTags, path, boost::is_any_of(std::string("/")));
foreach(std::string& stag, nodeTags) {
if(!shortcutKey.empty())
shortcutKey += '.';
shortcutKey += stag;
if(node)
node = node->at(stag);
}
if(node)
return node;
else
return at(shortcutKey);
}
OTMLNode* OTMLNode::createNode(std::string tag)
{
OTMLNode* node = new OTMLNode;
node->setTag(tag);
addNode(node);
return node;
}
void OTMLNode::addNode(OTMLNode* node) {
if(node->hasTag() && node->hasValue())
if(OTMLNode* other = at(node->tag()))
removeNode(other);
m_children.push_back(node);
node->setParent(this);
}
bool OTMLNode::removeNode(OTMLNode* node) {
for(NodeList::iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == node) {
m_children.erase(it);
return true;
}
}
return false;
}
std::string OTMLNode::generateErrorMessage(const std::string& message) const {
std::stringstream ss;
ss << "OTML error";
if(!what().empty())
ss << " in '" << what() << "'";
if(m_line > 0)
ss << " at line " << m_line;
if(m_line > 0 && hasTag())
ss << ", in node '" << tag() << "'";
ss << ": " << message;
return ss.str();
}
void OTMLNode::throwError(const std::string& message) const
{
throw OTMLException(generateErrorMessage(message));
}

View File

@@ -0,0 +1,260 @@
#ifndef OTMLNODE_H
#define OTMLNODE_H
#include <string>
#include <vector>
#include <list>
#include <map>
#include <stdexcept>
#include <typeinfo>
#include <util/foreach.h>
#include <util/makestring.h>
#include <util/convert.h>
class OTMLException : public std::runtime_error {
public:
OTMLException(const std::string& what) : std::runtime_error(what) {}
};
class OTMLNode
{
public:
typedef std::vector<OTMLNode*> NodeList;
typedef NodeList::iterator iterator;
typedef NodeList::const_iterator const_iterator;
OTMLNode(std::string what = "");
~OTMLNode();
bool hasTag() const { return !m_tag.empty(); }
bool hasChildren() const { return size() > 0; }
bool hasValue() const { return !m_value.empty(); }
bool hasNode(const std::string ctag) const { return at(ctag) != 0; }
void setTag(std::string tag) { m_tag = tag; }
void setLine(int line) { m_line = line; }
void setValue(const std::string& value) { m_value = value; }
void setParent(OTMLNode* parent) { m_parent = parent; }
std::string tag() const { return m_tag; }
int line() const { return m_line; }
int size() const { return m_children.size(); }
OTMLNode* parent() { return m_parent; }
std::string what() const { return (m_parent ? m_parent->what() : m_what); }
iterator begin() { return m_children.begin(); }
iterator end() { return m_children.end(); }
const_iterator begin() const { return m_children.begin(); }
const_iterator end() const { return m_children.end(); }
OTMLNode* front() const { return at(0); }
OTMLNode* back() const { return at(size()-1); }
OTMLNode* at(const std::string& ctag) const;
OTMLNode* at(int pos) const;
OTMLNode *atPath(const std::string& path) const;
OTMLNode* createNode(std::string tag = "");
void addNode(OTMLNode* node);
bool removeNode(OTMLNode* node);
std::string generateErrorMessage(const std::string& message) const;
void throwError(const std::string& message) const;
std::string value(const std::string& def = "") const { return (m_value.empty() ? def : m_value); }
std::string valueAt(const std::string ctag, const std::string& def = "") const {
OTMLNode* c = at(ctag);
return (c ? c->value() : def);
}
std::string valueAt(int pos, const std::string& def = "") const {
OTMLNode* n = at(pos);
return (n ? n->value() : def);
}
std::string valueAtPath(const std::string path, const std::string& def = "") const {
OTMLNode* c = atPath(path);
return (c ? c->value() : def);
}
// read into memory
template <typename T>
void read(T* v) const {
if(!(*this >> *v))
throwError(make_string("failed to cast node value to type ", std::string(typeid(T).name())));
}
template <typename T>
bool readAt(const std::string& ctag, T* v) const {
if(OTMLNode* node = at(ctag)) {
node->read<T>(v);
return true;
}
return false;
}
template <typename T>
bool readAt(int pos, T* v) const {
if(OTMLNode* node = at(pos)) {
node->read<T>(v);
return true;
}
return false;
}
template <typename T>
bool readAtPath(const std::string& ctag, T* v) const {
if(OTMLNode* node = atPath(ctag)) {
node->read<T>(v);
return true;
}
return false;
}
// read returning the result
template <typename T>
T read() const { T v; read<T>(&v); return v;}
template <typename T>
T readAt(const std::string& ctag) const {
T v;
if(!readAt(ctag, &v))
throwError(make_string("child node \'", ctag, "\' not found"));
return v;
}
template <typename T>
T readAt(int pos) const {
T v;
if(!readAt(pos, &v))
throwError(make_string("child node at pos ", pos, " not found"));
return v;
}
template <typename T>
T readAtPath(const std::string& ctag) const {
T v;
if(!readAtPath(ctag, &v))
throwError(make_string("child node in path \'", ctag, "\' not found"));
return v;
}
// read with default supplied
template <typename T>
T readAt(const std::string& ctag, const T& def) const {
OTMLNode* c = at(ctag);
return (c ? c->read<T>() : def);
}
template <typename T>
T readAt(int pos, const T& def) const {
OTMLNode* c = at(pos);
return (c ? c->read<T>() : def);
}
template <typename T>
T readAtPath(const std::string& path, const T& def) const {
OTMLNode* c = atPath(path);
return (c ? c->read<T>() : def);
}
// writing
template <typename T>
void write(T v) {
if(!(*this << v))
throwError(make_string("failed to cast to string node value of type ", typeid(T).name()));
}
template <typename T>
void writeIn(int pos, T v) {
OTMLNode* c;
while(!at(pos))
c = createNode();
c->write<T>(v);
}
template <typename T>
void writeIn(const std::string& ctag, T v) {
OTMLNode* c = at(ctag);
if(!c)
c = createNode(ctag);
c->write<T>(v);
}
private:
OTMLNode* m_parent;
int m_line;
std::string m_what;
NodeList m_children;
std::string m_tag;
std::string m_value;
};
// read operators
template <typename T>
bool operator >> (const OTMLNode& node, T& v) { return safe_convert(node.value(), v); }
template <typename T>
bool operator >> (const OTMLNode& node, std::vector<T>& v) {
v.resize(node.size());
for(unsigned i=0;i<node.size();++i)
v[i] = node.readAt<T>(i);
return true;
}
template <typename T>
bool operator >> (const OTMLNode& node, std::list<T>& v) {
for(unsigned i=0;i<node.size();++i)
v.push_back(node.readAt<T>(i));
return true;
}
template <typename K, typename T>
bool operator >> (const OTMLNode& node, std::map<K, T>& m) {
for(int i=0;i<node.size();++i) {
K k;
if(!safe_convert(node.at(i)->tag(), k))
return false;
m[k] = node.at(i)->read<T>();
}
return true;
}
// write operators
template <typename T>
bool operator << (OTMLNode& node, const T& v) {
std::string out;
if(!safe_convert(v, out))
return false;
node.setValue(out);
return true;
}
template <typename T>
bool operator << (OTMLNode& node, const std::vector<T>& v) {
for(unsigned i=0;i<v.size();++i)
node.createNode()->write(v[i]);
return true;
}
template <typename T>
bool operator << (OTMLNode& node, const std::list<T>& v) {
for(unsigned i=0;i<v.size();++i)
node.createNode()->write(v[i]);
return true;
}
template <typename K, typename T>
bool operator << (OTMLNode& node, const std::map<K, T>& m) {
typename std::map<K, T>::const_iterator it;
for(it = m.begin(); it != m.end(); ++it) {
std::string k;
if(!safe_convert(it->first, k))
return false;
node.createNode(k)->write(it->second);
}
return true;
}
#include "otmlnodeext.h"
#endif // OTMLNODE_H

View File

@@ -0,0 +1,84 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* 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.
*/
#ifndef OTMLNODEEXT_H
#define OTMLNODEEXT_H
#include <util/point.h>
#include <util/color.h>
#include <util/rect.h>
#include <util/size.h>
inline bool operator>>(const OTMLNode& node, Color& color)
{
int r, g, b, a;
if(node.readAt(0, &r) &&
node.readAt(1, &g) &&
node.readAt(2, &b)) {
a = 255;
node.readAt(3, &a);
color.setRGBA(r,g,b,a);
return true;
}
return false;
}
template <class T>
inline bool operator>>(const OTMLNode& node, TPoint<T>& point)
{
T x, y;
if(node.readAt(0, &x) && node.readAt(1, &y)) {
point.x = x;
point.y = y;
return true;
}
return false;
}
template <class T>
inline bool operator>>(const OTMLNode& node, TSize<T>& size)
{
T w, h;
if(node.readAt(0, &w) && node.readAt(1, &h)) {
size.setSize(w, h);
return true;
}
return false;
}
template <class T>
inline bool operator>>(const OTMLNode& node, TRect<T>& rect)
{
T x, y, width, height;
if(node.readAt(0, &x) &&
node.readAt(1, &y) &&
node.readAt(2, &width) &&
node.readAt(3, &height)) {
rect.setRect(x, y, width, height);
return true;
}
return false;
}
#endif // OTMLNODEEXT_H

View File

@@ -0,0 +1,259 @@
#include "otmlparser.h"
#include "otmlnode.h"
#include <boost/algorithm/string.hpp>
OTMLParser::OTMLParser(std::istream& in, std::string what) :
m_currentDepth(0), m_currentLine(0),
m_rootNode(new OTMLNode), m_currentParent(m_rootNode), m_previousNode(0),
m_what(what), m_in(in)
{
parse();
}
OTMLParser::~OTMLParser()
{
delete m_rootNode;
}
void OTMLParser::throwError(const std::string& message, int line)
{
std::stringstream ss;
ss << "OTML syntax error";
if(!m_what.empty())
ss << " in '" << m_what << "'";
if(line > 0)
ss << " at line " << line;
ss << ": " << message;
throw OTMLException(ss.str());
}
void OTMLParser::parse()
{
m_rootNode->setTag("document");
while(m_in.good() && !m_in.eof()) {
m_currentLine++;
std::string line;
std::getline(m_in, line);
parseLine(line);
}
}
void OTMLParser::parseLine(std::string line)
{
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// trim left whitespaces
boost::trim_left(line);
// skip comment or empty lines
if(line[0] == '#' || line.empty())
return;
// calculate depth
int depth = 0;
if(numSpaces != std::string::npos)
depth = numSpaces / 2;
// check for syntax error
if(numSpaces != std::string::npos && numSpaces % 2 != 0)
throwError("file must be idented every 2 whitespaces", m_currentLine);
// a depth above, change current parent to the previous added node
if(depth == m_currentDepth+1) {
m_currentParent = m_previousNode;
// a depth below, change parent to previus parent
} else if(depth < m_currentDepth) {
for(int i=0;i<m_currentDepth-depth;++i)
m_currentParent = m_currentParent->parent();
// else if it isn't the current depth it's a syntax error
} else if(depth != m_currentDepth) {
throwError("invalid indentation level", m_currentLine);
}
// update current depth
m_currentDepth = depth;
// add node
OTMLNode* node = m_currentParent->createNode();
m_previousNode = node;
parseNode(node, line);
}
void OTMLParser::parseNode(OTMLNode* node, std::string data)
{
std::string tag;
std::string value;
std::size_t dotsPos = data.find_first_of(':');
// its a node that has a value but no tag
if(!data.empty() && data[0] == '-') {
value = data.substr(1);
boost::trim(value);
// check if it's value is shortcut for adding a child node
if(dotsPos != std::string::npos && !value.empty() && value[0] != '"') {
OTMLNode* child = node->createNode();
parseNode(child, value);
value.clear();
}
}
// its a node that has tag and possible a value
else if(dotsPos != std::string::npos) {
tag = data.substr(0, dotsPos);
value = data.substr(dotsPos+1);
}
// its a node that has only a tag
else {
tag = data;
}
// set node tag
boost::trim(tag);
node->setTag(tag);
// set node line
node->setLine(m_currentLine);
// process node value
parseNodeValue(node, value);
}
void OTMLParser::parseNodeValue(OTMLNode* node, std::string value)
{
boost::trim(value);
if(value.empty())
return;
// multiline text scalar
if(value[0] == '|') {
std::string multiLineData;
do {
std::string line;
size_t lastPos = m_in.tellg();
std::getline(m_in, line);
// calculate numspaces
std::size_t numSpaces = line.find_first_not_of(' ');
// depth above current depth, add the text to the multiline
if(numSpaces != std::string::npos && (int)numSpaces >= (m_currentDepth+1)*2) {
boost::trim(line);
parseTextValue(line);
multiLineData += line + "\n";
}
// if has contents below the current depth, its a node
else if(numSpaces != std::string::npos) {
m_in.seekg(lastPos, std::ios::beg);
break;
}
// else its just a new line
else {
multiLineData += "\n";
}
} while(!m_in.eof());
// determine how to treat last new lines
if(value.length() == 1 || (value.length() == 2 && value[1] == '-')) {
// remove all new lines at the end
while(*multiLineData.rbegin() == '\n')
multiLineData.erase(multiLineData.length()-1, 1);
// keep just one extra line
if(value[0] == '-')
multiLineData.append("\n");
} else if(value.length() > 2 || value[1] != '+')
throwError("invalid multiline identifier", m_currentLine);
node->setValue(multiLineData);
}
// sequence
else if(value[0] == '[') {
std::vector<std::string> tokens;
parseTokens(value.substr(1), tokens);
foreach(std::string& token, tokens) {
OTMLNode* child = node->createNode();
child->setLine(m_currentLine);
parseNodeValue(child, token);
}
}
// inline map
else if(value[0] == '{') {
std::vector<std::string> tokens;
parseTokens(value.substr(1), tokens);
foreach(std::string& token, tokens) {
OTMLNode* child = node->createNode();
parseNode(child, token);
}
}
// text scalar
else {
parseTextValue(value);
node->setValue(value);
}
}
void OTMLParser::parseTextValue(std::string& value)
{
if(value[0] == '"' && value[value.length()-1] == '"') {
value = value.substr(1, value.length()-2);
// escape characters
boost::replace_all(value, "\\\\", "\\");
boost::replace_all(value, "\\\"", "\"");
boost::replace_all(value, "\\n", "\n");
}
}
void OTMLParser::parseTokens(std::string data, std::vector<std::string>& out)
{
bool inQuote = false;
int brackets = 1;
std::string tmp;
uint i = 0;
do {
if(i<data.length()) {
char c = data[i];
tmp += c;
if(c == '"') {
if(!inQuote)
inQuote = true;
else if(data[i-1] != '\\')
inQuote = false;
} else if(!inQuote) {
if(c == '{' || c == '[')
brackets++;
else if(c == '}' || c == ']')
brackets--;
else if(c == ',' && brackets == 1) {
tmp.erase(tmp.length()-1);
boost::trim(tmp);
if(!tmp.empty())
out.push_back(tmp);
tmp.clear();
}
}
if(brackets == 0) {
tmp.erase(tmp.length()-1);
boost::trim(tmp);
if(!tmp.empty())
out.push_back(tmp);
break;
}
}
if(i+1 >= data.length() && !m_in.eof()) {
std::string line;
std::getline(m_in, line);
boost::trim(line);
data += " ";
data += line;
}
++i;
} while(i<data.length());
if(brackets != 0)
throwError("no matching bracket while parsing, did you forget to close one?", m_currentLine);
}

View File

@@ -0,0 +1,38 @@
#ifndef OTMLPARSER_H
#define OTMLPARSER_H
#include <global.h>
class OTMLNode;
class OTMLParser
{
public:
OTMLParser(std::istream& in, std::string what = "");
~OTMLParser();
OTMLNode* getDocument() const { return m_rootNode; }
std::string what() { return m_what; }
void throwError(const std::string& message, int line);
protected:
void parse();
void parseLine(std::string line);
void parseNode(OTMLNode* node, std::string data);
void parseNodeValue(OTMLNode* node, std::string value);
void parseTextValue(std::string& value);
void parseTokens(std::string data, std::vector<std::string>& out);
private:
int m_currentDepth;
int m_currentLine;
OTMLNode* m_rootNode;
OTMLNode* m_currentParent;
OTMLNode* m_previousNode;
std::string m_what;
std::istream& m_in;
};
#endif // OTMLPARSER_H