source: rtems/c/src/lib/libc/assoc.c @ d7aecdc

4.104.114.84.95
Last change on this file since d7aecdc was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

  • configure.in: Add libc/config.h
  • libc/Makefile.am: Add INCLUDES += -I. to pickup config.h
  • libc/.cvsignore: Add config.h and stamp-h
  • libc/*.c: Add config.h support.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * assoc.c
3 *      rtems assoc routines
4 *
5 *  $Id$
6 */
7
8#if HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include <rtems.h>
13#include <rtems/assoc.h>
14
15#include <string.h>             /* strcat, strcmp */
16
17#define STREQ(a,b)      (strcmp((a), (b)) == 0)
18#define rtems_assoc_is_default(ap)  ((ap)->name && STREQ(ap->name, RTEMS_ASSOC_DEFAULT_NAME))
19
20const rtems_assoc_t *
21rtems_assoc_ptr_by_name(
22    const rtems_assoc_t *ap,
23    const char *name
24  )
25{
26    const rtems_assoc_t *default_ap = 0;
27
28    if (rtems_assoc_is_default(ap))
29        default_ap = ap++;
30   
31    for ( ; ap->name; ap++)
32        if (strcmp(ap->name, name) == 0)
33            return ap;
34
35    return default_ap;
36}
37
38const rtems_assoc_t *
39rtems_assoc_ptr_by_local(
40    const rtems_assoc_t *ap,
41    unsigned32     local_value
42  )
43{
44    const rtems_assoc_t *default_ap = 0;
45
46    if (rtems_assoc_is_default(ap))
47        default_ap = ap++;
48   
49    for ( ; ap->name; ap++)
50        if (ap->local_value == local_value)
51            return ap;
52
53    return default_ap;
54}
55
56
57const rtems_assoc_t *
58rtems_assoc_ptr_by_remote(
59    const rtems_assoc_t *ap,
60    unsigned32     remote_value
61  )
62{
63    const rtems_assoc_t *default_ap = 0;
64
65    if (rtems_assoc_is_default(ap))
66        default_ap = ap++;
67   
68    for ( ; ap->name; ap++)
69        if (ap->remote_value == remote_value)
70            return ap;
71
72    return default_ap;
73}
74
75
76/*
77 * Get values
78 */
79
80unsigned32
81rtems_assoc_remote_by_local(
82    const rtems_assoc_t *ap,
83    unsigned32     local_value
84  )
85{
86    const rtems_assoc_t *nap;
87    nap = rtems_assoc_ptr_by_local(ap, local_value);
88    if (nap)
89        return nap->remote_value;
90   
91    return 0;
92}
93
94unsigned32
95rtems_assoc_local_by_remote(
96    const rtems_assoc_t *ap,
97    unsigned32     remote_value
98  )
99{
100    const rtems_assoc_t *nap;
101    nap = rtems_assoc_ptr_by_remote(ap, remote_value);
102    if (nap)
103        return nap->local_value;
104   
105    return 0;
106}
107
108unsigned32
109rtems_assoc_remote_by_name(
110    const rtems_assoc_t *ap,
111    const char          *name
112  )
113{
114    const rtems_assoc_t *nap;
115    nap = rtems_assoc_ptr_by_name(ap, name);
116    if (nap)
117        return nap->remote_value;
118   
119    return 0;
120}
121
122unsigned32
123rtems_assoc_local_by_name(
124    const rtems_assoc_t *ap,
125    const char          *name
126  )
127{
128    const rtems_assoc_t *nap;
129    nap = rtems_assoc_ptr_by_name(ap, name);
130    if (nap)
131        return nap->local_value;
132   
133    return 0;
134}
135
136/*
137 * what to return if a value is not found
138 * this is not reentrant, but it really shouldn't be invoked anyway
139 */
140
141const char *rtems_assoc_name_bad(
142    unsigned32 bad_value
143);
144
145/* body in separate file to reduce dependency on printf */
146
147
148const char *
149rtems_assoc_name_by_local(
150    const rtems_assoc_t *ap,
151    unsigned32     local_value
152  )
153{
154    const rtems_assoc_t *nap;
155    nap = rtems_assoc_ptr_by_local(ap, local_value);
156    if (nap)
157        return nap->name;
158   
159    return rtems_assoc_name_bad(local_value);
160}
161
162const char *
163rtems_assoc_name_by_remote(
164    const rtems_assoc_t *ap,
165    unsigned32     remote_value
166  )
167{
168    const rtems_assoc_t *nap;
169    nap = rtems_assoc_ptr_by_remote(ap, remote_value);
170    if (nap)
171        return nap->name;
172   
173    return rtems_assoc_name_bad(remote_value);
174}
175
176/*
177 * Bitfield functions assume just 1 bit set in each of remote and local
178 *      entries; they do not check for this.
179 */
180
181unsigned32 rtems_assoc_remote_by_local_bitfield(
182    const rtems_assoc_t *ap,
183    unsigned32     local_value
184  )
185
186    unsigned32 b;
187    unsigned32 remote_value = 0;
188
189    for (b = 1; b; b <<= 1)
190        if (b & local_value)
191            remote_value |= rtems_assoc_remote_by_local(ap, b);
192       
193    return remote_value;
194}
195
196
197unsigned32 rtems_assoc_local_by_remote_bitfield(
198    const rtems_assoc_t *ap,
199    unsigned32     remote_value
200  )
201
202    unsigned32 b;
203    unsigned32 local_value = 0;
204
205    for (b = 1; b; b <<= 1)
206        if (b & remote_value)
207            local_value |= rtems_assoc_local_by_remote(ap, b);
208       
209    return local_value;
210}
211
212char *
213rtems_assoc_name_by_remote_bitfield(
214    const rtems_assoc_t *ap,
215    unsigned32     value,
216    char          *buffer
217  )
218
219    unsigned32 b;
220
221    *buffer = 0;
222
223    for (b = 1; b; b <<= 1)
224        if (b & value)
225        {
226            if (*buffer)
227                strcat(buffer, " ");
228            strcat(buffer, rtems_assoc_name_by_remote(ap, b));
229        }
230       
231    return buffer;
232}
233
234char *
235rtems_assoc_name_by_local_bitfield(
236    const rtems_assoc_t *ap,
237    unsigned32     value,
238    char          *buffer
239  )
240
241    unsigned32 b;
242
243    *buffer = 0;
244
245    for (b = 1; b; b <<= 1)
246        if (b & value)
247        {
248            if (*buffer)
249                strcat(buffer, " ");
250            strcat(buffer, rtems_assoc_name_by_local(ap, b));
251        }
252       
253    return buffer;
254}
Note: See TracBrowser for help on using the repository browser.