home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / gotch175.zip / src / settings / thth_settings.h < prev   
C/C++ Source or Header  |  1999-10-24  |  10KB  |  379 lines

  1. /***
  2.  thth_settings.h
  3.  ***/
  4.  
  5. #ifndef _THTH_SETTINGS_H_
  6. #define _THTH_SETTINGS_H_
  7.  
  8. #define OS2EMX_PLAIN_CHAR
  9.  
  10. #define INCL_PM
  11. #define INCL_GPI
  12.  
  13. #include "os2.h"
  14. #include "string.h"
  15.  
  16. typedef USHORT ID;
  17.  
  18. // ------------------------------------------------------------------------
  19.  
  20. typedef class ththSettingsEntry
  21. {
  22.     friend class ththSettings;
  23.  
  24. protected:
  25.     enum { SET_FLAG, SET_LONG, SET_STRING, SET_BINARY };
  26.  
  27.     ththSettingsEntry (ID id, PSZ pszGroup, PSZ pszKey, USHORT usType);
  28.  
  29.     ID       QueryID (VOID) { return id; }
  30.     USHORT   QueryType (VOID) { return usType; }
  31.     PSZ      QueryGroup (VOID) { return pszGroup; }
  32.     PSZ      QueryKey (VOID)  { return pszKey; }
  33.  
  34.     virtual  VOID Reset (VOID) = 0;
  35.     virtual  VOID Load (HINI hini) = 0;
  36.     virtual  VOID Save (HINI hini) = 0;
  37.  
  38. private:
  39.     ID       id;
  40.     PSZ      pszGroup;
  41.     PSZ      pszKey;
  42.     USHORT   usType;
  43. } THTH_SE;
  44.  
  45. typedef THTH_SE *PTHTH_SE;
  46.  
  47. inline THTH_SE :: ththSettingsEntry (ID id, PSZ pszGroup, PSZ pszKey, USHORT usType)
  48. {
  49.     this->id = id;
  50.     this->pszGroup = pszGroup;
  51.     this->pszKey = pszKey;
  52.     this->usType = usType;
  53. }
  54.  
  55. // ------------------------------------------------------------------------
  56.  
  57. typedef class ththSettingsEntryFlag : public ththSettingsEntry
  58. {
  59.     friend class ththSettings;
  60.  
  61. public:
  62.     ththSettingsEntryFlag (ID id, PSZ pszGroup, PSZ pszKey, BOOL f)
  63.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_FLAG) {
  64.             fDefault = f; Reset (); }
  65.  
  66.     ththSettingsEntryFlag (ID id, PSZ pszGroup, PSZ pszKey, BOOL f, HINI hini)
  67.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_FLAG) {
  68.             fDefault = f; Load (hini); }
  69.  
  70.     VOID   Set (BOOL f) { this->f = f; }
  71.     BOOL   Query (VOID) { return f; }
  72.     VOID   Reset (VOID) { f = fDefault; }
  73.  
  74.     VOID Load (HINI hini)
  75.     {
  76.         BOOL  f;
  77.         ULONG ul = sizeof (f);
  78.         if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (),
  79.                                    PVOID (&f), &ul))
  80.             Reset ();
  81.         else
  82.             Set (f);
  83.     }
  84.  
  85.     VOID Save (HINI hini)
  86.     {
  87.         BOOL f = Query ();
  88.         PrfWriteProfileData (hini, QueryGroup (), QueryKey (),
  89.                              PVOID (&f), sizeof (f));
  90.     }
  91.  
  92. private:
  93.     BOOL   f, fDefault;
  94. } THTH_SEF;
  95.  
  96. typedef THTH_SEF *PTHTH_SEF;
  97.  
  98. // ------------------------------------------------------------------------
  99.  
  100. typedef class ththSettingsEntryLong : public ththSettingsEntry
  101. {
  102.     friend class ththSettings;
  103.  
  104. public:
  105.     ththSettingsEntryLong (ID id, PSZ pszGroup, PSZ pszKey, LONG l)
  106.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_LONG) {
  107.             lDefault = l; Reset (); }
  108.  
  109.     ththSettingsEntryLong (ID id, PSZ pszGroup, PSZ pszKey, LONG l, HINI hini)
  110.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_LONG) {
  111.             lDefault = l; Load (hini); }
  112.  
  113.     VOID   Set (LONG l) { this->l = l; }
  114.     LONG   Query (VOID) { return l; }
  115.     VOID   Reset (VOID) { l = lDefault; }
  116.  
  117.     VOID Load (HINI hini)
  118.     {
  119.         LONG  l;
  120.         ULONG ul = sizeof (l);
  121.         if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (),
  122.                                    PVOID (&l), &ul))
  123.             Reset ();
  124.         else
  125.                 Set (l);
  126.     }
  127.  
  128.     VOID Save (HINI hini)
  129.     {
  130.         LONG l = Query ();
  131.         PrfWriteProfileData (hini, QueryGroup (), QueryKey (),
  132.                              PVOID (&l), sizeof (l));
  133.     }
  134.  
  135. private:
  136.     LONG   l, lDefault;
  137. } THTH_SEL;
  138.  
  139. typedef THTH_SEL *PTHTH_SEL;
  140.  
  141. // ------------------------------------------------------------------------
  142.  
  143. typedef class ththSettingsEntryString : public ththSettingsEntry
  144. {
  145.     friend class ththSettings;
  146.  
  147. public:
  148.     ththSettingsEntryString (ID id, PSZ pszGroup, PSZ pszKey, PSZ psz)
  149.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_STRING) {
  150.             pszDefault = psz; Reset (); }
  151.  
  152.     ththSettingsEntryString (ID id, PSZ pszGroup, PSZ pszKey, PSZ psz, HINI hini)
  153.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_STRING) {
  154.             pszDefault = psz; Load (hini); }
  155.  
  156.     VOID Set (PSZ psz)
  157.     {
  158.         if (this->psz)
  159.             delete this->psz;
  160.         this->psz = strdup (psz);
  161.     }
  162.  
  163.     PSZ Query (VOID)
  164.     {
  165.         if (! psz)
  166.                 return "";
  167.         return psz;
  168.     }
  169.  
  170.     VOID   Reset (VOID) { Set (pszDefault); }
  171.  
  172.     VOID Load (HINI hini)
  173.     {
  174.         ULONG ul;
  175.         if (PrfQueryProfileSize (hini, QueryGroup (), QueryKey (), &ul))
  176.         {
  177.             CHAR *ach = new char[ul];
  178.             PrfQueryProfileString (hini, QueryGroup (), QueryKey (),
  179.                                    pszDefault, ach, ul);
  180.             Set (ach);
  181.             delete ach;
  182.         }
  183.     }
  184.  
  185.     VOID Save (HINI hini)
  186.     {
  187.         PrfWriteProfileString (hini, QueryGroup (), QueryKey (), Query ());
  188.     }
  189.  
  190. private:
  191.     PSZ   psz, pszDefault;
  192. } THTH_SES;
  193.  
  194. typedef THTH_SES *PTHTH_SES;
  195.  
  196. // ------------------------------------------------------------------------
  197.  
  198. typedef class ththSettingsEntryBinary : public ththSettingsEntry
  199. {
  200.     friend class ththSettings;
  201.  
  202. public:
  203.     ththSettingsEntryBinary (ID id, PSZ pszGroup, PSZ pszKey, PVOID pv, size_t size)
  204.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_BINARY) {
  205.             pvDefault = pv; this->sizeDefault = size; Reset (); }
  206.  
  207.     ththSettingsEntryBinary (ID id, PSZ pszGroup, PSZ pszKey, PVOID pv, size_t size, HINI hini)
  208.         : ththSettingsEntry (id, pszGroup, pszKey, ththSettingsEntry::SET_BINARY) {
  209.             pvDefault = pv; this->sizeDefault = size; Load (hini); }
  210.  
  211.     VOID Set (PVOID pv, size_t size)
  212.     {
  213.         if (this->pv)
  214.             delete this->pv;
  215.         this->pv = new BYTE[size];
  216.         memcpy (this->pv, pv, size);
  217.         this->size = size;
  218.     }
  219.  
  220.     PVOID Query (size_t *psize = NULL)
  221.     {
  222.         if (! pv)
  223.         {
  224.             if (psize)
  225.                 *psize = 0L;
  226.             return NULL;
  227.         }
  228.         if (psize)
  229.             *psize = size;
  230.         return pv;
  231.     }
  232.  
  233.     VOID Reset (VOID) { Set (pvDefault, sizeDefault); }
  234.  
  235.     VOID Load (HINI hini)
  236.     {
  237.         ULONG ul;
  238.         if (PrfQueryProfileSize (hini, QueryGroup (), QueryKey (), &ul))
  239.         {
  240.             pv = new BYTE[ul];
  241.             if (! PrfQueryProfileData (hini, QueryGroup (), QueryKey (),
  242.                                        pv, &ul))
  243.                 Reset ();
  244.         }
  245.     }
  246.  
  247.     VOID Save (HINI hini)
  248.     {
  249.         PrfWriteProfileData (hini, QueryGroup (), QueryKey (), pv, size);
  250.     }
  251.  
  252. private:
  253.     PVOID   pv, pvDefault;
  254.     size_t  size, sizeDefault;
  255. } THTH_SEB;
  256.  
  257. typedef THTH_SEB *PTHTH_SEB;
  258.  
  259. // ------------------------------------------------------------------------
  260.  
  261. typedef class ththSettings
  262. {
  263. public:
  264.     ththSettings (PTHTH_SE *ppse, PSZ pszInifile = NULL, ULONG ulVersion = 0L);
  265.     virtual ~ththSettings (VOID);
  266.  
  267.     VOID Reset (VOID)
  268.     {
  269.         if (ppse)
  270.             for (LONG i = 0; ppse[i]; i++)
  271.                 ppse[i]->Reset ();
  272.     }
  273.  
  274.     VOID Load (VOID)
  275.     {
  276.         if (ppse && hini)
  277.             for (LONG i = 0; ppse[i]; i++)
  278.                 ppse[i]->Load (hini);
  279.     }
  280.  
  281.     BOOL Save (VOID);
  282.  
  283.     VOID SetFlag (ID id, BOOL f = TRUE)
  284.     {
  285.         if ((pse = ID2Entry (id)))
  286.             if (pse->QueryType () == ththSettingsEntry::SET_FLAG)
  287.                 PTHTH_SEF (pse)->Set  (f);
  288.     }
  289.  
  290.     VOID   ClearFlag (ID id) { SetFlag (id, FALSE); }
  291.     VOID   ToggleFlag (ID id) { SetFlag (id, ! FlagSet (id)); }
  292.  
  293.     BOOL FlagSet (ID id)
  294.     {
  295.         if ((pse = ID2Entry (id)))
  296.             if (pse->QueryType () == ththSettingsEntry::SET_FLAG)
  297.                 return PTHTH_SEF (pse)->Query ();
  298.         return FALSE;
  299.     }
  300.  
  301.     BOOL   QueryFlag (ID id) { return FlagSet (id); }
  302.  
  303.     VOID SetLong (ID id, LONG l)
  304.     {
  305.         if ((pse = ID2Entry (id)))
  306.             if (pse->QueryType () == ththSettingsEntry::SET_LONG)
  307.                 PTHTH_SEL (pse)->Set (l);
  308.     }
  309.  
  310.     LONG QueryLong (ID id)
  311.     {
  312.         if ((pse = ID2Entry (id)))
  313.             if (pse->QueryType () == ththSettingsEntry::SET_LONG)
  314.                 return PTHTH_SEL (pse)->Query ();
  315.         return 0L;
  316.     }
  317.  
  318.     VOID SetString (ID id, const PSZ psz)
  319.     {
  320.         if ((pse = ID2Entry (id)))
  321.             if (pse->QueryType () == ththSettingsEntry::SET_STRING)
  322.                 PTHTH_SES (pse)->Set  (psz);
  323.     }
  324.  
  325.     PSZ QueryString (ID id)
  326.     {
  327.         if ((pse = ID2Entry (id)))
  328.             if (pse->QueryType () == ththSettingsEntry::SET_STRING)
  329.                 return PTHTH_SES (pse)->Query ();
  330.         return "";
  331.     }
  332.  
  333.     VOID SetBinary (ID id, PVOID pv, size_t size)
  334.     {
  335.         if ((pse = ID2Entry (id)))
  336.             if (pse->QueryType () == ththSettingsEntry::SET_BINARY)
  337.                 PTHTH_SEB (pse)->Set (pv, size);
  338.     }
  339.  
  340.     PVOID QueryBinary (ID id, size_t *psize = NULL)
  341.     {
  342.         if ((pse = ID2Entry (id)))
  343.             if (pse->QueryType () == ththSettingsEntry::SET_BINARY)
  344.                 return PTHTH_SEB (pse)->Query (psize);
  345.         *psize = 0L;
  346.         return NULL;
  347.     }
  348.  
  349. protected:
  350.     PSZ   QueryProfileName (VOID) { return psz; }
  351.     ULONG Error (VOID) { return ulError; }
  352.  
  353.     ULONG QueryVersion (VOID) { return ulVersion; }
  354.     VOID  SetVersion (ULONG ul) { ulVersion = ul; }
  355.  
  356.     HINI  QueryPrfHandle (VOID) { return hini; }
  357.  
  358. private:
  359.     PTHTH_SE ID2Entry (ID id)
  360.     {
  361.         if (ppse)
  362.             for (LONG i = 0; ppse[i]; i++)
  363.                 if (ppse[i]->QueryID () == id)
  364.                     return ppse[i];
  365.         return NULL;
  366.     }
  367.  
  368.     PSZ    psz;
  369.     HINI   hini;
  370.     PTHTH_SE    *ppse, pse;
  371.     ULONG  ulVersion;
  372.     ULONG  ulError;
  373. } THTH_SETTINGS;
  374.  
  375. typedef THTH_SETTINGS *PTHTH_SETTINGS;
  376.  
  377. // ------------------------------------------------------------------------
  378. #endif
  379.