Flag list conversion


Detailed Description

The flag list family of functions manages a list of possible values of a bitmap using strings as key into the list.

A bitmap is simply an integer with certain bits being 1 (enabled) and 0 (disabled).

The FLIST32_START and FLIST64_START macros provides a shortcut for list declaration and initialization; followed by one or more of FLIST32_NODE, FLIST32_NODE1, FLIST64_NODE and FLIST64_NODE1 to insert nodes into the list. The FLIST32_NODE1 and FLIST64_NODE1 macros are the same as FLIST32_NODE and FLIST64_NODE, respectively, except that they convert the bit index to a bitmap value before storing it in the list. The list should then be terminated using FLIST32_END or FLIST64_END, respectively.

The flist32_getval(), flist64_getval(), flist32_getkey() and flist64_getkey() functions provide lookup routines by key and value, respectively.

The flist32_from_str() and flist64_from_str() functions convert a string consisting of zero or more flag list keys seperated by a delimiter and optionally prefixed with a clear modifier to a bitmap/bitmask pair according to a given list.

The flist32_to_str() and flist64_to_str() functions convert a bitmap according to a given list to a string consisting of zero or more flag list keys seperated by a delimiter.


Data Structures

struct  flist32_t
 32 bit list object More...
struct  flist64_t
 64 bit list object More...

Defines

#define FLIST32_START(LIST)   const flist32_t LIST[] = {
 32 bit list initialization
#define FLIST32_NODE(PREFIX, NAME)   { #NAME, PREFIX ## _ ## NAME },
 32 bit list node
#define FLIST32_NODE1(PREFIX, NAME)   { #NAME, (1 << PREFIX ## _ ## NAME) },
 32 bit list node from index
#define FLIST32_END   { 0, 0 } };
 32 bit list termination
#define FLIST64_START(LIST)   const flist64_t LIST[] = {
 64 bit list initialization
#define FLIST64_NODE(PREFIX, NAME)   { #NAME, PREFIX ## _ ## NAME },
 64 bit list node
#define FLIST64_NODE1(PREFIX, NAME)   { #NAME, (1 << PREFIX ## _ ## NAME) },
 64 bit list node from index
#define FLIST64_END   { 0, 0 } };
 64 bit list termination

Functions

uint32_t flist32_getval (const flist32_t list[], const char *key)
 get 32 bit value by key
const char * flist32_getkey (const flist32_t list[], uint32_t val)
 get key from 32 bit value
int flist32_from_str (const char *str, const flist32_t list[], uint32_t *flags, uint32_t *mask, char clmod, char *delim)
 parse flag list string
char * flist32_to_str (const flist32_t list[], uint32_t val, char *delim)
 convert bit mask to flag list string
uint64_t flist64_getval (const flist64_t list[], const char *key)
 get 64 bit value by key
const char * flist64_getkey (const flist64_t list[], uint64_t val)
 get key from 64 bit value
int flist64_from_str (const char *str, const flist64_t list[], uint64_t *flags, uint64_t *mask, char clmod, char *delim)
 parse flag list string
char * flist64_to_str (const flist64_t list[], uint64_t val, char *delim)
 convert bit mask to flag list string


Define Documentation

#define FLIST32_START ( LIST   )     const flist32_t LIST[] = {

32 bit list initialization

Definition at line 61 of file flist.h.

