home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LOOKUP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  86 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  LOOKUP.C
  5. **
  6. **  This is the lookup function for reading a binary file index to an ASCII
  7. **  file formatted as follows:
  8. **
  9. **  Mark Corgan
  10. **  550 Foothill Rd.
  11. **  Gardnerville, NV 89410
  12. **  (702) 265-2388
  13. **  .
  14. **  Hello World
  15. **  123 Anywhere St.
  16. **  Anytown, CA 12345
  17. **  (123) 456-7890
  18. **  .
  19. **  etc...
  20. **  
  21. **  The period is what  LOOKUP.C looks for to indicate the end
  22. **  of record. Of course, you could have any format you like, so long as the
  23. **  first line is the information you are looking for. Also, there is no
  24. **  limit to the number of lines of infomation after the first line and
  25. **  before the period as fputs() continues until the period. Enjoy!
  26. **
  27. **  by Mark Corgan, 09-May-1993, and donated to the public domain
  28. */
  29.  
  30. #include <stdio.h>
  31. #include "errors.h"
  32. #include "indxlook.h"
  33.  
  34. long bsearch_(FILE *ifp, long first, long last, char *target);
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38.       FILE *afp, *ifp;
  39.       char line[MAX_LINE];
  40.       INDEX header;
  41.       long pos;
  42.  
  43.       if (argc != 3)
  44.       {
  45.             puts("Usage: LOOKUP text_file_name index_file_name\n");
  46.             puts("Note: The text file must consist of a number of records "
  47.                  "separated by lines");
  48.             puts("      containing a single period (\".\")");
  49.             return EXIT_FAILURE;
  50.       }
  51.       afp = cant(argv[1], "r");
  52.       ifp = cant(argv[2], "rb");
  53.       if (fread((char *) &header, sizeof(INDEX), 1, ifp) == 0)
  54.             fprintf(stderr, "Can't read header of \"%s\"\n", argv[2]);
  55.       else  while(printf("Name? "), fgets(line, sizeof(line), stdin))
  56.       {
  57.             if ((pos = bsearch_(ifp, 1L, header.pos, line)) == -1)
  58.                   printf("Couldn't find: %s\n", line);
  59.             else if (fseek(afp, pos, 0) != 0)
  60.                   fprintf(stderr, "Can't read record at position %ld\n", pos);
  61.             else while(fgets(line,sizeof(line),afp) && strcmp(line,END_REC)!=0)
  62.                   fputs(line, stdout);
  63.       }
  64.       fclose(ifp);
  65.       fclose(afp);
  66.  
  67.       exit(0);
  68. }
  69.  
  70. long bsearch_(FILE *ifp, long first, long last, char *target)
  71. {
  72.       long pos, mid =(first + last) / 2;
  73.       INDEX next;
  74.       int cmp;
  75.  
  76.       if (mid < first || fseek(ifp, mid * sizeof(INDEX), 0) != 0 ||
  77.           fread((char *) &next, sizeof(INDEX), 1, ifp) == 0)
  78.       {
  79.             pos = -1;
  80.       }
  81.       else  pos = ((cmp = strncmp(target, next.key, MAX_KEY)) == 0) ?
  82.                   next.pos : ((cmp < 0) ? bsearch_(ifp, first, mid - 1, target)
  83.                                         : bsearch_(ifp, mid + 1, last, target));
  84.       return pos;
  85. }
  86.