home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / bsearch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  1.9 KB  |  91 lines  |  [TEXT/KAHL]

  1. /*
  2. *********************************************************************
  3. *    
  4. *    bsearch.c
  5. *    Disked-based binary search in index file on CD
  6. *        
  7. *    Rainer Fuchs
  8. *    EMBL Data Library
  9. *    Postfach 10.2209
  10. *    D-6900 Heidelberg, FRG
  11. *    E-mail: fuchs@embl-heidelberg.de
  12. *
  13. *    Copyright © 1992 EMBL Data Library
  14. *        
  15. **********************************************************************
  16. *    
  17. */ 
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #include "EMBL-Search.h"
  23. #include "EMBL-Search.rsrc.h"
  24.  
  25. /*
  26. ******************************* Prototypes ***************************
  27. */
  28.  
  29. #include "bsearch.h"
  30. #include "util.h"
  31.  
  32.  
  33. /*
  34. ******************************** Global variables ********************
  35. */
  36.  
  37. extern char gError[256];
  38.  
  39.  
  40. /**************************************
  41. *    General binary search routine
  42. *    This routine looks for "key" in file "fName" (assuming this file is in the index
  43. *    directory). Record size is specified by "size" and "nrec" gives the number of
  44. *    records. Comparison is done by "*compare".
  45. *    Return value:    TRUE, if key found
  46. *                    FALSE, if not 
  47. *    Side-effect:     *rec contains the hit record and *hitrec the record number
  48. */
  49.  
  50. Boolean CDIndex_BSearch(void *key,StringPtr fName, short fd,
  51.                         u_long size, u_long nrec,
  52.                         short (*compare)(void *,void *), void *rec,u_long *hitrec)
  53. {
  54.     register long    max;
  55.     register long    min;
  56.     register long    pos;
  57.     long            count;
  58.     OSErr            err;
  59.     short            comp;
  60.         
  61.     max = (long)nrec - 1;
  62.     min = 0L;
  63.     comp = 0;
  64.     
  65.     /* binary search */
  66.     
  67.     do {
  68.         RotateWaitCursor();
  69.         pos=(min+max)/2;
  70.         count=size;
  71.         if( (err=SetFPos(fd,fsFromStart,pos*size+sizeof(Header))) != noErr) {
  72.             sprintf(gError,LoadErrorStr(ERR_READFILE,FALSE),
  73.                         PtoCstr(fName),err );
  74.             CtoPstr((char *)fName);
  75.             return(ErrorMsg(0));
  76.         }
  77.  
  78.         if( ReadMacFile(fd,&count,rec,fName, TRUE) ) {
  79.             return(FALSE);
  80.         }
  81.         
  82.         if( (comp = (*compare)(key,rec)) < 0)
  83.             max=pos-1;
  84.         else
  85.             min=pos+1;
  86.     } while(comp && min <= max);
  87.     
  88.     *hitrec = (u_long)pos;
  89.     return(!comp);
  90. }
  91.