source: rtems/c/src/libmisc/assoc/assoc.c @ 5a36154a

4.104.114.84.95
Last change on this file since 5a36154a was c64e4ed4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/15/96 at 21:50:28

updates from Tony Bennett for PA and UNIX ports

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 *      @(#)assoc.c     1.6 - 95/10/25
3 *
4 *
5 * assoc.c
6 *      rtems assoc routines
7 *
8 *  $Id$
9 */
10
11#include <rtems.h>
12#include "assoc.h"
13
14#include <stdio.h>              /* sprintf */
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 *
142rtems_assoc_name_bad(
143    unsigned32 bad_value
144)
145{
146#ifdef RTEMS_DEBUG
147    static char bad_buffer[32];
148
149    sprintf(bad_buffer, "< %d [0x%x] >", bad_value, bad_value);
150#else
151    static char bad_buffer[32] = "<assoc.c: BAD NAME>";
152#endif
153    return bad_buffer;
154}
155
156
157const char *
158rtems_assoc_name_by_local(
159    const rtems_assoc_t *ap,
160    unsigned32     local_value
161  )
162{
163    const rtems_assoc_t *nap;
164    nap = rtems_assoc_ptr_by_local(ap, local_value);
165    if (nap)
166        return nap->name;
167   
168    return rtems_assoc_name_bad(local_value);
169}
170
171const char *
172rtems_assoc_name_by_remote(
173    const rtems_assoc_t *ap,
174    unsigned32     remote_value
175  )
176{
177    const rtems_assoc_t *nap;
178    nap = rtems_assoc_ptr_by_remote(ap, remote_value);
179    if (nap)
180        return nap->name;
181   
182    return rtems_assoc_name_bad(remote_value);
183}
184
185/*
186 * Bitfield functions assume just 1 bit set in each of remote and local
187 *      entries; they do not check for this.
188 */
189
190unsigned32 rtems_assoc_remote_by_local_bitfield(
191    const rtems_assoc_t *ap,
192    unsigned32     local_value
193  )
194
195    unsigned32 b;
196    unsigned32 remote_value = 0;
197
198    for (b = 1; b; b <<= 1)
199        if (b & local_value)
200            remote_value |= rtems_assoc_remote_by_local(ap, b);
201       
202    return remote_value;
203}
204
205
206unsigned32 rtems_assoc_local_by_remote_bitfield(
207    const rtems_assoc_t *ap,
208    unsigned32     remote_value
209  )
210
211    unsigned32 b;
212    unsigned32 local_value = 0;
213
214    for (b = 1; b; b <<= 1)
215        if (b & remote_value)
216            local_value |= rtems_assoc_local_by_remote(ap, b);
217       
218    return local_value;
219}
220
221char *
222rtems_assoc_name_by_remote_bitfield(
223    const rtems_assoc_t *ap,
224    unsigned32     value,
225    char          *buffer
226  )
227
228    unsigned32 b;
229
230    *buffer = 0;
231
232    for (b = 1; b; b <<= 1)
233        if (b & value)
234        {
235            if (*buffer)
236                strcat(buffer, " ");
237            strcat(buffer, rtems_assoc_name_by_remote(ap, b));
238        }
239       
240    return buffer;
241}
242
243char *
244rtems_assoc_name_by_local_bitfield(
245    const rtems_assoc_t *ap,
246    unsigned32     value,
247    char          *buffer
248  )
249
250    unsigned32 b;
251
252    *buffer = 0;
253
254    for (b = 1; b; b <<= 1)
255        if (b & value)
256        {
257            if (*buffer)
258                strcat(buffer, " ");
259            strcat(buffer, rtems_assoc_name_by_local(ap, b));
260        }
261       
262    return buffer;
263}
Note: See TracBrowser for help on using the repository browser.