home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c023 / 1.img / PROGRAMS / CBTRSEQ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-09  |  3.8 KB  |  146 lines

  1. /* (C) Copyright 1984,85,86,87 Walter L. Peacock   All Rights Reserved   */
  2.  
  3. /* THIS PROGRAM BELONGS TO WALTER L. PEACOCK.  IT IS CONSIDERED A TRADE */
  4. /* SECRET AND IS NOT TO BE DIVULGED OR USED BY PARTIES WHO HAVE NOT   */
  5. /* RECEIVED WRITTEN AUTHORIZATION FROM THE OWNER.                  */
  6.  
  7. /* 01/30/85  C B T R S E Q . C   Process B+tree sequentially.         */
  8.  
  9. #include <stdio.h>
  10. #include "cbtree.h"
  11. /* btfio.h *must* be included when bt_open() is used */
  12. #include "btfio.h"
  13. #include "patrec.str"
  14.  
  15. #if AmigaDOS & LC
  16. #define printf iprintf
  17. #endif
  18.  
  19. #define MAXLINES 21
  20.  
  21. #if CI | DC
  22. #else
  23. #include <ctype.h>
  24. #endif
  25.  
  26. void main(argc, argv)
  27. int   argc;
  28. char *argv[];
  29. {
  30.    extern long getroot();
  31.    extern char *calloc();
  32.    extern void free();
  33.    BTC       btcommo;
  34.    BTIDXBLK *idxblk;
  35.    PATREC    pr;
  36.    long moreidx, rootptr;
  37.    int  fdpat, fdnme, varlen;
  38.    char *nam_btdat, *nam_btnme;
  39.    char dummy[14];
  40.    register int j, m;
  41.  
  42.    wopen("CON:0/0/640/200/Cbtrseq");
  43.  
  44.    varlen = (argc > 1 && strcmp(argv[1], "-v") == 0);
  45.  
  46.    if (varlen)
  47.    {
  48.       nam_btdat = "patient.var";
  49.       nam_btnme = "btvarnme";
  50.    }
  51.    else
  52.    {
  53.       nam_btdat = "patient.dat";
  54.       nam_btnme = "btpatnme";
  55.    }
  56.  
  57.    if (btrinit(nam_btnme, &btcommo) == ERR)
  58.       ckerror(- CK_BTRIN, "cbtrseq.c");
  59.  
  60.    /* open .idx file   */
  61.    if( (fdnme = bt_open(btcommo.idxname, O_RDWR) ) == ERR)
  62.       ckerror(- CK_OPEN, "cbtrseq: btpatnme");
  63.  
  64.    /* open patient.dat file   */
  65.    if( (fdpat = bt_open(nam_btdat, O_RDWR) ) == ERR)
  66.       ckerror(- CK_OPEN, "cbtrseq: patient.dat");
  67.  
  68.    /* CLIMB DOWN THE TREE TO THE LEAF NODE */
  69.  
  70.    /* make buffer for index node */
  71.    if( !(idxblk = (BTIDXBLK *)calloc(
  72.          sizeof(BTIDXBLK) + btcommo.btcells * SZBTIDX, SZCHAR) ))
  73.       ckerror(- CK_NOMEM, "cbtrseq: idxblk");
  74.  
  75.    idxblk->blkalloc = 0;
  76.  
  77.    /* read up the root node into the buffer */
  78.    rootptr = getroot(fdnme, btcommo.findroot);  /* get ptr to idx root node */
  79.    
  80.    getidxr(fdnme, rootptr, idxblk, btcommo.btidxlen, btcommo.btkeylen);
  81.  
  82.    /* keep reading the first child node until we hit a LEAF node */
  83.    while(idxblk->blktype == 0)     /* until data block type = 1 located  */
  84.       getidxr(fdnme, idxblk->btpage[0].btptr, idxblk, btcommo.btidxlen,
  85.             btcommo.btkeylen);
  86.  
  87.    scr_clr();
  88.  
  89.    j = 0;
  90.    moreidx = 1;
  91.  
  92.    /* traverse the BOTTOM level of the tree SIDEWAYS and pick up all */
  93.    /* record locations in order */
  94.    while(moreidx){
  95.  
  96.       /* first do all the cells in the current node */
  97.       for(m = 0; m < idxblk->cellicnt; m++){
  98.  
  99.          if (idxblk->btpage[m].btptr == 0L)
  100.             break;
  101.  
  102.          if (varlen)
  103.          {
  104.             btcommo.btloc = idxblk->btpage[m].btptr;
  105.             getfpatv(&pr, fdpat, &btcommo);
  106.          }
  107.          else
  108.             getfpat(&pr, fdpat, (long) idxblk->btpage[m].btptr);
  109.  
  110.          /* process records retrieved   */
  111.          scr_curs(j, 1);
  112.          printf("%s, %s %s",pr.lname, pr.fname, pr.minit);
  113.          scr_curs(j, 27);
  114.          printf("%-9.9s %-12.12s %-.30s", pr.ssn, pr.hphone, pr.addr);
  115.  
  116.          if(++j > MAXLINES)
  117.          {
  118.             printf("\nPress <CR> to continue...");
  119.             gets(dummy);
  120.             scr_clr();
  121.             j = 0;
  122.          }
  123.       }
  124.  
  125.       /* then move sideways to the next node if there is one */
  126.       if (idxblk->fwdpage > 1)
  127.       {
  128.          /* read the brother node */
  129.          getidxr(fdnme, idxblk->fwdpage, idxblk, btcommo.btidxlen, btcommo.btkeylen);
  130.          if(idxblk->fwdpage < 1)
  131.             idxblk->cellicnt--;         /* don't print null cell   */
  132.       }
  133.       else
  134.          moreidx = 0;
  135.    }
  136.  
  137.    freekeys(idxblk);
  138.    FREE(idxblk);
  139.  
  140.    close(fdnme);
  141.    close(fdpat);
  142.  
  143.    btrterm(&btcommo);
  144.    wclose();   /* close Amiga window */
  145. }
  146.