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

4.104.114.84.95
Last change on this file since 5c491aef was b06e68ef, checked in by Joel Sherrill <joel.sherrill@…>, on 08/17/95 at 19:51:51

Numerous miscellaneous features incorporated from Tony Bennett
(tbennett@…) including the following major additions:

+ variable length messages
+ named devices
+ debug monitor
+ association tables/variables

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