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

  1. /* Copyright (C)  Walter L. Peacock 1984-1988.  All rights reserved */
  2. /*  U S E R B T R . C   7/26/84                           */
  3.  
  4. /* userbtr:  7/26/84  user application program that uses cbtree calls   */
  5.  
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  7. /*  This program illustrates how to invoke CBTREE from an application   */
  8. /*  program.  The program stores patient data in a data file and provides*/
  9. /*  online access by last name concatenated with first name and middle   */
  10. /*  initial, or access by the patient's SSN.  Two CBTREE index files are */
  11. /*  maintained by the program:  btpatnme is the btree for the name index */
  12. /*  and btpatssn is the btree for the SSN index.  The btrees are stored  */
  13.  
  14. /*  patient data is stored in patient.dat.                              */
  15. /*                                                                  */
  16. /*  The search is invoked by keying the patient's name in the designated */
  17. /*  fields on the screen or by keying in the patient's SSN in the   */
  18. /*  Last Name field.  A numeric character as the first character in the  */
  19. /*  last name search field indicates to the program that the search      */
  20. /*  key is the SSN and the program automatically invokes the proper      */
  21. /*  btree.                                                               */
  22. /*                                                                  */
  23. /*  The calltype is entered in the space provided and should be 1 - 18,  */
  24. /*  or 99 to exit.  If no value is entered for calltype then the         */
  25. /*  default type 2 (GETNXT) will be used by the program.  This feature   */
  26. /*  allows the user to step through the records in key sequence.         */
  27. /*                                                                  */
  28. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  29.  
  30. #include <stdio.h>
  31. #include "cbtree.h"
  32. /* btfio.h *must* be included when bt_open() is used */
  33. #include "btfio.h"
  34. #include "patrec.str"
  35.  
  36. #define SINGLE 1   /* both trees in a SINGLE file */
  37.  
  38. #if CI | DC
  39. #else
  40. #include <ctype.h>
  41. #endif
  42.  
  43. #if EC | DC
  44. extern void free_gtkey();
  45. #endif
  46.  
  47. #if AmigaDOS & LC
  48. #define printf iprintf      /* use fast integer printf routine */
  49. #endif
  50.  
  51. struct btcommo btnme;   /* one for each btree key to be accessed */
  52. struct btcommo btssn;
  53.  
  54. int svoptype;
  55.  
  56. char srchtree = 'N';   /* tracks tree to search on GETNXT & GETPRV */
  57.  
  58. char *srchkey;
  59.  
  60. void main(argc, argv)
  61. int   argc;
  62. char *argv[];
  63. {
  64.    extern long atol(), getnewloc();
  65.    extern char *strcpy(), *strnncpy(), *calloc();
  66.    extern uword patvar();
  67.    extern int   open(), selrec(), close();
  68.    extern void displrtn(), displerr(), free(), input();
  69.    PATREC  pr;
  70.    char    varlenpr[sizeof(PATREC)];
  71.    char    dummy[7], *nam_btdat, *nam_btnme, *nam_btssn;
  72.    int     fdnme, fdssn, fddat;
  73.    int     btretcd, calltype, varlen;
  74.  
  75.    varlen = (argc > 1 && strcmp(argv[1], "-v") == 0);
  76.  
  77.    if (varlen)
  78.    {
  79.       nam_btdat = "patient.var";  /* run BLD2KEYS.EXE */
  80.       nam_btnme = "btvarnme";    /* set B+tree names for var len recs */
  81.       nam_btssn = "btvarssn";
  82.    }
  83.    else
  84.    {
  85.       nam_btdat = "patient.dat"; /* run BLD2KEYS.EXE -v */
  86.       nam_btnme = "btpatnme";    /* fixed length records */
  87.       nam_btssn = "btpatssn";
  88.    }
  89.  
  90.    wopen("CON:0/0/640/200/Userbtr");   /* Open Amiga Window */
  91.  
  92. #if EC
  93. #else
  94.    scr_clr();   /* clear screen   */
  95.  
  96. printf("\nThis program illustrates how to invoke CBTREE from an application  ");
  97. printf("\nprogram.  The program stores patient data in a data file and provides");
  98. printf("\nonline access by last name concatenated with first name and middle ");
  99. printf("\ninitial, or access by the patient's SSN. Two CBTREE index files are");
  100. printf("\nmaintained by the program: btpatnme is the btree for the name index");
  101. printf("\nand btpatssn is the btree for the SSN index.  The btrees are stored");
  102. printf("\nin the disk files  btpatnme.idx and btpatssn.idx respectively.  The");
  103. printf("\npatient data is stored in patient.dat.                             ");
  104. printf("\n\nThe search is invoked by keying the patient's name in the designated");
  105. printf("\nfields on the screen or by keying in the patient's SSN in the      ");
  106. printf("\nLast Name field.  A numeric character as the first character in the");
  107. printf("\nlast name search field indicates to the program that the search    ");
  108. printf("\nkey is the SSN and the program automatically invokes the proper    ");
  109. printf("\nbtree.                                                             ");
  110. printf("\n\nThe calltype is entered in the space provided and should be one of");
  111. printf("\nthe selections listed on the screen.  The default opertaion is     ");
  112. printf("\nGETNXT, which allows you to step through the records in key        ");
  113. printf("\nsequence.  To exit from the system, type 'EXIT' in the type field. ");
  114. printf("\n\n          Press <RETURN> to continue... ");
  115.  
  116. gets(dummy);
  117. #endif
  118.  
  119. scr_clr();   /* clear screen   */
  120.  
  121. printf("\nCBTREE OP   DESCRIPTION   \n");
  122. printf("\n  GETFRST   Get the first key in the B-tree.");
  123. printf("\n  GETLAST   Get the last key in the B-tree.");
  124. printf("\n  GETPRV    Get the previous record from current position.");
  125. printf("\n  GETNXT    Get the next record from current position.");
  126. printf("\n  GETSEQ    Get blocks of records sequentially (up to btmax).");
  127. printf("\n  GETREC    Get the first record with exact key match.");
  128. printf("\n  GETPAR    Get the first record matching the partial key.");
  129. printf("\n  GETALL    Get all records that match the key (up to btmax).");
  130. printf("\n  GETKEYS   Get a list of keys and their record numbers.");
  131. printf("\n  GETLT     Get first key less than given key.");
  132. printf("\n  GETLE     Get first key less than or equal to given key.");
  133. printf("\n  GETGT     Get first key greater than given key.");
  134. printf("\n  GETGE     Get first key greater than or equal to given key.");
  135. printf("\n  INSERT    Insert a new key and data record.");
  136. printf("\n  ISRTKY    Insert a key but not the data record.");
  137. printf("\n  DELETE    Delete a key and data record. ");
  138. printf("\n  DELTKY    Delete a key but not the data record.");
  139. printf("\n  NEWLOC    Change the record location only of a given key.");
  140. printf("\n\n          Press <RETURN> to continue... ");
  141. gets(dummy);
  142.  
  143.    if( (btrinit(nam_btnme, &btnme) ) == ERR)
  144.       ckerror(- CK_BTRIN, "userbtr: btnme");
  145.  
  146.    if( (btrinit(nam_btssn, &btssn) ) == ERR)
  147.       ckerror(- CK_BTRIN, "userbtr: btssn");
  148.  
  149.    /* open PRIMARY INDEX FILE  */
  150.    if( (fdnme = bt_open(btnme.idxname,O_RDWR) ) == ERR)
  151.    {
  152.       puts("\n\n***  CAN'T OPEN INDEX FILE,  RUN BLD2KEYS.EXE.  ***\n");
  153.       ckerror(- CK_OPEN, "userbtr: btnme.idx");
  154.    }
  155.  
  156.    /* open SECONDARY INDEX FILE */
  157.    if (strcmp(btssn.idxname, btnme.idxname) == 0)
  158.       fdssn = fdnme;   /* same file */
  159.    else if( (fdssn = bt_open(btssn.idxname,O_RDWR) ) == ERR)
  160.    {
  161.       puts("Can't open btpatssn.idx.  Run bld2keys.");
  162.       ckerror(- CK_OPEN, "userbtr: btssn.idx");
  163.    }
  164.  
  165.    /* open DATA FILE */
  166.    if( (fddat = bt_open(nam_btdat, O_RDWR) ) == ERR)
  167.       ckerror(- CK_OPEN, "userbtr: patient.dat");
  168.  
  169.    if( !(srchkey = calloc(btnme.btkeylen + 1, 1) ))
  170.       ckerror(- CK_NOMEM, "userbtr: srchkey");
  171.  
  172.     svoptype = 0;
  173.  
  174.    /*   M A I N   C O N T R O L   P R O G R A M   */
  175.  
  176.    /* selrec: user routine to build search key   */
  177.  
  178.    while ( (calltype = selrec(&pr) ) != 99){
  179.  
  180.       /* check for previous block retrieval calls */
  181.       if (svoptype == GETALL || svoptype == GETKEYS || svoptype == GETPAR){
  182.          if (svoptype != calltype){
  183.             if (svoptype == GETKEYS){
  184.                if (srchtree == 'N')
  185.                     free_gtkey(&btnme);   /* free get key array */
  186.                else
  187.                     free_gtkey(&btssn);   /* free get key array */
  188.             }
  189.             else{
  190.                if (srchtree == 'N')
  191.                     free_svkey(&btnme);   /* in case this isn't the first pass*/
  192.                else
  193.                     free_svkey(&btssn);   /* free get key array */
  194.             }
  195.          }
  196.       }
  197.  
  198.       /* btxxxxxx.btloc will contain record location returned from btree  */
  199.  
  200.       if ( isalpha(*pr.lname) )
  201.       {   /* Name search */
  202.          srchtree = 'N';
  203.  
  204.          bldkey(srchkey, btnme.btkeylen, &pr); /* build concatenated key */
  205.          strcpy(btnme.btkey, srchkey);
  206.  
  207.          if (calltype == DELETE)            /* first GET the record */
  208.          {
  209.             btnme.btoptype = GETREC;      /* type of cbtree call   */
  210.             btnme.btloc = 0L;                  /* no continuation */
  211.  
  212.             /* issue cbtree call */
  213.             if( (btretcd = cbtree(fddat, fdnme, &btnme) ) != BTCALLOK)
  214.             {
  215.                displerr(btretcd, &btnme);
  216.                continue;
  217.             }
  218.  
  219.             /* read record into structure */
  220.             if (varlen)
  221.                getfpatv(&pr, fddat, &btnme);
  222.             else
  223.                getfpat(&pr, fddat, btnme.btloc);
  224.          }
  225.  
  226.          btnme.btoptype = calltype;      /* type of cbtree call   */
  227.  
  228.          if (calltype == GETALL || calltype == GETKEYS || calltype == GETPAR)
  229.             btnme.btloc = 0L;                  /* no continuation */
  230.          else if (calltype == NEWLOC)
  231.             *btnme.btrecnum = getnewloc(btnme.btloc); /* prompt user */
  232.  
  233.          if (calltype == INSERT)
  234.          {
  235.             input(&pr);
  236.             if (varlen)
  237.                btnme.btdtalen = patvar(varlenpr, &pr);  /* reckon rec len */
  238.          }
  239.  
  240.          /* issue cbtree call */
  241.          if( (btretcd = cbtree(fddat, fdnme, &btnme) ) == BTCALLOK)
  242.             displrtn(fddat, &btnme, fdnme, fdssn, &pr, varlenpr);
  243.          else
  244.             displerr(btretcd, &btnme);
  245.       }
  246.  
  247.       /***   SSN search   ***/
  248.       else if ( isdigit(*pr.lname) )
  249.       {
  250.          srchtree = 'S';
  251.  
  252.          strcpy(srchkey, pr.lname);         /* contains SSN ! */
  253.          strcpy(btssn.btkey, pr.lname);
  254.  
  255.          if (calltype == DELETE)            /* first GET the record */
  256.          {
  257.             btssn.btoptype = GETREC;      /* type of cbtree call   */
  258.             btssn.btloc = 0L;                  /* no continuation */
  259.  
  260.             /* issue cbtree call */
  261.             if( (btretcd = cbtree(fddat, fdssn, &btssn) ) != BTCALLOK)
  262.             {
  263.                displerr(btretcd, &btssn);
  264.                continue;
  265.             }
  266.  
  267.             /* read record into structure */
  268.             if (varlen)
  269.                getfpatv(&pr, fddat, &btssn);
  270.             else
  271.                getfpat(&pr, fddat, btssn.btloc);
  272.          }
  273.  
  274.          btssn.btoptype = calltype;   /* type of cbtree call   */
  275.  
  276.          if (calltype == GETALL || calltype == GETKEYS || calltype == GETPAR)
  277.             btssn.btloc = 0L;   /* no cont to find */
  278.          else if (calltype == NEWLOC)
  279.             *btssn.btrecnum = getnewloc(btssn.btloc); /* prompt user */
  280.  
  281.          if (calltype == INSERT)
  282.          {
  283.             input(&pr);
  284.             if (varlen)
  285.                btnme.btdtalen = patvar(varlenpr, &pr);  /* reckon rec len */
  286.          }
  287.  
  288.          /* issue cbtree call */
  289.          if ((btretcd = cbtree(fddat, fdssn, &btssn)) == BTCALLOK)
  290.             displrtn(fddat, &btssn, fdnme, fdssn, &pr, varlenpr);
  291.          else
  292.             displerr(btretcd, &btssn);
  293.       }
  294.       else      /* no new key: search b-tree from last established position */
  295.       {
  296.          if (srchtree == 'N')
  297.          {   /* btpatnme btree */
  298.  
  299.             *pr.lname = *btnme.btkey;
  300.             if (calltype == GETALL   ||  calltype == GETKEYS)
  301.             {
  302.                if ((calltype == GETALL  && btnme.btoptype != GETALL) ||
  303.                   (calltype == GETKEYS && btnme.btoptype != GETKEYS))
  304.                   btnme.btloc = 0L;   /* only on first time through */
  305.             }
  306.             else if (calltype == NEWLOC)
  307.                *btnme.btrecnum = getnewloc(btnme.btloc); /* prompt user */
  308.             btnme.btoptype = calltype;
  309.  
  310.             if (calltype == INSERT)
  311.             {
  312.                input(&pr);
  313.                if (varlen)
  314.                   btnme.btdtalen = patvar(varlenpr, &pr);  /* set rec len */
  315.             }
  316.  
  317.             /* issue cbtree call */
  318.             if ((btretcd = cbtree(fddat, fdnme, &btnme)) == BTCALLOK)
  319.                displrtn(fddat, &btnme, fdnme, fdssn, &pr, varlenpr);
  320.             else
  321.                displerr(btretcd, &btnme);
  322.          }
  323.          else   /* searchtree == SSN */
  324.          {
  325.             *pr.lname = *btssn.btkey;
  326.             if (calltype == GETALL   ||  calltype == GETKEYS)
  327.             {
  328.                if ((calltype == GETALL   && btssn.btoptype != GETALL) ||
  329.                   (calltype == GETKEYS && btssn.btoptype != GETKEYS))
  330.                   btssn.btloc = 0L;
  331.             }
  332.             btssn.btoptype = calltype;
  333.  
  334.             if (calltype == INSERT)
  335.             {
  336.                input(&pr);
  337.                if (varlen)
  338.                   btnme.btdtalen = patvar(varlenpr, &pr);  /* set rec len */
  339.             }
  340.  
  341.             /* issue cbtree call */
  342.             if ((btretcd = cbtree(fddat,fdssn,&btssn)) == BTCALLOK)
  343.                displrtn(fddat, &btssn, fdnme, fdssn, &pr, varlenpr);
  344.             else
  345.                displerr(btretcd, &btssn);
  346.          }
  347.       }
  348.  
  349.       svoptype = calltype;
  350.    }
  351.  
  352.  
  353.    wclose();   /* close Amiga window */
  354.  
  355.    free( (char *) srchkey);
  356.    close(fdnme);
  357.    close(fddat);
  358.    close(fdssn);
  359.    btrterm(&btnme);
  360.    btrterm(&btssn);
  361.    puts("");
  362. }
  363.  
  364. long getnewloc(recnum)
  365. long recnum;
  366. {
  367.    long atol();
  368.    char input[18];
  369.  
  370.    printf("\n\n   Enter new record number for this record <%lu>: ___\b\b\b",
  371.             recnum);
  372.    *input = '\0';   /* safety */
  373.    gets(input);
  374.    if (*input == '\0')
  375.       return (recnum);
  376.    return (atol(input));
  377. }
  378.  
  379.    /* build the search key   */
  380. selrec(prp)
  381. PATREC *prp;
  382. {
  383.    extern long atol();
  384. #if CI
  385. #else
  386.    extern int   atoi();
  387. #endif
  388.    extern int   strcmp();
  389.    extern int   strlen();
  390.    extern BTOPTYPE optypes[];
  391.    extern int noptypes;
  392.    int calltype;
  393.    char callchar[8];
  394.    char *cp;
  395.    int i;
  396.  
  397.    scr_clr();   /* clear screen   */
  398.  
  399.    scr_curs(1, 0);
  400.    printf("     GETFRST    GETLAST    GETPRV     GETNXT     GETSEQ     GETREC\n");
  401.    printf("     GETPAR     GETALL     GETKEYS    GETLT      GETLE      GETGT \n");
  402.    printf("     GETGE      INSERT     ISRTKY     DELETE     DELTKY     NEWLOC     EXIT\n");
  403.    printf("\nCurrently active key <%s>, data record location <%lu>",
  404.             (srchtree == 'N') ?  btnme.btkey : btssn.btkey,
  405.             (srchtree == 'N') ?  btnme.btloc : btssn.btloc);
  406.  
  407.    scr_curs(7, 0);
  408.    printf("Last Name:___________________   First:____________   MI:_   Type:GETNXT_ ");
  409.  
  410.    scr_curs(7, 10);
  411.    gets(prp->lname);
  412.  
  413.    for (cp = prp->lname; *cp != '\0'; ++cp)
  414.       *cp = toupper(*cp);
  415.  
  416.    if (prp->lname[0] != '\0')   /* if last name GIVEN, request first name */
  417.    {
  418.  
  419.       scr_curs(7, 38);
  420.       gets(prp->fname);
  421.  
  422.       for (cp = prp->fname; *cp != '\0'; ++cp)
  423.          *cp = toupper(*cp);
  424.  
  425.       scr_curs(7, 56);
  426.       gets(prp->minit);
  427.  
  428.       *prp->minit = toupper(*prp->minit);
  429.    }
  430.  
  431.    /*** always get an optype ***/
  432.  
  433.    repeat
  434.    {
  435.       scr_curs(7, 65);
  436.       printf("GETNXT_\b\b\b\b\b\b\b");
  437.       gets(callchar);
  438.       scr_curs(7, 65 + strlen(callchar));
  439.  
  440.       /* force to upper for match */
  441.       cp = callchar;
  442.       while ((*cp = toupper(*cp)) != '\0')
  443.          ++cp;
  444.  
  445.       /* cleanup the residue on entry line */
  446.       if (*callchar != '\0')   /* if something was entered */
  447.       {
  448.          printf("      \r");                  /* left overs */
  449.          if (*callchar == 'X')
  450.          {
  451.             calltype = 99;
  452.             break;
  453.          }
  454.       }
  455.       else
  456.       {
  457.          printf("GETNXT \r");   /* refresh */
  458.          calltype = 2;               /* default to GETNXT */
  459.          break;
  460.       }
  461.  
  462.       for (i = 0; i < noptypes; ++i)
  463.       {
  464.          if (strcmp(callchar, optypes[i].op_name) == 0)
  465.             break;
  466.       }
  467.  
  468.       if (i < noptypes)
  469.          calltype = optypes[i].op_value;
  470.       else
  471.       {
  472.          calltype = 0;
  473.          continue;      /* error try again */
  474.       }
  475.  
  476.       if (calltype == 99)   /* exit */
  477.          break;
  478.    }
  479.    until( calltype >= 1   &&   calltype <= GETLE );
  480.  
  481.    return(calltype);
  482. }
  483.  
  484. void displrtn(fddat, btparmst, fdnme, fdssn, prp, varlenpr)
  485. int fddat, fdnme, fdssn;
  486. struct btcommo *btparmst;
  487. PATREC *prp;
  488. char *varlenpr;   /* variable length patient record */
  489. {
  490.    extern char *strnncpy();
  491.    extern void displerr();
  492.    extern void free_gtkey();
  493.    register int i, j;
  494.    int btretcd;
  495.    char dummy[25];
  496.    struct btidx **btidxpp;
  497.  
  498.    switch (btparmst->btoptype)
  499.    {
  500.    case GETREC :
  501.    case GETNXT :
  502.    case GETPRV :
  503.    case GETPAR :
  504.    case GETFRST:
  505.    case GETLAST:
  506.    case GETGT   :
  507.    case GETGE   :
  508.    case GETLT   :
  509.    case GETLE   :
  510.  
  511.       /* read record into structure */
  512.       if (btparmst->btvarlen)
  513.          getfpatv(prp, fddat, btparmst);
  514.       else
  515.          getfpat(prp, fddat, btparmst->btloc);
  516.  
  517.       /* display record retrieved   */
  518.       scr_curs(10, 1);
  519.       printf("%s, %s %s ", prp->lname, prp->fname, prp->minit);
  520.       scr_curs(10, 27);
  521.       printf("%-9.9s %-12.12s %-.35s", prp->ssn, prp->hphone, prp->addr);
  522.       break;
  523.  
  524.    case DELETE :
  525.  
  526.       /* display record retrieved   */
  527.       scr_curs(10, 1);
  528.       printf("%s, %s %s ", prp->lname, prp->fname, prp->minit);
  529.       scr_curs(10, 27);
  530.       printf("%-9.9s %-12.12s %-.35s", prp->ssn, prp->hphone, prp->addr);
  531.  
  532.       /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  533.       /*   CBTREE has marked record as deleted and placed            */
  534.       /*   the record on the free space chain.   in so doing, the      */
  535.       /*   first 7 bytes of the last name field was overwritten.      */
  536.       /*   if file layout were designed such that the key did not   */
  537.       /*   occupy the first 7 bytes, then the key could be built      */
  538.       /*   by reading in the record and then issuing a DELTKY         */
  539.       /*   call to the btree for the key.                           */
  540.       /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  541.  
  542.       if (srchtree == 'N')
  543.       {
  544.          /* delete key from other btree */
  545.          strnncpy(btssn.btkey, prp->ssn, btssn.btkeylen); /* mk key */
  546.          btssn.btoptype = DELTKY;
  547.          btssn.btloc = btparmst->btloc;      /* patrec location  */
  548.  
  549.          /* issue cbtree call */
  550.          if ((btretcd = cbtree(fddat, fdssn, &btssn)) != BTCALLOK)
  551.             displerr(btretcd, &btssn);
  552.       }
  553.       else if (srchtree == 'S')
  554.       {
  555.          /* delete key from other btree */
  556.          bldkey(btnme.btkey, btnme.btkeylen, prp);   /* make key */
  557.          btnme.btoptype = DELTKY;
  558.          btnme.btloc = btparmst->btloc;   /* patrec location  */
  559.  
  560.          /* issue cbtree call */
  561.          if ((btretcd = cbtree(fddat, fdnme, &btnme)) != BTCALLOK)
  562.             displerr(btretcd, &btnme);
  563.       }
  564.       break;
  565.  
  566.    case GETALL:
  567.    case GETSEQ:
  568.  
  569.       /* process records whose locations are stored   */
  570.       /* in btparmst->btrecnum[].                     */
  571.       j = 10;   /* start on screen line 11 */
  572.       i = 0;
  573.       while (btparmst->btrecnum[i] > 0L)
  574.       {
  575.          /* read record into structure */
  576.          if (btparmst->btvarlen)
  577.          {
  578.             btparmst->btloc = btparmst->btrecnum[i++];
  579.             getfpatv(prp, fddat, btparmst);
  580.          }
  581.          else
  582.             getfpat(prp, fddat, btparmst->btrecnum[i++]);
  583.  
  584.          /* process records retrieved   */
  585.          scr_curs(j, 1);
  586.          printf("%s, %s %s ", prp->lname, prp->fname, prp->minit);
  587.          scr_curs(j, 28);
  588.          printf("%-9.9s %-12.12s %-.35s", prp->ssn,prp->hphone,prp->addr);
  589.  
  590.          if (++j > 20)
  591.          {
  592.             printf("\nPress <RETURN> to continue...");
  593.             gets(dummy);
  594.  
  595.             scr_clr();
  596.             j = 0;
  597.          }
  598.       }
  599.       break;
  600.  
  601.    case GETKEYS:
  602.  
  603.       /* process records whose keys and locations are stored   */
  604.       /* in btparmst->btrecnum[].                  */
  605.       j = 10;                  /* first time through start on line line */
  606.       i = 0;
  607.       btidxpp = (struct btidx **)btparmst->btrecnum;         /* cast it */
  608.       while (*btidxpp != NULL)
  609.       {
  610.          /* read record into structure */
  611.          if (btparmst->btvarlen)
  612.          {
  613.             btparmst->btloc = (*btidxpp)->btptr;   /* read up the record */
  614.             getfpatv(prp, fddat, btparmst);
  615.          }
  616.          else
  617.             getfpat(prp, fddat, (*btidxpp)->btptr);   /* read up the record */
  618.  
  619.          /* process records retrieved   */
  620.          scr_curs(j, 0);
  621.          printf("key: \"%s\"", (*btidxpp)->skeynme);
  622.          scr_curs(j, 27);
  623.          printf("location: %03lu,  %.19s, %s %s",
  624.                   (*btidxpp)->btptr, prp->lname, prp->fname, prp->minit);
  625.  
  626.          /* finish up with this record */
  627.          ++btidxpp;
  628.  
  629.          if (++j > 20)
  630.          {
  631.             printf("\nPress <RETURN> to continue...");
  632.             gets(dummy);            /* CI C86 style */
  633.             scr_clr();      /* clear screen */
  634.             j = 0;
  635.          }
  636.       }
  637.       free_gtkey(btparmst);
  638.       break;
  639.  
  640.    case INSERT:
  641.  
  642.       if (srchtree == 'N')
  643.       {
  644.          /* insert key into other btree */
  645.          strnncpy(btssn.btkey, prp->ssn, btssn.btkeylen);   /* make key */
  646.          btssn.btoptype = ISRTKY;
  647.          btssn.btloc = btparmst->btloc;      /* patrec location  */
  648.  
  649.          /* issue cbtree call */
  650.          if ((btretcd = cbtree(fddat, fdssn, &btssn)) != BTCALLOK)
  651.             displerr(btretcd,  &btssn);
  652.       }
  653.       else   /* (srctree == 'S') */
  654.       {
  655.          /* insert key into other btree */
  656.          bldkey(btnme.btkey, btnme.btkeylen, prp);
  657.          btnme.btoptype = ISRTKY;
  658.          btnme.btloc = btparmst->btloc;      /* patrec location  */
  659.  
  660.          /* issue cbtree call */
  661.          if( (btretcd = cbtree(fddat, fdnme, &btnme) ) != BTCALLOK)
  662.                displerr(btretcd, &btnme);
  663.       }
  664.  
  665.       /* write DATA record to disk   */
  666.       if (btparmst->btvarlen)
  667.          putfpatv(fddat, btparmst, varlenpr);
  668.       else
  669.          putfpat(fddat, btparmst->btloc, prp);
  670.  
  671.       /* print new record   */
  672.       scr_curs(16, 1);
  673.       printf("%s, %s %s ", prp->lname,prp->fname,prp->minit);
  674.       scr_curs(16, 27);
  675.       printf("%-9.9s %-12.12s %-.35s", prp->ssn,prp->hphone, prp->addr);
  676.       break;
  677.  
  678.    default:
  679.       break;
  680.  
  681.    }   /* end switch */
  682.  
  683.    printf("\n\nPress <RETURN> to continue...");
  684.    gets(dummy);
  685. }
  686.  
  687. void input(prp)
  688. PATREC *prp;
  689. {
  690.    char *cp;
  691.  
  692.    if (srchtree == 'N')
  693.    {
  694.       scr_curs(10, 0);
  695.       printf("Soc Sec# :_________");
  696.       scr_curs(10, 10);
  697.       gets(prp->ssn);
  698.    }
  699.    else   /* (srctree == 'S') */
  700.    {
  701.       scr_curs(10, 0);
  702.       printf("Last Name:___________________   First:____________   MI:_   Type:__ ");
  703.       scr_curs(10, 10);
  704.       gets(prp->lname);
  705.       for (cp = prp->lname; *cp != '\0'; ++cp)
  706.          *cp = toupper(*cp);
  707.  
  708.       scr_curs(10, 38);
  709.       gets(prp->fname);
  710.       for (cp = prp->fname; *cp != '\0'; ++cp)
  711.          *cp = toupper(*cp);
  712.  
  713.       scr_curs(10, 56);
  714.       gets(prp->minit);
  715.       for (cp = prp->minit; *cp != '\0'; ++cp)
  716.          *cp = toupper(*cp);
  717.    }
  718.  
  719.    scr_curs(12, 0);
  720.    printf("Phone No.:____________");
  721.    scr_curs(12, 10);
  722.    gets(prp->hphone);
  723.  
  724.    scr_curs(14, 0);
  725.    printf("Address  :___________________________________");
  726.    scr_curs(14, 10);
  727.    gets(prp->addr);
  728. }
  729.  
  730. void displerr(errcode, btcp)
  731. int errcode;
  732. struct btcommo *btcp;
  733. {
  734.    char dummy[2];
  735.  
  736.    puts("\n");
  737.    cberror(errcode, "Userbtr", btcp);
  738.    printf("\nPress <RETURN> to continue...");
  739.    gets(dummy);
  740. }
  741.