From 257fbcc2556ccb5009539545bd02695bb40095fb Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Wed, 30 Jun 2010 11:39:51 -0400
Subject: [PATCH] [#2524] Implemented first yaml parser

---
 sflphone-common/src/config/engine.h           |  13 +-
 sflphone-common/src/config/yamlemitter.cpp    |  43 +++++
 sflphone-common/src/config/yamlemitter.h      |  56 +++++++
 sflphone-common/src/config/yamlengine.cpp     |  43 +++++
 sflphone-common/src/config/yamlengine.h       |  61 +++++++
 sflphone-common/src/config/yamlparser.cpp     | 153 ++++++++++++++++++
 .../src/config/{engine.cpp => yamlparser.h}   |  75 +++++----
 7 files changed, 397 insertions(+), 47 deletions(-)
 create mode 100644 sflphone-common/src/config/yamlemitter.cpp
 create mode 100644 sflphone-common/src/config/yamlemitter.h
 create mode 100644 sflphone-common/src/config/yamlengine.cpp
 create mode 100644 sflphone-common/src/config/yamlengine.h
 create mode 100644 sflphone-common/src/config/yamlparser.cpp
 rename sflphone-common/src/config/{engine.cpp => yamlparser.h} (64%)

diff --git a/sflphone-common/src/config/engine.h b/sflphone-common/src/config/engine.h
index 014116872c..c2345b656e 100644
--- a/sflphone-common/src/config/engine.h
+++ b/sflphone-common/src/config/engine.h
@@ -37,19 +37,14 @@ class Engine {
 
  public:
 
-  Engine();
+  virtual void open() = 0;
 
-  ~Engine();
+  virtual void close() = 0;
 
-  void open();
+  virtual void write() = 0;
 
-  void write();
+  virtual void read() = 0;
 
- private:
-
-  yaml_emitter_t emitter;
-
-  yaml_event_t event;
 };
 
 #endif
diff --git a/sflphone-common/src/config/yamlemitter.cpp b/sflphone-common/src/config/yamlemitter.cpp
new file mode 100644
index 0000000000..abb04e1676
--- /dev/null
+++ b/sflphone-common/src/config/yamlemitter.cpp
@@ -0,0 +1,43 @@
+/*
+ *  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 "yamlemitter.h"
+
+YamlEmitter::YamlEmitter() {}
+
+YamlEmitter::~YamlEmitter() {}
+
+void YamlEmitter::open() {}
+
+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
new file mode 100644
index 0000000000..411ebba7e1
--- /dev/null
+++ b/sflphone-common/src/config/yamlemitter.h
@@ -0,0 +1,56 @@
+/*
+ *  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 __YAMLEMITTER_H__
+#define __YAMLEMITTER_H__
+
+class YamlEmitter {
+
+ public:
+
+  YamlEmitter();
+
+  ~YamlEmitter();
+
+  void open();
+
+  void close();
+
+  void read();
+
+  void write();
+
+
+ private:
+
+};
+
+
+#endif
diff --git a/sflphone-common/src/config/yamlengine.cpp b/sflphone-common/src/config/yamlengine.cpp
new file mode 100644
index 0000000000..9b9f3a8108
--- /dev/null
+++ b/sflphone-common/src/config/yamlengine.cpp
@@ -0,0 +1,43 @@
+/*
+ *  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 "yamlengine.h"
+
+YamlEngine::YamlEngine() {}
+
+YamlEngine::~YamlEngine() {}
+
+void YamlEngine::open() {}
+
+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
new file mode 100644
index 0000000000..22cb6198d8
--- /dev/null
+++ b/sflphone-common/src/config/yamlengine.h
@@ -0,0 +1,61 @@
+/*
+ *  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 __YAMLENGINE_H__
+#define __YAMLENGINE_H__
+
+#include "yamlparser.h"
+#include "yamlemiter.h"
+
+class YamlEngine : Engine {
+
+ public:
+
+  YamlEngine();
+
+  ~YamlEngine();
+
+  virtual void open();
+
+  virtual void close();
+
+  virtual void write();
+
+  virtual void read();
+
+ private:
+
+  YamlParser *parser;
+
+  YamlEmiter *emiter;
+
+};
+
+#endif
diff --git a/sflphone-common/src/config/yamlparser.cpp b/sflphone-common/src/config/yamlparser.cpp
new file mode 100644
index 0000000000..8a30402343
--- /dev/null
+++ b/sflphone-common/src/config/yamlparser.cpp
@@ -0,0 +1,153 @@
+/*
+ *  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 "yamlparser.h"
+
+#include <assert.h>
+
+YamlParser::YamlParser() 
+{
+  memset(buffer, 0, BUFFER_SIZE);
+
+  open();
+}
+
+YamlParser::~YamlParser() 
+{
+  close();
+}
+
+void YamlParser::open() 
+{
+
+  fd = fopen("test.yaml", "rb");
+  if(!fd)
+    throw 20;
+
+  if(!yaml_parser_initialize(&parser))
+    throw 20;
+
+  yaml_parser_set_input_file(&parser, fd);
+}
+
+void YamlParser::close() 
+{
+
+  yaml_parser_delete(&parser);
+
+  if(!fclose(fd))
+    throw 20;
+
+}
+
+void YamlParser::parse() 
+{
+  bool done;
+  yaml_event_t event;
+
+  while(!done) {
+
+    if(!yaml_parser_parse(&parse, &event))
+      throw 20;
+
+    done = (event.type == YAML_STREAM_END_EVENT);
+    
+    if(eventNumber > PARSER_MAXEVENT)
+      throw 20;
+
+    if(!copyEvent(&(events[eventNumber++]), &event))
+      throw 20;
+
+  }
+}
+
+
+int YamlParser::copyEvent(yaml_event_t *event_to, yaml_event_t *event_from) 
+{
+  switch (event_from->type) {
+  case YAML_STREAM_START_EVENT:
+    return yaml_stream_start_event_initialize(event_to,
+						event_from->data.stream_start.encoding);
+
+  case YAML_STREAM_END_EVENT:
+    return yaml_stream_end_event_initialize(event_to);
+
+  case 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:
+    return yaml_document_end_event_initialize(event_to,
+					      event_from->data.document_end.implicit);
+
+  case YAML_ALIAS_EVENT:
+    return yaml_alias_event_initialize(event_to,
+				       event_from->data.alias.anchor);
+
+  case YAML_SCALAR_EVENT:
+    return yaml_scalar_event_initialize(event_to,
+					event_from->data.scalar.anchor,
+					event_from->data.scalar.tag,
+					event_from->data.scalar.value,
+					event_from->data.scalar.length,
+					event_from->data.scalar.plain_implicit,
+					event_from->data.scalar.quoted_implicit,
+					event_from->data.scalar.style);
+
+  case 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:
+    return yaml_sequence_end_event_initialize(event_to);
+
+  case 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:
+    return yaml_mapping_end_event_initialize(event_to);
+
+  default:
+    assert(1);
+
+  }
+
+  return 0;
+}
diff --git a/sflphone-common/src/config/engine.cpp b/sflphone-common/src/config/yamlparser.h
similarity index 64%
rename from sflphone-common/src/config/engine.cpp
rename to sflphone-common/src/config/yamlparser.h
index 4074552118..b1e8dcef7e 100644
--- a/sflphone-common/src/config/engine.cpp
+++ b/sflphone-common/src/config/yamlparser.h
@@ -28,59 +28,58 @@
  *  as that of the covered work.
  */
 
+#ifndef __YAMLPARSER_H__
+#define __YAMLPARSER_H__
 
-#include "engine.h"
+#include <yaml.h>
+#include <stdio.h>
 
+#define PARSER_BUFFERSIZE 65536
+#define PARSER_MAXEVENT 1024
 
-/* Set a generic writer. */
-void *ext = ...;
-int write_handler(void *ext, char *buffer, int size) {
+class YamlParser {
 
-  int error = 0;
+ public:
 
-  /*
-       ...
-       Write `size` bytes.
-       ...
-  */
-  return error ? 0 : 1;
-}
+  YamlParser();
 
+  ~YamlParsere();
 
-Engine::Engine() 
-{
-  open();
-}
+  void open();
 
-Engine::~Engine() 
-{
-  close();
-}
+  void close();
 
-void Engine::open() 
-{
+  void parse();
 
-  yaml_emitter_initialize(&emitter);
+ private:
 
-  yaml_emitter_set_output(&emitter, write_handler, ext);
+  /**
+   * Copy yaml parser event in event_to according to their type.
+   */
+  int copyEvent(yaml_event_t *event_to, yaml_event_t *event_from);
 
-  yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
-  if (!yaml_emitter_emit(&emitter, &event))
-    return;
+  FILE *fd;
 
-}
+  /**
+   * The parser structure. 
+   */
+  yaml_parser_t parser;
 
-void Engine::close() {
+  /**
+   * The event structure array.
+   */ 
+  yaml_event_t event[PARSER_MAXEVENT];
 
-  /* Create and emit the STREAM-END event. */
-  yaml_stream_end_event_initialize(&event);
-  if (!yaml_emitter_emit(&emitter, &event))
-    return;
+  /**
+   * 
+   */
+  unsigned char buffer[PARSER_BUFFERSIZE];
 
-  /* Destroy the Emitter object. */
-  yaml_emitter_delete(&emitter);
+  /**
+   * Number of event actually parsed
+   */
+  int eventNumber;
 
-}
-
-void Engine::write() {}
+};
 
+#endif
-- 
GitLab