Functions

src/optarg.c File Reference

#include "optarg.h"
#include "helper.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

OptArgoptarg_ptr (const char *key, void *ptr)
 create a new OptArg* containing a pointer.
OptArgoptarg_scalar (const char *key, double scalar)
 create a new OptArg* containing a scalar value.
OptArgListoptarglist (char *format,...)
 Creates an OptArgList out of one or many arguments.
OptArgListoptarglist_append_arg (OptArgList *list, OptArg *arg)
 create a new optarglist that is the former optarglist with the new arg appended.
OptArgListoptarglist_delete_arg (OptArgList *list, OptArg *arg)
 delete an argument from the argument list.
void optarglist_free (OptArgList *list)
 free an optarglist.
bool optarglist_has_key (OptArgList *list, const char *key)
 return TRUE if key is found in list, else FALSE.
OptArgoptarglist_optarg_by_key (OptArgList *list, const char *key)
 Return a pointer to an OptArg struct with the key from the list.
void optarglist_print (OptArgList *list, FILE *out)
 print an optarglist.
void * optarglist_ptr_by_key (OptArgList *list, const char *key)
 Return a pointer to the data_ptr field of the OptArg struct.
OptArgListoptarglist_remove_freeflag (OptArgList *list, bool *removedflag)
 remove the optarglist_free-flag from the list.
double optarglist_scalar_by_key (OptArgList *list, const char *key)
 Return the value of the scalar argument.
OptArgListoptarglisttmp (char *format,...)
 Creates an OptArgList including a optarglist_free arg.

Function Documentation

OptArg* optarg_ptr ( const char *  key,
void *  ptr 
)

create a new OptArg* containing a pointer.

Definition at line 414 of file optarg.c.

OptArg* optarg_scalar ( const char *  key,
double  scalar 
)

create a new OptArg* containing a scalar value.

Definition at line 402 of file optarg.c.

OptArgList* optarglist ( char *  format,
  ... 
)

Creates an OptArgList out of one or many arguments.

The Format string has the following convention:

	 key1=double*,key2=void*,key3=int,key=double,...
	 

If you have at least one asterisk (*) in the specification, then the corresponding argument needs to be a pointer. Internally, all pointer types are void* and stored along with the given specification.

Be VERY careful to typecast all variables to match the type you indicated. For example, if you have a float-variable and pass it as double, you need to typecast to (double).

Only "basic" scalar values (without asterisk) are supported:

  • char
  • short
  • int
  • long
  • float
  • double

Blanks/Newlines etc are stripped from key and type, so

	 key = double , key2=void*
	 

are ok.

Definition at line 218 of file optarg.c.

OptArgList* optarglist_append_arg ( OptArgList list,
OptArg arg 
)

create a new optarglist that is the former optarglist with the new arg appended.

The caller is responsible for freeing the old list.

Parameters:
list the pointer to the Optarglist
Returns:
the new list (the caller is responsible for freeing the old one)

Definition at line 307 of file optarg.c.

OptArgList* optarglist_delete_arg ( OptArgList list,
OptArg arg 
)

delete an argument from the argument list.

The function returns a new OptArgList containing which is the old list minus the argument. The arguments copied!

The caller is responsible for freeing the old list.

Parameters:
list the list
arg the argument to delete
Returns:
the new list (the caller is responsible for freeing the old one)

Definition at line 330 of file optarg.c.

void optarglist_free ( OptArgList list  ) 

free an optarglist.

does not free any data_ptr.

Definition at line 231 of file optarg.c.

bool optarglist_has_key ( OptArgList list,
const char *  key 
)

return TRUE if key is found in list, else FALSE.

Definition at line 290 of file optarg.c.

OptArg* optarglist_optarg_by_key ( OptArgList list,
const char *  key 
)

Return a pointer to an OptArg struct with the key from the list.

NULL if no such key has been found.

Definition at line 278 of file optarg.c.

void optarglist_print ( OptArgList list,
FILE *  out 
)

print an optarglist.

Definition at line 28 of file optarg.c.

void* optarglist_ptr_by_key ( OptArgList list,
const char *  key 
)

Return a pointer to the data_ptr field of the OptArg struct.

with the key from the list. NULL if no such key has been found.

Definition at line 262 of file optarg.c.

OptArgList* optarglist_remove_freeflag ( OptArgList list,
bool *  removedflag 
)

remove the optarglist_free-flag from the list.

This function is only interesting for you, if you want to write an own function that takes an OptArgList* as an argument.

The function removes the optarglist_free-flag from the list and returns a new list without the flag. The old list is free'd in case that the optarglist_free argument is found.

The removedflag-flag is set to FALSE if it is the same list, to TRUE if it is a new (truncated) list.

Usage is as follows:

     void testfct( OptArgList *opts ){   
        // overwrite the pointer
         bool freeflag_removed=FALSE;
        opts=optarglist_remove_freeflag( opts, &freeflag_removed );

         // do some stuff including passing the argument to some other function
         call_other_function( opts );
         
         // clean up in case the flag was indeed removed
         if( freeflag_removed )
            optarglist_free( opts );
Parameters:
list the list
removedflag is set to FALSE if it is the same list, to TRUE if it is a new (truncated) list.
Returns:
a pointer to the new (or the old) list

Definition at line 386 of file optarg.c.

double optarglist_scalar_by_key ( OptArgList list,
const char *  key 
)

Return the value of the scalar argument.

or NaN if not available. Check returned value with isnan()!

Definition at line 242 of file optarg.c.

OptArgList* optarglisttmp ( char *  format,
  ... 
)

Creates an OptArgList including a optarglist_free arg.

The Format string has the following convention:

	 key1=double*,key2=void*,key3=int,key=double,...
	 

If you have at least one asterisk (*) in the specification, then the corresponding argument needs to be a pointer. Internally, all pointer types are void* and stored along with the given specification.

Be VERY careful to typecast all variables to match the type you indicated. For example, if you have a float-variable and pass it as double, you need to typecast to (double).

Only "basic" scalar values (without asterisk) are supported:

  • char
  • short
  • int
  • long
  • float
  • double

Blanks/Newlines etc are stripped from key and type, so

	 key = double , key2=void*
	 

are ok.

Definition at line 167 of file optarg.c.