1
0

cfg_var.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef _PFC_CFG_VAR_H_
  2. #define _PFC_CFG_VAR_H_
  3. #ifndef NOVTABLE
  4. #define NOVTABLE _declspec(novtable)
  5. #endif
  6. #include "string.h"
  7. #include "string_unicode.h"
  8. //unicode in reg functions is NOT working
  9. class NOVTABLE cfg_var
  10. {
  11. private:
  12. string var_name;
  13. static cfg_var * list;
  14. cfg_var * next;
  15. static HKEY reg_open(const char * regname);
  16. protected:
  17. cfg_var(const char * name) : var_name(name) {next=list;list=this;};
  18. const char * var_get_name() {return var_name;}
  19. //override me
  20. virtual void read(HKEY hk)=0;
  21. virtual void write(HKEY hk)=0;
  22. virtual void reset()=0;
  23. //helper
  24. int reg_get_struct_size(HKEY hk);
  25. bool reg_read_struct(HKEY hk,void * ptr,UINT size);
  26. void reg_write_struct(HKEY hk,const void * ptr,UINT size);
  27. void reg_write_int(HKEY hk,int val);
  28. int reg_read_int(HKEY hk,int def);
  29. public:
  30. static void config_read(const char *inifile, const char *section);
  31. static void config_write(const char *inifile, const char *section);
  32. static void config_reset();
  33. };
  34. class cfg_int : private cfg_var
  35. {
  36. private:
  37. int val,def;
  38. virtual void read(HKEY hk);
  39. virtual void write(HKEY hk);
  40. virtual void reset() {val=def;}
  41. public:
  42. cfg_int(const wchar_t* name, int v) : cfg_var(string_utf8(name)) { val = def = v; }
  43. cfg_int(const char * name,int v) : cfg_var(name) {val=def=v;}
  44. operator int() const {return val;}
  45. int operator=(int v) {return val=v;}
  46. inline int get_def() {return def;}
  47. };
  48. class cfg_string : private cfg_var
  49. {
  50. private:
  51. string val,def;
  52. virtual void read(HKEY hk);
  53. virtual void write(HKEY hk);
  54. virtual void reset() {val=def;}
  55. public:
  56. cfg_string(const char * name,const char * v) : cfg_var(name), val(v), def(v) {}
  57. operator const char * () const {return val;}
  58. const char * operator=(const char* v) {val=v;return val;}
  59. string & get_string() {return val;}
  60. void s_SetDlgItemText(HWND wnd,int id) {val.s_SetDlgItemText(wnd,id);}
  61. void s_GetDlgItemText(HWND wnd,int id) {val.s_GetDlgItemText(wnd,id);}
  62. };
  63. #ifdef PFC_UNICODE
  64. class cfg_string_w : private cfg_var
  65. {
  66. private:
  67. string_w val,def;
  68. virtual void read(HKEY hk);
  69. virtual void write(HKEY hk);
  70. virtual void reset() {val=def;}
  71. public:
  72. cfg_string_w(const char * name,const WCHAR * v) : cfg_var(name), val(v), def(v) {}
  73. cfg_string_w(const char * name,const char * v) : cfg_var(name), val(string_w(v)), def(string_w(v)) {}
  74. operator const WCHAR * () const {return val;}
  75. void operator=(const WCHAR* v) {val=v;}
  76. string_w & get_string() {return val;}
  77. void s_SetDlgItemText(HWND wnd,int id) {val.s_SetDlgItemText(wnd,id);}
  78. void s_GetDlgItemText(HWND wnd,int id) {val.s_GetDlgItemText(wnd,id);}
  79. };
  80. #endif
  81. template<class T>
  82. class cfg_struct_t : private cfg_var
  83. {
  84. private:
  85. T val,def;
  86. virtual void read(HKEY hk)
  87. {
  88. reg_read_struct(hk,&val,sizeof(T));
  89. }
  90. virtual void write(HKEY hk)
  91. {
  92. T temp = def;
  93. reg_read_struct(hk,&temp,sizeof(T));
  94. if (memcmp(&temp,&val,sizeof(T)))
  95. reg_write_struct(hk,&val,sizeof(T));
  96. }
  97. virtual void reset() {val=def;}
  98. public:
  99. cfg_struct_t(const char * name,const T& v) : cfg_var(name) {val=def=v;}
  100. cfg_struct_t(const char * name,int filler) : cfg_var(name) {memset(&val,filler,sizeof(T));memset(&def,filler,sizeof(T));}
  101. T& get_val() {return val;}
  102. operator T&() {return val;}
  103. T* operator=(const T& v) {val=v; return (T*)&val;}
  104. };
  105. #endif