-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSettings.c
250 lines (209 loc) · 5.64 KB
/
Settings.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Save and restore program settings (user preferences)
*/
#include "sunclock.h"
/* The following table provides pointers to the settings specified by
settings resource. These pointers must correspond, line for line
with the settings named in the resource. */
static void *setvars[] = {
&satIconIndex,
&showmoon,
&cuckooClock,
&displaymode,
&imageBitmap,
&lincr,
&idir,
&anirate,
&satvterm,
¤tObjCat,
&precessionCalculation,
&skyShowConstellations,
&skyShowConbounds,
&skyShowConnames,
&skyAlignConnames,
&skyShowCoords,
&skyShowPlanets,
&skyShowName,
&skyShowBflam,
&skyShowDeep,
&telShowConstellations,
&telShowConbounds,
&telShowConnames,
&telShowCoords,
&telShowPlanets,
&telShowName,
&telShowBflam,
&telShowMag,
&telShowDeep,
&telSouthUp,
&starCatalogue,
&starQuality,
&showStarColours,
&horShowName,
&horShowBflam,
&horShowDeep,
&horShowConstellations,
&horShowConbounds,
&horShowConnames,
&horShowCoords,
&horShowPlanets,
&horShowTerrain,
&horShowScenery,
// Floating point settings
&siteLat,
&siteLon,
&telNameMag,
&telBflamMag,
&telShowMagMax,
&telShowMagMin,
&telDeepMag,
&horNameMag,
&horBflamMag,
&horDeepMag,
&horSceneryRoughness,
// Strings, all of which MUST be 128 characters in length
whichSat,
satDBFile,
siteName,
userCat
};
static char restype[] = "Settings"; // Settings resource type code
static HKEY keyh; // Registry key handle
/* ENUMSETTINGS -- Iterate over all settings, calling a given procedure for each. */
static void enumSettings(char *appName,
void (*action)(void *vptr, LPCSTR varname, LPCSTR varval))
{
HGLOBAL settabH;
LPCSTR settab;
char rsc[256];
settabH = LoadResource(hInst, FindResource(hInst, appName, restype));
if (settabH != NULL) {
settab = LockResource(settabH);
if (settab != NULL) {
int i = 0;
while (*settab != ';') {
LPSTR es = strchr(settab, '\r'),
vs = strchr(settab, '=');
if (es == 0 || vs == 0) {
char errm[80];
wsprintf((LPSTR) errm, (LPSTR) "Error in Settings, line %d.", i + 1);
MessageBox(NULL, errm, appName, MB_ICONSTOP | MB_OK);
break;
}
if ((es - settab) >= (sizeof rsc)) {
char errm[80];
wsprintf((LPSTR) errm, (LPSTR) "Length overflow in Settings, line %d.", i + 1);
MessageBox(NULL, errm, appName, MB_ICONSTOP | MB_OK);
break;
}
memcpy(rsc, settab, (es - settab) + 1);
rsc[es - settab] = 0;
rsc[vs - settab] = 0;
vs = rsc + (vs - settab);
action(setvars[i], rsc, vs + 1);
settab = es + 2;
i++;
}
UnlockResource(settabH);
}
FreeResource(settabH);
}
}
/* SAVESETTINGS -- Save current settings in the registry. */
static void saveAction(void *vptr, LPCSTR varname, LPCSTR varval)
{
char tbuf[80];
#define regStore(name, value) RegSetValueEx(keyh, name, 0, REG_DWORD, (BYTE *) value, sizeof(*value))
#define regStoreS(name, value) RegSetValueEx(keyh, name, 0, REG_SZ, (BYTE *) value, lstrlen(value) + 1)
switch (varval[0]) {
case 'i':
regStore(varname, (DWORD *) vptr);
break;
case 'f':
sprintf(tbuf, "%.6g", *((double *) vptr));
regStoreS(varname, tbuf);
break;
case 's':
regStoreS(varname, (LPCSTR) vptr);
break;
}
#undef regStore
#undef regStoreS
}
void saveSettings(char *appName)
{
TCHAR keyname[80];
strcpy(keyname, rstring(IDS_REGISTRY_SECTION));
strcat(keyname, rstring(IDS_APPNAME));
if (RegCreateKeyEx(HKEY_CURRENT_USER,
keyname, // Subkey name
0, // Reserved
"", // Bogus class string
REG_OPTION_NON_VOLATILE, // Options
KEY_ALL_ACCESS, // Access rights
NULL, // Security attributes
&keyh, // Returned handle to the key
NULL) == ERROR_SUCCESS) { // Returned disposition (not required)
enumSettings(appName, saveAction);
}
}
/* LOADSETTINGS -- Load current settings from initialisation file. */
static void loadAction(void *vptr, LPCSTR varname, LPCSTR varval)
{
char tbuf[128];
DWORD vsize;
#define regGet(name, value) vsize = sizeof(*value); RegQueryValueEx(keyh, name, NULL, NULL, (BYTE *) value, &vsize)
#define regGetS(name, value) vsize = 128; RegQueryValueEx(keyh, name, NULL, NULL, (BYTE *) value, &vsize)
#define regGetF(name, value) regGetS(name, tbuf); sscanf(tbuf, "%lf", value)
switch (varval[0]) {
case 'i':
regGet(varname, (DWORD *) vptr);
break;
case 'f':
regGetF(varname, (double *) vptr);
break;
case 's':
regGetS(varname, (LPSTR) vptr);
break;
}
#undef regGet
#undef regGetS
#undef regGetF
}
void loadSettings(char *appName)
{
TCHAR keyname[80];
strcpy(keyname, rstring(IDS_REGISTRY_SECTION));
strcat(keyname, rstring(IDS_APPNAME));
if (RegOpenKeyEx(HKEY_CURRENT_USER,
keyname, // Subkey name
0, // Reserved
KEY_QUERY_VALUE, // Security access mask
&keyh) == ERROR_SUCCESS) {
enumSettings(appName, loadAction);
} else {
defSettings(appName);
}
}
/* DEFSETTINGS -- Reset all settings to initial defaults. */
static void defAction(void *vptr, LPCSTR varname, LPCSTR varval)
{
char tbuf[80];
switch (varval[0]) {
case 'i':
lstrcpy(tbuf, varval + 1);
*((int *) vptr) = atoi(tbuf);
break;
case 'f':
lstrcpy(tbuf, varval + 1);
*((double *) vptr) = atof(tbuf);
break;
case 's':
lstrcpy((LPSTR) vptr, varval + 1);
break;
}
}
void defSettings(char *appName)
{
enumSettings(appName, defAction);
}