home *** CD-ROM | disk | FTP | other *** search
- /*
- * Example 6: BTree-on-disk. Retrieves birthdays, given a name
- *
- * $Header: E:/vcs/toolexam/example6.cpv 1.1 01 Apr 1992 16:51:12 keffer $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-2311 FAX: (503) 757-7350
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/toolexam/example6.cpv $
- *
- * Rev 1.1 01 Apr 1992 16:51:12 keffer
- * Now includes <rw/xxx.h>
- *
- */
-
- #include <rw/disktree.h>
- #include <rw/filemgr.h>
- #include <rw/rwdate.h>
- #include <rw/cstring.h>
- #include <rw/rstream.h>
-
- static const int TRIM = KEY_SIZE-1;
-
- // Function to print a key and value:
- void
- printKey(const char* a, storedValue sv, void* x)
- {
- RWDate bday;
- RWFileManager& fm = *(RWFileManager*)x;
- fm.SeekTo(sv);
- bday.restoreFrom(fm);
- cout << "Name: " << a << " birthday: " << bday << NL;
- }
-
- main()
- {
- RWCString name, filename, response;
- RWDate birthday;
- RWoffset loc;
-
- cout << " RWBTreeOnDisk sample program.\n";
- cout << " Stores names and birthdays in a BTree.\n";
-
- cout << "Enter either the name of a new file or of a pre-existing file: ";
- cin >> filename;
-
- RWFileManager fmgr(filename.data());
- RWBTreeOnDisk btod(fmgr);
-
- // Determine whether B-Tree is empty or not:
- if (btod.isEmpty())
- cout << "Tree is empty.\n";
- else
- cout << "Tree has " << btod.entries() << " entries.\n";
-
- // Loop to do various things to the tree:
-
- while (1) {
- cout << "(i)ns (n)umber of entries (f)ind (d)el (l)ist (c)lear e(x)it: ";
-
- /*
- * Read the character; check for EOF.
- */
- if ( !(cin >> response) ) break;
-
- if ( response(0) == 'x' || response(0) == 'X' ) break; // Check for exit
-
- switch ( response(0) ) {
-
- // Insert a key-value pair.
- case 'i':
- case 'I':
-
- cout << "Person's name: ";
- name.readLine(cin); // Get the person's name.
- name.resize(TRIM); // Trim to the size of the B-Tree key.
- cout << "Birthday: "; // Get the person's birthday.
- cin >> birthday;
- if( birthday.isValid() ){
- loc = fmgr.allocate(birthday.binaryStoreSize());
- fmgr.SeekTo(loc);
- birthday.saveOn(fmgr);
- btod.insertKeyAndValue(name.data(), loc); // Remember the location.
- } else {
- cin.clear();
- cout << "Bad date.\n";
- }
- break;
-
- // Find a person's birthday:
- case 'f':
- case 'F':
-
- cout << "Person's name: ";
- name.readLine(cin); // Get the person's name, to use as the key.
- name.resize(TRIM); // Trim to the size of the B-Tree key.
- btod.findKeyAndValue(name.data(), loc); // Go find the birthday location.
- if( loc==NIL )
- cout << "Not found.\n";
- else {
- fmgr.SeekTo(loc);
- birthday.restoreFrom(fmgr); // Retrieve the birthday.
- cout << "Birthday is " << birthday << NL;
- }
- break;
-
- // Delete a person:
- case 'd':
- case 'D':
-
- cout << "Name of person to delete: ";
- name.readLine(cin);
- name.resize(TRIM);
- btod.remove(name.data());
- break;
-
- // List the number of entries:
- case 'n':
- case 'N':
-
- cout << "Number of items in tree: " << btod.entries() << NL;
- break;
-
- // Clear the tree:
- case 'c':
- case 'C':
-
- btod.clear(); // Remove all keys.
- break;
-
- // Print all entries:
- case 'l':
-
- btod.applyToKeyAndValue(printKey, &fmgr);
- break;
-
- default:
-
- cerr << "Input not recognized...try again\n";
-
- } // end switch
- }
- return 0;
- }
-