Skip to content

Commit

Permalink
Revision of libcommon and adaptation of code
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-hamel committed Sep 5, 2016
1 parent 5f3952a commit 22db6b2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 39 deletions.
101 changes: 75 additions & 26 deletions lib/libcommon/src/common-args.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

Expand All @@ -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 );

}
48 changes: 35 additions & 13 deletions lib/libcommon/src/common-args.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdint.h>
# include <inttypes.h>
# include "common.h"

/*
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 22db6b2

Please sign in to comment.