source: rtems/cpukit/libcsupport/src/assoc32tostring.c @ 0c43130

5
Last change on this file since 0c43130 was 0c43130, checked in by Sebastian Huber <sebastian.huber@…>, on 01/11/17 at 10:03:24

Add rtems_assoc_32_to_string()

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/*
2 * Copyright (c) 2016 embedded brains GmbH.
3 *
4 * The license and distribution terms for this file may be
5 * found in the file LICENSE in this distribution or at
6 * http://www.rtems.org/license/LICENSE.
7 */
8
9#if HAVE_CONFIG_H
10#include "config.h"
11#endif
12
13#include <rtems/assoc.h>
14
15#include <stdbool.h>
16#include <string.h>
17
18static size_t space( size_t buffer_size, size_t len )
19{
20  if ( len < buffer_size ) {
21    return buffer_size - len;
22  } else {
23    return 0;
24  }
25}
26
27size_t rtems_assoc_32_to_string(
28  uint32_t                   value,
29  char                      *buffer,
30  size_t                     buffer_size,
31  const rtems_assoc_32_pair *pairs,
32  size_t                     pair_count,
33  const char                *separator,
34  const char                *fallback
35)
36{
37  size_t len;
38  size_t i;
39
40  len = 0;
41
42  for ( i = 0; i < pair_count ; ++i ) {
43    const rtems_assoc_32_pair *p;
44
45    p = &pairs[ i ];
46
47    if ( ( value & p->bits ) != 0 ) {
48      if ( len > 0 ) {
49        len += strlcpy( &buffer[ len ], separator, space( buffer_size, len ) );
50      }
51
52      len += strlcpy( &buffer[ len ], p->name, space( buffer_size, len ) );
53    }
54  }
55
56  if ( len == 0 ) {
57    len += strlcpy( buffer, fallback, buffer_size );
58  }
59
60  return len;
61}
Note: See TracBrowser for help on using the repository browser.