1 | /* |
---|
2 | * assoc.c |
---|
3 | * rtems assoc routines |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <rtems.h> |
---|
9 | #include "assoc.h" |
---|
10 | |
---|
11 | #include <stdio.h> /* sprintf */ |
---|
12 | #include <string.h> /* strcat, strcmp */ |
---|
13 | |
---|
14 | #define STREQ(a,b) (strcmp((a), (b)) == 0) |
---|
15 | #define rtems_assoc_is_default(ap) ((ap)->name && STREQ(ap->name, RTEMS_ASSOC_DEFAULT_NAME)) |
---|
16 | |
---|
17 | const rtems_assoc_t * |
---|
18 | rtems_assoc_ptr_by_name( |
---|
19 | const rtems_assoc_t *ap, |
---|
20 | const char *name |
---|
21 | ) |
---|
22 | { |
---|
23 | const rtems_assoc_t *default_ap = 0; |
---|
24 | |
---|
25 | if (rtems_assoc_is_default(ap)) |
---|
26 | default_ap = ap++; |
---|
27 | |
---|
28 | for ( ; ap->name; ap++) |
---|
29 | if (strcmp(ap->name, name) == 0) |
---|
30 | return ap; |
---|
31 | |
---|
32 | return default_ap; |
---|
33 | } |
---|
34 | |
---|
35 | const rtems_assoc_t * |
---|
36 | rtems_assoc_ptr_by_local( |
---|
37 | const rtems_assoc_t *ap, |
---|
38 | unsigned32 local_value |
---|
39 | ) |
---|
40 | { |
---|
41 | const rtems_assoc_t *default_ap = 0; |
---|
42 | |
---|
43 | if (rtems_assoc_is_default(ap)) |
---|
44 | default_ap = ap++; |
---|
45 | |
---|
46 | for ( ; ap->name; ap++) |
---|
47 | if (ap->local_value == local_value) |
---|
48 | return ap; |
---|
49 | |
---|
50 | return default_ap; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | const rtems_assoc_t * |
---|
55 | rtems_assoc_ptr_by_remote( |
---|
56 | const rtems_assoc_t *ap, |
---|
57 | unsigned32 remote_value |
---|
58 | ) |
---|
59 | { |
---|
60 | const rtems_assoc_t *default_ap = 0; |
---|
61 | |
---|
62 | if (rtems_assoc_is_default(ap)) |
---|
63 | default_ap = ap++; |
---|
64 | |
---|
65 | for ( ; ap->name; ap++) |
---|
66 | if (ap->remote_value == remote_value) |
---|
67 | return ap; |
---|
68 | |
---|
69 | return default_ap; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /* |
---|
74 | * Get values |
---|
75 | */ |
---|
76 | |
---|
77 | unsigned32 |
---|
78 | rtems_assoc_remote_by_local( |
---|
79 | const rtems_assoc_t *ap, |
---|
80 | unsigned32 local_value |
---|
81 | ) |
---|
82 | { |
---|
83 | const rtems_assoc_t *nap; |
---|
84 | nap = rtems_assoc_ptr_by_local(ap, local_value); |
---|
85 | if (nap) |
---|
86 | return nap->remote_value; |
---|
87 | |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|
91 | unsigned32 |
---|
92 | rtems_assoc_local_by_remote( |
---|
93 | const rtems_assoc_t *ap, |
---|
94 | unsigned32 remote_value |
---|
95 | ) |
---|
96 | { |
---|
97 | const rtems_assoc_t *nap; |
---|
98 | nap = rtems_assoc_ptr_by_remote(ap, remote_value); |
---|
99 | if (nap) |
---|
100 | return nap->local_value; |
---|
101 | |
---|
102 | return 0; |
---|
103 | } |
---|
104 | |
---|
105 | unsigned32 |
---|
106 | rtems_assoc_remote_by_name( |
---|
107 | const rtems_assoc_t *ap, |
---|
108 | const char *name |
---|
109 | ) |
---|
110 | { |
---|
111 | const rtems_assoc_t *nap; |
---|
112 | nap = rtems_assoc_ptr_by_name(ap, name); |
---|
113 | if (nap) |
---|
114 | return nap->remote_value; |
---|
115 | |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|
119 | unsigned32 |
---|
120 | rtems_assoc_local_by_name( |
---|
121 | const rtems_assoc_t *ap, |
---|
122 | const char *name |
---|
123 | ) |
---|
124 | { |
---|
125 | const rtems_assoc_t *nap; |
---|
126 | nap = rtems_assoc_ptr_by_name(ap, name); |
---|
127 | if (nap) |
---|
128 | return nap->local_value; |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * what to return if a value is not found |
---|
135 | * this is not reentrant, but it really shouldn't be invoked anyway |
---|
136 | */ |
---|
137 | |
---|
138 | const char * |
---|
139 | rtems_assoc_name_bad( |
---|
140 | unsigned32 bad_value |
---|
141 | ) |
---|
142 | { |
---|
143 | #ifdef RTEMS_DEBUG |
---|
144 | static char bad_buffer[32]; |
---|
145 | |
---|
146 | sprintf(bad_buffer, "< %d [0x%x] >", bad_value, bad_value); |
---|
147 | #else |
---|
148 | static char bad_buffer[32] = "<assoc.c: BAD NAME>"; |
---|
149 | #endif |
---|
150 | return bad_buffer; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | const char * |
---|
155 | rtems_assoc_name_by_local( |
---|
156 | const rtems_assoc_t *ap, |
---|
157 | unsigned32 local_value |
---|
158 | ) |
---|
159 | { |
---|
160 | const 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 | |
---|
168 | const char * |
---|
169 | rtems_assoc_name_by_remote( |
---|
170 | const rtems_assoc_t *ap, |
---|
171 | unsigned32 remote_value |
---|
172 | ) |
---|
173 | { |
---|
174 | const 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 | |
---|
187 | unsigned32 rtems_assoc_remote_by_local_bitfield( |
---|
188 | const 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 | |
---|
203 | unsigned32 rtems_assoc_local_by_remote_bitfield( |
---|
204 | const 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 | |
---|
218 | char * |
---|
219 | rtems_assoc_name_by_remote_bitfield( |
---|
220 | const rtems_assoc_t *ap, |
---|
221 | unsigned32 value, |
---|
222 | char *buffer |
---|
223 | ) |
---|
224 | { |
---|
225 | unsigned32 b; |
---|
226 | |
---|
227 | *buffer = 0; |
---|
228 | |
---|
229 | for (b = 1; b; b <<= 1) |
---|
230 | if (b & value) |
---|
231 | { |
---|
232 | if (*buffer) |
---|
233 | strcat(buffer, " "); |
---|
234 | strcat(buffer, rtems_assoc_name_by_remote(ap, b)); |
---|
235 | } |
---|
236 | |
---|
237 | return buffer; |
---|
238 | } |
---|
239 | |
---|
240 | char * |
---|
241 | rtems_assoc_name_by_local_bitfield( |
---|
242 | const rtems_assoc_t *ap, |
---|
243 | unsigned32 value, |
---|
244 | char *buffer |
---|
245 | ) |
---|
246 | { |
---|
247 | unsigned32 b; |
---|
248 | |
---|
249 | *buffer = 0; |
---|
250 | |
---|
251 | for (b = 1; b; b <<= 1) |
---|
252 | if (b & value) |
---|
253 | { |
---|
254 | if (*buffer) |
---|
255 | strcat(buffer, " "); |
---|
256 | strcat(buffer, rtems_assoc_name_by_local(ap, b)); |
---|
257 | } |
---|
258 | |
---|
259 | return buffer; |
---|
260 | } |
---|