home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 9 < prev    next >
Encoding:
Text File  |  1992-04-01  |  3.8 KB  |  154 lines

  1. /*
  2.  * Example 6: BTree-on-disk.  Retrieves birthdays, given a name
  3.  *
  4.  * $Header:   E:/vcs/toolexam/example6.cpv   1.1   01 Apr 1992 16:51:12   keffer  $
  5.  *
  6.  ****************************************************************************
  7.  *
  8.  * Rogue Wave Software, Inc.
  9.  * P.O. Box 2328
  10.  * Corvallis, OR 97339
  11.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  12.  *
  13.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  14.  * protection under the laws of the United States and other countries.
  15.  *
  16.  ***************************************************************************
  17.  *
  18.  * $Log:   E:/vcs/toolexam/example6.cpv  $
  19.  * 
  20.  *    Rev 1.1   01 Apr 1992 16:51:12   keffer
  21.  * Now includes <rw/xxx.h>
  22.  * 
  23.  */
  24.  
  25. #include <rw/disktree.h>
  26. #include <rw/filemgr.h>
  27. #include <rw/rwdate.h>
  28. #include <rw/cstring.h>
  29. #include <rw/rstream.h>
  30.  
  31. static const int TRIM = KEY_SIZE-1;
  32.  
  33. // Function to print a key and value:
  34. void 
  35. printKey(const char* a, storedValue sv, void* x)
  36. {
  37.   RWDate bday;
  38.   RWFileManager& fm = *(RWFileManager*)x;
  39.   fm.SeekTo(sv);
  40.   bday.restoreFrom(fm);
  41.   cout << "Name: " << a << " birthday: " << bday << NL;
  42. }
  43.  
  44. main()
  45. {
  46.   RWCString    name, filename, response;
  47.   RWDate    birthday;
  48.   RWoffset    loc;
  49.  
  50.   cout << "       RWBTreeOnDisk sample program.\n";
  51.   cout << "       Stores names and birthdays in a BTree.\n";
  52.  
  53.   cout << "Enter either the name of a new file or of a pre-existing file: ";
  54.   cin >> filename;
  55.  
  56.   RWFileManager fmgr(filename.data());
  57.   RWBTreeOnDisk btod(fmgr);
  58.  
  59.   // Determine whether B-Tree is empty or not:
  60.   if (btod.isEmpty())    
  61.     cout << "Tree is empty.\n";
  62.   else
  63.     cout << "Tree has " << btod.entries() << " entries.\n";
  64.   
  65.   // Loop to do various things to the tree:
  66.  
  67.   while (1) {
  68.     cout << "(i)ns (n)umber of entries (f)ind (d)el (l)ist (c)lear e(x)it: ";
  69.  
  70.     /*
  71.      * Read the character; check for EOF.
  72.      */
  73.     if ( !(cin >> response) ) break;
  74.  
  75.     if ( response(0) == 'x' || response(0) == 'X' ) break; // Check for exit
  76.   
  77.     switch ( response(0) ) {
  78.  
  79.     // Insert a key-value pair.
  80.     case 'i':
  81.     case 'I':
  82.  
  83.       cout << "Person's name: ";
  84.       name.readLine(cin);    // Get the person's name.
  85.       name.resize(TRIM);    // Trim to the size of the B-Tree key.
  86.       cout << "Birthday: ";    // Get the person's birthday.
  87.       cin >> birthday;
  88.       if( birthday.isValid() ){
  89.         loc = fmgr.allocate(birthday.binaryStoreSize());
  90.         fmgr.SeekTo(loc);
  91.         birthday.saveOn(fmgr);
  92.         btod.insertKeyAndValue(name.data(), loc);    // Remember the location.
  93.       } else {
  94.         cin.clear();
  95.         cout << "Bad date.\n";
  96.       }
  97.       break;
  98.  
  99.     // Find a person's birthday:
  100.     case 'f':
  101.     case 'F':
  102.  
  103.       cout << "Person's name: ";
  104.       name.readLine(cin);    // Get the person's name, to use as the key.
  105.       name.resize(TRIM);    // Trim to the size of the B-Tree key.
  106.       btod.findKeyAndValue(name.data(), loc); // Go find the birthday location.
  107.       if( loc==NIL ) 
  108.     cout << "Not found.\n";
  109.       else {
  110.     fmgr.SeekTo(loc);
  111.     birthday.restoreFrom(fmgr); // Retrieve the birthday.
  112.     cout << "Birthday is " << birthday << NL;
  113.       }
  114.       break;
  115.  
  116.     // Delete a person:
  117.     case 'd':
  118.     case 'D':
  119.  
  120.       cout << "Name of person to delete: ";
  121.       name.readLine(cin);
  122.       name.resize(TRIM);
  123.       btod.remove(name.data());
  124.       break;
  125.  
  126.     // List the number of entries:
  127.     case 'n':
  128.     case 'N':
  129.  
  130.       cout << "Number of items in tree: " << btod.entries() << NL;
  131.       break;
  132.  
  133.     // Clear the tree:
  134.     case 'c':
  135.     case 'C':
  136.  
  137.        btod.clear();        // Remove all keys.
  138.        break;
  139.  
  140.     // Print all entries:
  141.     case 'l':
  142.  
  143.        btod.applyToKeyAndValue(printKey, &fmgr);
  144.        break;
  145.  
  146.     default:
  147.  
  148.       cerr << "Input not recognized...try again\n";
  149.  
  150.     }  // end switch
  151.   }
  152.   return 0;
  153. }
  154.