From a16ecfd938c6c9cafcf6d6bd486dac7a6b869614 Mon Sep 17 00:00:00 2001 From: Mathieu Faverge <mathieu.faverge@inria.fr> Date: Thu, 6 Jul 2023 11:24:42 +0200 Subject: [PATCH] header: Move the environment variable management function to the main include dir to allow their use in the testings --- control/auxiliary.h | 83 ------------------------- control/common.h | 1 + include/chameleon/getenv.h | 123 +++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 83 deletions(-) create mode 100644 include/chameleon/getenv.h diff --git a/control/auxiliary.h b/control/auxiliary.h index f4246973b..b7502ac70 100644 --- a/control/auxiliary.h +++ b/control/auxiliary.h @@ -35,89 +35,6 @@ extern "C" { #endif -/* - * Get environment variable - */ -#if defined(CHAMELEON_OS_WINDOWS) - -static inline int -chameleon_setenv( const char *var, const char *value, int overwrite ) { - return !(SetEnvironmentVariable( var, value )); -} - -static inline char * -chameleon_getenv( const char *var ) { - char *str; - int len = 512; - int rc; - str = (char*)malloc(len * sizeof(char)); - rc = GetEnvironmentVariable(var, str, len); - if (rc == 0) { - free(str); - str = NULL; - } - return str; -} - -static inline void -chameleon_cleanenv( char *str ) { - if (str != NULL) free(str); -} - -#else /* Other OS systems */ - -static inline int -chameleon_setenv( const char *var, const char *value, int overwrite ) { - return setenv( var, value, overwrite ); -} - -static inline char * -chameleon_getenv( const char *var ) { - return getenv( var ); -} - -static inline void -chameleon_cleanenv( char *str ) { - (void)str; -} - -#endif - - -static inline int -chameleon_env_is_set_to(char * str, char * value) { - char * val; - if ( (val = chameleon_getenv(str)) && - !strcmp(val, value)) - return 1; - return 0; -} - -static inline int -chameleon_env_on_off( char * str, int default_value ) { - if ( chameleon_env_is_set_to(str, "1") ) { - return CHAMELEON_TRUE; - } - if ( chameleon_env_is_set_to(str, "0") ) { - return CHAMELEON_FALSE; - } - return default_value; -} - -static inline int -chameleon_getenv_get_value_int(char * string, int default_value) { - long int ret; - char *str = chameleon_getenv(string); - if (str == NULL) return default_value; - - if ( sscanf( str, "%ld", &ret ) != 1 ) { - perror("sscanf"); - return default_value; - } - - return (int)ret; -} - /** * Internal routines */ diff --git a/control/common.h b/control/common.h index 0739e878c..4c4d4acb5 100644 --- a/control/common.h +++ b/control/common.h @@ -72,6 +72,7 @@ #include "control/context.h" #include "control/descriptor.h" #include "control/async.h" +#include "chameleon/getenv.h" /** * Global shortcuts diff --git a/include/chameleon/getenv.h b/include/chameleon/getenv.h new file mode 100644 index 000000000..315106744 --- /dev/null +++ b/include/chameleon/getenv.h @@ -0,0 +1,123 @@ +/** + * + * @file getenv.h + * + * @copyright 2009-2014 The University of Tennessee and The University of + * Tennessee Research Foundation. All rights reserved. + * @copyright 2012-2023 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, + * Univ. Bordeaux. All rights reserved. + * + *** + * + * @brief Chameleon auxiliary file to manage environment variables. Must be included after chameleon.h. + * + * @version 1.2.0 + * @author Jakub Kurzak + * @author Piotr Luszczek + * @author Emmanuel Agullo + * @author Cedric Castagnede + * @author Florent Pruvost + * @author Mathieu Faverge + * @author Loris Lucido + * @date 2022-02-22 + * + */ +#ifndef _chameleon_getenv_h_ +#define _chameleon_getenv_h_ + +#include <string.h> +#include <stdlib.h> +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Get environment variable + */ +#if defined(CHAMELEON_OS_WINDOWS) + +static inline int +chameleon_setenv( const char *var, const char *value, int overwrite ) { + return !(SetEnvironmentVariable( var, value )); +} + +static inline char * +chameleon_getenv( const char *var ) { + char *str; + int len = 512; + int rc; + str = (char*)malloc(len * sizeof(char)); + rc = GetEnvironmentVariable(var, str, len); + if (rc == 0) { + free(str); + str = NULL; + } + return str; +} + +static inline void +chameleon_cleanenv( char *str ) { + if (str != NULL) free(str); +} + +#else /* Other OS systems */ + +static inline int +chameleon_setenv( const char *var, const char *value, int overwrite ) { + return setenv( var, value, overwrite ); +} + +static inline char * +chameleon_getenv( const char *var ) { + return getenv( var ); +} + +static inline void +chameleon_cleanenv( char *str ) { + (void)str; +} + +#endif + + +static inline int +chameleon_env_is_set_to(char * str, char * value) { + char * val; + if ( (val = chameleon_getenv(str)) && + !strcmp(val, value)) + return 1; + return 0; +} + +static inline int +chameleon_env_on_off( char * str, int default_value ) { + if ( chameleon_env_is_set_to(str, "1") ) { + return CHAMELEON_TRUE; + } + if ( chameleon_env_is_set_to(str, "0") ) { + return CHAMELEON_FALSE; + } + return default_value; +} + +static inline int +chameleon_getenv_get_value_int(char * string, int default_value) { + long int ret; + char *str = chameleon_getenv(string); + if (str == NULL) return default_value; + + if ( sscanf( str, "%ld", &ret ) != 1 ) { + perror("sscanf"); + return default_value; + } + + return (int)ret; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _chameleon_getenv_h_ */ -- GitLab