[b06e68ef] | 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 | |
---|
[c64e4ed4] | 17 | const rtems_assoc_t * |
---|
[b06e68ef] | 18 | rtems_assoc_ptr_by_name( |
---|
[c64e4ed4] | 19 | const rtems_assoc_t *ap, |
---|
| 20 | const char *name |
---|
[b06e68ef] | 21 | ) |
---|
| 22 | { |
---|
[c64e4ed4] | 23 | const rtems_assoc_t *default_ap = 0; |
---|
[b06e68ef] | 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 | |
---|
[c64e4ed4] | 35 | const rtems_assoc_t * |
---|
[b06e68ef] | 36 | rtems_assoc_ptr_by_local( |
---|
[c64e4ed4] | 37 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 38 | unsigned32 local_value |
---|
| 39 | ) |
---|
| 40 | { |
---|
[c64e4ed4] | 41 | const rtems_assoc_t *default_ap = 0; |
---|
[b06e68ef] | 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 | |
---|
[c64e4ed4] | 54 | const rtems_assoc_t * |
---|
[b06e68ef] | 55 | rtems_assoc_ptr_by_remote( |
---|
[c64e4ed4] | 56 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 57 | unsigned32 remote_value |
---|
| 58 | ) |
---|
| 59 | { |
---|
[c64e4ed4] | 60 | const rtems_assoc_t *default_ap = 0; |
---|
[b06e68ef] | 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( |
---|
[c64e4ed4] | 79 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 80 | unsigned32 local_value |
---|
| 81 | ) |
---|
| 82 | { |
---|
[c64e4ed4] | 83 | const rtems_assoc_t *nap; |
---|
[b06e68ef] | 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( |
---|
[c64e4ed4] | 93 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 94 | unsigned32 remote_value |
---|
| 95 | ) |
---|
| 96 | { |
---|
[c64e4ed4] | 97 | const rtems_assoc_t *nap; |
---|
[b06e68ef] | 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( |
---|
[c64e4ed4] | 107 | const rtems_assoc_t *ap, |
---|
| 108 | const char *name |
---|
[b06e68ef] | 109 | ) |
---|
| 110 | { |
---|
[c64e4ed4] | 111 | const rtems_assoc_t *nap; |
---|
[b06e68ef] | 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( |
---|
[c64e4ed4] | 121 | const rtems_assoc_t *ap, |
---|
| 122 | const char *name |
---|
[b06e68ef] | 123 | ) |
---|
| 124 | { |
---|
[c64e4ed4] | 125 | const rtems_assoc_t *nap; |
---|
[b06e68ef] | 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 | |
---|
[c64e4ed4] | 138 | const char * |
---|
[b06e68ef] | 139 | rtems_assoc_name_bad( |
---|
| 140 | unsigned32 bad_value |
---|
| 141 | ) |
---|
| 142 | { |
---|
[c64e4ed4] | 143 | #ifdef RTEMS_DEBUG |
---|
[b06e68ef] | 144 | static char bad_buffer[32]; |
---|
| 145 | |
---|
| 146 | sprintf(bad_buffer, "< %d [0x%x] >", bad_value, bad_value); |
---|
[c64e4ed4] | 147 | #else |
---|
| 148 | static char bad_buffer[32] = "<assoc.c: BAD NAME>"; |
---|
| 149 | #endif |
---|
[b06e68ef] | 150 | return bad_buffer; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
[c64e4ed4] | 154 | const char * |
---|
[b06e68ef] | 155 | rtems_assoc_name_by_local( |
---|
[c64e4ed4] | 156 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 157 | unsigned32 local_value |
---|
| 158 | ) |
---|
| 159 | { |
---|
[c64e4ed4] | 160 | const rtems_assoc_t *nap; |
---|
[b06e68ef] | 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 | |
---|
[c64e4ed4] | 168 | const char * |
---|
[b06e68ef] | 169 | rtems_assoc_name_by_remote( |
---|
[c64e4ed4] | 170 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 171 | unsigned32 remote_value |
---|
| 172 | ) |
---|
| 173 | { |
---|
[c64e4ed4] | 174 | const rtems_assoc_t *nap; |
---|
[b06e68ef] | 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( |
---|
[c64e4ed4] | 188 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 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( |
---|
[c64e4ed4] | 204 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 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 | |
---|
[c64e4ed4] | 218 | char * |
---|
| 219 | rtems_assoc_name_by_remote_bitfield( |
---|
| 220 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 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 | |
---|
[c64e4ed4] | 240 | char * |
---|
| 241 | rtems_assoc_name_by_local_bitfield( |
---|
| 242 | const rtems_assoc_t *ap, |
---|
[b06e68ef] | 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 | } |
---|