source: rtems/cpukit/libcsupport/src/assoc.c @ 3ba74c73

4.104.114.84.95
Last change on this file since 3ba74c73 was 3ba74c73, checked in by Joel Sherrill <joel.sherrill@…>, on 11/01/00 at 21:08:14

2000-11-01 Joel Sherrill <joel@…>

  • include/Makefile.am, include/rtems/libio_.h, libc/Makefile.am, libc/assoc.c, libc/assocnamebad.c, libc/base_fs.c, libc/cfsetispeed.c, libc/cfsetospeed.c, libc/chdir.c, libc/chmod.c, libc/chown.c, libc/close.c, libc/closedir.c, libc/dup2.c, libc/error.c, libc/eval.c, libc/fchdir.c, libc/fchmod.c, libc/fcntl.c, libc/fdatasync.c, libc/fpathconf.c, libc/fstat.c, libc/fsync.c, libc/ftruncate.c, libc/getdents.c, libc/ioctl.c, libc/libio.c, libc/libio_sockets.c, libc/link.c, libc/lseek.c, libc/malloc.c, libc/mallocfreespace.c, libc/mknod.c, libc/mount.c, libc/newlibc.c, libc/no_libc.c, libc/open.c, libc/read.c, libc/readlink.c, libc/rmdir.c, libc/stat.c, libc/symlink.c, libc/tcsetattr.c, libc/telldir.c, libc/ttyname.c, libc/ttyname_r.c, libc/umask.c, libc/unlink.c, libc/unmount.c, libc/utime.c, libc/write.c: assoc.h, error.h, libio_.h, libio.h, and libcsupport.h moved from libc to lib/include/rtems and now must be referenced as <rtems/XXX.h>.
  • include/rtems/Makefile.am, include/rtems/.cvsignore: New file.
  • include/rtems/assoc.h, include/rtems/error.h, include/rtems/libcsupport.h, include/rtems/libio.h, include/rtems/libio_.h: New/moved files.
  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * assoc.c
3 *      rtems assoc routines
4 *
5 *  $Id$
6 */
7
8#include <rtems.h>
9#include <rtems/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
16const rtems_assoc_t *
17rtems_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
34const rtems_assoc_t *
35rtems_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
53const rtems_assoc_t *
54rtems_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
76unsigned32
77rtems_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
90unsigned32
91rtems_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
104unsigned32
105rtems_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
118unsigned32
119rtems_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
137const char *rtems_assoc_name_bad(
138    unsigned32 bad_value
139);
140
141/* body in separate file to reduce dependency on printf */
142
143
144const char *
145rtems_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
158const char *
159rtems_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
177unsigned32 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
193unsigned32 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
208char *
209rtems_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
230char *
231rtems_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}
Note: See TracBrowser for help on using the repository browser.