commit 23565032b03cc6638890b4abf33305f9d255668f
parent e7c982bf3cfafe8adacd8649ec57ae04665d1b96
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 15 Dec 2016 11:54:57 +0100
Begin the parsing of the solstice arguments
Diffstat:
4 files changed, 150 insertions(+), 31 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -34,6 +34,11 @@ find_package(SolSolver 0.1 REQUIRED)
find_package(Star3DUT REQUIRED)
find_package(StarSP 0.4 REQUIRED)
+if(MSVC)
+ find_package(MuslGetopt REQUIRED)
+ include_directories(${MuslGetopt_INCLUDE_DIR})
+endif()
+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
include(rcmake)
include(rcmake_runtime)
@@ -61,11 +66,13 @@ set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
set(SOLSTICE_FILES_SRC
solstice.c
+ solstice_args.c
solstice_entity.c
solstice_material.c
solstice_object.c)
set(SOLSTICE_FILES_INC
solstice.h
+ solstice_args.h
solstice_c.h)
set(SOLSTICE_FILES_DOC COPYING README.md)
diff --git a/src/main.c b/src/main.c
@@ -13,48 +13,21 @@
* 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 "parser/solparser.h"
+#include "solstice_args.h"
#include <rsys/rsys.h>
int
main(int argc, char** argv)
{
- FILE* file = NULL;
- struct solparser* parser = NULL;
+ struct solstice_args args;
res_T res;
int err = 0;
- int i;
- if(argc < 2) {
- fprintf(stderr, "Usage: %s FILE [FILE ...]\n", argv[0]);
- err = 1;
- goto error;
- }
-
- res = solparser_create(NULL, &parser);
+ res = solstice_args_init(&args, argc, argv);
if(res != RES_OK) goto error;
- FOR_EACH(i, 1, argc) {
- file = fopen(argv[i], "rb");
- if(!file) {
- fprintf(stderr, "Could not open the file `%s'.\n", argv[i]);
- goto error;
- }
-
- res = solparser_setup(parser, argv[i], file);
- if(res != RES_OK) break;
-
- do {
- res = solparser_load(parser);
- } while(res != RES_BAD_OP);
-
- fclose(file);
- file = NULL;
- }
-
exit:
- if(parser) solparser_ref_put(parser);
- if(file) fclose(file);
+ solstice_args_release(&args);
return err;
error:
err = -1;
diff --git a/src/solstice_args.c b/src/solstice_args.c
@@ -0,0 +1,98 @@
+/* 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/>. */
+
+#define _POSIX_C_SOURCE 2
+
+#include "solstice_args.h"
+
+#include <rsys/cstr.h>
+
+#ifdef COMPILER_CL
+ #include <getopt.h>
+#else
+ #include <unistd.h>
+#endif
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+print_help(const char* program)
+{
+ printf(
+"Usage: %s [OPTIONS] [FILE]\n"
+"Integrate the solar flux in complex solar facilities.\n",
+ program);
+ /* TODO print short help for the options */
+}
+
+static res_T
+parse_rendering(const char* str, struct solstice_args* args)
+{
+ ASSERT(args);
+ (void)str, (void)args;
+ /* TODO */
+ return RES_OK;
+}
+
+/*******************************************************************************
+ * Local function
+ ******************************************************************************/
+res_T
+solstice_args_init(struct solstice_args* args, const int argc, char** argv)
+{
+ int opt;
+ res_T res = RES_OK;
+ ASSERT(args && argc && argv);
+
+ *args = SOLSTICE_ARGS_NULL;
+
+ while((opt = getopt(argc, argv, "hn:o:qr:")) != -1) {
+ switch(opt) {
+ case 'h':
+ print_help(argv[0]);
+ solstice_args_release(args);
+ goto exit;
+ case 'n':
+ res = cstr_to_ulong(optarg, &args->nrealisations);
+ if(res != RES_OK && !args->nrealisations) res = RES_BAD_ARG;
+ break;
+ case 'o': args->output_filename = optarg; break;
+ case 'q': args->quiet = 1; break;
+ case 'r': res = parse_rendering(optarg, args); break;
+ default: res = RES_BAD_ARG; break;
+ }
+ if(res != RES_OK) {
+ if(optarg) {
+ fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n",
+ argv[0], optarg, opt);
+ }
+ goto error;
+ }
+ }
+exit:
+ return res;
+error:
+ solstice_args_release(args);
+ goto exit;
+}
+
+void
+solstice_args_release(struct solstice_args* args)
+{
+ ASSERT(args);
+ *args = SOLSTICE_ARGS_NULL;
+}
+
diff --git a/src/solstice_args.h b/src/solstice_args.h
@@ -0,0 +1,41 @@
+/* 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_ARGS_H
+#define SOLSTICE_ARGS_H
+
+#include <rsys/rsys.h>
+
+struct solstice_args {
+ const char* output_filename;
+ unsigned long nrealisations; /* #realisations */
+ int quiet;
+};
+
+#define SOLSTICE_ARGS_NULL__ {NULL, 0, 0}
+static const struct solstice_args SOLSTICE_ARGS_NULL = SOLSTICE_ARGS_NULL__;
+
+extern LOCAL_SYM res_T
+solstice_args_init
+ (struct solstice_args* args,
+ const int argc,
+ char** argv);
+
+extern LOCAL_SYM void
+solstice_args_release
+ (struct solstice_args* args);
+
+#endif /* SOLSTICE_ARGS_H */
+