From 3fd35272e2c2039150f4ae7fc7334913b1bf3044 Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Fri, 2 Jul 2010 10:15:18 -0400
Subject: [PATCH] [#3647] First implementation of the yaml parser

---
 sflphone-common/src/config/Makefile.am     |   6 +-
 sflphone-common/src/config/config.cpp      |   4 +-
 sflphone-common/src/config/config.h        |   0
 sflphone-common/src/config/engine.h        |   0
 sflphone-common/src/config/serializable.h  |   0
 sflphone-common/src/config/yamlemitter.cpp |   0
 sflphone-common/src/config/yamlemitter.h   |   0
 sflphone-common/src/config/yamlengine.cpp  |   0
 sflphone-common/src/config/yamlengine.h    |   0
 sflphone-common/src/config/yamlnode.cpp    |  68 +++++++++
 sflphone-common/src/config/yamlnode.h      | 154 +++++++++++++++++++++
 sflphone-common/src/config/yamlparser.cpp  |  32 ++---
 sflphone-common/src/config/yamlparser.h    |  18 +--
 13 files changed, 241 insertions(+), 41 deletions(-)
 mode change 100644 => 100755 sflphone-common/src/config/Makefile.am
 mode change 100644 => 100755 sflphone-common/src/config/config.cpp
 mode change 100644 => 100755 sflphone-common/src/config/config.h
 mode change 100644 => 100755 sflphone-common/src/config/engine.h
 mode change 100644 => 100755 sflphone-common/src/config/serializable.h
 mode change 100644 => 100755 sflphone-common/src/config/yamlemitter.cpp
 mode change 100644 => 100755 sflphone-common/src/config/yamlemitter.h
 mode change 100644 => 100755 sflphone-common/src/config/yamlengine.cpp
 mode change 100644 => 100755 sflphone-common/src/config/yamlengine.h
 create mode 100755 sflphone-common/src/config/yamlnode.cpp
 create mode 100755 sflphone-common/src/config/yamlnode.h
 mode change 100644 => 100755 sflphone-common/src/config/yamlparser.cpp
 mode change 100644 => 100755 sflphone-common/src/config/yamlparser.h

diff --git a/sflphone-common/src/config/Makefile.am b/sflphone-common/src/config/Makefile.am
old mode 100644
new mode 100755
index 483a4ac246..4d84e41e59
--- a/sflphone-common/src/config/Makefile.am
+++ b/sflphone-common/src/config/Makefile.am
@@ -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@
 
diff --git a/sflphone-common/src/config/config.cpp b/sflphone-common/src/config/config.cpp
old mode 100644
new mode 100755
index 6635821993..74169b7547
--- a/sflphone-common/src/config/config.cpp
+++ b/sflphone-common/src/config/config.cpp
@@ -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());
diff --git a/sflphone-common/src/config/config.h b/sflphone-common/src/config/config.h
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/engine.h b/sflphone-common/src/config/engine.h
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/serializable.h b/sflphone-common/src/config/serializable.h
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/yamlemitter.cpp b/sflphone-common/src/config/yamlemitter.cpp
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/yamlemitter.h b/sflphone-common/src/config/yamlemitter.h
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/yamlengine.cpp b/sflphone-common/src/config/yamlengine.cpp
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/yamlengine.h b/sflphone-common/src/config/yamlengine.h
old mode 100644
new mode 100755
diff --git a/sflphone-common/src/config/yamlnode.cpp b/sflphone-common/src/config/yamlnode.cpp
new file mode 100755
index 0000000000..1daa148ce9
--- /dev/null
+++ b/sflphone-common/src/config/yamlnode.cpp
@@ -0,0 +1,68 @@
+/*
+ *  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);
+}
+
+
+
+}
+
diff --git a/sflphone-common/src/config/yamlnode.h b/sflphone-common/src/config/yamlnode.h
new file mode 100755
index 0000000000..ad8c240bc3
--- /dev/null
+++ b/sflphone-common/src/config/yamlnode.h
@@ -0,0 +1,154 @@
+/*
+ *  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
diff --git a/sflphone-common/src/config/yamlparser.cpp b/sflphone-common/src/config/yamlparser.cpp
old mode 100644
new mode 100755
index 796bfd8a91..a603293708
--- a/sflphone-common/src/config/yamlparser.cpp
+++ b/sflphone-common/src/config/yamlparser.cpp
@@ -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;
-}
-
 
 }
diff --git a/sflphone-common/src/config/yamlparser.h b/sflphone-common/src/config/yamlparser.h
old mode 100644
new mode 100755
index 1f0dfc05b6..73cc66331a
--- a/sflphone-common/src/config/yamlparser.h
+++ b/sflphone-common/src/config/yamlparser.h
@@ -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;
-
 };
 
 }
-- 
GitLab