home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / btree.zip / EXERCISE.CPP < prev    next >
C/C++ Source or Header  |  1992-02-16  |  8KB  |  355 lines

  1. #include <stdio.h>
  2. #include "dbfiles.hpp"
  3. #include "btree.hpp"
  4. long atol();
  5. void pad(char *, int);
  6. int compound_compare(union Key * , struct KeyGroup * , int );
  7.  
  8. main(int argc,char **argv)
  9. {
  10. char     indxbuff[40];
  11. char     buff1[65];
  12. char     buff2[65];
  13. int     dups,reply,quit,index_exists,tfd,tok,lng,stat,print_key;
  14. struct     KeyGroup     k,*kp;
  15. union     Key         bk;
  16. long    lk;
  17. double    dk;
  18.  
  19.     printf("==========================================================\n");
  20.     printf("        Test program to exercise the B+Tree\n");
  21.     printf("Copr. 1991 Larry A. Walker & Co. Sterling, Va 22170\n");
  22.     printf("==========================================================\n");
  23.  
  24.     //
  25.     // Get the name of the index
  26.     //
  27.     if(argc == 2){
  28.         strcpy(indxbuff,*(argv+1));
  29.     }else{
  30.         printf("\n\nWhat is the name of the index\n");
  31.         gets(indxbuff);
  32.     }
  33.     index_exists = ( (tfd=open(indxbuff,O_RDONLY)) == -1 )?0:1;
  34.     if(index_exists){
  35.         close(tfd);
  36.         dups = 0;
  37.         lng = 0;
  38.         tok = UNDEF_KEY;
  39.     }else{
  40.         //
  41.         // Duplicates required needed for opening new index
  42.         //
  43.         printf("1.  Duplicates allowed.\n");
  44.         printf("2.  No duplicates allowed.\n");
  45.         printf("\nEnter a number to select an item.\n: ");
  46.         gets(buff1);
  47.         dups=atoi(buff1);
  48.         if( (dups < 1) || (dups > 2) ){
  49.             fprintf(stderr,"Must be either 1 or 2\n");
  50.             exit(1);
  51.         }
  52.         dups=(dups == 2)?0:1;
  53.         //
  54.         // Type of key required for opening new index
  55.         //
  56.         printf("Please enter the type of key to use.\n");
  57.         printf("%d.  Variable length keys\n",VAR_LNG_KEY);
  58.         printf("%d.  Fixed length keys\n",FIX_LNG_KEY);
  59.         printf("%d.  Long keys\n",LONG_KEY);
  60.         printf("%d.  Double keys\n",DOUBLE_KEY);
  61.         printf("\nEnter a number to select an item.\n: ");
  62.         gets(buff1);
  63.         tok=atoi(buff1);
  64.         switch(tok){
  65.             case VAR_LNG_KEY:
  66.                 lng = -1;
  67.                 break;
  68.             case FIX_LNG_KEY:
  69.                 printf("Please enter the key length.\n: ");
  70.                 gets(buff1);
  71.                 lng = atoi(buff1);
  72.                 break;
  73.             case LONG_KEY:
  74.                 lng = sizeof(long);
  75.                 break;
  76.             case DOUBLE_KEY:
  77.                 lng = sizeof(double);
  78.                 break;
  79.             default:
  80.                 fprintf(stderr,"Invalid response.\n");
  81.                 exit(1);
  82.                 break;
  83.         }
  84.     }
  85.     // index_name, create, type of key, 
  86.     // duplicates, fixed length key size
  87.     
  88.     Btree tree(indxbuff, (index_exists)?0:CREATE, tok, dups, lng);
  89.     // If you already know the index exists, you can open it
  90.     // with default parameters as "Btree tree(indxbuff)"
  91.  
  92.     // test the install routine
  93.     
  94.     if(tok == UNDEF_KEY){
  95.         // then, we opened an index but don't know anything about
  96.         // it, we can however get the information
  97.         tok = (int)tree.ReportState(TYPE_OF_KEY);
  98.         dups = (int)tree.ReportState(DUPLICATES);
  99.         lng = (int)tree.ReportState(KEY_LENGTH);
  100.         printf("Type of key = ");
  101.         switch(tok){
  102.             case VAR_LNG_KEY:
  103.                 printf("VAR_LNG_KEY");
  104.                 break;
  105.             case FIX_LNG_KEY:
  106.                 printf("FIX_LNG_KEY");
  107.                 break;
  108.             case LONG_KEY:
  109.                 printf("LONG_KEY");
  110.                 break;
  111.             case DOUBLE_KEY:
  112.                 printf("DOUBLE_KEY");
  113.                 break;
  114.         }
  115.         printf(" duplicates = %s ",(dups)?"YES":"NO");
  116.         printf(" key length = %d ",lng);
  117.         printf("\n number of keys = %ld, last access %ld\n", tree.ReportState(NUMBER_OF_KEYS),tree.ReportState(LAST_ACCESS) );
  118.     }
  119.     printf("Turn on print error log? (y/n)\n: ");
  120.     gets(buff2);
  121.     if((buff2[0] == 'y') || (buff2[0] == 'Y')){
  122.         tree.set_display(ON,stderr);
  123.         tree.set_log(ON,"mylog",1); // third param 0 would be to truncate
  124.                       // defaults to 1
  125.     }
  126.     quit=0;
  127.     if(tok == FIX_LNG_KEY){
  128.         if(tree.InstallCompare(compound_compare) == SUCCESS)
  129.             printf("Installed key compare routine\n");
  130.     }
  131.     while(!quit) {
  132.     
  133.         printf( "\n===========================================\n");
  134.         printf( "|Pick an operation to perform on the index|\n");
  135.         printf( "|1.  Find a key   2.  Insert a key        |\n");
  136.         printf( "|3.  Delete a key 4.  Next key            |\n");
  137.         printf( "|5.  Prior key    6.  First key           |\n");
  138.         printf( "|7.  Last key     8.  Current key         |\n");
  139.         printf( "|9.  Quit                                 |\n");
  140.         printf( "===========================================\n");
  141.         printf( ": ");
  142.         gets(buff2);
  143.         reply = atoi(buff2);
  144.         print_key = 0;
  145.         switch(reply) {
  146.             case 1:
  147.                 printf("Check index for what key?\n: ");
  148.                 gets(buff2);
  149.                 printf("finding ->%s<-\n",buff2);
  150.                 switch(tok){
  151.                     case VAR_LNG_KEY:
  152.                         create_index_string(bk.key,buff2);
  153.                         break;
  154.                     case FIX_LNG_KEY:
  155.                         if(strlen(buff2) < lng )
  156.                             pad(buff2,lng);
  157.                         memcpy(bk.key,buff2,lng);
  158.                         break;
  159.                     case LONG_KEY:
  160.                         bk.long_key = atol(buff2);
  161.                         break;
  162.                     case DOUBLE_KEY:
  163.                         bk.double_key = atof(buff2);
  164.                         break;
  165.                 }
  166.                 if(tree.Locate(&bk) == FAIL) {
  167.                     printf("not in index\n");
  168.                     break;
  169.                 } else {
  170.                     if( (kp = tree.CurrentKey()) == NULL) {
  171.                         printf("No current key\n");
  172.                         break;
  173.                     }
  174.                 }
  175.                 print_key = 1;
  176.                 break;
  177.     
  178.             case 3:
  179.                 printf("What key to delete\n");
  180.                 gets(buff2);
  181.                 switch(tok){
  182.                     case VAR_LNG_KEY:
  183.                         create_index_string(bk.key,buff2);
  184.                         break;
  185.                     case FIX_LNG_KEY:
  186.                         if(strlen(buff2) < lng )
  187.                             pad(buff2,lng);
  188.                         memcpy(bk.key,buff2,lng);
  189.                         break;
  190.                     case LONG_KEY:
  191.                         bk.long_key = atol(buff2);
  192.                         break;
  193.                     case DOUBLE_KEY:
  194.                         bk.double_key = atof(buff2);
  195.                         break;
  196.                 }
  197.                 if( (stat = tree-=(&bk)) == FAIL) {
  198.                     printf("Key not deleted\n");
  199.                 }
  200.                 else printf("Successful delete\n");
  201.                 break;
  202.         
  203.             case 2:
  204.                 printf("What key to add\n: ");
  205.                 gets(buff1);
  206.                 switch(tok){
  207.                     case VAR_LNG_KEY:
  208.                         create_index_string(k.k.key,buff1);
  209.                         break;
  210.                     case FIX_LNG_KEY:
  211.                         if(strlen(buff1) < lng )
  212.                             pad(buff1,lng);
  213.                         memcpy(k.k.key,buff1,lng);
  214.                         break;
  215.                     case LONG_KEY:
  216.                         k.k.long_key = atol(buff1);
  217.                         break;
  218.                     case DOUBLE_KEY:
  219.                         k.k.double_key = atof(buff1);
  220.                         break;
  221.                 }
  222.                 printf("What data\n: ");
  223.                 gets(buff2);
  224.                 k.address = atol(buff2);
  225.                 if( (stat = tree+= (&k) ) == FAIL){
  226.                     printf("Key add failed\n");
  227.                 }
  228.                 break;
  229.         
  230.             case 4:
  231.     
  232.                 kp = ++tree;
  233.                 if(kp == NULL){
  234.                     printf("next key not found\n");
  235.                     break;
  236.                 }
  237.                 print_key = 1;
  238.                 break;
  239.         
  240.             case 5:
  241.     
  242.                 kp = --tree;
  243.                 if(kp == NULL){
  244.                     printf("previous key not found\n");
  245.                     break;
  246.                 }
  247.                 print_key = 1;
  248.                 break;
  249.     
  250.             case 6:
  251.     
  252.                 kp=tree.First();
  253.                 if(kp == NULL){
  254.                     printf("first key not found\n");
  255.                     break;
  256.                 }
  257.                 print_key = 1;
  258.                 break;
  259.         
  260.             case 7:
  261.     
  262.                 kp=tree.Last();
  263.                 if(kp == NULL){
  264.                     printf("last key not found\n");
  265.                     break;
  266.                 }
  267.                 print_key = 1;
  268.                 break;
  269.         
  270.             case 8:
  271.     
  272.                 if((kp=tree.CurrentKey()) == NULL) {
  273.                     printf("No current key\n");
  274.                     break;
  275.                 }
  276.                 print_key = 1;
  277.                 break;
  278.     
  279.             case 9:
  280.                 quit=1;
  281.                 break;
  282.             }
  283.         switch(reply) {
  284.             // these items display the key in kp
  285.             case 1:
  286.             case 4:
  287.             case 5:
  288.             case 6:
  289.             case 7:
  290.             case 8:
  291.             if(print_key){
  292.                 switch(tok){
  293.                     case VAR_LNG_KEY:
  294.                         create_c_string(buff1,kp->k.key);
  295.                         printf("found ->%s<-\ndata -> %ld\n",buff1, kp->address);
  296.                         break;
  297.                     case FIX_LNG_KEY:
  298.                         // this doesn't take care of 
  299.                         // a fix length key being
  300.                         // a struct of info
  301.                         memcpy(buff1,kp->k.key,lng);
  302.                         buff1[lng] = 0;
  303.                             
  304.                         printf("found ->%s<-\ndata -> %ld\n",buff1, kp->address);
  305.                         break;
  306.                     case LONG_KEY:
  307.                         printf("found ->%ld<-\ndata -> %ld\n",kp->k.long_key, kp->address);
  308.                         break;
  309.                     case DOUBLE_KEY:
  310.                         printf("found ->%f<-\ndata -> %ld\n",kp->k.double_key, kp->address);
  311.                         break;
  312.                     }
  313.                 }
  314.             }
  315.     }
  316.     tree.Close();
  317.     exit(0);
  318. }
  319.  
  320. //
  321. // pad an area with null values to a length
  322. //
  323. void pad( char *ptr ,int lng)
  324. {
  325.     int i;
  326.     for(i = strlen(ptr); i < lng; i++ ){
  327.         *(ptr+i) = (char)0;
  328.     }
  329. }
  330.  
  331.  
  332.  
  333. //
  334. // Function:    compound_compare
  335. //
  336. // Discussion:    Compare two compound values, returning the result
  337. //
  338. // Returns:    -1 LT, 0 EQ, 1 GT
  339. //
  340. //
  341. int compound_compare(union Key * value, struct KeyGroup * active_key, int lng){
  342. char *ptr,*ptr1;
  343.     ptr=(char *)value->key;
  344.     ptr1=(char *)active_key->k.key;
  345.     for(int i = 0; i < lng;i++){
  346.         if(*ptr < *ptr1    )
  347.             return -1;
  348.         if(*ptr > *ptr1    )
  349.             return 1;
  350.         ptr++;ptr1++;
  351.     }
  352.     return 0;
  353. }
  354.  
  355.