replace YAML with custom made library for reading text files named FML

This commit is contained in:
Eduardo Bart
2011-05-21 15:15:46 -03:00
parent 0cd4bcd926
commit 80e42b0f96
29 changed files with 826 additions and 412 deletions

View File

@@ -76,4 +76,14 @@ inline std::ostream& operator<<(std::ostream& out, const Color& color)
return out;
}
inline void operator>>(const FML::Node& node, Color& color)
{
int r, g, b, a;
*node.at(0) >> r;
*node.at(1) >> g;
*node.at(2) >> b;
*node.at(3) >> a;
color.setRGBA(r,g,b,a);
}
#endif // COLOR_H

359
src/framework/util/fml.cpp Normal file
View File

@@ -0,0 +1,359 @@
#include "fml.h"
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
namespace FML {
///////////////////////////////////////////////////////////////////////////////
// Utilities
bool fml_convert(const std::string& input, bool& b)
{
std::string names[5][2] = { { "1", "0" },
{ "y", "n" },
{ "yes", "no" },
{ "true", "false" },
{ "on", "off" } };
std::string processedInput = input;
boost::trim(processedInput);
boost::to_lower(processedInput);
for(int i=0;i<5;i++) {
if(names[i][0] == processedInput) {
b = true;
return true;
}
if(names[i][1] == processedInput) {
b = false;
return true;
}
}
return false;
}
bool fml_convert(const std::string& input, std::string& output) {
output = input;
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Node
Node::~Node()
{
for(NodeList::iterator it = m_children.begin(); it != m_children.end(); ++it)
delete (*it);
}
Node* Node::at(const std::string& childTag) const
{
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == childTag)
return (*it);
}
return NULL;
}
Node* Node::at(int pos) const
{
if(pos < 0 || pos >= size())
return NULL;
return m_children[pos];
}
std::string Node::value(const std::string& def) const
{
if(!m_value.empty())
return m_value;
return def;
}
std::string Node::valueAt(const std::string childTag, const std::string& def) const
{
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == childTag)
return (*it)->value();
}
return def;
}
void Node::addNode(Node *node)
{
if(node->hasTag() && node->hasValue()) {
// remove nodes wit the same tag
for(NodeList::iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == node->tag()) {
delete (*it);
m_children.erase(it);
break;
}
}
}
m_children.push_back(node);
node->setParent(this);
}
std::string Node::generateErrorMessage(const std::string& message) const
{
std::stringstream ss;
ss << "FML error ";
if(m_parser && !m_parser->getWhat().empty())
ss << "in " << m_parser->getWhat();
if(m_line > 0)
ss << "at line " << m_line;
ss << ": " << message;
return ss.str();
}
///////////////////////////////////////////////////////////////////////////////
// Parser
Parser::~Parser()
{
if(m_rootNode)
delete m_rootNode;
}
bool Parser::load(std::istream& in)
{
// initialize root node
if(m_rootNode)
delete m_rootNode;
m_rootNode = new Node();
m_rootNode->setTag("root");
m_currentParent = m_rootNode;
m_currentDepth = 0;
m_currentLine = 0;
m_multilineMode = DONT_MULTILINE;
m_multilineData.clear();
while(in.good() && !in.eof()) {
m_currentLine++;
std::string line;
std::getline(in, line);
parseLine(line);
if(hasError())
return false;
}
// stop multilining if enabled
if(isMultilining())
stopMultilining();
return !hasError();
}
void Parser::parseLine(std::string& line)
{
// process multiline data first
if(isMultilining() && parseMultiline(line))
return;
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// trim left whitespaces
boost::trim_left(line);
// skip comment lines
if(line[0] == '#')
return;
// skip empty lines
if(line.empty())
return;
int depth = 0;
if(numSpaces != std::string::npos) {
depth = numSpaces / 2;
// check for syntax error
if(numSpaces % 2 != 0) {
setErrorMessage("file must be idented every 2 whitespaces", m_currentLine);
return;
}
}
// a depth above
if(depth == m_currentDepth+1) {
// change current parent to the previous added node
m_currentParent = m_previousNode;
// a depth below, change parent to previus parent and add new node inside previuos parent
} else if(depth < m_currentDepth) {
// change current parent to the the new depth parent
for(int i=0;i<m_currentDepth-depth;++i)
m_currentParent = m_currentParent->parent();
// else if nots the same depth it's a syntax error
} else if(depth != m_currentDepth) {
setErrorMessage("invalid indentation level", m_currentLine);
return;
}
// update current depth
m_currentDepth = depth;
// add node
Node *node = parseNode(line);
m_currentParent->addNode(node);
m_previousNode = node;
}
Node *Parser::parseNode(std::string& line)
{
// determine node tag and value
std::string tag;
std::string value;
// its a node that has tag and possible a value
std::size_t dotsPos = line.find_first_of(':');
if(dotsPos != std::string::npos) {
tag = line.substr(0, dotsPos);
value = line.substr(dotsPos+1);
}
// its a node that has a value but no tag
else if(line[0] == '-') {
value = line.substr(1);
}
// its a node that has only a tag
else {
tag = line;
}
// trim the tag and value
boost::trim(tag);
boost::trim(value);
// create the node
Node *node = new Node(this);
node->setLine(m_currentLine);
node->setTag(tag);
// process node value
if(!value.empty()) {
// multiline text scalar
if(value[0] == '|') {
startMultilining(value);
}
// sequence
else if(value[0] == '[') {
if(boost::ends_with(value, "]")) {
value.erase(value.length()-1, 1);
value.erase(0, 1);
boost::trim(value);
boost::tokenizer<boost::escaped_list_separator<char> > tokens(value);
for(boost::tokenizer<boost::escaped_list_separator<char> >::iterator it = tokens.begin(); it != tokens.end(); ++it) {
value = *it;
boost::trim(value);
Node *child = new Node(this);
child->setLine(m_currentLine);
child->setValue(value);
node->addNode(child);
}
} else
setErrorMessage("missing ']' in sequence", m_currentLine);
}
// text scalar
else {
node->setValue(parseTextScalar(value));
}
}
return node;
}
std::string Parser::parseTextScalar(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");
}
return value;
}
void Parser::startMultilining(const std::string& param)
{
m_multilineMode = MULTILINE_DONT_FOLD;
m_currentDepth++;
if(param.length() == 2) {
switch(param[1]) {
case '-':
m_multilineMode = MULTILINE_FOLD_BLOCK;
break;
case '+':
m_multilineMode = MULTILINE_FOLD_FLOW;
break;
default:
setErrorMessage("invalid multiline identifier", m_currentLine);
break;
}
}
}
void Parser::stopMultilining()
{
// remove all new lines at the end
if(m_multilineMode == MULTILINE_DONT_FOLD || m_multilineMode == MULTILINE_FOLD_BLOCK) {
while(true) {
int lastPos = m_multilineData.length()-1;
if(m_multilineData[lastPos] != '\n')
break;
m_multilineData.erase(lastPos, 1);
}
}
if(m_multilineMode == MULTILINE_FOLD_BLOCK)
m_multilineData.append("\n");
m_previousNode->setValue(m_multilineData);
m_multilineMode = DONT_MULTILINE;
m_currentDepth--;
m_multilineData.clear();
}
bool Parser::parseMultiline(std::string line)
{
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// depth above or equal current depth, add the text to the multiline
if(numSpaces != std::string::npos && (int)numSpaces >= m_currentDepth*2) {
m_multilineData += parseTextScalar(line.substr(m_currentDepth*2)) + "\n";
return true;
// depth below the current depth, check if it is a node
} else if(numSpaces == std::string::npos || (int)numSpaces < m_currentDepth*2) {
// if it has contents, its a node, then we must end multiline
if(line.find_first_not_of(' ') != std::string::npos) {
stopMultilining();
// the line still have a node on it
}
// no contents, just an extra line
else {
m_multilineData += "\n";
return true;
}
}
return false;
}
void Parser::setErrorMessage(const std::string& message, int line)
{
std::stringstream ss;
ss << "FML syntax error in ";
if(!m_what.empty())
ss << m_what << " ";
if(line > 0)
ss << "at line " << line;
ss << ": " << message;
m_error = ss.str();
}
} // namespace FML {

