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