source: rtems/cpukit/libcsupport/src/assoc.c @ f26145b

4.104.114.84.95
Last change on this file since f26145b was 50f32b11, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/18/04 at 06:05:35

Remove stray white spaces.

  • Property mode set to 100644
File size: 4.6 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    uint32_t       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    uint32_t       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
80uint32_t
81rtems_assoc_remote_by_local(
82    const rtems_assoc_t *ap,
83    uint32_t       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
94uint32_t
95rtems_assoc_local_by_remote(
96    const rtems_assoc_t *ap,
97    uint32_t       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
108uint32_t
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
122uint32_t
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    uint32_t   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    uint32_t       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    uint32_t       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
181uint32_t   rtems_assoc_remote_by_local_bitfield(
182    const rtems_assoc_t *ap,
183    uint32_t       local_value
184  )
185{
186    uint32_t   b;
187    uint32_t   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
197uint32_t   rtems_assoc_local_by_remote_bitfield(
198    const rtems_assoc_t *ap,
199    uint32_t       remote_value
200  )
201{
202    uint32_t   b;
203    uint32_t   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    uint32_t       value,
216    char          *buffer
217  )
218{
219    uint32_t   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    uint32_t       value,
238    char          *buffer
239  )
240{
241    uint32_t   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.