home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / BTREE.ZIP / BTA.C next >
Text File  |  1989-08-31  |  13KB  |  456 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bta.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include <btree.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. /* #include <stdlib.h> */
  12. void exit();        /* delete if you have stdlib.h */
  13. #define EXIT_SUCCESS    (0)
  14. #define EXIT_FAILURE    (1)
  15.  
  16. #define FILENAME    ("demo.key")
  17. #define M        (4)
  18. #define KS        (24)
  19. #define BS        ((size_t)10)
  20. int cmp(/* void *buf1, void *buf2, size_t count */);
  21.  
  22. #define USAGE    ("Usage: bta")
  23.  
  24. /*man---------------------------------------------------------------------------
  25. NAME
  26.      bta - btree access
  27.  
  28. SYNOPSIS
  29.      bta
  30.  
  31. DESCRIPTION
  32.      bta is a simple program to directly access a btree.  Its purpose is
  33.      primarily instructional.
  34.  
  35. SEE ALSO
  36.      btdump, btlist, btree.
  37.  
  38. NOTES
  39.      bta is written for btrees sorted using the strncmp function.  To
  40.      use it with with keys of a different type, modify the cmp function
  41.      in the file bta.c.
  42.  
  43. ------------------------------------------------------------------------------*/
  44. int main(argc, argv)
  45. int argc;
  46. char *argv[];
  47. {
  48.     int        rs    = 0;
  49.     btree_t *    btp    = NULL;
  50.     char         key[KS];
  51.     int        c    = 0;
  52.  
  53.     /* initialize storage */
  54.     memset((void *)key, 0, sizeof(key));
  55.  
  56.     /* process command line arguments */
  57.     if (argc != 1) {
  58.         printf("%s\n", USAGE);
  59.         bexit(EXIT_FAILURE);
  60.     }
  61.  
  62.     /* open btree */
  63.     btp = btopen(FILENAME, "r+", cmp);
  64.     if (btp == NULL) {
  65.         if (errno == ENOENT) {
  66.             printf("B-tree does not exist.  Creating.\n");
  67.             rs = btcreate(FILENAME, M, KS, cmp, 0);
  68.             if (rs == -1) {
  69.                 printf("*** Error creating B-tree.  Error code %d.\n", errno);
  70.                 bexit(EXIT_FAILURE);
  71.             }
  72.             btp = btopen(FILENAME, "r+", cmp);
  73.             if (btp == NULL) {
  74.                 printf("*** Error opening B-tree.  Error code %d.\n", errno);
  75.                 bexit(EXIT_FAILURE);
  76.             }
  77.         } else {
  78.             printf("*** Error opening B-tree.  Error code %d.\n", errno);
  79.             bexit(EXIT_FAILURE);
  80.         }
  81.     }
  82.     rs = btsetvbuf(btp, NULL, BS);
  83.     if (rs == -1) {
  84.         printf("*** Error assigning buffering.  Error code %d.\n", errno);
  85.         bexit(EXIT_FAILURE);
  86.     }
  87.  
  88.     printf("\n------------------------------\n");
  89.     printf("| btree Library Demo Program |\n");
  90.     printf("------------------------------\n");
  91.  
  92.     /* main loop */
  93.     while (1) {
  94.         printf("\n\n");
  95.         printf("----------------------\n");
  96.         printf("| I - Insert key     |\n");
  97.         printf("| D - Delete key     |\n");
  98.         printf("| S - Search for key |\n");
  99.         printf("| N - Next key       |\n");
  100.         printf("| P - Previous key   |\n");
  101.         printf("| F - First key      |\n");
  102.         printf("| L - Last key       |\n");
  103.         printf("| A - list All keys  |\n");
  104.         printf("| X - eXit           |\n");
  105.         printf("----------------------\n\n");
  106.     
  107.         printf("Enter selection:  ");
  108.         c = getchar();
  109.         if (getchar() != '\n') {
  110.             printf("\a");
  111.             continue;
  112.         }
  113.         c = toupper(c);
  114.         if (c == 'X') {
  115.             break;
  116.         }
  117.  
  118.         switch (c) {
  119.         case 'I':    /* Insert key */
  120.             rs = btlock(btp, BT_WRLCK);
  121.             if (rs == -1) {
  122.                 if (errno == EAGAIN) {
  123.                     printf("B-tree currently locked by another process.\n");
  124.                     break;
  125.                 }
  126.                 printf("*** Error write locking B-tree.  Error code %d.\n", errno);
  127.                 bexit(EXIT_FAILURE);
  128.             }
  129.             printf("\nEnter key to insert:  ");
  130.             memset(key, 0, sizeof(key));
  131.             if (fgets(key, sizeof(key), stdin) == NULL) {
  132.                 printf("*** Error reading input.\n");
  133.                 bexit(EXIT_FAILURE);
  134.             }
  135.             if (key[strlen(key) - 1] == '\n') {
  136.                 key[strlen(key) - 1] = '\0';
  137.             }
  138.             rs = btinsert(btp, key);
  139.             if (rs == -1) {
  140.                 if (errno == BTEDUPKEY) {
  141.                     printf("Key %s already in tree.\n", key);
  142.                 } else {
  143.                     printf("*** Error inserting key into B-tree.  Error code %d.\n", errno);
  144.                     bexit(EXIT_FAILURE);
  145.                 }
  146.             } else {
  147.                 printf("key %s inserted in tree.\n", key);
  148.             }
  149.             rs = btlock(btp, BT_UNLCK);
  150.             if (rs == -1) {
  151.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  152.                 bexit(EXIT_FAILURE);
  153.             }
  154.             break;    /* case 'I': */
  155.         case 'D':    /* */
  156.             rs = btlock(btp, BT_WRLCK);
  157.             if (rs == -1) {
  158.                 if (errno == EAGAIN) {
  159.                     printf("B-tree currently locked by another process.\n");
  160.                     break;
  161.                 }
  162.                 printf("*** Error write locking B-tree.  Error code %d.\n", errno);
  163.                 bexit(EXIT_FAILURE);
  164.             }
  165.             printf("\nEnter key to insert:  ");
  166.             memset(key, 0, sizeof(key));
  167.             if (fgets(key, sizeof(key), stdin) == NULL) {
  168.                 printf("*** Error reading input.\n");
  169.                 bexit(EXIT_FAILURE);
  170.             }
  171.             if (key[strlen(key) - 1] == '\n') {
  172.                 key[strlen(key) - 1] = '\0';
  173.             }
  174.             rs = btdelete(btp, key);
  175.             if (rs == -1) {
  176.                 if (errno == BTENKEY) {
  177.                     printf("Key not found.\n");
  178.                 } else {
  179.                     printf("*** Error deleting key.  Error code %d.\n", errno);
  180.                     bexit(EXIT_FAILURE);
  181.                 }
  182.             } else {
  183.                 printf("Key deleted.\n");
  184.             }
  185.             rs = btlock(btp, BT_UNLCK);
  186.             if (rs == -1) {
  187.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  188.                 bexit(EXIT_FAILURE);
  189.             }
  190.             break;    /* case 'D': */
  191.         case 'S':    /* Search for key */
  192.             rs = btlock(btp, BT_RDLCK);
  193.             if (rs == -1) {
  194.                 if (errno == EAGAIN) {
  195.                     printf("B-tree currently write locked by another process.\n");
  196.                     break;
  197.                 }
  198.                 printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  199.                 bexit(EXIT_FAILURE);
  200.             }
  201.             printf("\nEnter key to search for:  ");
  202.             memset(key, 0, sizeof(key));
  203.             if (fgets(key, sizeof(key), stdin) == NULL) {
  204.                 printf("*** Error reading input.\n");
  205.                 bexit(EXIT_FAILURE);
  206.             }
  207.             if (key[strlen(key) - 1] == '\n') {
  208.                 key[strlen(key) - 1] = '\0';
  209.             }
  210.             rs = btsearch(btp, (void *)key);
  211.             if (rs == -1) {
  212.                 printf("*** Error searching for key.  Error code %d.\n", errno);
  213.                 bexit(EXIT_FAILURE);
  214.             }
  215.             if (rs == 1) {
  216.                 printf("Key found.\n");
  217.             } else {
  218.                 printf("Key not found.\n");
  219.             }
  220.             rs = btlock(btp, BT_UNLCK);
  221.             if (rs == -1) {
  222.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  223.                 bexit(EXIT_FAILURE);
  224.             }
  225.             break;    /* case 'S': */
  226.         case 'N':    /* Next key */
  227.             rs = btlock(btp, BT_RDLCK);
  228.             if (rs == -1) {
  229.                 if (errno == EAGAIN) {
  230.                     printf("B-tree currently write locked by another process.\n");
  231.                     break;
  232.                 }
  233.                 printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  234.                 bexit(EXIT_FAILURE);
  235.             }
  236.             if (btkeycnt(btp) == 0) {
  237.                 printf("B-tree is empty.\n");
  238.                 rs = btlock(btp, BT_UNLCK);
  239.                 if (rs == -1) {
  240.                     printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  241.                     bexit(EXIT_FAILURE);
  242.                 }
  243.                 break;
  244.             }
  245.             rs = btsearch(btp, key);
  246.             if (rs == -1) {
  247.                 printf("*** Error searching for key.  Error code %d.\n", errno);
  248.                 bexit(EXIT_FAILURE);
  249.             }
  250.             if (rs == 1) {
  251.                 rs = btnext(btp);
  252.                 if (rs == -1) {
  253.                     printf("*** Error finding next key.  Error code %d.\n", errno);
  254.                     bexit(EXIT_FAILURE);
  255.                 }
  256.             }
  257.             if (btcursor(btp) == NULL) {
  258.                 memset((void *)key, 0, sizeof(key));
  259.                 printf("That was the last key.\n");
  260.             } else {
  261.                 rs = btgetk(btp, (void *)key);
  262.                 if (rs == -1) {
  263.                     printf("*** Error reading key.  Error code %d.\n", errno);
  264.                     bexit(EXIT_FAILURE);
  265.                 }
  266.                 printf("The next key is %s.\n", (char *)key);
  267.             }
  268.             rs = btlock(btp, BT_UNLCK);
  269.             if (rs == -1) {
  270.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  271.                 bexit(EXIT_FAILURE);
  272.             }
  273.             break;    /* case 'N': */
  274.         case 'P':    /* Previous key */
  275.             rs = btlock(btp, BT_RDLCK);
  276.             if (rs == -1) {
  277.                 if (errno == EAGAIN) {
  278.                     printf("B-tree currently write locked by another process.\n");
  279.                     break;
  280.                 }
  281.                 printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  282.                 bexit(EXIT_FAILURE);
  283.             }
  284.             if (btkeycnt(btp) == 0) {
  285.                 printf("B-tree is empty.\n");
  286.                 rs = btlock(btp, BT_UNLCK);
  287.                 if (rs == -1) {
  288.                     printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  289.                     bexit(EXIT_FAILURE);
  290.                 }
  291.                 break;
  292.             }
  293.             rs = btsearch(btp, key);
  294.             if (rs == -1) {
  295.                 printf("*** Error searching for key.  Error code %d.\n", errno);
  296.                 bexit(EXIT_FAILURE);
  297.             }
  298.             if (rs == 1) {
  299.                 rs = btprev(btp);
  300.                 if (rs == -1) {
  301.                     printf("*** Error finding previous key.  Error code %d.\n", errno);
  302.                     bexit(EXIT_FAILURE);
  303.                 }
  304.             }
  305.             if (btcursor(btp) == NULL) {
  306.                 memset((void *)key, 0, sizeof(key));
  307.                 printf("That was the first key.\n");
  308.             } else {
  309.                 rs = btgetk(btp, (void *)key);
  310.                 if (rs == -1) {
  311.                     printf("*** Error reading key.  Error code %d.\n", errno);
  312.                     bexit(EXIT_FAILURE);
  313.                 }
  314.                 printf("The previous key is %s.\n", (char *)key);
  315.             }
  316.             rs = btlock(btp, BT_UNLCK);
  317.             if (rs == -1) {
  318.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  319.                 bexit(EXIT_FAILURE);
  320.             }
  321.             break;    /* case 'P': */
  322.         case 'F':    /* First key */
  323.             rs = btlock(btp, BT_RDLCK);
  324.             if (rs == -1) {
  325.                 if (errno == EAGAIN) {
  326.                     printf("B-tree currently write locked by another process.\n");
  327.                     break;
  328.                 }
  329.                 printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  330.                 bexit(EXIT_FAILURE);
  331.             }
  332.             if (btkeycnt(btp) == 0) {
  333.                 printf("B-tree is empty.\n");
  334.                 rs = btlock(btp, BT_UNLCK);
  335.                 if (rs == -1) {
  336.                     printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  337.                     bexit(EXIT_FAILURE);
  338.                 }
  339.                 break;
  340.             }
  341.             rs = btfirst(btp);
  342.             if (rs == -1) {
  343.                 printf("*** Error finding first key.  Error code %d.\n", errno);
  344.                 bexit(EXIT_FAILURE);
  345.             }
  346.             rs = btgetk(btp, (void *)key);
  347.             if (rs == -1) {
  348.                 printf("*** Error reading key.  Error code %d.\n", errno);
  349.                 bexit(EXIT_FAILURE);
  350.             }
  351.             printf("The first key is %s.\n", (char *)key);
  352.             rs = btlock(btp, BT_UNLCK);
  353.             if (rs == -1) {
  354.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  355.                 bexit(EXIT_FAILURE);
  356.             }
  357.             break;    /* case 'F': */
  358.         case 'L':    /* Last key */
  359.             rs = btlock(btp, BT_RDLCK);
  360.             if (rs == -1) {
  361.                 if (errno == EAGAIN) {
  362.                     printf("B-tree currently write locked by another process.\n");
  363.                     break;
  364.                 }
  365.                 printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  366.                 bexit(EXIT_FAILURE);
  367.             }
  368.             if (btkeycnt(btp) == 0) {
  369.                 printf("B-tree is empty.\n");
  370.                 rs = btlock(btp, BT_UNLCK);
  371.                 if (rs == -1) {
  372.                     printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  373.                     bexit(EXIT_FAILURE);
  374.                 }
  375.                 break;
  376.             }
  377.             rs = btlast(btp);
  378.             if (rs == -1) {
  379.                 printf("*** Error finding last key.  Error code %d.\n", errno);
  380.                 bexit(EXIT_FAILURE);
  381.             }
  382.             rs = btgetk(btp, (void *)key);
  383.             if (rs == -1) {
  384.                 printf("*** Error reading key.  Error code %d.\n", errno);
  385.                 bexit(EXIT_FAILURE);
  386.             }
  387.             printf("The last key is %s.\n", (char *)key);
  388.             rs = btlock(btp, BT_UNLCK);
  389.             if (rs == -1) {
  390.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  391.                 bexit(EXIT_FAILURE);
  392.             }
  393.             break;    /* case 'L': */
  394.         case 'A':
  395.             rs = btlock(btp, BT_RDLCK);
  396.             if (rs == -1) {
  397.                 if (errno == EAGAIN) {
  398.                     printf("B-tree currently write locked by another process.\n");
  399.                     break;
  400.                 }
  401.                 printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  402.                 bexit(EXIT_FAILURE);
  403.             }
  404.             printf("There are %lu keys in the btree.\n", (unsigned long)btkeycnt(btp));
  405.             rs = btsetcur(btp, NULL);
  406.             if (rs == -1) {
  407.                 printf("*** Error setting cursor.  Error code %d.\n", errno);
  408.                 bexit(EXIT_FAILURE);
  409.             }
  410.             while (1) {
  411.                 rs = btnext(btp);
  412.                 if (rs == -1) {
  413.                     printf("*** Error finding next key.  Error code %d.\n", errno);
  414.                     bexit(EXIT_FAILURE);
  415.                 }
  416.                 if (btcursor(btp) == NULL) {
  417.                     break;
  418.                 }
  419.                 rs = btgetk(btp, (void *)key);
  420.                 if (rs == -1) {
  421.                     printf("Error calling btgetk.  errno = %d.\n", errno);
  422.                     bexit(EXIT_FAILURE);
  423.                 }
  424.                 printf("%s\n", key);
  425.             }
  426.             rs = btlock(btp, BT_UNLCK);
  427.             if (rs == -1) {
  428.                 printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  429.                 bexit(EXIT_FAILURE);
  430.             }
  431.             break;    /* case 'A': */
  432.         default:
  433.             printf("Invalid selection.\a\n");
  434.             break;    /* default: */
  435.         }
  436.     }
  437.  
  438.     /* close btree */
  439.     rs = btclose(btp);
  440.     if (rs == -1) {
  441.         printf("*** Error closing B-tree.  Error code %d.\n", errno);
  442.         bexit(EXIT_FAILURE);
  443.     }
  444.  
  445.     bexit(EXIT_SUCCESS);
  446. }
  447.  
  448. /* comparison function */
  449. int cmp(buf1, buf2, count)
  450. void *buf1;
  451. void *buf2;
  452. size_t count;
  453. {
  454.     return strncmp((char *)buf1, (char *)buf2, count);
  455. }
  456.