]> icculus.org git repositories - btb/d2x.git/blob - unused/win95/winregs.c
remove rcs tags
[btb/d2x.git] / unused / win95 / winregs.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #define WIN95
16 #define _WIN32
17 #define WIN32_LEAN_AND_MEAN
18 #include <windows.h>
19 #include <string.h>
20
21 #include "error.h"
22 #include "mono.h"
23 #include "winregs.h"
24
25
26 static HKEY hRootKey=HKEY_LOCAL_MACHINE;
27
28
29 void registry_setpath(HKEY hKey)
30 {
31         hRootKey = HKEY_LOCAL_MACHINE;
32 }
33                 
34
35
36 registry_handle *registry_open(const char *path)
37 {
38         char *regtoken;
39         int keys;
40         int i;
41         char *regpath;
42         long lres;
43         registry_handle *handle;
44
45 //      Find number of keys 
46         regpath = (char *)malloc(strlen(path)+1);
47         if (!regpath) return NULL;
48
49         strcpy(regpath, path);
50
51         i = 0;
52         regtoken = strtok(regpath, "\\");
53         if (!regtoken) { 
54                 free(regpath);  
55                 return NULL;
56         }
57
58         while (regtoken)
59         {
60                 i++;
61                 regtoken = strtok(NULL, "\\");
62         }
63
64         keys = i;
65         handle = (registry_handle*)malloc(sizeof(registry_handle));
66         handle->keys = keys;
67         handle->hKey = (HKEY *)malloc(sizeof(HKEY)*keys);
68         if (!handle->hKey) {
69                 free(handle);
70                 return NULL;
71         }
72
73         memset(handle->hKey, 0, sizeof(HKEY)*keys);
74
75 //      Create keys
76         i = 0;
77         strcpy(regpath, path);
78         regtoken = strtok(regpath, "\\");
79
80         if (regtoken) mprintf((0, "%s\\", regtoken));
81
82         lres = RegOpenKeyEx(hRootKey, regtoken, 0, KEY_EXECUTE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE, &handle->hKey[i]);
83         if (lres != ERROR_SUCCESS) 
84                 goto RegistryOpen_Cleanup;
85         
86         regtoken = strtok(NULL, "\\");
87         i++;
88         if (regtoken) mprintf((0, "%s\\", regtoken));
89
90
91         while (regtoken)
92         {
93                 lres = RegOpenKeyEx(handle->hKey[i-1], regtoken, 0, KEY_EXECUTE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE, &handle->hKey[i]);
94                 if (lres != ERROR_SUCCESS) 
95                         goto RegistryOpen_Cleanup;
96
97                 regtoken = strtok(NULL, "\\");
98                 if (regtoken) mprintf((0, "%s\\", regtoken));
99                 i++;
100         }
101
102         free(regpath);
103
104         mprintf((0, "\n"));
105
106         return handle;
107
108 RegistryOpen_Cleanup:
109         for (i = handle->keys-1; i >=0; i--)
110                 if (handle->hKey[i])  {
111                         RegCloseKey(handle->hKey[i]);
112                         handle->hKey[i]=0;
113                 }
114         
115         free(handle->hKey);
116         free(handle);
117         free(regpath);
118         return NULL;
119 }       
120
121
122 int registry_getint(registry_handle *handle, const char *label, int *val)
123 {
124         long lres;
125         DWORD type, size;
126
127         *val = 0;
128         size = sizeof(int);
129         lres = RegQueryValueEx(handle->hKey[handle->keys-1], label, NULL, 
130                                 &type, (LPBYTE)val, &size);
131         if (lres != ERROR_SUCCESS) return 0;
132         if (type != REG_DWORD) return 0;
133                 
134         return 1;
135 }
136
137
138 int registry_getstring(registry_handle *handle, const char *label, char *str, int bufsize)
139 {
140         long lres;
141         DWORD type, size;
142
143         size = bufsize;
144         lres = RegQueryValueEx(handle->hKey[handle->keys-1], label, NULL, &type, 
145                                 (LPBYTE)str, &size);
146
147         if (lres != ERROR_SUCCESS) return 0;
148         if (type != REG_SZ && type != REG_EXPAND_SZ) return 0;
149         
150         return 1;
151 }               
152
153                 
154         
155 int registry_close(registry_handle *handle)
156 {
157         int i;
158
159         for (i = handle->keys-1; i >=0; i--)
160                 if (handle->hKey[i])  {
161                         RegCloseKey(handle->hKey[i]);
162                         handle->hKey[i]=0;
163                 }
164         
165         free(handle->hKey);
166         free(handle);
167
168         return 1;
169 }
170
171
172
173
174
175 /*
176 int registry_create(const char *name)
177 {
178         HANDLE hSubKey, hGroupKey, hOurKey;
179         DWORD disp;
180         int result;
181
182         result = RegOpenKeyEx(HKEY_CURRENT_USER,  REG_DIR_PARENT, 0, KEY_ALL_ACCESS,
183                                                                 &hSubKey);
184         if (result!=ERROR_SUCCESS) return 0;
185         result = RegOpenKeyEx(hSubKey, REG_DIR_GROUP, 0, KEY_ALL_ACCESS, &hGroupKey);
186         if (result!=ERROR_SUCCESS) {
187                 result = RegCreateKeyEx(hSubKey, REG_DIR_GROUP, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
188                                                         &hGroupKey, &disp);
189                 if (result!=ERROR_SUCCESS) {
190                         RegCloseKey(hSubKey);
191                         return 0;
192                 }
193                 result = RegCreateKeyEx(hGroupKey, name, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
194                                                         &hOurKey, &disp);
195                 if (result !=ERROR_SUCCESS) {
196                         RegCloseKey(hGroupKey);
197                         RegCloseKey(hSubKey);
198                         return 0;
199                 }
200         }
201         RegCloseKey(hOurKey);
202         RegCloseKey(hGroupKey);
203         RegCloseKey(hSubKey);
204         return 1;
205 }
206         
207
208 int registry_delete(const char *name)
209 {
210         Int3();
211         return 0;
212 }
213
214
215 int registry_open(const char *name)
216 {
217         HANDLE hKey;
218         int result;     
219         char path[256];
220
221         Assert(hMyRegKey == NULL);
222
223         strcpy(path, REG_DIR_PATH);
224         strcat(path, name);
225
226         result = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, 0, &hKey);
227         if (result != ERROR_SUCCESS) return 0;
228
229         hMyRegKey = hKey;
230         
231         return 1;
232 }
233
234         
235 int registry_close()
236 {
237         Assert(hMyRegKey != NULL);
238         RegCloseKey(hMyRegKey);
239         hMyRegKey = NULL;
240         return 1;
241 }
242
243
244
245 int registry_addint(const char *valname, int val)
246 {
247         int result;
248
249         result = RegSetValueEx(hMyRegKey, valname, 0, REG_DWORD, &val, sizeof(val));
250
251         if (result != ERROR_SUCCESS) return 0;
252         return 1;
253 }
254
255
256 int registry_addstring(const char *valname, const char *str)
257 {
258         int result;
259
260         result = RegSetValueEx(hMyRegKey, valname, 0, REG_SZ, str, lstrlen(str));
261
262         if (result != ERROR_SUCCESS) return 0;
263         return 1;
264 }
265 */