commit 761657c964ae57e8a638c7ea528b900d7840b33a
parent 4e0ed02e96e71023f0d540bfda9a107d076cd253
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 3 Nov 2016 11:50:01 +0100
Begin the implementation of the input fileformat parser
Diffstat:
5 files changed, 358 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -0,0 +1,83 @@
+# Copyright (C) CNRS 2016
+#
+# 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, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 2.8)
+project(solstice C CXX)
+enable_testing()
+
+set(SOLSTICE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
+option(NO_TEST "Do not build tests" OFF)
+
+################################################################################
+# Check dependencies
+################################################################################
+get_filename_component(_current_source_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
+set(LibYAML_DIR ${_current_source_dir}/)
+
+find_package(LibYAML REQUIRED)
+find_package(RCMake 0.2 REQUIRED)
+find_package(RCMake 0.2.3 REQUIRED)
+find_package(RSys 0.3 REQUIRED)
+find_package(StarSP 0.4 REQUIRED)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
+include(rcmake)
+include(rcmake_runtime)
+
+include_directories(
+ ${LibYAML_INCLUDE_DIR}
+ ${RSys_INCLUDE_DIR}
+ ${StarSP_INCLUDE_DIR})
+
+################################################################################
+# Configure and define targets
+################################################################################
+set(VERSION_MAJOR 0)
+set(VERSION_MINOR 1)
+set(VERSION_PATCH 0)
+set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
+
+set(SOLSTICE_FILES_SRC
+ solstice.c
+ solstice_facility.c)
+set(SOLSTICE_FILES_INC solstice_facility.h)
+set(SOLSTICE_FILES_DOC COPYING README.md)
+
+# Prepend each file in the `SOLSTICE_FILES_<SRC|INC>' list by `SOLSTICE_SOURCE_DIR'
+rcmake_prepend_path(SOLSTICE_FILES_SRC ${SOLSTICE_SOURCE_DIR})
+rcmake_prepend_path(SOLSTICE_FILES_INC ${SOLSTICE_SOURCE_DIR})
+rcmake_prepend_path(SOLSTICE_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
+
+add_executable(solstice ${SOLSTICE_FILES_SRC} ${SOLSTICE_FILES_INC})
+target_link_libraries(solstice LibYAML RSys StarSP)
+set_target_properties(solstice PROPERTIES
+ VERSION ${VERSION}
+ SOVERSION ${VERSION_MAJOR})
+rcmake_copy_runtime_libraries(solstice)
+
+################################################################################
+# Tests
+################################################################################
+
+################################################################################
+# Define output & install directories
+################################################################################
+install(TARGETS solstice
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin)
+install(FILES ${SOLSTICE_FILES_INC_API} DESTINATION include/solstice)
+install(FILES ${SOLSTICE_FILES_DOC} DESTINATION share/doc/solstice)
+
diff --git a/cmake/LibYAMLConfig.cmake b/cmake/LibYAMLConfig.cmake
@@ -0,0 +1,44 @@
+# Copyright (C) 2015-2016 CNRS
+#
+# 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, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 2.8)
+
+# Try to find the LibYAML devel. Once done this will define:
+# - LibYAML_FOUND: system has LibYAML
+# - LibYAML_INCLUDE_DIR: the include directory
+# - LibYAML: Link this to use LibYAML
+
+find_path(LibYAML_INCLUDE_DIR yaml.h)
+unset(LibYAML_LIBRARY CACHE)
+unset(LibYAML_LIBRARY_DEBUG CACHE)
+unset(LibYAML_LIBRARY_RELWITHDEBINFO CACHE)
+unset(LibYAML_LIBRARY_MINSIZEREL CACHE)
+find_library(LibYAML_LIBRARY yaml DOC "Path to library yaml.")
+
+# Create the imported library target
+if(CMAKE_HOST_WIN32)
+ set(_property IMPORTED_IMPLIB)
+else(CMAKE_HOST_WIN32)
+ set(_property IMPORTED_LOCATION)
+endif(CMAKE_HOST_WIN32)
+add_library(LibYAML SHARED IMPORTED)
+set_target_properties(LibYAML PROPERTIES ${_property} ${LibYAML_LIBRARY})
+
+# Check the package
+include(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibYAML DEFAULT_MSG
+ LibYAML_INCLUDE_DIR
+ LibYAML_LIBRARY)
+
diff --git a/src/solstice.c b/src/solstice.c
@@ -0,0 +1,44 @@
+/* Copyright (C) CNRS 2016
+ *
+ * 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, see <http://www.gnu.org/licenses/>. */
+
+#include "solstice_facility.h"
+#include <rsys/rsys.h>
+
+int
+main(int argc, char** argv)
+{
+ res_T res;
+ int err;
+ int i;
+
+ if(argc < 2) {
+ fprintf(stderr, "Usage: %s FILE [FILE ...]\n", argv[0]);
+ err = 1;
+ goto error;
+ }
+
+ FOR_EACH(i, 1, argc) {
+ res = solstice_facility_load(argv[i]);
+ if(res != RES_OK) {
+ err = 1;
+ goto error;
+ }
+ }
+
+exit:
+ return err;
+error:
+ goto exit;
+}
diff --git a/src/solstice_facility.c b/src/solstice_facility.c
@@ -0,0 +1,161 @@
+/* Copyright (C) CNRS 2016
+ *
+ * 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, see <http://www.gnu.org/licenses/>. */
+
+#include "solstice_facility.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <yaml.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static INLINE void
+log_err
+ (const char* filename,
+ const yaml_node_t* node,
+ const char* fmt,
+ ...)
+{
+ va_list vargs_list;
+ ASSERT(node && fmt);
+
+ fprintf(stderr, "%s:%lu:%lu: ",
+ filename,
+ (unsigned long)node->start_mark.line+1,
+ (unsigned long)node->start_mark.column+1);
+ va_start(vargs_list, fmt);
+ vfprintf(stderr, fmt, vargs_list);
+ va_end(vargs_list);
+}
+
+static res_T
+parse_item
+ (const char* filename,
+ yaml_document_t* doc,
+ const yaml_node_t* item)
+{
+ yaml_node_t* key;
+ yaml_node_t* val;
+ intptr_t nattrs;
+ res_T res = RES_OK;
+ ASSERT(item && item->type == YAML_MAPPING_NODE);
+
+ nattrs = item->data.mapping.pairs.top - item->data.mapping.pairs.start;
+ if(nattrs != 1) {
+ log_err(filename, item,
+ "expecting only one `key : value' pair while %lu are submitted.\n",
+ (unsigned long)nattrs);
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ key = yaml_document_get_node(doc, item->data.mapping.pairs.start[0].key);
+ val = yaml_document_get_node(doc, item->data.mapping.pairs.start[0].value);
+ if(key->type != YAML_SCALAR_NODE) {
+ log_err(filename, key, "expecting a scalar YAML value.\n");
+ res = RES_BAD_ARG;
+ goto error;
+ }
+ (void)val;
+
+ if(!strcmp((char*)key->data.scalar.value, "material")) { /* TODO */
+ } else if(!strcmp((char*)key->data.scalar.value, "node")) { /* TODO */
+ } else if(!strcmp((char*)key->data.scalar.value, "object")) { /* TODO */
+ } else if(!strcmp((char*)key->data.scalar.value, "pivot")) { /* TODO */
+ } else if(!strcmp((char*)key->data.scalar.value, "spawn")) { /* TODO */
+ } else {
+ log_err(filename, key, "unknown item `%s'.\n", key->data.scalar.value);
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+solstice_facility_load(const char* filename)
+{
+ yaml_parser_t parser;
+ yaml_document_t doc;
+ yaml_node_t* root;
+ FILE* file = NULL;
+ size_t i, n;
+ int doc_is_init;
+ res_T res = RES_OK;
+
+ if(!yaml_parser_initialize(&parser)) {
+ fprintf(stderr, "Could not initialise the YAML parser.\n");
+ return RES_UNKNOWN_ERR;
+ }
+
+ file = fopen(filename, "rb");
+ if(!file) {
+ fprintf(stderr, "Could not open the YAML file `%s'.\n", filename);
+ res = RES_IO_ERR;
+ goto error;
+ }
+
+ yaml_parser_set_input_file(&parser, file);
+
+ if(!yaml_parser_load(&parser, &doc)) {
+ fprintf(stderr, "%s:%lu:%lu: %s.\n",
+ filename,
+ (unsigned long)parser.problem_mark.line+1,
+ (unsigned long)parser.problem_mark.column+1,
+ parser.problem);
+ res = RES_IO_ERR;
+ goto error;
+ }
+ doc_is_init = 1;
+
+ root = yaml_document_get_root_node(&doc);
+ if(root->type != YAML_SEQUENCE_NODE) {
+ log_err(filename, root, "expecting a YAML sequence.\n");
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ n = (size_t)(root->data.sequence.items.top - root->data.sequence.items.start);
+ FOR_EACH(i, 0, n) {
+ yaml_node_t* item;
+
+ item = yaml_document_get_node(&doc, root->data.sequence.items.start[i]);
+ if(item->type != YAML_MAPPING_NODE) {
+ log_err(filename, item, "expecting a YAML mapping.\n");
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ res = parse_item(filename, &doc, item);
+ if(res != RES_OK) goto error;
+ }
+
+exit:
+ yaml_parser_delete(&parser);
+ if(doc_is_init) yaml_document_delete(&doc);
+ if(file) fclose(file);
+ return res;
+error:
+ goto exit;
+}
+
diff --git a/src/solstice_facility.h b/src/solstice_facility.h
@@ -0,0 +1,26 @@
+/* Copyright (C) CNRS 2016
+ *
+ * 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, see <http://www.gnu.org/licenses/>. */
+
+#ifndef SOLSTICE_FACILITY_H
+#define SOLSTICE_FACILITY_H
+
+#include <rsys/rsys.h>
+
+extern LOCAL_SYM res_T
+solstice_facility_load
+ (const char* filename);
+
+#endif /* SOLSTICE_FACILITY_H */
+