diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-01-11 11:03:24 +0100 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-01-12 07:44:36 +0100 |
commit | 0c431303cc7f0e91c70e06126353072e9a9bdb05 (patch) | |
tree | 2f08e6704668b961f81baa2e554b2e1b1ead2459 /cpukit/libcsupport/src/assoc32tostring.c | |
parent | score: Remove unused _States_Is_*() (diff) | |
download | rtems-0c431303cc7f0e91c70e06126353072e9a9bdb05.tar.bz2 |
Add rtems_assoc_32_to_string()
Diffstat (limited to 'cpukit/libcsupport/src/assoc32tostring.c')
-rw-r--r-- | cpukit/libcsupport/src/assoc32tostring.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/cpukit/libcsupport/src/assoc32tostring.c b/cpukit/libcsupport/src/assoc32tostring.c new file mode 100644 index 0000000000..31b08857d3 --- /dev/null +++ b/cpukit/libcsupport/src/assoc32tostring.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016 embedded brains GmbH. + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include <rtems/assoc.h> + +#include <stdbool.h> +#include <string.h> + +static size_t space( size_t buffer_size, size_t len ) +{ + if ( len < buffer_size ) { + return buffer_size - len; + } else { + return 0; + } +} + +size_t rtems_assoc_32_to_string( + uint32_t value, + char *buffer, + size_t buffer_size, + const rtems_assoc_32_pair *pairs, + size_t pair_count, + const char *separator, + const char *fallback +) +{ + size_t len; + size_t i; + + len = 0; + + for ( i = 0; i < pair_count ; ++i ) { + const rtems_assoc_32_pair *p; + + p = &pairs[ i ]; + + if ( ( value & p->bits ) != 0 ) { + if ( len > 0 ) { + len += strlcpy( &buffer[ len ], separator, space( buffer_size, len ) ); + } + + len += strlcpy( &buffer[ len ], p->name, space( buffer_size, len ) ); + } + } + + if ( len == 0 ) { + len += strlcpy( buffer, fallback, buffer_size ); + } + + return len; +} |