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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btdump.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /* #include <stdlib.h> */
  9. #define EXIT_SUCCESS    0        /* delete if you have stdlib.h */
  10. #define EXIT_FAILURE    1
  11. #include "btree_.h"
  12.  
  13. int cmp(/* void *buf1, void *buf2, size_t count */);
  14.  
  15. #define USAGE    ("Usage: btdump filename")
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btdump - dump contents of a btree
  20.  
  21. SYNOPSIS
  22.      btdump filename
  23.  
  24. DESCRIPTION
  25.      The btdump command dumps the contents of btree file.  The contents
  26.      of the header are displayed, followed by each individual node (in
  27.      preorder).
  28.  
  29. SEE ALSO
  30.      bta, btlist, btree.
  31.  
  32. NOTES
  33.      btdump is written for btrees sorted using the strncmp function.  To
  34.      use it with with keys of a different type, modify the cmp function
  35.      in the file btdump.c.
  36.  
  37. ------------------------------------------------------------------------------*/
  38. int main(argc, argv)
  39. int argc;
  40. char *argv[];
  41. {
  42.     int        rs        = 0;
  43.     btree_t *    btp        = NULL;
  44.  
  45.     /* process command line arguments */
  46.     if (argc != 2) {
  47.         printf("%s\n", USAGE);
  48.         bexit(EXIT_FAILURE);
  49.     }
  50.  
  51.     /* open btree */
  52.     btp = btopen(argv[1], "r", cmp);
  53.     if (btp == NULL) {
  54.         printf("*** Error opening btree.  Error code %d.\n", errno);
  55.         bexit(EXIT_FAILURE);
  56.     }
  57.  
  58.     /* lock btree */
  59.     rs = btlock(btp, BT_RDLKW);
  60.     if (rs == -1) {
  61.         printf("*** Error locking btree.  Error code %d.\n", errno);
  62.         bexit(EXIT_FAILURE);
  63.     }
  64.  
  65.     printf("HEADER:\n");
  66.     bt_dgbtree(btp);
  67.  
  68.     rs = btdump(btp, btp->bthdr.root);
  69.     if (rs == -1) {
  70.         BTEPRINT;
  71.         bexit(EXIT_FAILURE);
  72.     }
  73.  
  74.     /* unlock btree */
  75.     rs = btlock(btp, BT_UNLCK);
  76.     if (rs == -1) {
  77.         printf("*** Error unlocking btree.  Error code %d.\n", errno);
  78.         bexit(EXIT_FAILURE);
  79.     }
  80.  
  81.     /* close btree */
  82.     if (btclose(btp) == -1) {
  83.         printf("*** Error closing btree.  Error code %d.\n", errno);
  84.         bexit(EXIT_FAILURE);
  85.     }
  86.  
  87.     bexit(EXIT_SUCCESS);
  88. }
  89.  
  90. int btdump(btp, node)
  91. btree_t *btp;
  92. bpos_t node;
  93. {
  94.     int        rs    = 0;
  95.     int        i    = 0;
  96.     btnode_t *    btnp    = NULL;
  97.  
  98.  
  99.     if (node == 0) {
  100.         return 0;
  101.     }
  102.  
  103.     /* read in node and display it */
  104.     btnp = bt_ndalloc(btp);
  105.     if (btnp == NULL) {
  106.         BTEPRINT;
  107.         return -1;
  108.     }
  109.     rs = bt_ndget(btp, node, btnp);
  110.     if (rs == -1) {
  111.         BTEPRINT;
  112.         bt_ndfree(btnp);
  113.         return -1;
  114.     }
  115.     printf("NODE POSITION:  %ld.\n", node);
  116.     bt_dgnode(btp, btnp);
  117.     printf("\n");
  118.  
  119.     for (i = 0; (i < btp->bthdr.m); i++) {
  120.         rs = btdump(btp, *bt_kychild_p(btnp, i));
  121.         if (rs == -1) {
  122.             bt_ndfree(btnp);
  123.             return -1;
  124.         }
  125.     }
  126.  
  127.     bt_ndfree(btnp);
  128.     btnp = NULL;
  129.  
  130.     return 0;
  131. }
  132.  
  133. /* comparison function */
  134. int cmp(buf1, buf2, count)
  135. void *buf1;
  136. void *buf2;
  137. size_t count;
  138. {
  139.     return strncmp((char *)buf1, (char *)buf2, count);
  140. }
  141.