source: rtems/cpukit/score/src/objectgetnameasstring.c @ dea4bbe3

5
Last change on this file since dea4bbe3 was b8774933, checked in by Sebastian Huber <sebastian.huber@…>, on 05/25/18 at 12:12:22

score: Simplify _Objects_Name_to_string()

Do not use isprint() from <ctype.h> since it depends on the heavy weight
C locale implementation in Newlib.

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief Extracts a Node from a Chain
5 *
6 * @ingroup Score
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2008.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.org/license/LICENSE.
16 */
17
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <rtems/score/threadimpl.h>
23
24/*
25 * Do not use isprint() from <ctypes.h> since this depends on the heavy weight
26 * C locale support of Newlib.
27 */
28static bool _Objects_Name_char_is_printable( char c )
29{
30  unsigned char uc;
31
32  uc = (unsigned char) c;
33  return uc >= ' ' && uc <= '~';
34}
35
36size_t _Objects_Name_to_string(
37  Objects_Name  name,
38  bool          is_string,
39  char         *buffer,
40  size_t        buffer_size
41)
42{
43  char        lname[ 5 ];
44  const char *s;
45  char       *d;
46  size_t      i;
47
48#if defined(RTEMS_SCORE_OBJECT_ENABLE_STRING_NAMES)
49  if ( is_string ) {
50    s = name.name_p;
51  } else
52#endif
53  {
54    lname[ 0 ] = (name.name_u32 >> 24) & 0xff;
55    lname[ 1 ] = (name.name_u32 >> 16) & 0xff;
56    lname[ 2 ] = (name.name_u32 >>  8) & 0xff;
57    lname[ 3 ] = (name.name_u32 >>  0) & 0xff;
58    lname[ 4 ] = '\0';
59    s = lname;
60  }
61
62  d = buffer;
63  i = 1;
64
65  if ( s != NULL ) {
66    while ( *s != '\0' ) {
67      if ( i < buffer_size ) {
68        *d = _Objects_Name_char_is_printable(*s) ? *s : '*';
69        ++d;
70      }
71
72      ++s;
73      ++i;
74    }
75  }
76
77  if ( buffer_size > 0 ) {
78    *d = '\0';
79  }
80
81  return i - 1;
82}
83
84/*
85 *  This method objects the name of an object and returns its name
86 *  in the form of a C string.  It attempts to be careful about
87 *  overflowing the user's string and about returning unprintable characters.
88 */
89
90char *_Objects_Get_name_as_string(
91  Objects_Id        id,
92  size_t            length,
93  char             *name
94)
95{
96  Objects_Information   *information;
97  Objects_Control       *the_object;
98  ISR_lock_Context       lock_context;
99  Objects_Id             tmpId;
100
101  if ( length == 0 )
102    return NULL;
103
104  if ( name == NULL )
105    return NULL;
106
107  tmpId = (id == OBJECTS_ID_OF_SELF) ? _Thread_Get_executing()->Object.id : id;
108
109  information = _Objects_Get_information_id( tmpId );
110  if ( !information )
111    return NULL;
112
113  the_object = _Objects_Get( tmpId, &lock_context, information );
114  if ( the_object == NULL ) {
115    return NULL;
116  }
117
118  _Objects_Name_to_string(
119    the_object->name,
120#if defined(RTEMS_SCORE_OBJECT_ENABLE_STRING_NAMES)
121    information->is_string,
122#else
123    false,
124#endif
125    name,
126    length
127  );
128
129  _ISR_lock_ISR_enable( &lock_context );
130  return name;
131}
Note: See TracBrowser for help on using the repository browser.