From 22db6b235773385520a8dcdefdc30a13bdb6ef22 Mon Sep 17 00:00:00 2001 From: nils-hamel Date: Mon, 5 Sep 2016 09:31:26 +0200 Subject: [PATCH] Revision of libcommon and adaptation of code --- lib/libcommon/src/common-args.c | 101 ++++++++++++++++++++++++-------- lib/libcommon/src/common-args.h | 48 +++++++++++---- 2 files changed, 110 insertions(+), 39 deletions(-) diff --git a/lib/libcommon/src/common-args.c b/lib/libcommon/src/common-args.c index 11cb183..931af54 100644 --- a/lib/libcommon/src/common-args.c +++ b/lib/libcommon/src/common-args.c @@ -26,19 +26,21 @@ int lc_read_flag( int const argc, char ** argv, char const * const er_long, char const * const er_short ) { - /* Parses arguments and parameters */ + /* Parsing arguments and parameters */ for ( int er_parse = 0 ; er_parse < argc; er_parse ++ ) { - /* Check argument */ + /* Check argument short and long forms */ if ( ( strcmp( argv[er_parse], er_long ) == 0 ) || ( strcmp( argv[er_parse], er_short ) == 0 ) ) { - /* Return positive answer */ + /* Return answer */ return( LC_TRUE ); } - /* Return negative value */ - } return( LC_FALSE ); + } + + /* Return answer */ + return( LC_FALSE ); } @@ -48,55 +50,102 @@ char * lc_read_string( int const argc, char ** argv, char const * const er_long, char const * const er_short ) { - /* Parses arguments and parameters */ + /* Parsing arguments and parameters */ for ( int er_parse = 0 ; er_parse < argc; er_parse ++ ) { - /* Check argument */ + /* Check argument short and long forms */ if ( ( strcmp( argv[er_parse], er_long ) == 0 ) || ( strcmp( argv[er_parse], er_short ) == 0 ) ) { - /* Check consistency - return pointer */ - if ( ( ++ er_parse ) < argc ) return( argv[er_parse] ); else return( NULL ); + /* Check consistency */ + if ( ( ++ er_parse ) < argc ) { + + /* Return parameter */ + return( argv[er_parse] ); + + /* Return default value */ + } else { return( NULL ); } } - /* Return pointer */ + /* Return default value */ } return( NULL ); } - unsigned int lc_read_uint( int const argc, char ** argv, char const * const er_long, char const * const er_short, unsigned int er_default ) { - - /* Parses arguments and parameters */ - for( int er_parse = 0 ; er_parse < argc; er_parse ++ ) { + intmax_t lc_read_signed( int const argc, char ** argv, char const * const er_long, char const * const er_short, intmax_t const er_default ) { + + /* Parsing arguments and parameters */ + for ( int er_parse = 0; er_parse < argc; er_parse ++ ) { + + /* Check argument short and long forms */ + if ( ( strcmp( argv[er_parse], er_long ) == 0 ) || ( strcmp( argv[er_parse], er_short ) == 0 ) ) { + + /* Check consistency */ + if ( ( ++ er_parse ) < argc ) { + + /* Convert and return parameter */ + return( strtoimax( argv[er_parse], NULL, 10 ) ); + + /* Return default value */ + } else { return( er_default ); } + + } - /* Check argument */ + } + + /* Return default value */ + return( er_default ); + + } + + uintmax_t lc_read_unsigned( int const argc, char ** argv, char const * const er_long, char const * const er_short, uintmax_t const er_default ) { + + /* Parsing arguments and parameters */ + for ( int er_parse = 0; er_parse < argc; er_parse ++ ) { + + /* Check argument short and long forms */ if ( ( strcmp( argv[er_parse], er_long ) == 0 ) || ( strcmp( argv[er_parse], er_short ) == 0 ) ) { - /* Check consistency - return parameter */ - if ( ( ++ er_parse ) < argc ) return( LC_ATOUI( argv[er_parse] ) ); else return( er_default ); + /* Check consistency */ + if ( ( ++ er_parse ) < argc ) { + + /* Convert and return parameter */ + return( strtoumax( argv[er_parse], NULL, 10 ) ); + + /* Return default value */ + } else { return( er_default ); } } - /* Return parameter */ - } return( er_default ); + } + + /* Return default value */ + return( er_default ); } double lc_read_double( int const argc, char ** argv, char const * const er_long, char const * const er_short, double er_default ) { - /* Parses arguments and parameters */ + /* Parsing arguments and parameters */ for( int er_parse = 0 ; er_parse < argc; er_parse ++ ) { - /* Check argument */ + /* Check argument short and long forms */ if ( ( strcmp( argv[er_parse], er_long ) == 0 ) || ( strcmp( argv[er_parse], er_short ) == 0 ) ) { - /* Check consistency - return parameter */ - if ( ( ++ er_parse ) < argc ) return( LC_ATODP( argv[er_parse] ) ); else return( er_default ); + /* Check consistency */ + if ( ( ++ er_parse ) < argc ) { + + /* Convert and return parameter */ + return( atof( argv[er_parse] ) ); + + /* Return default value */ + } else { return( er_default ); } } - /* Return parameter */ - } return( er_default ); + } - } + /* Return default value */ + return( er_default ); + } diff --git a/lib/libcommon/src/common-args.h b/lib/libcommon/src/common-args.h index 38daf79..981cadb 100644 --- a/lib/libcommon/src/common-args.h +++ b/lib/libcommon/src/common-args.h @@ -46,6 +46,8 @@ # include # include # include + # include + # include # include "common.h" /* @@ -77,7 +79,7 @@ /*! \brief switches parsers * * This function searches in the provided arguments list if the specified - * switch is present. + * flag is present, in short or long form. * * \param argc Main function parameters * \param argv Main function parameters @@ -92,9 +94,9 @@ /*! \brief arguments and parameters parsers * * This function searches in the provided arguments list if the specified - * argument is present. When it is, the function returns the pointer to - * the string containing the parameter of the found argument, NULL is - * returned otherwise. + * argument is present. As it is, the function returns the pointer to the + * string containing the parameter of the found argument. The implicit + * default value is NULL. * * \param argc Main function parameters * \param argv Main function parameters @@ -109,11 +111,11 @@ /*! \brief arguments and parameters parsers * * This function searches in the provided argument list if the specified - * argument is present. As it is, the function converts into unsigned int - * the value of the parameter corresponding to the found argument. + * argument is present. As it is, the function converts the parameter into + * the largest signed int type and returns it. * - * When the argument is not found in the list, the function simply returns - * the provided default value. + * When the argument is not found in the list or on missing parameter, the + * function simply returns the provided default value. * * \param argc Main function parameters * \param argv Main function parameters @@ -124,16 +126,36 @@ * \return Parameter value on success, the default value otherwise */ - unsigned int lc_read_uint( int const argc, char ** argv, char const * const er_long, char const * const er_short, unsigned int er_default ); + intmax_t lc_read_signed( int const argc, char ** argv, char const * const er_long, char const * const er_short, intmax_t const er_default ); /*! \brief arguments and parameters parsers * * This function searches in the provided argument list if the specified - * argument is present. As it is, the function converts into double the - * value of the parameter corresponding to the found argument. + * argument is present. As it is, the function converts the parameter into + * the largest unsigned int type and returns it. * - * When the argument is not found in the list, the function simply returns - * the provided default value. + * When the argument is not found in the list or on missing parameter, the + * function simply returns the provided default value. + * + * \param argc Main function parameters + * \param argv Main function parameters + * \param er_long Argument string - long form + * \param er_short Argument string - short form + * \param er_default Parameter default value + * + * \return Parameter value on success, the default value otherwise + */ + + uintmax_t lc_read_unsigned( int const argc, char ** argv, char const * const er_long, char const * const er_short, uintmax_t const er_default ); + + /*! \brief arguments and parameters parsers + * + * This function searches in the provided argument list if the specified + * argument is present. As it is, the function converts the parameter into + * the double type and returns it. + * + * When the argument is not found in the list or on missing parameter, the + * function simply returns the provided default value. * * \param argc Main function parameters * \param argv Main function parameters