CNDP  22.08.0
kvargs.h File Reference

Go to the source code of this file.

Data Structures

struct  kvargs_pair
 
struct  kvargs
 

Macros

#define KVARGS_MAX   64
 
#define KVARGS_PAIRS_DELIM_1   ","
 
#define KVARGS_KV_DELIM   "="
 

Typedefs

typedef int(* arg_handler_t) (const char *key, const char *value, void *opaque)
 

Functions

struct kvargskvargs_parse (const char *args, const char *const valid_keys[])
 
struct kvargskvargs_parse_delim (const char *args, const char *const valid_keys[], const char *valid_ends)
 
void kvargs_free (struct kvargs *kvlist)
 
int kvargs_process (const struct kvargs *kvlist, const char *key_match, arg_handler_t handler, void *opaque_arg)
 
int kvargs_process_type (const struct kvargs *kvlist, const char *key_match, kvargs_type_t typ, void *opaque_arg)
 
static int kvargs_ptr (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_int8 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_uint8 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_int16 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_uint16 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_int32 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_uint32 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_int64 (const struct kvargs *kv, const char *key, void *arg)
 
static int kvargs_uint64 (const struct kvargs *kv, const char *key, void *arg)
 
unsigned kvargs_count (const struct kvargs *kvlist, const char *key_match)
 
int kvargs_strcmp (const char *key, const char *value, void *opaque)
 

Detailed Description

CNE Argument parsing

This module can be used to parse arguments whose format is key1=value1;key2=value2;key3=value3;...

The same key can appear several times with the same or a different value. The arguments are stored as a list of key/values associations and not as a dictionary.

This file provides some helpers that are especially used by virtual ethernet devices at initialization for arguments parsing.

Definition in file kvargs.h.

Macro Definition Documentation

◆ KVARGS_MAX

#define KVARGS_MAX   64

Maximum number of key/value associations

Definition at line 45 of file kvargs.h.

◆ KVARGS_PAIRS_DELIM_1

#define KVARGS_PAIRS_DELIM_1   ","

Separator characters used between each pair

Definition at line 50 of file kvargs.h.

◆ KVARGS_KV_DELIM

#define KVARGS_KV_DELIM   "="

Separator character used between key and value

Definition at line 56 of file kvargs.h.

Typedef Documentation

◆ arg_handler_t

typedef int(* arg_handler_t) (const char *key, const char *value, void *opaque)

Type of callback function used by kvargs_process()

Definition at line 61 of file kvargs.h.

Function Documentation

◆ kvargs_parse()

struct kvargs* kvargs_parse ( const char *  args,
const char *const  valid_keys[] 
)

Allocate a kvargs and store key/value associations from a string

The function allocates and fills a kvargs structure from a given string whose format is key1=value1,key2=value2,...

The structure can be freed with kvargs_free().

Parameters
argsThe input string containing the key/value associations
valid_keysA list of valid keys (table of const char *, the last must be NULL). This argument is ignored if NULL
Returns
  • A pointer to an allocated kvargs structure on success
  • NULL on error

◆ kvargs_parse_delim()

struct kvargs* kvargs_parse_delim ( const char *  args,
const char *const  valid_keys[],
const char *  valid_ends 
)

Allocate a kvargs and store key/value associations from a string. This version will consider any byte from valid_ends as a possible terminating character, and will not parse beyond any of their occurrence.

The function allocates and fills an kvargs structure from a given string whose format is key1=value1,key2=value2,...

The structure can be freed with kvargs_free().

Parameters
argsThe input string containing the key/value associations
valid_keysA list of valid keys (table of const char *, the last must be NULL). This argument is ignored if NULL
valid_endsAcceptable terminating characters. If NULL, the behavior is the same as kvargs_parse.
Returns
  • A pointer to an allocated kvargs structure on success
  • NULL on error

◆ kvargs_free()

void kvargs_free ( struct kvargs kvlist)

Free a kvargs structure

Free a kvargs structure previously allocated with kvargs_parse().

Parameters
kvlistThe kvargs structure. No error if NULL.

◆ kvargs_process()

int kvargs_process ( const struct kvargs kvlist,
const char *  key_match,
arg_handler_t  handler,
void *  opaque_arg 
)

Call a handler function for each key/value matching the key

For each key/value association that matches the given key, calls the handler function with the for a given arg_name passing the value on the dictionary for that key and a given extra argument.

Parameters
kvlistThe kvargs structure. No error if NULL.
key_matchThe key on which the handler should be called, or NULL to process handler on all associations
handlerThe function to call for each matching key
opaque_argA pointer passed unchanged to the handler
Returns
  • 0 on success
  • A negative value on error

◆ kvargs_process_type()

int kvargs_process_type ( const struct kvargs kvlist,
const char *  key_match,
kvargs_type_t  typ,
void *  opaque_arg 
)

Process the kvargs by type of k/v pair

Parameters
kvlistThe kvargs structure. No error if NULL.
key_matchThe key on which the handler should be called, or NULL to process handler on all associations
typparse based on the type of argument
opaque_argA pointer passed unchanged to the handler
Returns
  • 0 on success
  • A negative value on error

◆ kvargs_ptr()

static int kvargs_ptr ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a pointer value

Definition at line 187 of file kvargs.h.

◆ kvargs_int8()

static int kvargs_int8 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a int8 value

Definition at line 196 of file kvargs.h.

◆ kvargs_uint8()

static int kvargs_uint8 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a uint8 value

Definition at line 205 of file kvargs.h.

◆ kvargs_int16()

static int kvargs_int16 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a int16 value

Definition at line 214 of file kvargs.h.

◆ kvargs_uint16()

static int kvargs_uint16 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a uint16 value

Definition at line 223 of file kvargs.h.

◆ kvargs_int32()

static int kvargs_int32 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a int32 value

Definition at line 232 of file kvargs.h.

◆ kvargs_uint32()

static int kvargs_uint32 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a uint32 value

Definition at line 241 of file kvargs.h.

◆ kvargs_int64()

static int kvargs_int64 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a int64 value

Definition at line 250 of file kvargs.h.

◆ kvargs_uint64()

static int kvargs_uint64 ( const struct kvargs kv,
const char *  key,
void *  arg 
)
inlinestatic

Helper routine to parse and return a uint64 value

Definition at line 259 of file kvargs.h.

◆ kvargs_count()

unsigned kvargs_count ( const struct kvargs kvlist,
const char *  key_match 
)

Count the number of associations matching the given key

Parameters
kvlistThe kvargs structure
key_matchThe key that should match, or NULL to count all associations
Returns
The number of entries

◆ kvargs_strcmp()

int kvargs_strcmp ( const char *  key,
const char *  value,
void *  opaque 
)

Generic kvarg handler for string comparison.

This function can be used for a generic string comparison processing on a list of kvargs.

Parameters
keykvarg pair key.
valuekvarg pair value.
opaqueOpaque pointer to a string.
Returns
0 if the strings match. !0 otherwise or on error.

Unless strcmp, comparison ordering is not kept. In order for kvargs_process to stop processing on match error, a negative value is returned even if strcmp had returned a positive one.