home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / DSKUSAGE.C < prev    next >
Text File  |  1993-05-07  |  5KB  |  112 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  dskusage.C -  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 <isrtmap.h>
  52.                        // Use the default Sorted Relation as is:
  53.   #include <isrtrel.h>
  54.  
  55.   int main (int argc, char* argv[])
  56.   {
  57.  
  58.       char* fspec = "dsu.dat"; // Default for input file
  59.  
  60.       if (argc > 1)   fspec = argv[1];
  61.  
  62.       ifstream  inputfile(fspec);
  63.       if (!inputfile)
  64.           errorExit(20, "Unable to open input file", fspec);
  65.  
  66.       ISortedMap     <DiskSpaceUR, char*> dsurByName;
  67.       ISortedMap     <DiskSpaceUR, char*>::Cursor
  68.                                            curByName(dsurByName);
  69.  
  70.       IGSortedRelation <DiskSpaceUR, int, DSURBySpaceOps>
  71.          dsurBySpace;
  72.       IGSortedRelation <DiskSpaceUR, int, DSURBySpaceOps>::Cursor
  73.          curBySpace(dsurBySpace);
  74.  
  75.                            // Read all records into dsurByName
  76.       while (inputfile.good())
  77.       {
  78.          DiskSpaceUR dsur(inputfile);
  79.          if (dsur.isValid())
  80.          {
  81.             dsurByName.add(dsur);
  82.             dsurBySpace.add(dsur);
  83.          }
  84.       }
  85.       if (! inputfile.eof())
  86.                errorExit(39, "Error during read of", fspec);
  87.  
  88.       cout << "\n\nAll Disk Space Usage records "
  89.            << "sorted (ascending) by name:\n\n";
  90.  
  91.       forCursor(curByName)
  92.          cout << "  " << dsurByName.elementAt(curByName) << "\n";
  93.  
  94.       cout << "\n\nAll Disk Space Usage records "
  95.            << "sorted (descending) by space:\n\n";
  96.  
  97.       forCursor(curBySpace)
  98.          cout << "  " << dsurBySpace.elementAt(curBySpace) << "\n";
  99.  
  100.       return 0;
  101.   }
  102.  
  103.   #include <stdlib.h>
  104.                                       // for exit() definition
  105.  
  106.   void errorExit (int rc, char* s1, char* s2)
  107.   {
  108.  
  109.       cerr << s1 << " " << s2 << "\n";
  110.       exit(rc);
  111.   }
  112.