home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 155_01 / example.c < prev    next >
C/C++ Source or Header  |  1990-10-09  |  6KB  |  174 lines

  1. /* **** EXAMPLE.C ****
  2.  *  This program is an example of how to use Ray Swartz's btree library.
  3.  *  To use this program, a key file must be initialized first.
  4.  *  This program maintains no data entries besides the keys entered
  5.  *  into the key file.
  6.  *
  7.  *
  8.  *
  9.  *
  10.  * Author: Ray Swartz
  11.  *         P.O. Box 2528
  12.  *         Santa Cruz, Calif. 95063
  13.  * Last Modified: 4/28/85
  14.  *
  15.  * ANY USE OF THESE LIBRARY ROUTINES EITHER PERSONAL OR COMMERCIAL
  16.  * IS ALLOWED UPON THE CONDITION THAT THE USER IDENTIFY THEM
  17.  * AS BEING USED IN THE PROGRAM.  IDENTIFYING INFORMATION MUST
  18.  * APPEAR IN THE PROGRAM DOCUMENTATION OR ON THE TERMINAL SCREEN.
  19.  *
  20.  *         #################################    
  21.  *         # UNATTRIBUTED USE IS FORBIDDEN #
  22.  *         #################################
  23.  *
  24.  */
  25.  
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <alloc.h>
  31. #include "btree.h"
  32. #include "beeprot.h"
  33.  
  34. extern void cursor(int row, int col);  /* cursor movement on a MS-DOS machine */
  35. extern int main_prompt(char *prompt);   /* prompt used as database interface */
  36. extern void get_key(char *prompt, char *key, struct keyinfo *fileinfo);
  37. extern void print_msg(char *prompt);  /* go to line 20, clear window, ring bell, prompt */
  38. extern void t_delay(); /* display message delay */
  39. extern int yes_or_no(char *prompt);  /* print prompt and return YES or NO */
  40.  
  41. void show_rec(struct node *current_node);
  42. void enter_data(struct node *current_node, struct keyinfo *fileinfo);
  43.  
  44. extern char instr[];       /* general input strings */
  45.  
  46. void main()
  47. {
  48.     struct keyinfo header;     /* keyfile being used */
  49.     struct node current_node;  /* stores current node's information */
  50.     char *key1;                /* key to search for */
  51.     int choice;                /* user's menu choice */
  52.     long node_nbr;             /* number of current_ node */
  53.     int sentinel;
  54.     
  55.     printf("Key file to use: ");
  56.     gets(instr);
  57.     header.file = open_keyfile(instr, &header);
  58.         /* must allocate space for the key pointers */
  59.     key1 = malloc(header.keylength + 1);
  60.     current_node.key = malloc(header.keylength + 1);
  61.     cursor(1,1);
  62.     CLS;
  63.       while ((choice = main_prompt("Choice: ")) != QUIT) {
  64.         if (choice == FIND) {
  65.             cursor(1,1);
  66.             CLS;
  67.             get_key("Key to Find: ", key1, &header);
  68.             if(find_key(key1, &node_nbr, ¤t_node, &header)==NOT_FOUND) {
  69.                 print_msg("Key not found");
  70.                 show_rec(¤t_node);  /* show key node "found" */
  71.             }
  72.             else if(node_nbr == END)  /* no undeleted node found */
  73.                 printf("node_nbr == END\n");  /* tree "empty" */
  74.             else
  75.                 show_rec(¤t_node);  /* exact match found */
  76.         }
  77.         else if (choice == NEXT) {
  78.             cursor(1,1);
  79.             CLS;
  80.             ym1:
  81.             sentinel=0;
  82.             switch (get_next(&node_nbr, ¤t_node, &header)) {
  83.                 case(NO):            
  84.                     sentinel=1;    /* no current node */
  85.                     break;  
  86.                 case(YES):
  87.                     show_rec(¤t_node); /* found one */
  88.                     break;    
  89.                 case(AT_END):
  90.                     print_msg("At end of file");
  91.                     show_rec(¤t_node);
  92.             }
  93.             if (sentinel == 1) goto ym1;
  94.         }
  95.         else if (choice == PREVIOUS) {
  96.             cursor(1,1);
  97.             CLS;
  98.             ym2:
  99.         sentinel = 0;
  100.             switch (get_previous(&node_nbr, ¤t_node, &header)) {
  101.                 case(NO):            
  102.                     sentinel=1;    /* no current node */
  103.                     break;  
  104.                 case(YES):
  105.                     show_rec(¤t_node); /* found one */
  106.                     break;    
  107.                 case(AT_END):
  108.                     print_msg("At end of file");
  109.                     show_rec(¤t_node);
  110.             }
  111.             if (sentinel == 1) goto ym2;
  112.         }
  113.         else if (choice == FIRST) {
  114.             cursor(1,1);
  115.             CLS;
  116.             switch (get_first(&node_nbr, ¤t_node, &header)) {
  117.                 case(YES):
  118.                     show_rec(¤t_node); /* found first */
  119.                     break;    
  120.                 case(NO):
  121.                     print_msg("Must FIND a record first");
  122.                     show_rec(¤t_node);
  123.             }
  124.         }
  125.         else if (choice == LAST) {
  126.             cursor(1,1);
  127.             CLS;
  128.             switch (get_last(&node_nbr, ¤t_node, &header)) {
  129.                 case(YES):
  130.                     show_rec(¤t_node); /* found last */
  131.                     break;    
  132.                 case(NO):
  133.                     print_msg("Must FIND a record first");
  134.                     show_rec(¤t_node);
  135.             }
  136.         }
  137.         else if (choice == INSERT) {
  138.             cursor(1,1);
  139.             CLS;
  140.             enter_data(¤t_node, &header);  /* get key to insert */
  141.             insert(current_node.key, header.next_avail, &header);
  142.         }
  143.         else if (choice == DELETE) {
  144.             if (yes_or_no("Delete this record? (y/n) ") == YES)
  145.                 delete_key(node_nbr, ¤t_node, &header);
  146.         }
  147.     }  /* QUIT chosen by user */
  148.     close_keyfile(&header);
  149.     exit(0);
  150. }
  151.  
  152.  
  153. void show_rec(struct node *current_node)
  154. /*
  155. struct node *current_node;
  156. */
  157. {
  158.     cursor(1,1);
  159.     CLS;
  160.     print_node(current_node);
  161. }
  162.  
  163. void enter_data(struct node *current_node, struct keyinfo *fileinfo)
  164. /*
  165. struct keyinfo *fileinfo;
  166. struct node *current_node;
  167. */
  168. {
  169.     printf("Enter a key: ");
  170.     gets(instr);
  171.     strncpy(current_node->key, instr, fileinfo->keylength);
  172.     current_node->key[fileinfo->keylength] = '\0';
  173. }
  174.