home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d034 / btree.lha / Btree / btree.fe.h < prev    next >
Encoding:
C/C++ Source or Header  |  1986-09-03  |  4.6 KB  |  229 lines

  1. /********************************************************************
  2. *********************************************************************
  3.  
  4. Module Name:        btree.fe.h
  5. ============
  6.  
  7. Function:        Front end for btree code ...
  8. =========        
  9.  
  10. Description:
  11. ============
  12.     Implements a front-end program for the btree code
  13.  
  14. ****************************************************************************
  15. ****************************************************************************/
  16.  
  17. static char btreefrontend[] = "@(#)btree.fe.h    1.2 8/18/86";
  18.  
  19.  
  20.  
  21. /*
  22. ** FRONTEND
  23. ** ========
  24. **
  25. ** Purpose:    Front panel type thing for btree code - allows interactive
  26. **        manipulation of tree
  27. **
  28. ** Parameters:    none
  29. **
  30. ** Returns:    none
  31. **
  32. ** Description:    The following 'commands' are implemented
  33. **        <n>    Insert key 'n'
  34. **        i<n>    ditto
  35. **        d<n>    Delete key 'n'
  36. **        l<n>    Locate key 'n'
  37. **        p<n>    Print tree
  38. **        s    Set up default tree
  39. **        x    Exit from front end
  40. */
  41.  
  42. frontend()
  43. {
  44.     int     status;
  45.     BTREE    tree;
  46.     DATUM    dtm,
  47.         dtm2;
  48.     KEY    key;
  49.     char    buf[BUFSIZ];
  50.  
  51.     tree = (BTREE)NULL;
  52.  
  53.     printf("Command: "); fflush(stdout);
  54.     while (fgets(buf, sizeof buf, stdin) != NULL) 
  55.     {
  56.         buf[strlen(buf) - 1] = '\0';
  57.         switch (buf[0]) 
  58.         {
  59.         default:        /* Error case */
  60.             fprintf(stderr, "i, d, l, p, s or x please!\n");
  61.             break;
  62.  
  63.         case '0':
  64.         case '1':
  65.         case '2':
  66.         case '3':
  67.         case '4':
  68.         case '5':
  69.         case '6':
  70.         case '7':
  71.         case '8':
  72.         case '9':
  73.             sscanf(buf, "%d", &dtm.key);
  74.             status = Insert(&tree, dtm);
  75.             if (status != SUCCESS)
  76.                 btree_err(status);
  77.             break;
  78.  
  79.         case 's':    /* Set up default tree */
  80.             tree = (BTREE)NULL;
  81.             dtm.key = 20;
  82.             status = Insert(&tree, dtm);
  83.             if (status != SUCCESS)
  84.                 btree_err(status);
  85.             dtm.key = 10;
  86.             status = Insert(&tree, dtm);
  87.             if (status != SUCCESS)
  88.                 btree_err(status);
  89.             dtm.key = 15;
  90.             status = Insert(&tree, dtm);
  91.             if (status != SUCCESS)
  92.                 btree_err(status);
  93.             dtm.key = 30;
  94.             status = Insert(&tree, dtm);
  95.             if (status != SUCCESS)
  96.                 btree_err(status);
  97.             dtm.key = 40;
  98.             status = Insert(&tree, dtm);
  99.             if (status != SUCCESS)
  100.                 btree_err(status);
  101.             dtm.key = 7;
  102.             status = Insert(&tree, dtm);
  103.             if (status != SUCCESS)
  104.                 btree_err(status);
  105.             dtm.key = 18;
  106.             status = Insert(&tree, dtm);
  107.             if (status != SUCCESS)
  108.                 btree_err(status);
  109.             dtm.key = 22;
  110.             status = Insert(&tree, dtm);
  111.             if (status != SUCCESS)
  112.                 btree_err(status);
  113.             dtm.key = 26;
  114.             status = Insert(&tree, dtm);
  115.             if (status != SUCCESS)
  116.                 btree_err(status);
  117.             dtm.key = 5;
  118.             status = Insert(&tree, dtm);
  119.             if (status != SUCCESS)
  120.                 btree_err(status);
  121.             dtm.key = 35;
  122.             status = Insert(&tree, dtm);
  123.             if (status != SUCCESS)
  124.                 btree_err(status);
  125.             dtm.key = 13;
  126.             status = Insert(&tree, dtm);
  127.             if (status != SUCCESS)
  128.                 btree_err(status);
  129.             dtm.key = 27;
  130.             status = Insert(&tree, dtm);
  131.             if (status != SUCCESS)
  132.                 btree_err(status);
  133.             dtm.key = 32;
  134.             status = Insert(&tree, dtm);
  135.             if (status != SUCCESS)
  136.                 btree_err(status);
  137.             dtm.key = 42;
  138.             status = Insert(&tree, dtm);
  139.             if (status != SUCCESS)
  140.                 btree_err(status);
  141.             dtm.key = 46;
  142.             status = Insert(&tree, dtm);
  143.             if (status != SUCCESS)
  144.                 btree_err(status);
  145.             dtm.key = 24;
  146.             status = Insert(&tree, dtm);
  147.             if (status != SUCCESS)
  148.                 btree_err(status);
  149.             dtm.key = 45;
  150.             status = Insert(&tree, dtm);
  151.             if (status != SUCCESS)
  152.                 btree_err(status);
  153.             dtm.key = 25;
  154.             status = Insert(&tree, dtm);
  155.             if (status != SUCCESS)
  156.                 btree_err(status);
  157.             ShowTree(tree, 0);
  158.             break;
  159.  
  160.         case 'i':        /* Insert a key */
  161.             sscanf(buf+1, "%d", &dtm.key);
  162.             status = Insert(&tree, dtm);
  163.             if (status != SUCCESS)
  164.                 btree_err(status);
  165.             break;
  166.  
  167.         case 'd':        /* Delete a key */
  168.             sscanf(buf+1, "%d", &dtm.key);
  169.             status = Delete(&tree, dtm.key);
  170.             if (status != SUCCESS)
  171.                 btree_err(status);
  172.             break;
  173.  
  174.         case 'l':        /* Lookup a key */
  175.             sscanf(buf+1, "%d", &dtm.key);
  176.             status = Search(tree, dtm.key, &dtm2);
  177.             if (status != SUCCESS)
  178.                 btree_err(status);
  179.             printf("Found %d\n",dtm2.key);
  180.             break;
  181.  
  182.         case 'p':        /* Show the tree */
  183.             ShowTree(tree, 0);
  184.             break;
  185.         
  186.         case 'x':
  187.             break;
  188.         }
  189.         if (buf[0]=='x')
  190.             break;
  191.         else
  192.         {
  193.             printf("Command: "); 
  194.             fflush(stdout);
  195.         }
  196.     }
  197. }
  198.  
  199.  
  200. /*
  201. ** ERROR ROUTINE
  202. ** =============
  203. **
  204. ** Purpose:    Error message routine for btree front-end system
  205. **
  206. ** Parameters:    errcode    = errcode
  207. **
  208. ** Returns:    none
  209. **
  210. ** Description:    Pretty simple...
  211. */
  212.  
  213. btree_err(errcode)
  214. int    errcode;
  215. {
  216.     switch (errcode)
  217.     {
  218.     case KEY_EXISTS_ERROR:
  219.         fprintf(stderr,"Key already exists !\n");
  220.         break;
  221.     case KEY_NOT_FOUND_ERROR:
  222.         fprintf(stderr,"Key not found !\n");
  223.         break;
  224.     case TREE_CORRUPTED_ERROR:
  225.         fprintf(stderr,"Tree corrupted !\n");
  226.         break;
  227.     }
  228. }
  229.