#define FLIST32_NODE ( PREFIX,
NAME   )     { #NAME, PREFIX ## _ ## NAME },

32 bit list node

Definition at line 64 of file flist.h.

#define FLIST32_NODE1 ( PREFIX,
NAME   )     { #NAME, (1 << PREFIX ## _ ## NAME) },

32 bit list node from index

Definition at line 67 of file flist.h.

#define FLIST32_END   { 0, 0 } };

32 bit list termination

Definition at line 70 of file flist.h.

#define FLIST64_START ( LIST   )     const flist64_t LIST[] = {

64 bit list initialization

Definition at line 135 of file flist.h.

#define FLIST64_NODE ( PREFIX,
NAME   )     { #NAME, PREFIX ## _ ## NAME },

64 bit list node

Definition at line 138 of file flist.h.

#define FLIST64_NODE1 ( PREFIX,
NAME   )     { #NAME, (1 << PREFIX ## _ ## NAME) },

64 bit list node from index

Definition at line 141 of file flist.h.

#define FLIST64_END   { 0, 0 } };

64 bit list termination

Definition at line 144 of file flist.h.


Function Documentation

uint32_t flist32_getval ( const flist32_t  list[],
const char *  key 
)

get 32 bit value by key

Parameters:
[in] list list to use for conversion
[in] key key to look for
Returns:
32 bit value >= 1 if key found, 0 otherwise

Definition at line 20 of file flist32_getval.c.

References flist32_t::key, and str_equal().

Referenced by flist32_from_str().

00021 {
00022         int i;
00023 
00024         for (i = 0; list[i].key; i++)
00025                 if (str_equal(list[i].key, key))
00026                         return list[i].val;
00027 
00028         return 0;
00029 }

const char* flist32_getkey ( const flist32_t  list[],
uint32_t  val 
)

get key from 32 bit value

Parameters:
[in] list list to use for conversion
[in] val 32 bit key to look for
Returns:
key if value was found, NULL otherwise
Note:
this functions does not reset the flags or mask argument to an empty bitmap, thus allowing incremental changes to the map.

Definition at line 19 of file flist32_getkey.c.

References flist32_t::key.

00020 {
00021         int i;
00022 
00023         for (i = 0; list[i].key; i++)
00024                 if (list[i].val == val)
00025                         return list[i].key;
00026 
00027         return 0;
00028 }

int flist32_from_str ( const char *  str,
const flist32_t  list[],
uint32_t *  flags,
uint32_t *  mask,
char  clmod,
char *  delim 
)

parse flag list string

Parameters:
[in] str string to convert
[in] list list to use for conversion
[out] flags pointer to a bit mask
[out] mask pointer to a set mask
[in] clmod clear flag modifier
[in] delim flag delimiter
Returns:
0 on success, -1 on error with errno set

Definition at line 20 of file flist32_from_str.c.

References flist32_getval(), strtok_for_each, strtok_free(), and strtok_init_str().

00023 {
00024         char *token;
00025         int clear = 0;
00026         uint32_t cur_flag;
00027 
00028         strtok_t _st, *st = &_st, *p;
00029 
00030         if (!strtok_init_str(st, str, delim, 0))
00031                 return -1;
00032 
00033         strtok_for_each(st, p) {
00034                 token = p->token;
00035 
00036                 if (*token == clmod)
00037                         clear = 1;
00038 
00039                 cur_flag = flist32_getval(list, token+clear);
00040 
00041                 if (!cur_flag) {
00042                         strtok_free(st);
00043                         return -1;
00044                 }
00045 
00046                 if (clear) {
00047                         *flags &= ~cur_flag;
00048                         *mask  |=  cur_flag;
00049                 } else {
00050                         *flags |=  cur_flag;
00051                         *mask  |=  cur_flag;
00052                 }
00053         }
00054 
00055         strtok_free(st);
00056 
00057         return 0;
00058 }

char* flist32_to_str ( const flist32_t  list[],
uint32_t  val,
char *  delim 
)

convert bit mask to flag list string

Parameters:
[in] list list to use for conversion
[in] val bit mask
[in] delim flag delimiter
Returns:
flags list string
Note:
this function ignores set bits if they do not appear in the list

if no flag was found or the bitmap was empty, an empty string is returned, not NULL

Definition at line 21 of file flist32_to_str.c.

References flist32_t::key, str_len(), stralloc_catf(), stralloc_finalize(), stralloc_free(), and stralloc_init().

00022 {
00023         int i;
00024         char *buf;
00025         stralloc_t _sa, *sa = &_sa;
00026 
00027         stralloc_init(sa);
00028 
00029         for (i = 0; list[i].key; i++)
00030                 if (val & list[i].val)
00031                         stralloc_catf(sa, "%s%s", list[i].key, delim);
00032 
00033         if (sa->len > 0)
00034                 sa->len -= str_len(delim);
00035 
00036         buf = stralloc_finalize(sa);
00037 
00038         stralloc_free(sa);
00039         return buf;
00040 }

uint64_t flist64_getval ( const flist64_t  list[],
const char *  key 
)

get 64 bit value by key

Parameters:
[in] list list to use for conversion
[in] key key to look for
Returns:
64 bit value >= 1 if key was found, 0 otherwise

Definition at line 20 of file flist64_getval.c.

References flist64_t::key, and str_equal().

Referenced by flist64_from_str().

00021 {
00022         int i;
00023 
00024         for (i = 0; list[i].key; i++)
00025                 if (str_equal(list[i].key, key))
00026                         return list[i].val;
00027 
00028         return 0;
00029 }

const char* flist64_getkey ( const flist64_t  list[],
uint64_t  val 
)

get key from 64 bit value

Parameters:
[in] list list to use for conversion
[in] val 64 bit key to look for
Returns:
key if value was found, NULL otherwise

Definition at line 19 of file flist64_getkey.c.

References flist64_t::key.

00020 {
00021         int i;
00022 
00023         for (i = 0; list[i].key; i++)
00024                 if (list[i].val == val)
00025                         return list[i].key;
00026 
00027         return 0;
00028 }

int flist64_from_str ( const char *  str,
const flist64_t  list[],
uint64_t *  flags,
uint64_t *  mask,
char  clmod,
char *  delim 
)

parse flag list string

Parameters:
[in] str string to convert
[in] list list to use for conversion
[out] flags pointer to a bit mask
[out] mask pointer to a set mask
[in] clmod clear flag modifier
[in] delim flag delimiter
Returns:
0 on success, -1 on error with errno set
Note:
this functions does not reset the flags or mask argument to an empty bitmap, thus allowing incremental changes to the map.

Definition at line 20 of file flist64_from_str.c.

References flist64_getval(), strtok_for_each, strtok_free(), and strtok_init_str().

00023 {
00024         char *token;
00025         int clear = 0;
00026         uint64_t cur_flag;
00027 
00028         strtok_t _st, *st = &_st, *p;
00029 
00030         if (!strtok_init_str(st, str, delim, 0))
00031                 return -1;
00032 
00033         strtok_for_each(st, p) {
00034                 token = p->token;
00035 
00036                 if (*token == clmod)
00037                         clear = 1;
00038 
00039                 cur_flag = flist64_getval(list, token+clear);
00040 
00041                 if (!cur_flag) {
00042                         strtok_free(st);
00043                         return -1;
00044                 }
00045 
00046                 if (clear) {
00047                         *flags &= ~cur_flag;
00048                         *mask  |=  cur_flag;
00049                 } else {
00050                         *flags |=  cur_flag;
00051                         *mask  |=  cur_flag;
00052                 }
00053         }
00054 
00055         strtok_free(st);
00056 
00057         return 0;
00058 }

char* flist64_to_str ( const flist64_t  list[],
uint64_t  val,
char *  delim 
)

convert bit mask to flag list string

Parameters:
[in] list list to use for conversion
[in] val bit mask
[in] delim flag delimiter
Returns:
flags list string
Note:
this function ignores set bits if they do not appear in the list

if no flag was found or the bitmap was empty, an empty string is returned, not NULL

Definition at line 21 of file flist64_to_str.c.

References flist64_t::key, str_len(), stralloc_catf(), stralloc_finalize(), stralloc_free(), and stralloc_init().

00022 {
00023         int i;
00024         char *buf;
00025         stralloc_t _sa, *sa = &_sa;
00026 
00027         stralloc_init(sa);
00028 
00029         for (i = 0; list[i].key; i++)
00030                 if (val & list[i].val)
00031                         stralloc_catf(sa, "%s%s", list[i].key, delim);
00032 
00033         if (sa->len > 0)
00034                 sa->len -= str_len(delim);
00035 
00036         buf = stralloc_finalize(sa);
00037 
00038         stralloc_free(sa);
00039         return buf;
00040 }


Generated on Tue Jun 19 20:38:45 2007 for lucid by  doxygen 1.5.2