Commit 3fd35272 authored by Alexandre Savard's avatar Alexandre Savard

[#3647] First implementation of the yaml parser

parent 6be50311
......@@ -6,7 +6,8 @@ libconfig_la_SOURCES = \
config.cpp \
yamlengine.cpp \
yamlemitter.cpp \
yamlparser.cpp
yamlparser.cpp \
yamlnode.cpp
noinst_HEADERS = \
config.h \
......@@ -14,7 +15,8 @@ noinst_HEADERS = \
serializable.h \
yamlengine.h \
yamlemitter.h \
yamlparser.h
yamlparser.h \
yamlnode.h
libconfig_la_LDFLAGS = @yaml_LIBS@
......
......@@ -49,7 +49,7 @@ ConfigTree::ConfigTree() :_sections()
YamlParser *parser;
try {
parser = new YamlParser();
parser = new YamlParser("sequence.yml");
}
catch (YamlParserException &e) {
_error("ConfigTree: %s", e.what());
......@@ -57,7 +57,7 @@ ConfigTree::ConfigTree() :_sections()
try {
parser->parse();
parser->serializeEvents();
}
catch(YamlParserException &e) {
_error("ConfigTree: %s", e.what());
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#include "yamlnode.h"
namespace Conf {
void MappingNode::setKeyValue(Key key, YamlNode *value)
{
Mapping::iterator it = map.end();
map.insert(it, std::pair<Key, YamlNode *>(key, value));
}
void MappingNode::removeKeyValue(Key key)
{
Mapping::iterator it = map.find(key);
map.erase(it);
}
YamlNode *MappingNode::getValue(Key key)
{
Mapping::iterator it = map.find(key);
return it->second;
}
void SequenceNode::addNode(YamlNode *node)
{
Sequence::iterator it = seq.end();
seq.insert(it, node);
}
}
/*
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this program, or any covered work, by linking or
* combining it with the OpenSSL project's OpenSSL library (or a
* modified version of that library), containing parts covered by the
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
* grants you additional permission to convey the resulting work.
* Corresponding Source for a non-source form of such a combination
* shall include the source code for the parts of OpenSSL used as well
* as that of the covered work.
*/
#ifndef __YAMLNODE_H__
#define __YAMLNODE_H__
#include <string>
#include <list>
#include <map>
#include <exception>
namespace Conf {
class YamlNode;
typedef std::string Key;
typedef std::string Value;
typedef std::list<YamlNode *> Sequence;
typedef std::map<Key, YamlNode *> Mapping;
class YamlNodeException : public std::exception
{
public:
YamlNodeException(const std::string& str="") throw() : errstr(str) {}
virtual ~YamlNodeException() throw() {}
virtual const char *what() const throw() {
std::string expt("YamlNodeException occured: ");
expt.append(errstr);
return expt.c_str();
}
private:
std::string errstr;
};
enum NodeType { DOCUMENT, SCALAR, MAPPING, SEQUENCE };
class YamlNode {
public:
YamlNode(NodeType t) : type(t) {}
~YamlNode() {}
NodeType getType() { return type; }
private:
NodeType type;
};
class SequenceNode : public YamlNode {
public:
SequenceNode() : YamlNode(SEQUENCE) {}
~SequenceNode() {}
Sequence *getSequence() { return &seq; }
void addNode(YamlNode *node);
private:
Sequence seq;
};
class MappingNode : public YamlNode {
public:
MappingNode() : YamlNode(MAPPING) {}
~MappingNode() {}
Mapping *getMapping() { return &map; }
void setKeyValue(Key key, YamlNode *value);
void removeKeyValue(Key key);
YamlNode *getValue(Key key);
private:
Mapping map;
};
class ScalarNode : public YamlNode {
public:
ScalarNode(Value v="") : YamlNode(SCALAR), val(v) {}
~ScalarNode() {}
Value getValue() { return val; }
void setValue(Value v) { val = v; }
private:
Value val;
};
}
#endif
......@@ -32,11 +32,12 @@
#include "../global.h"
#include "config.h"
#include "yamlnode.h"
#include <stdio.h>
namespace Conf {
YamlParser::YamlParser()
YamlParser::YamlParser(const char *file) : filename(file)
{
memset(buffer, 0, PARSER_BUFFERSIZE);
......@@ -51,8 +52,6 @@ YamlParser::~YamlParser()
void YamlParser::open()
{
std::string filename = "sequence.yml";
fd = fopen(filename.c_str(), "rb");
if(!fd)
......@@ -77,7 +76,7 @@ void YamlParser::close()
}
void YamlParser::parse()
void YamlParser::serializeEvents()
{
bool done = false;
yaml_event_t event;
......@@ -203,12 +202,12 @@ void YamlParser::composeEvents() {
break;
case YAML_SCALAR_EVENT: {
_debug("YAML_SCALAR_EVENT: anchor %s, tag %s, value %s", events[i].data.scalar.anchor, events[i].data.scalar.tag, events[i].data.scalar.value);
// std::string tmp(events[i].data.scalar.value);
std::string tmp("ok");
size_t found = tmp.find("account");
// if this is an account
if(found != std::string::npos)
composeAccount(i);
char buffer[1000];
snprintf(buffer, 1000, "%s", events[i].data.scalar.value);
_debug("----------------------------- THE BUFFER: %s", buffer);
ScalarNode *sclr = new ScalarNode(buffer);
_debug("----------------------------- THE VALUE: %s", (sclr->getValue()).c_str());
// ScalarNode *sclr = new ScalarNode("ok");
}
break;
case YAML_SEQUENCE_START_EVENT:
......@@ -231,18 +230,5 @@ void YamlParser::composeEvents() {
}
}
int YamlParser::composeAccount(int index)
{
// YamlScalar accid((const char*)(events[index].data.scalar.value));
YamlScalar accid("ok");
YamlSequence seq;
// YamlAccount acc(accid, seq);
// accountlist.insert(acc);
return index+1;
}
}
......@@ -35,20 +35,12 @@
#include <stdio.h>
#include <exception>
#include <string>
#include <map>
#include <list>
namespace Conf {
#define PARSER_BUFFERSIZE 65536
#define PARSER_MAXEVENT 1024
typedef std::string YamlScalar;
typedef std::map<YamlScalar, YamlScalar> YamlMapping;
typedef std::list<YamlMapping> YamlSequence;
typedef std::map<YamlScalar, YamlSequence> YamlAccount;
typedef std::list<YamlAccount> AccountList;
class YamlParserException : public std::exception
{
public:
......@@ -71,7 +63,7 @@ class YamlParser {
public:
YamlParser();
YamlParser(const char *file);
~YamlParser();
......@@ -79,12 +71,10 @@ class YamlParser {
void close();
void parse();
void serializeEvents();
void composeEvents();
int composeAccount(int i);
private:
/**
......@@ -92,6 +82,8 @@ class YamlParser {
*/
int copyEvent(yaml_event_t *event_to, yaml_event_t *event_from);
std::string filename;
FILE *fd;
/**
......@@ -114,8 +106,6 @@ class YamlParser {
*/
int eventNumber;
AccountList accountlist;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment