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