217
src/framework/util/fml.h Normal file
View File

@@ -0,0 +1,217 @@
#ifndef FML_H
#define FML_H
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <map>
#include <boost/utility.hpp>
#include <boost/foreach.hpp>
namespace FML {
///////////////////////////////////////////////////////////////////////////////
// Utilities
bool fml_convert(const std::string& input, bool& b);
bool fml_convert(const std::string& input, std::string& output);
template <typename V, typename T>
bool fml_convert(const V& in, T& out) {
std::stringstream ss;
ss << in;
ss >> out;
return !!ss;
}
///////////////////////////////////////////////////////////////////////////////
// Node
class Parser;
class Node : boost::noncopyable
{
public:
typedef std::vector<Node*> NodeList;
Node(Parser* parser = 0) : m_parser(parser), m_parent(0), m_line(0) { }
~Node();
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 childTag) const { return at(childTag) != 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(Node* parent) { m_parent = parent; }
std::string tag() const { return m_tag; }
int line() const { return m_line; }
int size() const { return m_children.size(); }
Node* parent() { return m_parent; }
// iterators
typedef NodeList::iterator iterator;
typedef NodeList::const_iterator const_iterator;
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(); }
Node* front() const { return at(0); }
Node* back() const { return at(size()-1); }
// util for generating error message
std::string generateErrorMessage(const std::string& message) const;
// extracting values operator
template <typename T>
friend void operator >> (const Node& node, T& value);
// get nodes
Node* at(const std::string& childTag) const;
Node* at(int pos) const;
// get values
std::string value(const std::string& def = std::string()) const;
std::string valueAt(const std::string childTag, const std::string& def = std::string()) const;
// read values
template <typename T>
T read(T def = T()) const {
T value = def;
*this >> value;
return value;
}
template <typename T>
T readAt(const std::string childTag, T def = T()) const {
T v = def;
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == childTag) {
*(*it) >> v;
break;
}
}
return v;
}
void addNode(Node* node);
private:
Parser* m_parser;
Node* m_parent;
int m_line;
NodeList m_children;
std::string m_tag;
std::string m_value;
};
///////////////////////////////////////////////////////////////////////////////
// Node operators
template <typename T>
void operator >> (const Node& node, T& v)
{
fml_convert(node.value(), v);
}
template <typename T>
void operator >> (const Node& node, std::vector<T>& v)
{
v.clear();
v.resize(node.size());
for(unsigned i=0;i<node.size();++i)
*node.at(i) >> v[i];
}
template <typename K, typename T>
void operator >> (const Node& node, std::map<K, T>& m)
{
m.clear();
for(Node::const_iterator it = node.begin(); it != node.end(); ++it) {
Node* child = (*it);
K k;
T v;
if(fml_convert<std::string, K>(child->tag(), k)) {
*child >> v;
m[k] = v;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Parser
class Parser
{
enum MultilineMode {
DONT_MULTILINE = 0,
MULTILINE_DONT_FOLD,
MULTILINE_FOLD_BLOCK,
MULTILINE_FOLD_FLOW
};
public:
Parser(std::string what = std::string()) : m_rootNode(0), m_what(what) { }
Parser(std::istream& in, std::string what = std::string()) : m_rootNode(0), m_what(what) { load(in); }
~Parser();
bool load(std::istream& in);
Node* getDocument() const { return m_rootNode; }
bool hasError() const { return !m_error.empty(); }
std::string getErrorMessage() { return m_error; }
std::string getWhat() { return m_what; }
protected:
void parseLine(std::string& line);
Node* parseNode(std::string& line);
std::string parseTextScalar(std::string value);
void startMultilining(const std::string& param);
void stopMultilining();
bool parseMultiline(std::string line);
bool isMultilining() { return m_multilineMode != DONT_MULTILINE; }
void setErrorMessage(const std::string& message, int line = 0);
private:
int m_currentDepth;
int m_currentLine;
Node* m_currentParent;
Node* m_previousNode;
Node* m_rootNode;
MultilineMode m_multilineMode;
std::string m_multilineData;
std::string m_error;
std::string m_what;
};
} // namespace FML {
// enable usage with foreach
namespace boost
{
// specialize range_mutable_iterator and range_const_iterator in namespace boost
template<>
struct range_mutable_iterator< FML::Node >
{
typedef FML::Node::iterator type;
};
template<>
struct range_const_iterator< FML::Node >
{
typedef FML::Node::const_iterator type;
};
}
#endif // FML_H

View File

@@ -85,4 +85,11 @@ inline std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
return out;
}
template <class T>
inline void operator>>(const FML::Node& node, TPoint<T>& point)
{
*node.at(0) >> point.x;
*node.at(1) >> point.y;
}
#endif

View File

@@ -309,4 +309,15 @@ inline std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
return out;
}
template <class T>
inline void operator>>(const FML::Node& node, TRect<T>& rect)
{
T x, y, width, height;
*node.at(0) >> x;
*node.at(1) >> y;
*node.at(2) >> width;
*node.at(3) >> height;
rect.setRect(x, y, width, height);
}
#endif // RECT_H

