home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / database / bplus / ndb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-22  |  1.9 KB  |  64 lines

  1. /*****************************************************************
  2.  |  ndb - build Name DataBase
  3.  |----------------------------------------------------------------
  4.  |  takes data from a raw listing of names, phone numbers, room
  5.  |  numbers, etc and builds an index by name. Name is in upper
  6.  |  case, in the first 26 columns. The index points into the
  7.  |  original text file.
  8.  ****************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include "bplus.h"
  12. #define EOS     ((unsigned char) 0)
  13.  
  14. IX_DESC namesfile;
  15. ENTRY index;
  16. FILE *namedata, *fopen();
  17.  
  18. main() {
  19.     int status, n, nnames;
  20.     int names_count = 0, keys_count = 0;
  21.     long ftell();
  22.     char namebuf[132], names[6][30];
  23.  
  24.     status = make_index("phdata.idx", &namesfile, 1);
  25.     if (status != IX_OK) {
  26.         printf("Can't create 'phdata.idx' file\n");
  27.         exit(1);
  28.     }
  29.     namedata = fopen("phdata", "r");
  30.     if (namedata == NULL) {
  31.         printf("Can't find 'phdata' file\n");
  32.         exit(1);
  33.     }
  34.  
  35.     /* read loop */
  36.     while (index.recptr = ftell(namedata),
  37.         fgets(namebuf, 132, namedata) != NULL)
  38.     {        
  39.         namebuf[26] = EOS;
  40.         ++names_count;
  41.         nnames = sscanf(namebuf, "%s%s%s%s%s%s",
  42.             names[0], names[1], names[2],
  43.             names[3], names[4], names[5]);
  44.         for (n=0; n<nnames; ++n) {
  45.             if (strlen(names[n]) < 2) continue;
  46.             ++keys_count;
  47.             clearkey(&index);
  48.             strcpy(index.key, names[n]);
  49.             status = add_key(&index, &namesfile);
  50.             if (status != IX_OK) {
  51.                 printf("Bad status on add of key '%s'\n", names[n]);
  52.                 close_index(&namesfile);
  53.                 exit(1);
  54.             }
  55.         }
  56.     }
  57.  
  58.     /* wrapup */
  59.     printf("Build complete, %d names, %d keys\n",
  60.         names_count, keys_count);
  61.     close_index(&namesfile);
  62.     fclose(namedata);
  63. }
  64.