home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / dskusage / dsur.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  4.6 KB  |  141 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. |  dsur.h  -  Class for Disk Space Usage Records                |
  20. |             This is used by the coding sample for the         |
  21. |             Sorted Map and the Sorted Relation.               |
  22. \*-------------------------------------------------------------*/
  23.  
  24.   #include <fstream.h>
  25.   #include <string.h>
  26.  
  27.   #include <iglobals.h>
  28.  
  29.   const int bufSize = 60;
  30.  
  31.   class DiskSpaceUR    {
  32.      int       blocks;
  33.      char*     name;
  34.  
  35.    public:
  36.      DiskSpaceUR() {}
  37.  
  38.      DiskSpaceUR (DiskSpaceUR const& dsur)  {
  39.         init(dsur);
  40.      }
  41.  
  42.      void operator= (DiskSpaceUR const& dsur)   {
  43.         deInit();
  44.         init(dsur);
  45.      }
  46.  
  47.      DiskSpaceUR (istream& DSURfile)     {
  48.         DSURfile >> *this;
  49.      }
  50.  
  51.      ~DiskSpaceUR () { deInit(); }
  52.  
  53.      IBoolean operator == (DiskSpaceUR const& dsur) const  {
  54.        return (blocks == dsur.blocks)
  55.            && strcmp (name, dsur.name) == 0;
  56.      }
  57.  
  58.  
  59.      friend istream& operator >> (istream& DSURfile,
  60.                                   DiskSpaceUR& dsur)     {
  61.          DSURfile >> dsur.blocks;
  62.  
  63.          char temp[bufSize];
  64.          DSURfile.get(temp, bufSize);
  65.  
  66.          if (DSURfile.good())  {
  67.                                 // Remove leading tabs and blanks
  68.             for (int cnt=0;
  69.                  (temp[cnt] == '\t') || (temp[cnt] == ' ');
  70.                  cnt++) {}
  71.             dsur.name = new char[strlen(temp+cnt)+1];
  72.             strcpy(dsur.name, temp+cnt);
  73.          }
  74.          else   {
  75.             dsur.setInvalid();
  76.             dsur.name = new char[1];
  77.             dsur.name[0] = '\0';
  78.          }
  79.  
  80.          return DSURfile;
  81.      }
  82.  
  83.      friend ostream& operator << (ostream& outstream,
  84.                                   DiskSpaceUR& dsur)     {
  85.          outstream.width(bufSize);
  86.          outstream.setf(ios::left, ios::adjustfield);
  87.          outstream << dsur.name;
  88.  
  89.          outstream.width(9);
  90.          outstream.setf(ios::right, ios::adjustfield);
  91.          outstream << dsur.blocks;
  92.  
  93.          return outstream;
  94.      }
  95.  
  96.      inline int const& space () const {return blocks;}
  97.  
  98.      inline char* const& id () const {return name;}
  99.  
  100.      inline IBoolean isValid () const {return (blocks > 0);}
  101.  
  102.    protected:
  103.  
  104.      inline void init (DiskSpaceUR const& dsur)     {
  105.         blocks = dsur.blocks;
  106.         name = new char[strlen(dsur.name) + 1];
  107.         strcpy(name, dsur.name);
  108.      }
  109.  
  110.      inline void deInit() {  delete[] name;  }
  111.  
  112.      inline void setInvalid () { blocks = -1;}
  113.   };
  114.  
  115.  
  116.      // Key access on name
  117.   inline  char* const& key (DiskSpaceUR const& dsur)  {
  118.      return dsur.id();
  119.   }
  120.  
  121.      // Key access on space used
  122.      // Since we can not have two key functions with same args
  123.      // in global name space, we need to use an operations class.
  124.   #include <istdops.h>
  125.      // We can inherit all from the default operations class
  126.      // and then define just the key access function ourselfes.
  127.      // We can not use StdKeyOps here, because the in turn
  128.      // use the key function in global name space, which is
  129.      // already defined for keys of type char* above.
  130.   class DSURBySpaceOps :  public IStdMemOps,
  131.                           public IStdAsOps< DiskSpaceUR >,
  132.                           public IStdEqOps< DiskSpaceUR >    {
  133.     public:
  134.        IStdCmpOps < int > keyOps;
  135.  
  136.      // Key Access
  137.        int const& key (DiskSpaceUR const& dsur) const
  138.        { return dsur.space(); }
  139.   };
  140.  
  141.