home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / include / krprof.hpp < prev    next >
Text File  |  1997-02-18  |  5KB  |  151 lines

  1. // Kroni's Classes: objects for reading and writing ini-file data
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: krprof.hpp
  4.  
  5. // **********************************************************************************************************
  6. //
  7. // defines these classes:
  8. //
  9. //   KrProfile               Acts like a stream for in- and output of ini-file data
  10. //
  11. // defines those global symbols for private use:
  12. //
  13. //   _KrProfileBase
  14. //   _KrProfileBuf
  15. //
  16. // **********************************************************************************************************
  17.  
  18.  
  19. #ifndef __KRPROF_HPP__
  20. #define __KRPROF_HPP__
  21.  
  22.  
  23. #include <istring.hpp>
  24. #include <strstrea.h>
  25. #include <ihandle.hpp>
  26. #include <iomanip.h>
  27.  
  28.  
  29. class _KrProfileBase;                            // forward declarator for _KrProfileBuf
  30.  
  31. class _KrProfileBufCons : public strstreambuf    // The ultimate Profile string consumer
  32. {
  33.  
  34. public:
  35.  
  36.   _KrProfileBufCons (_KrProfileBase & aParent);
  37.  
  38.   virtual int sync();
  39.   virtual int overflow (int c=EOF);
  40.   virtual int underflow ();
  41.  
  42.  
  43. private:
  44.  
  45.   _KrProfileBase * parent;
  46.  
  47. };
  48.  
  49.  
  50. class _KrProfileBuf : public streambuf           // The buffer class attached to KrProfile
  51. {
  52.  
  53. public:
  54.  
  55.   _KrProfileBuf (_KrProfileBase & aParent);
  56.   ~_KrProfileBuf ();
  57.  
  58.   void renewBuffer ();                           // Discard the _KrProfileBufCons and get a new one
  59.  
  60.   virtual int sync();
  61.   virtual int overflow (int c=EOF);
  62.   virtual int underflow ();
  63.   virtual int doallocate ();
  64.  
  65.   _KrProfileBufCons * buf();                     // Returns the ultimate consumer buffer
  66.   iostream * stream();                           // Returns the ultimate consumer
  67.  
  68. private:
  69.  
  70.   iostream * theStream;
  71.   _KrProfileBufCons * buffer;                    // The ultimate consumer buffer
  72.   _KrProfileBase * parent;
  73.  
  74. };
  75.  
  76.  
  77.  
  78. class _KrProfileBase
  79. {
  80.  
  81. public:
  82.  
  83.   _KrProfileBase (const IString & aApp, Boolean iBuffered);
  84.   ~_KrProfileBase ();
  85.  
  86.   virtual void writeData (Boolean callFromBuff) = 0;
  87.   virtual void readData () = 0;
  88.  
  89.  
  90. protected:
  91.  
  92.   _KrProfileBuf * buf;
  93.   Boolean buffered;
  94.   IString keyName, appName;
  95.  
  96. };
  97.  
  98.  
  99. class KrProfile : public _KrProfileBase, public iostream
  100. {
  101.  
  102. public:
  103.  
  104.   enum profile { system, user, both };           // Which profile is to be used?
  105.  
  106.   KrProfile (const IString & aApp, profile aProfile = user, Boolean iBuffered = false);
  107.                                                  // Constructor: aApp = Name of application
  108.                                                  //   aProfile: sort of ini file to be used
  109.                                                  //   iBuffered: true = data must be explicitely written
  110.   KrProfile (const IString & aApp, const IString & aProfile, Boolean iBuffered = false);
  111.                                                  // aProfile: name of ini file to be used, this must be
  112.                                                  //   neither the user nor the system ini file!
  113.  
  114.   ~KrProfile ();
  115.  
  116.   Boolean good ();                               // Open profile successful?
  117.  
  118.   void clearBuffer ();                           // Clears the associated strstreambuf object
  119.   void removeKey ();                             // Removes the current key from the ini file
  120.   void removeApp ();                             // Removes the current application from the ini file
  121.   long size ();                                  // Returnes the size of the current key data
  122.  
  123.   IString & key ();                              // Returns the current key
  124.   void setKey (const IString & aKey = "Default");
  125.                                                  // Sets the new key
  126.  
  127.   Boolean exists ();                             // Exists the current application in the profile?
  128.   Boolean existsKey ();                          // Exists the key in the profile?
  129.  
  130.   virtual void writeData (Boolean callFromBuff); // Writes to the ini file if one of iBuffered and
  131.                                                  //   callFromBuff is false
  132.   virtual void readData ();                      // Reads from the ini file
  133.  
  134. private:
  135.  
  136.   IHandle hini;
  137.   IString profileName;
  138.   Boolean isGood;
  139. };
  140.  
  141.  
  142. // Manipulators for KrProfile
  143.  
  144. KrProfile & clear (KrProfile & aProfile);        // Calls KrProfile::clearBuffer()
  145. KrProfile & flush (KrProfile & aProfile);        // Writes to the ini file *now*
  146. KrProfile & remove (KrProfile & aProfile);       // Removes the current key from the ini file
  147. KrProfile & operator << (KrProfile & aProfile, KrProfile & (*f)(KrProfile &));
  148.  
  149. #endif
  150.  
  151.