Skip to content
Snippets Groups Projects
Commit 3fd35272 authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3647] First implementation of the yaml parser

parent 6be50311
No related branches found
No related tags found
No related merge requests found
Showing with 241 additions and 41 deletions
...@@ -6,7 +6,8 @@ libconfig_la_SOURCES = \ ...@@ -6,7 +6,8 @@ libconfig_la_SOURCES = \
config.cpp \ config.cpp \
yamlengine.cpp \ yamlengine.cpp \
yamlemitter.cpp \ yamlemitter.cpp \
yamlparser.cpp yamlparser.cpp \
yamlnode.cpp
noinst_HEADERS = \ noinst_HEADERS = \
config.h \ config.h \
...@@ -14,7 +15,8 @@ noinst_HEADERS = \ ...@@ -14,7 +15,8 @@ noinst_HEADERS = \
serializable.h \ serializable.h \
yamlengine.h \ yamlengine.h \
yamlemitter.h \ yamlemitter.h \
yamlparser.h yamlparser.h \
yamlnode.h
libconfig_la_LDFLAGS = @yaml_LIBS@ libconfig_la_LDFLAGS = @yaml_LIBS@
......
...@@ -49,7 +49,7 @@ ConfigTree::ConfigTree() :_sections() ...@@ -49,7 +49,7 @@ ConfigTree::ConfigTree() :_sections()
YamlParser *parser; YamlParser *parser;
try { try {
parser = new YamlParser(); parser = new YamlParser("sequence.yml");
} }
catch (YamlParserException &e) { catch (YamlParserException &e) {
_error("ConfigTree: %s", e.what()); _error("ConfigTree: %s", e.what());
...@@ -57,7 +57,7 @@ ConfigTree::ConfigTree() :_sections() ...@@ -57,7 +57,7 @@ ConfigTree::ConfigTree() :_sections()
try { try {
parser->parse(); parser->serializeEvents();
} }
catch(YamlParserException &e) { catch(YamlParserException &e) {
_error("ConfigTree: %s", e.what()); _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 @@ ...@@ -32,11 +32,12 @@
#include "../global.h" #include "../global.h"
#include "config.h" #include "config.h"
#include "yamlnode.h"
#include <stdio.h> #include <stdio.h>
namespace Conf { namespace Conf {
YamlParser::YamlParser() YamlParser::YamlParser(const char *file) : filename(file)
{ {
memset(buffer, 0, PARSER_BUFFERSIZE); memset(buffer, 0, PARSER_BUFFERSIZE);
...@@ -51,8 +52,6 @@ YamlParser::~YamlParser() ...@@ -51,8 +52,6 @@ YamlParser::~YamlParser()
void YamlParser::open() void YamlParser::open()
{ {
std::string filename = "sequence.yml";
fd = fopen(filename.c_str(), "rb"); fd = fopen(filename.c_str(), "rb");
if(!fd) if(!fd)
...@@ -77,7 +76,7 @@ void YamlParser::close() ...@@ -77,7 +76,7 @@ void YamlParser::close()
} }
void YamlParser::parse() void YamlParser::serializeEvents()
{ {
bool done = false; bool done = false;
yaml_event_t event; yaml_event_t event;
...@@ -203,12 +202,12 @@ void YamlParser::composeEvents() { ...@@ -203,12 +202,12 @@ void YamlParser::composeEvents() {
break; break;
case YAML_SCALAR_EVENT: { 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); _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); char buffer[1000];
std::string tmp("ok"); snprintf(buffer, 1000, "%s", events[i].data.scalar.value);
size_t found = tmp.find("account"); _debug("----------------------------- THE BUFFER: %s", buffer);
// if this is an account ScalarNode *sclr = new ScalarNode(buffer);
if(found != std::string::npos) _debug("----------------------------- THE VALUE: %s", (sclr->getValue()).c_str());
composeAccount(i); // ScalarNode *sclr = new ScalarNode("ok");
} }
break; break;
case YAML_SEQUENCE_START_EVENT: case YAML_SEQUENCE_START_EVENT:
...@@ -231,18 +230,5 @@ void YamlParser::composeEvents() { ...@@ -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 @@ ...@@ -35,20 +35,12 @@
#include <stdio.h> #include <stdio.h>
#include <exception> #include <exception>
#include <string> #include <string>
#include <map>
#include <list>
namespace Conf { namespace Conf {
#define PARSER_BUFFERSIZE 65536 #define PARSER_BUFFERSIZE 65536
#define PARSER_MAXEVENT 1024 #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 class YamlParserException : public std::exception
{ {
public: public:
...@@ -71,7 +63,7 @@ class YamlParser { ...@@ -71,7 +63,7 @@ class YamlParser {
public: public:
YamlParser(); YamlParser(const char *file);
~YamlParser(); ~YamlParser();
...@@ -79,12 +71,10 @@ class YamlParser { ...@@ -79,12 +71,10 @@ class YamlParser {
void close(); void close();
void parse(); void serializeEvents();
void composeEvents(); void composeEvents();
int composeAccount(int i);
private: private:
/** /**
...@@ -92,6 +82,8 @@ class YamlParser { ...@@ -92,6 +82,8 @@ class YamlParser {
*/ */
int copyEvent(yaml_event_t *event_to, yaml_event_t *event_from); int copyEvent(yaml_event_t *event_to, yaml_event_t *event_from);
std::string filename;
FILE *fd; FILE *fd;
/** /**
...@@ -114,8 +106,6 @@ class YamlParser { ...@@ -114,8 +106,6 @@ class YamlParser {
*/ */
int eventNumber; int eventNumber;
AccountList accountlist;
}; };
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment