Commit 6be50311 authored by Alexandre Savard's avatar Alexandre Savard

[#2524] Implemented initial composer from serialization

parent a8da4e1b
......@@ -278,7 +278,7 @@ AC_MSG_ERROR([You need the eXpat xml parser]
fi
yaml_CFLAGS=
yaml_LIBS=lyaml
yaml_LIBS=-lyaml
AC_SUBST(yaml_CFLAGS)
AC_SUBST(yaml_LIBS)
......
......@@ -82,7 +82,8 @@ libsflphone_la_LDFLAGS = \
@PULSEAUDIO_LIBS@ \
@SAMPLERATE_LIBS@ \
@libssl_LIBS@ \
@UUID_LIBS@
@UUID_LIBS@ \
@yaml_LIBS@
libsflphone_la_CFLAGS = \
@CCGNU2_CFLAGS@ \
......@@ -94,7 +95,8 @@ libsflphone_la_CFLAGS = \
@PULSEAUDIO_CFLAGS@ \
@SAMPLERATE_CFLAGS@ \
@libssl_CFLAGS@ \
@UUID_CFLAGS@
@UUID_CFLAGS@ \
@yaml_CFLAGS@
libsflphone_la_SOURCES =
......
......@@ -16,8 +16,6 @@ noinst_HEADERS = \
yamlemitter.h \
yamlparser.h
libconfig_la_LDFLAGS = \
${yaml_LIBS}
libconfig_la_LDFLAGS = @yaml_LIBS@
libconfig_la_CFLAGS = \
${yaml_CFLAGS}
\ No newline at end of file
libconfig_la_CFLAGS = @yaml_CFLAGS@
\ No newline at end of file
......@@ -38,6 +38,7 @@
#include <errno.h>
#include <iostream>
#include <string.h>
#include "yamlparser.h"
namespace Conf
{
......@@ -45,11 +46,44 @@ namespace Conf
// ctor
ConfigTree::ConfigTree() :_sections()
{
YamlParser *parser;
try {
parser = new YamlParser();
}
catch (YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
parser->parse();
}
catch(YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
parser->composeEvents();
}
catch(YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
delete parser;
parser = NULL;
}
catch (YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
}
// dtor
ConfigTree::~ConfigTree()
{
// erase every new ItemMap (by CreateSection)
SectionMap::iterator iter = _sections.begin();
......
......@@ -30,6 +30,8 @@
#include "yamlemitter.h"
namespace Conf {
YamlEmitter::YamlEmitter() {}
YamlEmitter::~YamlEmitter() {}
......@@ -41,3 +43,5 @@ void YamlEmitter::close() {}
void YamlEmitter::read() {}
void YamlEmitter::write() {}
}
......@@ -31,6 +31,17 @@
#ifndef __YAMLEMITTER_H__
#define __YAMLEMITTER_H__
#include <exception>
namespace Conf {
class YamlEmitterException : public std::exception {
virtual const char *what() const throw() {
return "YamlEmitterException occured";
}
};
class YamlEmitter {
public:
......@@ -52,5 +63,6 @@ class YamlEmitter {
};
}
#endif
......@@ -30,6 +30,8 @@
#include "yamlengine.h"
namespace Conf {
YamlEngine::YamlEngine() {}
YamlEngine::~YamlEngine() {}
......@@ -41,3 +43,5 @@ void YamlEngine::close() {}
void YamlEngine::read() {}
void YamlEngine::write() {}
}
......@@ -34,6 +34,16 @@
#include "engine.h"
#include "yamlparser.h"
#include "yamlemitter.h"
#include <exception>
namespace Conf {
class YamlEngineException : public std::exception {
virtual const char *what() const throw() {
return "YamlEngineException occured";
}
};
class YamlEngine : public Engine {
......@@ -59,4 +69,6 @@ class YamlEngine : public Engine {
};
}
#endif
......@@ -30,7 +30,11 @@
#include "yamlparser.h"
#include <assert.h>
#include "../global.h"
#include "config.h"
#include <stdio.h>
namespace Conf {
YamlParser::YamlParser()
{
......@@ -47,12 +51,15 @@ YamlParser::~YamlParser()
void YamlParser::open()
{
fd = fopen("test.yaml", "rb");
std::string filename = "sequence.yml";
fd = fopen(filename.c_str(), "rb");
if(!fd)
throw 20;
throw YamlParserException("Could not open file descriptor");
if(!yaml_parser_initialize(&parser))
throw 20;
throw YamlParserException("Could not open file descriptor");
yaml_parser_set_input_file(&parser, fd);
}
......@@ -62,8 +69,11 @@ void YamlParser::close()
yaml_parser_delete(&parser);
if(!fd)
throw YamlParserException("File descriptor not valid");
if(!fclose(fd))
throw 20;
throw YamlParserException("Error closing file descriptor");
}
......@@ -75,15 +85,15 @@ void YamlParser::parse()
while(!done) {
if(!yaml_parser_parse(&parser, &event))
throw 20;
throw YamlParserException("Error while parsing");
done = (event.type == YAML_STREAM_END_EVENT);
if(eventNumber > PARSER_MAXEVENT)
throw 20;
throw YamlParserException("Reached maximum of event");
if(!copyEvent(&(events[eventNumber++]), &event))
throw 20;
throw YamlParserException("Error copying event");
}
}
......@@ -91,30 +101,40 @@ void YamlParser::parse()
int YamlParser::copyEvent(yaml_event_t *event_to, yaml_event_t *event_from)
{
switch (event_from->type) {
case YAML_STREAM_START_EVENT:
case YAML_STREAM_START_EVENT: {
// _debug("YAML_STREAM_START_EVENT");
return yaml_stream_start_event_initialize(event_to,
event_from->data.stream_start.encoding);
event_from->data.stream_start.encoding);
}
case YAML_STREAM_END_EVENT:
case YAML_STREAM_END_EVENT: {
// _debug("YAML_STREAM_END_EVENT");
return yaml_stream_end_event_initialize(event_to);
}
case YAML_DOCUMENT_START_EVENT:
case YAML_DOCUMENT_START_EVENT: {
// _debug("YAML_DOCUMENT_START_EVENT");
return yaml_document_start_event_initialize(event_to,
event_from->data.document_start.version_directive,
event_from->data.document_start.tag_directives.start,
event_from->data.document_start.tag_directives.end,
event_from->data.document_start.implicit);
}
case YAML_DOCUMENT_END_EVENT:
case YAML_DOCUMENT_END_EVENT: {
// _debug("YAML_DOCUMENT_END_EVENT");
return yaml_document_end_event_initialize(event_to,
event_from->data.document_end.implicit);
case YAML_ALIAS_EVENT:
}
case YAML_ALIAS_EVENT:{
// _debug("YAML_ALIAS_EVENT");
return yaml_alias_event_initialize(event_to,
event_from->data.alias.anchor);
case YAML_SCALAR_EVENT:
}
case YAML_SCALAR_EVENT: {
// _debug("YAML_SCALAR_EVENT");
return yaml_scalar_event_initialize(event_to,
event_from->data.scalar.anchor,
event_from->data.scalar.tag,
......@@ -123,27 +143,32 @@ int YamlParser::copyEvent(yaml_event_t *event_to, yaml_event_t *event_from)
event_from->data.scalar.plain_implicit,
event_from->data.scalar.quoted_implicit,
event_from->data.scalar.style);
case YAML_SEQUENCE_START_EVENT:
}
case YAML_SEQUENCE_START_EVENT: {
// _debug("YAML_SEQUENCE_START_EVENT");
return yaml_sequence_start_event_initialize(event_to,
event_from->data.sequence_start.anchor,
event_from->data.sequence_start.tag,
event_from->data.sequence_start.implicit,
event_from->data.sequence_start.style);
case YAML_SEQUENCE_END_EVENT:
}
case YAML_SEQUENCE_END_EVENT: {
// _debug("YAML_SEQUENCE_END_EVENT");
return yaml_sequence_end_event_initialize(event_to);
case YAML_MAPPING_START_EVENT:
}
case YAML_MAPPING_START_EVENT: {
// _debug("YAML_MAPPING_START_EVENT");
return yaml_mapping_start_event_initialize(event_to,
event_from->data.mapping_start.anchor,
event_from->data.mapping_start.tag,
event_from->data.mapping_start.implicit,
event_from->data.mapping_start.style);
case YAML_MAPPING_END_EVENT:
}
case YAML_MAPPING_END_EVENT: {
// _debug("YAML_MAPPING_END_EVENT");
return yaml_mapping_end_event_initialize(event_to);
}
default:
assert(1);
......@@ -151,3 +176,73 @@ int YamlParser::copyEvent(yaml_event_t *event_to, yaml_event_t *event_from)
return 0;
}
void YamlParser::composeEvents() {
if(eventNumber == 0)
throw YamlParserException("No event available");
for (int i = 0; i < eventNumber;) {
switch(events[i].type) {
case YAML_STREAM_START_EVENT:
_debug("YAML_STREAM_START_EVENT");
break;
case YAML_STREAM_END_EVENT:
_debug("YAML_STREAM_END_EVENT");
break;
case YAML_DOCUMENT_START_EVENT:
_debug("YAML_DOCUMENT_START_EVENT");
break;
case YAML_DOCUMENT_END_EVENT:
_debug("YAML_DOCUMENT_END_EVENT");
break;
case YAML_ALIAS_EVENT:
_debug("YAML_ALIAS_EVENT");
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);
}
break;
case YAML_SEQUENCE_START_EVENT:
_debug("YAML_SEQUENCE_START_EVENT: anchor %s, tag %s", events[i].data.sequence_start.anchor, events[i].data.sequence_start.tag);
break;
case YAML_SEQUENCE_END_EVENT:
_debug("YAML_SEQUENCE_END_EVENT");
break;
case YAML_MAPPING_START_EVENT:
_debug("YAML_MAPPING_START_EVENT: anchor %s, tag %s", events[i].data.mapping_start.anchor, events[i].data.sequence_start.tag);
break;
case YAML_MAPPING_END_EVENT:
_debug("YAML_MAPPING_END_EVENT");
break;
default:
throw YamlParserException("Unknown Event");
}
i++;
}
}
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;
}
}
......@@ -33,10 +33,40 @@
#include <yaml.h>
#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:
YamlParserException(const std::string& str="") throw() : errstr(str) {}
virtual ~YamlParserException() throw() {}
virtual const char *what() const throw() {
std::string expt("YamlParserException occured: ");
expt.append(errstr);
return expt.c_str();
}
private:
std::string errstr;
};
class YamlParser {
public:
......@@ -51,6 +81,10 @@ class YamlParser {
void parse();
void composeEvents();
int composeAccount(int i);
private:
/**
......@@ -80,6 +114,10 @@ class YamlParser {
*/
int eventNumber;
AccountList accountlist;
};
}
#endif
......@@ -150,3 +150,13 @@ void ConfigurationTest::testInitAudioDriver() {
else
CPPUNIT_FAIL ("Wrong audio layer type");
}
void ConfigurationTest::testYamlParser()
{
YamlParser *parser = new YamlParser();
delete parser;
parser = NULL;
}
......@@ -50,6 +50,7 @@
#include "audio/audiolayer.h"
#include "global.h"
#include "user_cfg.h"
#include "config/yamlparser.h"
class ConfigurationTest: public CppUnit::TestFixture {
......@@ -62,6 +63,7 @@ CPPUNIT_TEST_SUITE( ConfigurationTest );
CPPUNIT_TEST( testDefaultValuePreferences );
CPPUNIT_TEST( testDefaultValueSignalisation );
CPPUNIT_TEST( testInitAudioDriver );
CPPUNIT_TEST( testYamlParser );
CPPUNIT_TEST_SUITE_END();
public:
......@@ -91,6 +93,8 @@ public:
void testInitVolume();
void testInitAudioDriver();
void testYamlParser();
};
/* Register our test module */
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConfigurationTest, "ConfigurationTest");
......
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