home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / LSEARCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.7 KB  |  151 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - lsearch.c
  3.  *
  4.  * function(s)
  5.  *        _lsearch - searches a table
  6.  *        lsearch  - searches and updates a table
  7.  *      lfind       - perform a linear search
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*[]------------------------------------------------------------[]*/
  11. /*|                                                              |*/
  12. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  13. /*|                                                              |*/
  14. /*|                                                              |*/
  15. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  16. /*|     All Rights Reserved.                                     |*/
  17. /*|                                                              |*/
  18. /*[]------------------------------------------------------------[]*/
  19.  
  20. #include <stdlib.h>
  21. #include <mem.h>
  22. #include <stddef.h>
  23.  
  24. /*---------------------------------------------------------------------*
  25.  
  26. Name        _lsearch - searches a table
  27.  
  28. Usage        static void *near
  29.         pascal _lsearch(const void    *key,
  30.                 void        *base,
  31.                 size_t        *nelem,
  32.                 size_t        width,
  33.                 int cdecl     (*fcmp)(const void *, const void *),
  34.                 int        flag)
  35.  
  36. Description    performs lfind or lsearch depending on the value of flag.
  37.         If flag is 1 it updates the table if no match, if flag
  38.         is 0 it only searches.
  39.  
  40. Return value    Nothing
  41.  
  42. *---------------------------------------------------------------------*/
  43. static void *near pascal _lsearch(const void    *key,
  44.                   register void *base,
  45.                   size_t        *nelem,
  46.                   size_t        width,
  47.                   int cdecl (*fcmp)(const void *, const void *),
  48.                   int            flag)
  49. {
  50.     register int    Wrk;
  51.  
  52.     for (Wrk = *nelem; Wrk > 0; Wrk--)
  53.     {
  54.         if (((*fcmp)(key, base)) == 0)
  55.             return(base);
  56.         ((char *)base) += width;
  57.     }
  58.     if (flag)
  59.     {
  60.         (*nelem)++;
  61.         movmem((void *)key, base, width);
  62.     }
  63.     else
  64.         base = NULL;
  65.     return(base);
  66. }
  67.  
  68.  
  69. /*--------------------------------------------------------------------------*
  70.  
  71. Name        lsearch - searches and updates a table
  72.         lfind    - perform a linear search
  73.  
  74. Usage        void *lsearch(const void *key,
  75.                   void *base,
  76.                   size_t *nelem,
  77.                   size_t width,
  78.                   int cdecl (*fcmp)(const void *, const void *));
  79.  
  80.         void *lfind(const void *key,
  81.                 const void *base,
  82.                 size_t *nelem,
  83.                 size_t width,
  84.                 int cdecl (*fcmp)(const void *, const void *));
  85.  
  86. Prototype in    stdlib.h
  87.  
  88. Description    lfind and lsearch search  a table for information. Because
  89.            these are linear searches, the table entries do not need to
  90.         be sorted before a call to lfind or lsearch. If the item that
  91.         key  points  to  is  not  in the table, lsearch appends that
  92.         item to the table, but lfind does not.
  93.  
  94.         . key points to  the item to be searched  for. ("the search
  95.           key")
  96.  
  97.         . base points to the base (0th element) of the search table
  98.  
  99.         . nelem points    to an integer containing  number of entries
  100.           in the table.
  101.  
  102.         . width contains the number of bytes in each entry
  103.  
  104.         . fcmp    points to  a user-written  comparison routine. That
  105.         routine compares 2  items and returns a value  based on the
  106.         comparison.
  107.         On each call to the comparison routine, the search function
  108.         passes 2 arguments: key, a pointer to the item being searched
  109.         for;  and elem,  a pointer   to the  element of  base being
  110.         compared.
  111.         fcmp  is free  to interpret  the search  key and  the table
  112.         entry any way it likes.
  113.  
  114. Return value    lsearch and lfind return the  address of the first entry in
  115.         the  table that  matches the   search key.  If no  match is
  116.         found, lfind returns 0.
  117.  
  118.         In lsearch and lfind:
  119.  
  120.             If the key is            fcmp returns
  121.             -------------            ------------
  122.             Not identical to *elem        An integer != 0
  123.             Identical to *elem        0
  124.  
  125. *---------------------------------------------------------------------------*/
  126. void *lsearch(const void *key, void *base, size_t *nelem, size_t width,
  127.               int cdecl (*fcmp)(const void *, const void *))
  128. {
  129.     return(_lsearch(key,base,nelem,width,fcmp,1));
  130. }
  131.  
  132.  
  133. /*---------------------------------------------------------------------*
  134.  
  135. Name        lfind - performs a linear search
  136.  
  137. Usage        void *lfind(void *key, void *base, size_t *nelem,
  138.                 size_t width,
  139.                             int (*fcmp)(const void *, const void *) );
  140.  
  141. Prototype in    stdlib.h
  142.  
  143. Description    see lsearch above
  144.  
  145. *---------------------------------------------------------------------*/
  146. void *lfind(const void *key, const void *base, size_t *nelem, size_t width,
  147.         int cdecl (*fcmp)(const void *, const void *))
  148. {
  149.     return(_lsearch(key,(void *)base,nelem,width,fcmp,0));
  150. }
  151.