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 | |
---|
20 | const rtems_assoc_t * |
---|
21 | rtems_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 | |
---|
38 | const rtems_assoc_t * |
---|
39 | rtems_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 | |
---|
57 | const rtems_assoc_t * |
---|
58 | rtems_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 | |
---|
80 | unsigned32 |
---|
81 | rtems_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 | |
---|
94 | unsigned32 |
---|
95 | rtems_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 | |
---|
108 | unsigned32 |
---|
109 | rtems_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 | |
---|
122 | unsigned32 |
---|
123 | rtems_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 | |
---|
141 | const char *rtems_assoc_name_bad( |
---|
142 | unsigned32 bad_value |
---|
143 | ); |
---|
144 | |
---|
145 | /* body in separate file to reduce dependency on printf */ |
---|
146 | |
---|
147 | |
---|
148 | const char * |
---|
149 | rtems_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 | |
---|
162 | const char * |
---|
163 | rtems_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 | |
---|
181 | unsigned32 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 | |
---|
197 | unsigned32 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 | |
---|
212 | char * |
---|
213 | rtems_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 | |
---|
234 | char * |
---|
235 | rtems_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 | } |
---|