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 93ea6c1b77accf850264b88fcba2140d9b22c9ef
parent a89459a28310cff8c122edf3aefc68b8879a4a6d
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri,  4 Feb 2022 12:08:00 +0100

Begin the library implementation

Write the CMakeLists and implement the device API

Diffstat:
Acmake/CMakeLists.txt | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran.h | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran_c.h | 34++++++++++++++++++++++++++++++++++
Asrc/shitran_log.c | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran_log.h | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 505 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,99 @@ +# Copyright (C) 2022 CNRS - LMD +# Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) +# Copyright (C) 2022 Université Paul Sabatier - IRIT/Laplace +# +# 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 3.1) +project(shitran C) +enable_testing() + +set(SHITRAN_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +option(NO_TEST "Do not build tests" OFF) + +################################################################################ +# Check dependencies +################################################################################ +find_package(RCMake 0.4 REQUIRED) +find_package(RSys 0.10 REQUIRED) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) +include(rcmake) +include(rcmake_runtime) + +include_directories(${RSys_INCLUDE_DIR}) + +################################################################################ +# Configure and define targets +################################################################################ +set(VERSION_MAJOR 0) +set(VERSION_MINOR 0) +set(VERSION_PATCH 0) +set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) + +set(SHITRAN_FILES_SRC + shitran.c + shitran_log.c) +set(SHITRAN_FILES_INC + shitran_c.h + shitran_log.h) +set(SHITRAN_FILES_INC_API + shitran.h) + +set(SHITRAN_FILES_DOC COPYING README.md) + +# Prepend each file in the `SHITRAN_FILES_<SRC|INC>' list by `SHITRAN_SOURCE_DIR' +rcmake_prepend_path(SHITRAN_FILES_SRC ${SHITRAN_SOURCE_DIR}) +rcmake_prepend_path(SHITRAN_FILES_INC ${SHITRAN_SOURCE_DIR}) +rcmake_prepend_path(SHITRAN_FILES_INC_API ${SHITRAN_SOURCE_DIR}) +rcmake_prepend_path(SHITRAN_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(shitran SHARED ${SHITRAN_FILES_SRC} ${SHITRAN_FILES_INC} ${SHITRAN_FILES_INC_API}) +target_link_libraries(shitran RSys) + +set_target_properties(shitran PROPERTIES + DEFINE_SYMBOL SHITRAN_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(shitran StarHITRAN ${VERSION} star/shitran_version.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + function(build_test _name) + add_executable(${_name} + ${SHITRAN_SOURCE_DIR}/${_name}.c + ${SHITRAN_SOURCE_DIR}/test_shitran_utils.h) + target_link_libraries(${_name} shitran RSys ${ARGN}) + endfunction() + + function(new_test _name) + build_test(${_name} ${ARGN}) + add_test(${_name} ${_name}) + endfunction() + +endif() + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS shitran + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${SHITRAN_FILES_INC_API} DESTINATION include/star) +install(FILES ${SHITRAN_FILES_DOC} DESTINATION share/doc/star-hitran) + diff --git a/src/shitran.c b/src/shitran.c @@ -0,0 +1,99 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT/Laplace + * + * 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 "shitran.h" +#include "shitran_c.h" +#include "shitran_log.h" + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE res_T +check_shitran_create_args(const struct shitran_create_args* args) +{ + return args ? RES_OK : RES_BAD_ARG; +} + +static void +release_shitran(ref_T* ref) +{ + struct shitran* shitran = CONTAINER_OF(ref, struct shitran, ref); + if(shitran->logger == &shitran->logger__) logger_release(&shitran->logger__); + MEM_RM(shitran->allocator, shitran); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +shitran_create + (const struct shitran_create_args* args, + struct shitran** out_shitran) +{ + struct mem_allocator* allocator = NULL; + struct shitran* shitran = NULL; + res_T res = RES_OK; + + if(out_shitran) { res = RES_BAD_ARG; goto error; } + res = check_shitran_create_args(args); + if(res != RES_OK) goto error; + + allocator = args->allocator ? args->allocator : &mem_default_allocator; + shitran = MEM_CALLOC(allocator, 1, sizeof(*shitran)); + if(!shitran) { + #define ERR_STR "Could not allocate the Star-HITRAN data structure.\n" + if(args->logger) { + logger_print(args->logger, LOG_ERROR, ERR_STR); + } else { + fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); + } + #undef ERR_STR + res = RES_MEM_ERR; + goto error; + } + ref_init(&shitran->ref); + shitran->allocator = allocator; + shitran->verbose = args->verbose; + if(args->logger) { + shitran->logger = args->logger; + } else { + setup_log_default(shitran); + } + +exit: + if(out_shitran) *out_shitran = shitran; + return res; +error: + if(shitran) { SHITRAN(ref_put(shitran)); shitran = NULL; } + goto exit; +} + +res_T +shitran_ref_get(struct shitran* shitran) +{ + if(shitran) return RES_BAD_ARG; + ref_get(&shitran->ref); + return RES_OK; +} + +res_T +shitran_ref_put(struct shitran* shitran) +{ + if(shitran) return RES_BAD_ARG; + ref_put(&shitran->ref, release_shitran); + return RES_OK; +} diff --git a/src/shitran.h b/src/shitran.h @@ -0,0 +1,75 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT/Laplace + * + * 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 SHITRAN_H +#define SHITRAN_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(SHITRAN_SHARED_BUILD) /* Build shared library */ + #define SHITRAN_API extern EXPORT_SYM +#elif defined(SHITRAN_STATIC) /* Use/build static library */ + #define SHITRAN_API extern LOCAL_SYM +#else + #define SHITRAN_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the sth function `Func' + * returns an error. One should use this macro on sth function calls for + * which no explicit error checking is performed */ +#ifndef NDEBUG + #define SHITRAN(Func) ASSERT(shitran_ ## Func == RES_OK) +#else + #define SHITRAN(Func) shitran_ ## Func +#endif + +struct shitran_create_args { + struct logger* logger; /* May be NULL <=> default logger */ + struct mem_allocator* allocator; /* NULL <=> use default allocator */ + int verbose; /* Verbosity level */ +}; +#define SHITRAN_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0} +static const struct shitran_create_args SHITRAN_CREATE_ARGS_DEFAULT = + SHITRAN_CREATE_ARGS_DEFAULT__; + +/* Forware declarations */ +struct shitran; +struct shitran_isotopologue_metadata; + +BEGIN_DECLS + +/******************************************************************************* + * SHITRAN API + ******************************************************************************/ +SHITRAN_API res_T +shitran_create + (const struct shitran_create_args* args, + struct shitran** shitran); + +SHITRAN_API res_T +shitran_ref_get + (struct shitran* shitran); + +SHITRAN_API res_T +shitran_ref_put + (struct shitran* shitran); + +END_DECLS + +#endif /* SHITRAN_H */ + diff --git a/src/shitran_c.h b/src/shitran_c.h @@ -0,0 +1,34 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT/Laplace + * + * 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 SHITRAN_C_H +#define SHITRAN_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> + +struct mem_allocator; + +struct shitran { + struct mem_allocator* allocator; + struct logger* logger; + struct logger logger__; /* Default logger */ + int verbose; + ref_T ref; +}; + +#endif /* SHITRAN_C_H */ diff --git a/src/shitran_log.c b/src/shitran_log.c @@ -0,0 +1,126 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT/Laplace + * + * 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 "shitran_c.h" +#include "shitran_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct shitran* shitran, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(shitran && msg); + if(shitran->verbose) { + res_T res; (void)res; + res = logger_vprint(shitran->logger, stream, msg, vargs); + ASSERT(res == RES_OK); + } +} + +static void +print_info(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_INFO_PREFIX"%s", msg); +} + +static void +print_err(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); +} + +static void +print_warn(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +setup_log_default(struct shitran* shitran) +{ + res_T res = RES_OK; + ASSERT(shitran); + + res = logger_init(shitran->allocator, &shitran->logger__); + if(res != RES_OK) { + if(shitran->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the default logger for the Star-HITRAN library -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&shitran->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&shitran->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&shitran->logger__, LOG_WARNING, print_warn, NULL); + shitran->logger = &shitran->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct shitran* shitran, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(shitran && msg); + + va_start(vargs_list, msg); + log_msg(shitran, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct shitran* shitran, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(shitran && msg); + + va_start(vargs_list, msg); + log_msg(shitran, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct shitran* shitran, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(shitran && msg); + + va_start(vargs_list, msg); + log_msg(shitran, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/shitran_log.h b/src/shitran_log.h @@ -0,0 +1,72 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT/Laplace + * + * 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 SHITRAN_LOG_H +#define SHITRAN_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "Star-HITRAN:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "Star-HITRAN:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "Star-HITRAN:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct shitran; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct shitran* shitran); + +/* Conditionally log a message on the LOG_OUTPUT stream of the shitran logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct shitran* shitran, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the shitran logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct shitran* shitran, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the shitran logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct shitran* shitran, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + + +#endif /* SHITRAN_LOG_H */ +