commit ea90342cac097d3cfc513fb733ad9e51a24a0136
parent 7f9d5ba080359fba78bc21641ad301aefda4e685
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Fri, 28 Mar 2025 09:59:15 +0100
Add the sphin utility to verify the input file consistency
Also, add the prototype for the doc
Diffstat:
7 files changed, 239 insertions(+), 6 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -9,6 +9,7 @@ file.txt
*.stl
tags
star-phor-input.5
+sphin
test_sphin
test_sphin_load_geometry
test_sphin_load_source
diff --git a/Makefile b/Makefile
@@ -31,7 +31,7 @@ LIBNAME = libsphin.so
default: library
-all: library tests
+all: library tool tests
################################################################################
# Library building
@@ -75,6 +75,38 @@ $(LIBNAME): $(OBJ)
$(CC) $(CFLAGS_LIB) -c $< -o $@
################################################################################
+# Executable building
+################################################################################
+SRC_TOOL = src/sphin_main.c
+OBJ_TOOL = $(SRC_TOOL:.c=.o)
+DEP_TOOL = $(SRC_TOOL:.c=.d)
+
+PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG)
+INCS_TOOL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sphin-local)
+LIBS_TOOL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sphin-local)
+
+# Compiler and linker options for the executable
+CFLAGS_TOOL = -std=c99 $(CFLAGS_EXE) $(INCS_TOOL)
+LDFLAGS_TOOL = $(LDFLAGS_EXE) $(LIBS_TOOL)
+
+tool: $(DEP_TOOL)
+ @$(MAKE) -fMakefile $$(for i in $(DEP_TOOL); do echo -f $${i}; done) sphin
+
+sphin: .config $(OBJ_TOOL) sphin-local.pc
+ $(CC) $(CFLAGS_EXE) -o $@ $(OBJ_TOOL) $(LDFLAGS_TOOL)
+
+$(DEP_TOOL) $(OBJ_TOOL): .config sphin-local.pc
+
+$(DEP_TOOL):
+ @$(CC) $(CFLAGS_TOOL) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+
+$(OBJ_TOOL):
+ $(CC) $(CFLAG_TOOL) -c $(@:.o=.c) -o $@
+
+clean_tool:
+ rm -f $(DEP_TOOL) $(OBJ_TOOL) sphin
+
+################################################################################
# Installation
################################################################################
pkg:
@@ -84,6 +116,9 @@ pkg:
-e 's#@SSTL_VERSION@#$(SSTL_VERSION)#g'\
sphin.pc.in > sphin.pc
+sphin.1: doc/sphin.scd
+ scdoc < doc/sphin.scd > $@
+
star-phor-input.5: doc/star-phor-input.scd
scdoc < doc/star-phor-input.scd > $@
@@ -96,28 +131,32 @@ sphin-local.pc: sphin.pc.in
-e 's#@SSTL_VERSION@#$(SSTL_VERSION)#g'\
sphin.pc.in > $@
-install: library pkg star-phor-input.5
+install: library tool pkg star-phor-input.5 sphin.1
install() { mode="$$1"; prefix="$$2"; shift 2; \
mkdir -p "$${prefix}"; \
cp "$$@" "$${prefix}"; \
chmod "$${mode}" "$$@"; \
}; \
install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \
+ install 755 "$(DESTDIR)$(BINPREFIX)" sphin; \
install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" sphin.pc; \
install 644 "$(DESTDIR)$(INCPREFIX)/star" src/sphin.h; \
install 644 "$(DESTDIR)$(MANPREFIX)/man5" star-phor-input.5; \
+ install 644 "$(DESTDIR)$(MANPREFIX)/man1" sphin.1; \
install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-phor-input" COPYING; \
install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-phor-input" README.md
uninstall:
rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)"
rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/sphin.pc"
+ rm -f "$(DESTDIR)$(BINPREFIX)/sphin"
rm -f "$(DESTDIR)$(INCPREFIX)/star/sphin.h"
rm -f "$(DESTDIR)$(MANPREFIX)/man5/star-phor-input.5"
+ rm -f "$(DESTDIR)$(MANPREFIX)/man1/sphin.1"
rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-phor-input/COPYING"
rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-phor-input/README.md"
-clean: clean_test
+clean: clean_test clean_tool
rm -f $(DEP) $(OBJ) $(LIBNAME)
rm -f .config sphin-local.pc sphin.pc
@@ -134,7 +173,6 @@ TEST_OBJ = $(TEST_SRC:.c=.o)
TEST_DEP = $(TEST_SRC:.c=.d)
TEST_TGT = $(TEST_SRC:.c=.t)
-PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG)
INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sphin-local)
LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sphin-local) -lm
diff --git a/README.md b/README.md
@@ -23,6 +23,7 @@ providing all the necessary geometries and physical properties for computation.
- pkg-config
- rsys
- star-stl
+- scdoc
## Installation
diff --git a/doc/sphin.scd b/doc/sphin.scd
@@ -0,0 +1,50 @@
+sphin(1) "UNIX"
+
+; Copyright (C) 2024-2025 Centre National de la Recherche Scientifique
+; Copyright (C) 2024-2025 Clermont Auvergne INP
+; Copyright (C) 2024-2025 INSA Lyon
+; Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux
+; Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse
+; Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com)
+; Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com)
+; Copyright (C) 2024-2025 Université de Lorraine
+; Copyright (C) 2024-2025 Université Paul Sabatier
+; Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès
+;
+; 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/>.
+
+# NAME
+
+sphin - numerical consistency utility for photoreactive system description files
+# SYNOPSIS
+
+*sphin* [*-hv*]
+
+# DESCRIPTION
+
+*sphin* is an utility that verifies the numerical consistency of a given
+_star-phor-input_(5) file.
+
+# EXAMPLES
+
+Load a photoreactive system description from a local file:
+
+sphin photoreactive_description_file.txt
+
+Load a photoreactive system description from the standard input:
+
+cat photoreactive_description_file.txt | sphin
+
+# SEE ALSO
+_star-phor-input_(5)
diff --git a/src/sphin_brdf.c b/src/sphin_brdf.c
@@ -21,6 +21,7 @@
*
* 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 200112L /* for strtok_r support */
#include "sphin.h"
@@ -31,7 +32,6 @@
#include <rsys/ref_count.h>
#include <rsys/text_reader.h>
-
struct sphin_brdf {
enum sphin_brdf_type type;
struct sphin_brdf_lambertian lambertian;
diff --git a/src/sphin_config.c b/src/sphin_config.c
@@ -136,7 +136,6 @@ load_stream
ASSERT(NULL != streamname);
ASSERT(NULL != out_config);
-
str_init(sphin->allocator, &line);
res = config_create(sphin, &config);
diff --git a/src/sphin_main.c b/src/sphin_main.c
@@ -0,0 +1,144 @@
+/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024-2025 Clermont Auvergne INP
+ * Copyright (C) 2024-2025 INSA Lyon
+ * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024-2025 Université de Lorraine
+ * Copyright (C) 2024-2025 Université Paul Sabatier
+ * Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès
+ *
+ * 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 200112L /* for getopt support */
+
+#include "sphin.h"
+#include "sphin_config.h"
+
+#include <rsys/cstr.h>
+#include <rsys/mem_allocator.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+struct args {
+ int verbose; /* Verbosity level. Bigger values => more verbosity */
+ int quit;
+ char* filename; /* NULL => use stdin */
+};
+
+#define ARGS_DEFAULT__ {0, 0, NULL}
+static const struct args ARGS_DEFAULT = ARGS_DEFAULT__;
+
+static void
+usage(FILE* stream)
+{
+ ASSERT(NULL != stream);
+ fprintf(stream, "Usage: sphin [-hv] [file]\n");
+}
+
+static res_T
+parse_args(struct args* args, int argc, char** argv)
+{
+ int opt = 0;
+ res_T res = RES_OK;
+
+ ASSERT(NULL != args);
+ ASSERT(NULL != argv);
+ ASSERT(argc > 0);
+
+ *args = ARGS_DEFAULT;
+
+ while((opt = getopt(argc, argv, "hv")) != -1) {
+ switch(opt) {
+ case 'v':
+
+ /* Increase the verbosity level counter for each 'v' entered, up to a maximum of 3.
+ We use the boolean value of (3 > args->verbose): if the condition is
+ true, it evaluates to 1, increasing the counter.
+ Otherwise, it evaluates to 0, leaving the counter unchanged. */
+
+ args->verbose += (3 > args->verbose);
+ break;
+ case 'h':
+ usage(stdout);
+ args->quit = 1;
+ break;
+ default:
+ res = RES_BAD_ARG;
+ goto error;
+ }
+ if (RES_OK != res) { goto error; }
+ }
+
+ /* opting is a global variable that indexes the next argv to be processed */
+ if (optind < argc) { /* True if not all the args have been processed */
+ args->filename = argv[optind];
+ }
+
+exit:
+ return res;
+error:
+ usage(stderr);
+ goto exit;
+}
+
+int
+main(int argc, char** argv)
+{
+ struct sphin* sphin = NULL;
+ struct sphin_config* config = NULL;
+ struct args args = ARGS_DEFAULT; /* Command line args */
+ struct sphin_create_args sphin_args = SPHIN_CREATE_ARGS_DEFAULT;
+ size_t sz = 0;
+ int err = EXIT_SUCCESS;
+ res_T res = RES_OK;
+
+ /* Process command line args */
+ res = parse_args(&args, argc, argv);
+ if (RES_OK != res) { goto error; }
+
+ if (1 == args.quit) { goto exit; }
+
+ /* Initialize sphin */
+ sphin_args.verbose = args.verbose;
+ sphin_create(&sphin_args, &sphin);
+
+ /* Load stream */
+ if (NULL != args.filename) {
+ sphin_load(sphin, args.filename, &config);
+ }
+ else {
+ sphin_load_stream(sphin, stdin, "stdin", &config);
+ }
+
+exit:
+ if (NULL != sphin) { SPHIN(ref_put(sphin)); }
+ if (NULL != config) { SPHIN(config_ref_put(config)); }
+ sz = mem_allocated_size();
+ if (sz != 0) {
+ char buffer[64] = {0}; /* Initialize all fields to 0 */
+ size_to_cstr(sz, SIZE_ALL, NULL, buffer, sizeof(buffer));
+ fprintf(stderr, "Memory leaks: %s\n", buffer);
+ err = EXIT_FAILURE;
+ }
+
+ return err;
+
+error:
+ err = EXIT_FAILURE;
+ goto exit;
+}