home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / source / krprof.cpp < prev    next >
Text File  |  1997-02-24  |  7KB  |  324 lines

  1. // Kroni's Classes: objects for reading and writing ini-file data
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: krprof.cpp
  4.  
  5.  
  6. #include "krprof.hpp"
  7.  
  8. #define INCL_WINSHELLDATA                        // Prf* API functions
  9. #include <os2.h>
  10.  
  11. #include <ithread.hpp>                           // To get the anchor block handle
  12.  
  13.  
  14.  
  15. _KrProfileBuf::_KrProfileBuf (_KrProfileBase & aParent)
  16.   : streambuf ()
  17. {
  18.   parent = &aParent;
  19.   buffer = new _KrProfileBufCons (*parent);
  20.   theStream = new iostream (buffer);
  21.   theStream->flags (ios::dec);
  22.   theStream->clear();
  23.   unbuffered(0);
  24.   allocate ();
  25. };
  26.  
  27.  
  28. _KrProfileBuf::~_KrProfileBuf ()
  29. {
  30.   delete theStream;
  31.   delete buffer;
  32. };
  33.  
  34.  
  35. void _KrProfileBuf::renewBuffer ()
  36. {
  37.   delete theStream;
  38.   delete buffer;
  39.   buffer = new _KrProfileBufCons (*parent);
  40.   theStream = new iostream (buffer);
  41.   theStream->flags (ios::dec);
  42.   theStream->clear();
  43. };
  44.  
  45.  
  46. int _KrProfileBuf::overflow (int c)
  47. {
  48.   if (out_waiting())
  49.      {
  50.      theStream->write (pbase(), out_waiting());
  51.      theStream->flush();
  52.      }
  53.   setp (pbase(),epptr());
  54.   if (c!=EOF)
  55.      {
  56.        sputc (c);
  57.      };
  58.   return EOF+1;                                  // No error -> don't return EOF.
  59. };
  60.  
  61.  
  62. int _KrProfileBuf::doallocate ()
  63. {
  64.   const int buffsize = 100;                      // size of each of the buffers
  65.   char * buff;
  66.   buff = new char [2*buffsize];                  // create get & put buffer
  67.   setg (buff+buffsize,buff+buffsize,buff+buffsize);
  68.                                                  // empty get area & putbackarea
  69.   setp (buff+buffsize, buff+2*buffsize);         // second half available for put area
  70.   return 0;
  71. };
  72.  
  73.  
  74. int _KrProfileBuf::underflow ()
  75. {
  76.   if (in_avail())
  77.      return *gptr();
  78.  
  79.   int c;
  80.   int i = buffer->in_avail();
  81.   if (!i)                                        // No input waiting -> try to get one char
  82.      {
  83.      c = theStream->get();
  84.      if (c==EOF)                                 // No more input possible
  85.         return c;
  86.      *(pbase()-1) = (char)c;                     // Put the char at the end of the possible get area
  87.      setg (pbase()-1, pbase()-1, pbase());       // Set get area as one char big
  88.      }
  89.   else
  90.      {
  91.      if (i>pbase()-base())                       // Possible input larger than available get area?
  92.         i = pbase()-base();
  93.      theStream->read (pbase()-i, i);             // Get as much as possible
  94.      setg (pbase()-i, pbase()-i, pbase());       // Gotten area = new get area, putback area = empty
  95.      }
  96.   return *gptr();
  97. };
  98.  
  99.  
  100. int _KrProfileBuf::sync()
  101. {
  102.   return overflow();
  103. };
  104.  
  105.  
  106. _KrProfileBufCons * _KrProfileBuf::buf()
  107. {
  108.   return buffer;
  109. };
  110.  
  111.  
  112. iostream * _KrProfileBuf::stream()
  113. {
  114.   return theStream;
  115. };
  116.  
  117.  
  118.  
  119. _KrProfileBufCons::_KrProfileBufCons (_KrProfileBase & aParent)
  120.   : strstreambuf ()
  121. {
  122.   parent = &aParent;
  123. };
  124.  
  125.  
  126. int _KrProfileBufCons::overflow (int c)
  127. {
  128.   int t = strstreambuf::overflow(c);
  129.   parent->writeData (false);
  130.   return t;
  131. };
  132.  
  133.  
  134. int _KrProfileBufCons::underflow ()
  135. {
  136.   if (!pcount())                                 // only try to read input from ultimate producer
  137.                                                  //   if string is empty
  138.      parent->readData ();
  139.  
  140.   return strstreambuf::underflow();
  141. };
  142.  
  143.  
  144. int _KrProfileBufCons::sync()
  145. {
  146.   return overflow();
  147. };
  148.  
  149.  
  150.  
  151. KrProfile::KrProfile (const IString & aApp, profile aProfile, Boolean iBuffered)
  152.   : _KrProfileBase (aApp,iBuffered)
  153.   , iostream (buf)
  154.   , hini (0)
  155. {
  156.   flags (ios::unitbuf|ios::dec);
  157.   profileName = "";
  158.   switch (aProfile)
  159.      {
  160.      case system:
  161.         hini = HINI_SYSTEMPROFILE;
  162.         break;
  163.      case user:
  164.         hini = HINI_USERPROFILE;
  165.         break;
  166.      case both:
  167.         hini = HINI_PROFILE;
  168.         break;
  169.      };
  170.   isGood = true;
  171. };
  172.  
  173.  
  174. KrProfile::KrProfile (const IString & aApp, const IString & aProfile, Boolean iBuffered)
  175.   : _KrProfileBase (aApp,iBuffered)
  176.   , iostream (buf)
  177.   , hini (0)
  178. {
  179.   flags (ios::unitbuf|ios::dec);
  180.   hini = PrfOpenProfile (IThread::current().anchorBlock(), aProfile);
  181.   isGood = (hini!=NULLHANDLE);
  182. };
  183.  
  184.  
  185. KrProfile::~KrProfile ()
  186. {
  187.   if (profileName != "" && isGood)
  188.      PrfCloseProfile (hini);
  189. };
  190.  
  191.  
  192. Boolean KrProfile::good ()
  193. {
  194.   return isGood;
  195. };
  196.  
  197.  
  198. IString & KrProfile::key ()
  199. {
  200.   return keyName;
  201. };
  202.  
  203.  
  204. void KrProfile::setKey (const IString & aKey)
  205. {
  206.   char buffer [100];
  207.   while (!eof())                                 // Make the buffer empty
  208.     read (buffer,50);
  209.   keyName = aKey;
  210.   clearBuffer ();
  211.   clear();
  212. };
  213.  
  214.  
  215. void KrProfile::clearBuffer ()
  216. {
  217.   buf->renewBuffer();
  218. };
  219.  
  220.  
  221.  
  222. _KrProfileBase::_KrProfileBase (const IString & aApp, Boolean iBuffered)
  223. {
  224.   buf = new _KrProfileBuf(*this);
  225.   buffered = iBuffered;
  226.   appName = aApp;
  227.   keyName = "Default";
  228. };
  229.  
  230.  
  231. _KrProfileBase::~_KrProfileBase ()
  232. {
  233.   delete buf;
  234. };
  235.  
  236.  
  237. void KrProfile::writeData (Boolean callFromBuff)
  238. {
  239.   if (!(buffered&&callFromBuff))
  240.      {
  241.      char * c = buf->buf()->str();
  242.      unsigned long size = buf->buf()->pcount();
  243.      PrfWriteProfileData (hini, appName, keyName, c, size);
  244.      buf->buf()->freeze(0);
  245.      }
  246. };
  247.  
  248.  
  249. void KrProfile::readData ()
  250. {
  251.   char * c;
  252.   unsigned long size;
  253.   PrfQueryProfileSize (hini, appName, keyName, &size);
  254.   c = new char [size];
  255.   PrfQueryProfileData (hini, appName, keyName, c, &size);
  256.   buf->stream()->write (c, size);
  257.   buf->stream()->flush();
  258.   delete c;
  259. };
  260.  
  261.  
  262. long KrProfile::size ()
  263. {
  264.   unsigned long size;
  265.   PrfQueryProfileSize (hini, appName, keyName, &size);
  266.   return size;
  267. };
  268.  
  269.  
  270. void KrProfile::removeKey ()
  271. {
  272.   PrfWriteProfileData (hini, appName, keyName, NULL, 0);
  273. };
  274.  
  275.  
  276. void KrProfile::removeApp ()
  277. {
  278.   PrfWriteProfileData (hini, appName, NULL, NULL, 0);
  279. };
  280.  
  281.  
  282. Boolean KrProfile::exists ()
  283. {
  284.   unsigned long ul;
  285.   PrfQueryProfileSize (hini, appName, NULL, &ul);
  286.   return (ul!=0);
  287. };
  288.  
  289.  
  290. Boolean KrProfile::existsKey ()
  291. {
  292.   unsigned long ul;
  293.   PrfQueryProfileSize (hini, appName, keyName, &ul);
  294.   return (ul!=0);
  295. };
  296.  
  297.  
  298. KrProfile & clear (KrProfile & aProfile)
  299. {
  300.   aProfile.clearBuffer ();
  301.   return aProfile;
  302. };
  303.  
  304.  
  305. KrProfile & flush (KrProfile & aProfile)
  306. {
  307.   aProfile.flush ();
  308.   return aProfile;
  309. };
  310.  
  311.  
  312. KrProfile & remove (KrProfile & aProfile)
  313. {
  314.   aProfile.removeKey ();
  315.   return aProfile;
  316. };
  317.  
  318.  
  319. KrProfile & operator << (KrProfile & aProfile, KrProfile & (*f)(KrProfile &))
  320. {
  321.   return f(aProfile);
  322. };
  323.  
  324.