home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / lfind.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  62 lines

  1. /***
  2. *lfind.c - do a linear search
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _lfind() - do a linear search of an array.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <search.h>
  13. #include <stddef.h>
  14.  
  15. /***
  16. *char *_lfind(key, base, num, width, compare) - do a linear search
  17. *
  18. *Purpose:
  19. *       Performs a linear search on the array, looking for the value key
  20. *       in an array of num elements of width bytes in size.  Returns
  21. *       a pointer to the array value if found, NULL if not found.
  22. *
  23. *Entry:
  24. *       char *key - key to search for
  25. *       char *base - base of array to search
  26. *       unsigned *num - number of elements in array
  27. *       int width - number of bytes in each array element
  28. *       int (*compare)() - pointer to function that compares two
  29. *               array values, returning 0 if they are equal and non-0
  30. *               if they are different.  Two pointers to array elements
  31. *               are passed to this function.
  32. *
  33. *Exit:
  34. *       if key found:
  35. *               returns pointer to array element
  36. *       if key not found:
  37. *               returns NULL
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43. void * __cdecl _lfind (
  44.         REG2 const void *key,
  45.         REG1 const void *base,
  46.         REG3 unsigned int *num,
  47.         unsigned int width,
  48.         int (__cdecl *compare)(const void *, const void *)
  49.         )
  50. {
  51.         unsigned int place = 0;
  52.         while (place < *num )
  53.                 if (!(*compare)(key,base))
  54.                         return( (void *)base );
  55.                 else
  56.                 {
  57.                         base = (char *)base + width;
  58.                         place++;
  59.                 }
  60.         return( NULL );
  61. }
  62.