diff --git a/sflphone-common/configure.ac b/sflphone-common/configure.ac index e057d6f955e2917e739c89e13500be1efdea8199..01576155701cb5969c42f8b0170f339c6fdd2ef8 100644 --- a/sflphone-common/configure.ac +++ b/sflphone-common/configure.ac @@ -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) diff --git a/sflphone-common/src/Makefile.am b/sflphone-common/src/Makefile.am index 10950a290304ca4bfb9a69a28ae31562e9b453eb..606f9811128236f1fba0d714ec110c2e3ba4387b 100644 --- a/sflphone-common/src/Makefile.am +++ b/sflphone-common/src/Makefile.am @@ -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 = diff --git a/sflphone-common/src/config/Makefile.am b/sflphone-common/src/config/Makefile.am index d3c2bb709a165ed1421515f5fa41659cb2ede24e..483a4ac24654d5760fa784568442d3b6c4072a3b 100644 --- a/sflphone-common/src/config/Makefile.am +++ b/sflphone-common/src/config/Makefile.am @@ -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 diff --git a/sflphone-common/src/config/config.cpp b/sflphone-common/src/config/config.cpp index 83bba767b1e7ff66891c2836485f68ddc7f8f966..6635821993a7bde12b2b95e6498cd00fffb38f46 100644 --- a/sflphone-common/src/config/config.cpp +++ b/sflphone-common/src/config/config.cpp @@ -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(); diff --git a/sflphone-common/src/config/yamlemitter.cpp b/sflphone-common/src/config/yamlemitter.cpp index abb04e1676d765556a785ee3d65b0197c8c8ecb7..1e0f1b481f5a1909ab8d53086d02bf0c24e88653 100644 --- a/sflphone-common/src/config/yamlemitter.cpp +++ b/sflphone-common/src/config/yamlemitter.cpp @@ -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() {} + +} diff --git a/sflphone-common/src/config/yamlemitter.h b/sflphone-common/src/config/yamlemitter.h index 411ebba7e1ab02f1dc49cb3429d5cd914dceb5a9..3e55d102b118ab45252a5668562c5565c05c20de 100644 --- a/sflphone-common/src/config/yamlemitter.h +++ b/sflphone-common/src/config/yamlemitter.h @@ -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 diff --git a/sflphone-common/src/config/yamlengine.cpp b/sflphone-common/src/config/yamlengine.cpp index 9b9f3a8108ae82b92345dbeeb311b52292aded52..2a75afacfaa68b298eecfbe8a3ef84988814fbdc 100644 --- a/sflphone-common/src/config/yamlengine.cpp +++ b/sflphone-common/src/config/yamlengine.cpp @@ -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() {} + +} diff --git a/sflphone-common/src/config/yamlengine.h b/sflphone-common/src/config/yamlengine.h index 665330be88e6911e2977f2b367af4dcaa46542a4..100fc8cc891d318034d5a85c9027674221fefb95 100644 --- a/sflphone-common/src/config/yamlengine.h +++ b/sflphone-common/src/config/yamlengine.h @@ -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 diff --git a/sflphone-common/src/config/yamlparser.cpp b/sflphone-common/src/config/yamlparser.cpp index 0d34db1760a81473dff6ba19b9c6a98aedc2482b..796bfd8a91ced6d30e6b2540edc1b9485bc7ae91 100644 --- a/sflphone-common/src/config/yamlparser.cpp +++ b/sflphone-common/src/config/yamlparser.cpp @@ -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; +} + + +} diff --git a/sflphone-common/src/config/yamlparser.h b/sflphone-common/src/config/yamlparser.h index 510cf00f8bdccc18d9e480b33dae0ccb5d67d02c..1f0dfc05b6fbbd9c587b20880995135c1b42b3f0 100644 --- a/sflphone-common/src/config/yamlparser.h +++ b/sflphone-common/src/config/yamlparser.h @@ -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 diff --git a/sflphone-common/test/configurationtest.cpp b/sflphone-common/test/configurationtest.cpp index 23294f67ccb7b2316212cf823f709768b5db0f8a..f42ea1be8342396de0a9dc2c4b6717ca7c2224df 100644 --- a/sflphone-common/test/configurationtest.cpp +++ b/sflphone-common/test/configurationtest.cpp @@ -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; + +} diff --git a/sflphone-common/test/configurationtest.h b/sflphone-common/test/configurationtest.h index 1e2f572aa55ecd36324eb1672f0954a6f7badb94..1688819ea6231dc5f4dfc27d0042d390d935f66b 100644 --- a/sflphone-common/test/configurationtest.h +++ b/sflphone-common/test/configurationtest.h @@ -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");