View File

@@ -112,11 +112,12 @@ typedef TSize<int> Size;
typedef TSize<float> SizeF;
template <class T>
inline std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
inline void operator>>(const FML::Node& node, TSize<T>& size)
{
out << "Size(" << size.width() << ","
<< size.height() << ")";
return out;
T w, h;
*node.at(0) >> w;
*node.at(1) >> h;
size.setSize(w, h);
}
#endif

View File

@@ -1,125 +0,0 @@
/* 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 YAML_H
#define YAML_H
#include <prerequisites.h>
#include <yaml-cpp/yaml.h>
inline void operator>>(const YAML::Node& node, Color& color)
{
int r, g, b, a;
node[0] >> r;
node[1] >> g;
node[2] >> b;
node[3] >> a;
color.setRGBA(r,g,b,a);
}
template <class T>
inline void operator>>(const YAML::Node& node, TPoint<T>& point)
{
node[0] >> point.x;
node[1] >> point.y;
}
template <class T>
inline void operator>>(const YAML::Node& node, TRect<T>& rect)
{
T x, y, width, height;
node[0] >> x;
node[1] >> y;
node[2] >> width;
node[3] >> height;
rect.setRect(x, y, width, height);
}
template <class T>
inline void operator>>(const YAML::Node& node, TSize<T>& size)
{
T w, h;
node[0] >> w;
node[1] >> h;
size.setSize(w, h);
}
template<class T>
inline T yamlRead(const YAML::Node& node)
{
return node.to<T>();
}
template<class T>
inline T yamlRead(const YAML::Node& node, const char *name)
{
T value;
if(node.Type() == YAML::NodeType::Map)
node[name] >> value;
return value;
}
template<class T>
inline T yamlRead(const YAML::Node& node, const char *name, const T& defaultValue)
{
T value = defaultValue;
if(node.Type() == YAML::NodeType::Map && node.FindValue(name))
node[name] >> value;
return value;
}
inline bool yamlHasValue(const YAML::Node& node, const char *name)
{
return (node.Type() == YAML::NodeType::Map && node.FindValue(name) != NULL);
}
inline std::string yamlErrorDesc(const YAML::Node& node, const std::string& error)
{
return YAML::Exception(node.GetMark(), error.c_str()).what();
}
inline void yamlThrowError(const YAML::Node& node, const std::string& error)
{
throw YAML::Exception(node.GetMark(), error.c_str());
}
template<class A, class B>
inline std::map<A,B> yamlReadMap(const YAML::Node& node, const char *name)
{
std::map<A,B> map;
if(node.Type() == YAML::NodeType::Map) {
if(const YAML::Node* mapNode = node.FindValue(name)) {
for(auto it = mapNode->begin(); it != mapNode->end(); ++it) {
A a;
B b;
it.first() >> a;
it.second() >> b;
map[a] = b;
}
}
}
return map;
}
#endif // YAML_H