star-hitran

Load line-by-line data from the HITRAN database
git clone git://git.meso-star.fr/star-hitran.git
Log | Files | Refs | README | LICENSE

commit 40732896b19659f320051f1172d48afa82c1f849
parent 1017e17a3421f01b58bb9fa43ba20656d08a82af
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 17 Dec 2025 11:58:43 +0100

Add the shtr tool

Currently, it loads isotope metadata and/or a set of lines and, once
completed, displays the memory usage of the shtr library.

More broadly, it is designed to verify and benchmark the shtr library.

No reference documentation is provided yet.

Diffstat:
M.gitignore | 1+
MMakefile | 37++++++++++++++++++++++++++++++++++---
Asrc/shtr_main.c | 204+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 239 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -10,3 +10,4 @@ tags test_* !test_*.c +shtr diff --git a/Makefile b/Makefile @@ -26,7 +26,7 @@ LIBNAME_SHARED = libshtr.so LIBNAME = $(LIBNAME_$(LIB_TYPE)) default: library -all: library tests +all: library tests utils ################################################################################ # Library building @@ -76,6 +76,35 @@ libshtr.o: $(OBJ) $(CC) $(CFLAGS_LIB) -c $< -o $@ ################################################################################ +# Utils +################################################################################ +UTIL_SRC = src/shtr_main.c +UTIL_OBJ = $(UTIL_SRC:.c=.o) +UTIL_DEP = $(UTIL_SRC:.c=.d) + +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) + +INCS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys shtr-local) +LIBS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys shtr-local) + +CFLAGS_UTIL = -std=c89 $(CFLAGS_EXE) $(INCS_UTIL) +LDFLAGS_UTIL = $(LDFLAGS_EXE) $(LIBS_UTIL) + +utils: library $(UTIL_DEP) + @$(MAKE) -fMakefile \ + $$(for i in $(UTIL_DEP); do printf -- '-f%s\n' "$${i}"; done) \ + shtr + +shtr: config.mk shtr-local.pc src/shtr_main.o $(LIBNAME) + $(CC) $(CFLAGS_UTIL) -o $@ src/shtr_main.o $(LDFLAGS_UTIL) + +$(UTIL_DEP): config.mk shtr-local.pc + $(CC) $(CFLAGS_UTIL) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + +$(UTIL_OBJ): config.mk shtr-local.pc + $(CC) $(CFLAGS_UTIL) -c $(@:.o=.c) -o $@ + +################################################################################ # Installation ################################################################################ pkg: @@ -92,7 +121,7 @@ shtr-local.pc: shtr.pc.in -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ shtr.pc.in > $@ -install: library pkg +install: library pkg utils install() { mode="$$1"; prefix="$$2"; shift 2; \ mkdir -p "$${prefix}"; \ cp "$$@" "$${prefix}"; \ @@ -103,18 +132,21 @@ install: library pkg if [ "$(LIB_TYPE)" = "STATIC" ]; then mode=644; else mode=755; fi; \ install "$${mode}" "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \ install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" shtr.pc; \ + install 755 "$(DESTDIR)$(BINPREFIX)" shtr; \ install 644 "$(DESTDIR)$(INCPREFIX)/star" src/shtr.h; \ install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-hitran" COPYING README.md uninstall: rm -f "$(DESTDIR)$(LIBPREFIX)/$(LIBNAME)" rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/shtr.pc" + rm -f "$(DESTDIR)$(BINPREFIX)/shtr" rm -f "$(DESTDIR)$(INCPREFIX)/star/shtr.h" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-hitran/COPYING" rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-hitran/README.md" clean: clean_test rm -f $(OBJ) $(DEP) $(LIBNAME) + rm $(UTIL_OBJ) $(UTIL_DEP) shtr rm -f .config libshtr.o shtr.pc shtr-local.pc lint: @@ -130,7 +162,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 shtr-local) LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys shtr-local) diff --git a/src/shtr_main.c b/src/shtr_main.c @@ -0,0 +1,204 @@ +/* Copyright (C) 2022, 2025 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2025 Université de Lorraine + * Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Université Paul Sabatier + * + * 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 /* getopt support */ + +#include "shtr.h" + +#include <rsys/cstr.h> +#include <rsys/mem_allocator.h> +#include <rsys/rsys.h> + +#include <stdio.h> +#include <string.h> +#include <unistd.h> /* getopt */ + +#define STDIN_NAME "-" + +struct args { + char* molparam; + char* lines; + + int verbose; /* Verbosity level */ + int quit; /* Quit the application */ +}; +static const struct args ARGS_DEFAULT = {0}; + +struct cmd { + struct mem_allocator allocator; + int allocator_is_init; + struct args args; + struct shtr* shtr; +}; +static const struct cmd CMD_NULL = {0}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +usage(FILE* stream) +{ + fprintf(stream, "usage: shtr [-hv] [-l lines] [-m molparam]\n"); +} + +static res_T +args_init(struct args* args, int argc, char** argv) +{ + int opt = 0; + res_T res = RES_OK; + + ASSERT(args); + + *args = ARGS_DEFAULT; + + while((opt = getopt(argc, argv, "hl:m:v")) != -1) { + switch(opt) { + case 'h': + usage(stdout); + args->quit = 1; + goto exit; + case 'l': args->lines = optarg; break; + case 'm': args->molparam = optarg; break; + case 'v': args->verbose += (args->verbose < 3); 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: + usage(stderr); + goto exit; +} + +static void +cmd_release(struct cmd* cmd) +{ + if(cmd->shtr) SHTR(ref_put(cmd->shtr)); + if(cmd->allocator_is_init) mem_shutdown_regular_allocator(&cmd->allocator); +} + +static res_T +cmd_init(struct cmd* cmd, const struct args* args) +{ + struct shtr_create_args create_args = SHTR_CREATE_ARGS_DEFAULT; + res_T res = RES_OK; + + ASSERT(cmd && args); + + cmd->args = *args; + + mem_init_regular_allocator(&cmd->allocator); + cmd->allocator_is_init = 1; + + create_args.allocator = &cmd->allocator; + create_args.verbose = args->verbose; + if((res = shtr_create(&create_args, &cmd->shtr)) != RES_OK) goto error; + +exit: + return res; +error: + cmd_release(cmd); + goto exit; +} + +static res_T +load_molparam(const struct cmd* cmd, struct shtr_isotope_metadata** molparam) +{ + ASSERT(cmd && molparam && cmd->args.molparam); + if(!strcmp(cmd->args.molparam, STDIN_NAME)) { + return shtr_isotope_metadata_load_stream(cmd->shtr, stdin, "stdin", molparam); + } else { + return shtr_isotope_metadata_load(cmd->shtr, cmd->args.molparam, molparam); + } +} + +static res_T +load_lines(const struct cmd* cmd, struct shtr_line_list** lines) +{ + ASSERT(cmd && lines && cmd->args.lines); + if(!strcmp(cmd->args.lines, STDIN_NAME)) { + return shtr_line_list_load_stream(cmd->shtr, stdin, "stdin", lines); + } else { + return shtr_line_list_load(cmd->shtr, cmd->args.lines, lines); + } +} + + +static res_T +cmd_run(const struct cmd* cmd) +{ + char buf[128] = {0}; + struct shtr_isotope_metadata* molparam = NULL; + struct shtr_line_list* lines = NULL; + size_t sz = 0; + res_T res = RES_OK; + ASSERT(cmd); + + if(cmd->args.molparam) { + if((res = load_molparam(cmd, &molparam)) != RES_OK) goto error; + } + if(cmd->args.lines) { + if((res = load_lines(cmd, &lines)) != RES_OK) goto error; + } + + sz = MEM_ALLOCATED_SIZE(&cmd->allocator); + + size_to_cstr(sz, SIZE_ALL, NULL, buf, sizeof(buf)); + printf("%s\n", buf); + +exit: + if(molparam) SHTR(isotope_metadata_ref_put(molparam)); + if(lines) SHTR(line_list_ref_put(lines)); + return res; +error: + goto exit; +} + +/******************************************************************************* + * The program + ******************************************************************************/ +int +main(int argc, char** argv) +{ + struct args args = ARGS_DEFAULT; + struct cmd cmd = CMD_NULL; + int err = 0; + res_T res = RES_OK; + + if((res = args_init(&args, argc, argv)) != RES_OK) goto error; + if(args.quit) goto exit; + + if((res = cmd_init(&cmd, &args)) != RES_OK) goto error; + if((res = cmd_run(&cmd)) != RES_OK) goto error; + +exit: + cmd_release(&cmd); + CHK(mem_allocated_size() == 0); + return err; +error: + err = 1; + goto exit; +}