OSDN Git Service

* fhandler_process.cc: Drop unneeded includes.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / lsearch.cc
1 /* Initial implementation:
2    Copyright (c) 2002 Robert Drehmel
3    All rights reserved.
4
5    As long as the above copyright statement and this notice remain
6    unchanged, you can do what ever you want with this file.  */
7
8 #include <stdint.h>     /* for uint8_t */
9 #include <string.h>     /* for memcpy () prototype */
10
11 static void *lwork (const void *, const void *, size_t *, size_t,
12                     int (*) (const void *, const void *), int);
13
14 extern "C" void *
15 lsearch (const void *key, void *base, size_t *nelp, size_t width,
16                int (*compar) (const void *, const void *))
17 {
18   return lwork (key, base, nelp, width, compar, 1);
19 }
20
21 extern "C" void *
22 lfind (const void *key, const void *base, size_t *nelp, size_t width,
23              int (*compar) (const void *, const void *))
24 {
25   return lwork (key, base, nelp, width, compar, 0);
26 }
27
28 static void *
29 lwork (const void *key, const void *base, size_t *nelp, size_t width,
30        int (*compar) (const void *, const void *), int addelem)
31 {
32   uint8_t *ep, *endp;
33
34   /*  Cast to an integer value first to avoid the warning for removing
35      'const' via a cast.  */
36   ep = (uint8_t *) (uintptr_t)base;
37   for (endp = (uint8_t *) (ep + width * *nelp); ep < endp; ep += width)
38     if (compar (key, ep) == 0)
39       return ep;
40
41   /* lfind () shall return when the key was not found. */
42   if (!addelem)
43     return NULL;
44
45   /* lsearch () adds the key to the end of the table and increments
46      the number of elements.  */
47   memcpy (endp, key, width);
48   ++*nelp;
49
50   return endp;
51 }