]> icculus.org git repositories - btb/d2x.git/blob - unused/win95/winregs.c
Removed automake stuff from "inert" subdirs. And there was much rejoicing.
[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 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: winregs.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
17 #pragma on (unreferenced)
18
19 #define WIN95
20 #define _WIN32
21 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23 #include <string.h>
24
25 #include "error.h"
26 #include "mono.h"
27 #include "winregs.h"
28
29
30 static HKEY hRootKey=HKEY_LOCAL_MACHINE;
31
32
33 void registry_setpath(HKEY hKey)
34 {
35         hRootKey = HKEY_LOCAL_MACHINE;
36 }
37                 
38
39
40 registry_handle *registry_open(const char *path)
41 {
42         char *regtoken;
43         int keys;
44         int i;
45         char *regpath;
46         long lres;
47         registry_handle *handle;
48
49 //      Find number of keys 
50         regpath = (char *)malloc(strlen(path)+1);
51         if (!regpath) return NULL;
52
53         strcpy(regpath, path);
54
55         i = 0;
56         regtoken = strtok(regpath, "\\");
57         if (!regtoken) { 
58                 free(regpath);  
59                 return NULL;
60         }
61
62         while (regtoken)
63         {
64                 i++;
65                 regtoken = strtok(NULL, "\\");
66         }
67
68         keys = i;
69         handle = (registry_handle*)malloc(sizeof(registry_handle));
70         handle->keys = keys;
71         handle->hKey = (HKEY *)malloc(sizeof(HKEY)*keys);
72         if (!handle->hKey) {
73                 free(handle);
74                 return NULL;
75         }
76
77         memset(handle->hKey, 0, sizeof(HKEY)*keys);
78
79 //      Create keys
80         i = 0;
81         strcpy(regpath, path);
82         regtoken = strtok(regpath, "\\");
83
84         if (regtoken) mprintf((0, "%s\\", regtoken));
85
86         lres = RegOpenKeyEx(hRootKey, regtoken, 0, KEY_EXECUTE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE, &handle->hKey[i]);
87         if (lres != ERROR_SUCCESS) 
88                 goto RegistryOpen_Cleanup;
89         
90         regtoken = strtok(NULL, "\\");
91         i++;
92         if (regtoken) mprintf((0, "%s\\", regtoken));
93
94
95         while (regtoken)
96         {
97                 lres = RegOpenKeyEx(handle->hKey[i-1], regtoken, 0, KEY_EXECUTE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE, &handle->hKey[i]);
98                 if (lres != ERROR_SUCCESS) 
99                         goto RegistryOpen_Cleanup;
100
101                 regtoken = strtok(NULL, "\\");
102                 if (regtoken) mprintf((0, "%s\\", regtoken));
103                 i++;
104         }
105
106         free(regpath);
107
108         mprintf((0, "\n"));
109
110         return handle;
111
112 RegistryOpen_Cleanup:
113         for (i = handle->keys-1; i >=0; i--)
114                 if (handle->hKey[i])  {
115                         RegCloseKey(handle->hKey[i]);
116                         handle->hKey[i]=0;
117                 }
118         
119         free(handle->hKey);
120         free(handle);
121         free(regpath);
122         return NULL;
123 }       
124
125
126 int registry_getint(registry_handle *handle, const char *label, int *val)
127 {
128         long lres;
129         DWORD type, size;
130
131         *val = 0;
132         size = sizeof(int);
133         lres = RegQueryValueEx(handle->hKey[handle->keys-1], label, NULL, 
134                                 &type, (LPBYTE)val, &size);
135         if (lres != ERROR_SUCCESS) return 0;
136         if (type != REG_DWORD) return 0;
137                 
138         return 1;
139 }
140
141
142 int registry_getstring(registry_handle *handle, const char *label, char *str, int bufsize)
143 {
144         long lres;
145         DWORD type, size;
146
147         size = bufsize;
148         lres = RegQueryValueEx(handle->hKey[handle->keys-1], label, NULL, &type, 
149                                 (LPBYTE)str, &size);
150
151         if (lres != ERROR_SUCCESS) return 0;
152         if (type != REG_SZ && type != REG_EXPAND_SZ) return 0;
153         
154         return 1;
155 }               
156
157                 
158         
159 int registry_close(registry_handle *handle)
160 {
161         int i;
162
163         for (i = handle->keys-1; i >=0; i--)
164                 if (handle->hKey[i])  {
165                         RegCloseKey(handle->hKey[i]);
166                         handle->hKey[i]=0;
167                 }
168         
169         free(handle->hKey);
170         free(handle);
171
172         return 1;
173 }
174
175
176
177
178
179 /*
180 int registry_create(const char *name)
181 {
182         HANDLE hSubKey, hGroupKey, hOurKey;
183         DWORD disp;
184         int result;
185
186         result = RegOpenKeyEx(HKEY_CURRENT_USER,  REG_DIR_PARENT, 0, KEY_ALL_ACCESS,
187                                                                 &hSubKey);
188         if (result!=ERROR_SUCCESS) return 0;
189         result = RegOpenKeyEx(hSubKey, REG_DIR_GROUP, 0, KEY_ALL_ACCESS, &hGroupKey);
190         if (result!=ERROR_SUCCESS) {
191                 result = RegCreateKeyEx(hSubKey, REG_DIR_GROUP, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
192                                                         &hGroupKey, &disp);
193                 if (result!=ERROR_SUCCESS) {
194                         RegCloseKey(hSubKey);
195                         return 0;
196                 }
197                 result = RegCreateKeyEx(hGroupKey, name, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
198                                                         &hOurKey, &disp);
199                 if (result !=ERROR_SUCCESS) {
200                         RegCloseKey(hGroupKey);
201                         RegCloseKey(hSubKey);
202                         return 0;
203                 }
204         }
205         RegCloseKey(hOurKey);
206         RegCloseKey(hGroupKey);
207         RegCloseKey(hSubKey);
208         return 1;
209 }
210         
211
212 int registry_delete(const char *name)
213 {
214         Int3();
215         return 0;
216 }
217
218
219 int registry_open(const char *name)
220 {
221         HANDLE hKey;
222         int result;     
223         char path[256];
224
225         Assert(hMyRegKey == NULL);
226
227         strcpy(path, REG_DIR_PATH);
228         strcat(path, name);
229
230         result = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, 0, &hKey);
231         if (result != ERROR_SUCCESS) return 0;
232
233         hMyRegKey = hKey;
234         
235         return 1;
236 }
237
238         
239 int registry_close()
240 {
241         Assert(hMyRegKey != NULL);
242         RegCloseKey(hMyRegKey);
243         hMyRegKey = NULL;
244         return 1;
245 }
246
247
248
249 int registry_addint(const char *valname, int val)
250 {
251         int result;
252
253         result = RegSetValueEx(hMyRegKey, valname, 0, REG_DWORD, &val, sizeof(val));
254
255         if (result != ERROR_SUCCESS) return 0;
256         return 1;
257 }
258
259
260 int registry_addstring(const char *valname, const char *str)
261 {
262         int result;
263
264         result = RegSetValueEx(hMyRegKey, valname, 0, REG_SZ, str, lstrlen(str));
265
266         if (result != ERROR_SUCCESS) return 0;
267         return 1;
268 }
269 */