home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / dskusage / dskusage.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  4.6 KB  |  108 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*-------------------------------------------------------------*\
  19. |  dskusage.CPP -  Disk Usage Information is handled using      |
  20. |                  a Sorted Map and a Sorted Relation.          |
  21. |                    """"""""""       """""""""""""""           |
  22. |                                                               |
  23. |  This program reads a file containing disk space usage        |
  24. |  records 'DiskSpaceUR'. Each record is added into two         |
  25. |  Equality Key Sorted Collections.                             |
  26. |                                                               |
  27. |  One of these collections, DSURbyName, is a Sorted Map.       |
  28. |  The key is the name, which is unique.                        |
  29. |                                                               |
  30. |  The other collection, DSURbySpace, is a Sorted Relation.     |
  31. |  The key is the space, which can occur multiple times.        |
  32. |                                                               |
  33. |  Using the iteration method allElementsDo, both Collections   |
  34. |  then print their contents in the sorted order.               |
  35. |                                                               |
  36. |  Note: If we could be sure that there would never be two      |
  37. |        identical records in the input data, it would be better|
  38. |        to use the corresponding collections that do not need  |
  39. |        element equality.  These are KeySorted Set and         |
  40. |        KeySorted Bag instead of Sorted Map and                |
  41. |        Sorted Relation.                                       |
  42. \*-------------------------------------------------------------*/
  43.  
  44.  
  45.  
  46. #include "dsur.h"
  47.                        // Our own common exit for all errors:
  48. void errorExit(int, char*, char* = "");
  49.  
  50.                        // Use the default Sorted Map as is:
  51. #include <ism.h>
  52.                        // Use the default Sorted Relation as is:
  53. #include <isr.h>
  54.  
  55. int main (int argc, char* argv[])
  56. { char* fspec = "dsu.dat"; // Default for input file
  57.  
  58.   if (argc > 1)   fspec = argv[1];
  59.  
  60.   ifstream  inputfile (fspec);
  61.   if (!inputfile)
  62.     errorExit(20, "Unable to open input file", fspec);
  63.  
  64.   ISortedMap <DiskSpaceUR, char*> dsurByName;
  65.   ISortedMap <DiskSpaceUR, char*>::Cursor curByName (dsurByName);
  66.  
  67.   IGSortedRelation <DiskSpaceUR, int, DSURBySpaceOps>
  68.          dsurBySpace;
  69.   IGSortedRelation <DiskSpaceUR, int, DSURBySpaceOps>::Cursor
  70.          curBySpace(dsurBySpace);
  71.  
  72.                            // Read all records into dsurByName
  73.   while (inputfile.good()) {
  74.     DiskSpaceUR dsur (inputfile);
  75.     if (dsur.isValid ()) {
  76.       dsurByName.add (dsur);
  77.       dsurBySpace.add (dsur);
  78.     }
  79.   }
  80.   if (! inputfile.eof ())
  81.     errorExit (39, "Error during read of", fspec);
  82.  
  83.   cout << endl << endl
  84.        << "All Disk Space Usage records "
  85.        << "sorted (ascending) by name:" << endl << endl;
  86.  
  87.   forICursor (curByName)
  88.     cout << "  " << dsurByName.elementAt (curByName) << endl;
  89.  
  90.   cout << endl << endl
  91.        << "All Disk Space Usage records "
  92.        << "sorted (descending) by space:" << endl << endl;
  93.  
  94.   for (curBySpace.setToLast ();
  95.        curBySpace.isValid ();
  96.        curBySpace.setToPrevious ())
  97.     cout << "  " << dsurBySpace.elementAt (curBySpace) << endl;
  98.  
  99.   return 0;
  100. }
  101.  
  102. #include <stdlib.h> // for exit () definition
  103.  
  104. void errorExit (int rc, char* s1, char* s2)
  105. { cerr << s1 << " " << s2 << endl;
  106.   exit (rc);
  107. }
  108.