home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / DSUR.H < prev    next >
C/C++ Source or Header  |  1993-05-07  |  5KB  |  141 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. |  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 = 62;
  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.      Boolean 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 Boolean 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)
  138.        { return dsur.space(); }
  139.   };
  140.  
  141.