summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/assoc32tostring.c
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2017-01-11 11:03:24 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2017-01-12 07:44:36 +0100
commit0c431303cc7f0e91c70e06126353072e9a9bdb05 (patch)
tree2f08e6704668b961f81baa2e554b2e1b1ead2459 /cpukit/libcsupport/src/assoc32tostring.c
parentscore: Remove unused _States_Is_*() (diff)
downloadrtems-0c431303cc7f0e91c70e06126353072e9a9bdb05.tar.bz2
Add rtems_assoc_32_to_string()
Diffstat (limited to 'cpukit/libcsupport/src/assoc32tostring.c')
-rw-r--r--cpukit/libcsupport/src/assoc32tostring.c61
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;
+}