Debian Codegraph @

linux/registry.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/linux/registry.cpp $
21 * $Revision: 1.4 $
22 * $Date: 2004/02/25 00:04:06 $
23 * $Author: ryan $
24 *
25 * Linux registry routines
26 *
27 * $Log: registry.cpp,v $
28 * Revision 1.4 2004/02/25 00:04:06 ryan
29 * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete).
30 *
31 * Revision 1.3 2000/06/24 01:15:15 icculus
32 * patched to compile.
33 *
34 * Revision 1.2 2000/04/28 20:19:29 icculus
35 * Minor mprintf update
36 *
37 * Revision 1.1.1.1 2000/04/18 00:00:39 icculus
38 * initial checkin
39 *
40 *
41 * 8 8/20/99 1:59p Jeff
42 * removed mprintfs...
43 *
44 * 7 7/14/99 9:09p Jeff
45 * added comment header
46 *
47 * $NoKeywords: $
48 */
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <ctype.h>
54 // #include "local_malloc.h"
55 #include "registry.h"
56 #include "mono.h"
57
58 #if defined(_WIN32)
59 #define strcasecmp stricmp
60 #endif
61
62 // Convert a string that represents a hex value into an int
63 int hextoi(char *p) {
64 int value = 0;
65 while ((p) && (*p) && isalnum(*p)) {
66 *p = toupper(*p);
67 if ((*p >= '0') && (*p <= '9'))
68 value = (value * 16) + ((*p) - '0');
69 else if ((*p >= 'A') && (*p <= 'F'))
70 value = (value * 16) + ((*p) - 'A');
71 else
72 return value;
73
74 p++;
75 }
76 return value;
77 }
78
79 // Removes whitespace from the start of the given string.
80 // Returns a pointer to the first non-white character
81 char *SkipWhite(char *p) {
82 while (isspace(*p))
83 p++;
84 return p;
85 }
86
87 // Parses a quoted string
88 // Returns true if got string ok, else false
89 char *ParseString(char *p, char *buf, int bufsize, char sdelim, char edelim) {
90 char *save_p;
91
92 p = SkipWhite(p);
93
94 save_p = p;
95
96 if (*p != sdelim) {
97 return NULL;
98 } else
99 p++; // skip initial quote
100
101 // Copy chars until endquote or out of space
102 while (*p && (*p != edelim) && --bufsize)
103 *buf++ = *p++;
104
105 // Check for buffer overflow
106 if (bufsize <= 0) {
107 return NULL;
108 }
109
110 // Check for missing endquote
111 if (!*p) {
112 return NULL;
113 }
114
115 // Write terminator
116 *buf = 0;
117
118 // Return new pointer (move over a char for end quote)
119 return p + 1;
120 }
121
122 // Parses a sequence of non-space characters
123 char *ParseToken(char *p, char *buf, int bufsize) {
124 char *save_p;
125 p = SkipWhite(p);
126 save_p = p;
127
128 while (!isspace(*p) && (*p != ',') && *p && --bufsize)
129 *buf++ = *p++;
130
131 *buf = 0;
132
133 // Check for buffer overflow
134 if (bufsize <= 0) {
135 return NULL;
136 }
137
138 return p;
139 }
140 #define PARSE_KEY(buf) \
141 do { \
142 ptr = ParseString(ptr, buf, sizeof(buf), '[', ']'); \
143 } while (0)
144 #define PARSE_STRING(buf) \
145 do { \
146 ptr = ParseString(ptr, buf, sizeof(buf), '"', '"'); \
147 } while (0)
148 #define PARSE_TOKEN(buf) \
149 do { \
150 ptr = ParseToken(ptr, buf, sizeof(buf)); \
151 } while (0)
152
153 CRegistry::CRegistry(const char *str) {
154 currentkey = root = NULL;
155 strcpy(name, str);
156 }
157
158 CRegistry::~CRegistry() { Destroy(); }
159
160 void CRegistry::GetSystemName(char *n) { strcpy(n, name); }
161
162 void CRegistry::SetSystemName(const char *n) { strcpy(name, n); }
163
164 void CRegistry::Destroy(void) {
165 tKey *curr, *next;
166 curr = next = root;
167 while (curr) {
168 next = curr->next;
169 DestroyKey(curr);
170 curr = next;
171 }
172 root = NULL;
173 }
174
175 void CRegistry::DestroyKey(tKey *key) {
176 tRecord *curr, *next;
177 curr = next = key->records;
178 while (curr) {
179 next = curr->next;
180 DestroyRecord(curr);
181 curr = next;
182 }
183 free(key);
184 }
185
186 void CRegistry::DestroyRecord(tRecord *record) {
187 if (record->data) {
188 free(record->data);
189 record->data = NULL;
190 }
191 free(record);
192 }
193
194 void CRegistry::Export() {
195 tKey *curr, *next;
196 curr = next = root;
197 FILE *file;
198 file = fopen(name, "wt");
199 if (!file)
200 return;
201 while (curr) {
202 next = curr->next;
203 ExportKey(curr, file);
204 curr = next;
205 }
206 fclose(file);
207 }
208
209 void CRegistry::ExportKey(tKey *key, FILE *file) {
210 tRecord *curr, *next;
211 curr = next = key->records;
212 // write out name
213 char buffer[256+16];
214 snprintf(buffer, sizeof(buffer), "[%s]\n", key->name);
215 fputs(buffer, file);
216 while (curr) {
217 next = curr->next;
218 ExportRecord(curr, file);
219 curr = next;
220 }
221 }
222
223 void CRegistry::ExportRecord(tRecord *record, FILE *file) {
224 int *dw;
225 char *st;
226 char buffer[512];
227 switch (record->type) {
228 case REGT_STRING: {
229 st = (char *)record->data;
230 snprintf(buffer, sizeof(buffer), "\"%s\"=\"%s\"\n", record->name, st);
231 } break;
232 case REGT_DWORD: {
233 dw = (int *)record->data;
234 snprintf(buffer, sizeof(buffer), "\"%s\"=dword:%X\n", record->name, *dw);
235 } break;
236 };
237 fputs(buffer, file);
238 }
239
240 bool CRegistry::Import() {
241 char buffer[500];
242 char newbuff[500];
243 FILE *file;
244 char *ptr;
245 file = fopen(name, "rt");
246 if (!file) {
247 mprintf(0, "REGISTRY: Unable to import %s\n", name);
248 return false;
249 }
250 mprintf(0, "REGISTRY: Importing %s\n", name);
251 Destroy();
252
253 bool oktocreate;
254 // loop till we are done
255 while (!feof(file)) {
256 oktocreate = true;
257 char type = REGT_STRING;
258
259 // read in the string
260 fgets(buffer, 500, file);
261 ptr = buffer;
262 if (feof(file)) // we are at the end of the file so there isn't data to continue
263 oktocreate = false;
264 // see what we read, a key or record, if the first character is a [ than a key " is record
265 if (oktocreate) {
266 if (buffer[0] == '[') {
267 // Create a key!
268 PARSE_KEY(newbuff);
269 // mprintf(0,"Found Key: |%s|\n",newbuff);
270 CreateKey(newbuff);
271 } else if (buffer[0] == '\"') {
272 // Create a record
273 // see what type of record by looking at whats after the =
274 char *p;
275 p = buffer;
276 bool done = false;
277 type = REGT_STRING;
278 while (!done) {
279 if ((!p) || (!*p))
280 done = true;
281 if ((p) && (*p == '=')) {
282 if (*(p + 1) == 'd')
283 type = REGT_DWORD;
284 else
285 type = REGT_STRING;
286 done = true;
287 }
288 p++;
289 }
290 // now we "SHOULD" know the type, parse the info
291 char data[300];
292 int idata;
293 switch (type) {
294 case REGT_STRING:
295 PARSE_STRING(newbuff);
296
297 // rcg06212000 getting a NULL ptr in here...added a check.
298 if (ptr == NULL)
299 continue;
300
301 ptr++; // blow by =
302 PARSE_STRING(data);
303 if (!CreateRecord(newbuff, REGT_STRING, data)) {
304 // mprintf(0,"Unable to create String record: %s\n",newbuff);
305 } else {
306 // mprintf(0,"Created String record %s = %s\n",newbuff,data);
307 }
308 break;
309 case REGT_DWORD:
310 PARSE_STRING(newbuff);
311 ptr += 7; // blow by =dword:
312 PARSE_TOKEN(data);
313 idata = hextoi(data);
314 if (!CreateRecord(newbuff, REGT_DWORD, &idata)) {
315 // mprintf(0,"Unable to create dword record: %s\n",newbuff);
316 } else {
317 // mprintf(0,"Created dword record %s = %X\n",newbuff,idata);
318 }
319 break;
320 };
321 } else {
322 // mprintf(0,"Expected [ or \"\n");
323 }
324 }
325 }
326 fclose(file);
327 return true;
328 }
329
330 void CRegistry::CreateKey(const char *name) {
331 tKey *curr;
332 if (LookupKey(name)) {
333 // mprintf(0,"Key: %s already exists\n",name);
334 return;
335 }
336 if (!root) {
337 root = (tKey *)malloc(sizeof(tKey));
338 if (!root)
339 return;
340 curr = root;
341 } else {
342 curr = root;
343 while (curr->next) {
344 curr = curr->next;
345 }
346 curr->next = (tKey *)malloc(sizeof(tKey));
347 if (!curr->next)
348 return;
349 curr = curr->next;
350 }
351 curr->next = NULL;
352 strcpy(curr->name, name);
353 curr->records = NULL;
354 currentkey = curr;
355 }
356
357 bool CRegistry::LookupKey(const char *name) {
358 tKey *curr;
359 curr = root;
360 while (curr) {
361 if (!strcasecmp(name, curr->name)) {
362 // found a match
363 currentkey = curr;
364 return true;
365 }
366 curr = curr->next;
367 }
368 return false;
369 }
370
371 tRecord *CRegistry::LookupRecord(const char *record, void *data) {
372 if (!currentkey)
373 return NULL;
374
375 tRecord *curr;
376 curr = currentkey->records;
377 while (curr) {
378 if (!strcasecmp(record, curr->name)) {
379 // found the record
380 switch (curr->type) {
381 case REGT_STRING:
382 char *st;
383 st = (char *)curr->data;
384 strcpy((char *)data, st);
385 break;
386 case REGT_DWORD:
387 int *dw;
388 dw = (int *)curr->data;
389 *((int *)data) = *dw;
390 break;
391 };
392 return curr;
393 }
394 curr = curr->next;
395 }
396 return NULL;
397 }
398
399 int CRegistry::GetDataSize(const char *record) {
400 if (!currentkey)
401 return false;
402 tRecord *curr;
403 curr = currentkey->records;
404 while (curr) {
405 if (!strcasecmp(record, curr->name)) {
406 // found the record
407 switch (curr->type) {
408 case REGT_STRING:
409 return strlen((char *)curr->data) + 1;
410 break;
411 case REGT_DWORD:
412 return sizeof(int);
413 break;
414 };
415 return 0;
416 }
417 curr = curr->next;
418 }
419 return 0;
420 }
421
422 bool CRegistry::CreateRecord(const char *name, char type, void *data) {
423 if (!currentkey)
424 return false;
425 tRecord *curr;
426 // first see if the record exists under this key
427 int datasize = GetDataSize(name);
428 if (datasize) {
429 char *olddata = (char *)malloc(datasize);
430 if (olddata) {
431 curr = LookupRecord(name, olddata);
432 if (curr) {
433 // ok we have an old value, replace it!
434 // mprintf(0,"Replacing %s\n",name);
435 if (curr->data)
436 free(curr->data);
437 free(olddata);
438 curr->type = type;
439 switch (type) {
440 case REGT_STRING:
441 curr->data = malloc(strlen((char *)data) + 1);
442 strcpy((char *)curr->data, (char *)data);
443 break;
444 case REGT_DWORD:
445 curr->data = malloc(sizeof(int));
446 *((int *)curr->data) = *((int *)data);
447 break;
448 }
449 return true;
450 }
451 }
452 }
453
454 // it is a new record
455 if (currentkey->records) {
456 curr = currentkey->records;
457 while (curr->next)
458 curr = curr->next;
459 curr->next = (tRecord *)malloc(sizeof(tRecord));
460 if (!curr->next)
461 return false;
462 curr = curr->next;
463 } else {
464 currentkey->records = (tRecord *)malloc(sizeof(tRecord));
465 if (!currentkey->records)
466 return false;
467 curr = currentkey->records;
468 }
469 curr->next = NULL;
470 strcpy(curr->name, name);
471 switch (type) {
472 case REGT_STRING:
473 curr->data = malloc(strlen((char *)data) + 1);
474 curr->type = REGT_STRING;
475 strcpy((char *)curr->data, (char *)data);
476 break;
477 case REGT_DWORD:
478 curr->data = malloc(sizeof(int));
479 curr->type = REGT_DWORD;
480 *((int *)curr->data) = *((int *)data);
481 break;
482 };
483 return true;
484 }