Debian Codegraph @

netgames/dmfc/dmfccfg.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/dmfc/dmfccfg.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:57:20 $
23 * $Author: kevinb $
24 *
25 * Functions for a D3M config file
26 *
27 * $Log: dmfccfg.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:57:20 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 7 9/12/99 12:06a Jeff
33 * fixed atoxi bug (stupid bug)
34 *
35 * 6 5/17/99 2:34p Ardussi
36 * changed to compile on Mac
37 *
38 * 5 5/13/99 4:55p Ardussi
39 * changes for compiling on the Mac
40 *
41 * 4 3/17/99 12:23p Jeff
42 * converted DMFC to be COM interface
43 *
44 * 3 9/21/98 7:11p Jeff
45 * made InputCommand interface API and moved existing input commands to
46 * the interface. Changed mprintf/ASSERT so they are valid in DMFC
47 *
48 * $NoKeywords: $
49 */
50
51 #include "gamedll_header.h"
52 #include "DMFC.h"
53 #include "dmfcinternal.h"
54
55
56 CRegistry *reg = NULL;
57
58 // DMFCBase::CFGOpen
59 //
60 // Opens the registry/cfg information for the DMFC game, see error codes for return values
61 int DMFCBase::CFGOpen(char *filename) {
62 if (!filename)
63 return CFG_NOCFGFILE;
64
65 if (reg)
66 return CFG_ALREADYOPEN;
67
68 reg = new CRegistry(filename);
69 if (!reg)
70 return CFG_OUTOFMEMORY;
71
72 reg->Import();
73
74 return CFG_NOERROR;
75 }
76
77 // DMFCBase::CFGClose
78 //
79 // Closes the registry information for the game.
80 void DMFCBase::CFGClose(void) {
81 if (reg) {
82 reg->Export();
83 delete reg;
84 reg = NULL;
85 }
86 }
87
88 // DMFCBase::CFGFlush
89 //
90 // Flushes out the registry information to fall
91 int DMFCBase::CFGFlush(void) {
92 if (reg) {
93 reg->Export();
94 return CFG_NOERROR;
95 } else
96 return CFG_NOTOPEN;
97 }
98
99 // DMFCBase::CFGCreateKey
100 //
101 // Creates a key in the registry
102 int DMFCBase::CFGCreateKey(char *name) {
103 if (reg) {
104 reg->CreateKey(name);
105 return CFG_NOERROR;
106 } else
107 return CFG_NOTOPEN;
108 }
109
110 // DMFCBase::CFGLookupKey
111 //
112 // Sets the active key in the registry
113 int DMFCBase::CFGLookupKey(char *name) {
114 if (reg) {
115 if (reg->LookupKey(name))
116 return CFG_NOERROR;
117 else
118 return CFG_KEYNOTFOUND;
119 } else
120 return CFG_NOTOPEN;
121 }
122
123 // DMFCBase::CFGLookupRecord
124 //
125 // Looks up a record in the active key
126 int DMFCBase::CFGLookupRecord(char *record, void *data) {
127 if (reg) {
128 if (reg->LookupRecord(record, data))
129 return CFG_NOERROR;
130 else
131 return CFG_RECORDNOTFOUND;
132 } else
133 return CFG_NOTOPEN;
134 }
135
136 // DMFCBase::CFGCreateRecord
137 //
138 // Create/overwrites a record in the active key. Type is either REGT_DWORD or REGT_STRING
139 int DMFCBase::CFGCreateRecord(char *name, char type, void *data) {
140 if (reg) {
141 if (reg->CreateRecord(name, type, data))
142 return CFG_NOERROR;
143 else
144 return CFG_CANTCREATE;
145 } else
146 return CFG_NOTOPEN;
147 }
148
149 // Convert a string that represents a hex value into an int
150 int
151 axtoi(char *p) {
152 int value = 0;
153 while ((p) && (*p)) {
154 *p = toupper(*p);
155 if ((*p >= '0') && (*p <= '9'))
156 value = (value * 16) + ((*p) - '0');
157 else if ((*p >= 'A') && (*p <= 'F'))
158 value = (value * 16) + (((*p) - 'A') + 10);
159 else
160 return 0;
161 p++;
162 }
163 return value;
164 }
165
166 // Removes whitespace from the start of the given string.
167 // Returns a pointer to the first non-white character
168 char *CRegistry::SkipWhite(char *p) {
169 while (isspace(*p))
170 p++;
171 return p;
172 }
173
174 // Parses a quoted string
175 // Returns true if got string ok, else false
176 char *CRegistry::ParseString(char *p, char *buf, int bufsize, char sdelim, char edelim) {
177 char *save_p;
178
179 p = SkipWhite(p);
180
181 save_p = p;
182
183 if (*p != sdelim) {
184 return NULL;
185 } else {
186 p++; // skip initial quote
187 }
188
189 // Copy chars until endquote or out of space
190 while (*p && (*p != edelim) && --bufsize)
191 *buf++ = *p++;
192
193 // Check for buffer overflow
194 if (bufsize <= 0) {
195 return NULL;
196 }
197
198 // Check for missing endquote
199 if (!*p) {
200 return NULL;
201 }
202
203 // Write terminator
204 *buf = 0;
205
206 // Return new pointer (move over a char for end quote)
207 return p + 1;
208 }
209
210 // Parses a sequence of non-space characters
211 char *CRegistry::ParseToken(char *p, char *buf, int bufsize) {
212 char *save_p;
213 p = SkipWhite(p);
214 save_p = p;
215
216 while (!isspace(*p) && (*p != ',') && *p && --bufsize)
217 *buf++ = *p++;
218
219 *buf = 0;
220
221 // Check for buffer overflow
222 if (bufsize <= 0) {
223 return NULL;
224 }
225
226 return p;
227 }
228
229 #define PARSE_KEY(buf) \
230 do { \
231 ptr = ParseString(ptr, buf, sizeof(buf), '[', ']'); \
232 } while (0)
233 #define PARSE_STRING(buf) \
234 do { \
235 ptr = ParseString(ptr, buf, sizeof(buf), '"', '"'); \
236 } while (0)
237 #define PARSE_TOKEN(buf) \
238 do { \
239 ptr = ParseToken(ptr, buf, sizeof(buf)); \
240 } while (0)
241
242 CRegistry::CRegistry(const char *str) {
243 currentkey = root = NULL;
244 strcpy(name, str);
245 }
246
247 CRegistry::~CRegistry() { Destroy(); }
248
249 void CRegistry::Destroy(void) {
250 tKey *curr, *next;
251 curr = next = root;
252
253 while (curr) {
254 next = curr->next;
255 DestroyKey(curr);
256 curr = next;
257 }
258 root = NULL;
259 }
260
261 void CRegistry::DestroyKey(tKey *key) {
262 tRecord *curr, *next;
263 curr = next = key->records;
264
265 while (curr) {
266 next = curr->next;
267 DestroyRecord(curr);
268 curr = next;
269 }
270
271 free(key);
272 }
273
274 void CRegistry::DestroyRecord(tRecord *record) {
275 if (record->data) {
276 free(record->data);
277 record->data = NULL;
278 }
279 free(record);
280 }
281
282 void CRegistry::Export() {
283 tKey *curr, *next;
284 curr = next = root;
285 CFILE *file;
286 DLLOpenCFILE(&file, name, "wt");
287
288 if (!file)
289 return;
290
291 while (curr) {
292 next = curr->next;
293 ExportKey(curr, file);
294 curr = next;
295 }
296 DLLcfclose(file);
297 }
298
299 void CRegistry::ExportKey(tKey *key, CFILE *file) {
300 tRecord *curr, *next;
301 curr = next = key->records;
302
303 // write out name
304 char buffer[258];
305 snprintf(buffer, sizeof(buffer), "[%s]", key->name);
306 DLLcf_WriteString(file, buffer);
307 while (curr) {
308 next = curr->next;
309 ExportRecord(curr, file);
310 curr = next;
311 }
312 }
313
314 void CRegistry::ExportRecord(tRecord *record, CFILE *file) {
315 int *dw;
316 char *st;
317 char buffer[512];
318 switch (record->type) {
319 case REGT_STRING: {
320 st = (char *)record->data;
321 snprintf(buffer, sizeof(buffer), "\"%s\"=\"%s\"", record->name, st);
322 } break;
323 case REGT_DWORD: {
324 dw = (int *)record->data;
325 snprintf(buffer, sizeof(buffer), "\"%s\"=dword:%X", record->name, *dw);
326 } break;
327 };
328 DLLcf_WriteString(file, buffer);
329 }
330
331 bool CRegistry::Import() {
332 char buffer[500];
333 char newbuff[500];
334 CFILE *file;
335 char *ptr;
336
337 DLLOpenCFILE(&file, name, "rt");
338
339 if (!file) {
340 mprintf(0, "Unable to import %s\n", name);
341 return false;
342 }
343 mprintf(0, "Importing %s\n", name);
344 Destroy();
345
346 bool oktocreate;
347 // loop till we are done
348 while (!DLLcfeof(file)) {
349 oktocreate = true;
350 char type = REGT_STRING;
351
352 // read in the string
353 DLLcf_ReadString(buffer, 500, file);
354 if (strlen(buffer) <= 1)
355 continue;
356
357 ptr = buffer;
358 if (DLLcfeof(file)) // we are at the end of the file so there isn't data to continue
359 oktocreate = false;
360
361 // see what we read, a key or record, if the first character is a [ than a key " is record
362 if (oktocreate) {
363 if (buffer[0] == '[') {
364 // Create a key!
365 PARSE_KEY(newbuff);
366 mprintf(0, "Found Key: |%s|\n", newbuff);
367 CreateKey(newbuff);
368 } else if (buffer[0] == '\"') {
369 // Create a record
370 // see what type of record by looking at whats after the =
371 char *p;
372 p = buffer;
373 bool done = false;
374 type = REGT_STRING;
375 while (!done) {
376 if ((!p) || (!*p))
377 done = true;
378 if ((p) && (*p == '=')) {
379 if (*(p + 1) == 'd')
380 type = REGT_DWORD;
381 else
382 type = REGT_STRING;
383 done = true;
384 }
385 p++;
386 }
387
388 // now we "SHOULD" know the type, parse the info
389 char data[300];
390 int idata;
391
392 switch (type) {
393 case REGT_STRING:
394 PARSE_STRING(newbuff);
395 ptr++; // blow by =
396 PARSE_STRING(data);
397 if (!CreateRecord(newbuff, REGT_STRING, data)) {
398 mprintf(0, "Unable to create String record: %s\n", newbuff);
399 } else {
400 mprintf(0, "Created String record %s = %s\n", newbuff, data);
401 }
402 break;
403 case REGT_DWORD:
404 PARSE_STRING(newbuff);
405 ptr += 7; // blow by =dword:
406 PARSE_TOKEN(data);
407 idata = axtoi(data);
408
409 if (!CreateRecord(newbuff, REGT_DWORD, &idata)) {
410 mprintf(0, "Unable to create dword record: %s\n", newbuff);
411 } else {
412 mprintf(0, "Created dword record %s = %X\n", newbuff, idata);
413 }
414 break;
415 }
416 } else {
417 mprintf(0, "Expected [ or \"\n");
418 }
419 }
420 }
421 DLLcfclose(file);
422 return true;
423 }
424
425 void CRegistry::CreateKey(const char *name) {
426 tKey *curr;
427 if (LookupKey(name)) {
428 mprintf(0, "Key: %s already exists\n", name);
429 return;
430 }
431
432 if (!root) {
433 root = (tKey *)malloc(sizeof(tKey));
434 if (!root)
435 return;
436 curr = root;
437 } else {
438 curr = root;
439 while (curr->next) {
440 curr = curr->next;
441 }
442
443 curr->next = (tKey *)malloc(sizeof(tKey));
444 if (!curr->next)
445 return;
446 curr = curr->next;
447 }
448
449 curr->next = NULL;
450 strcpy(curr->name, name);
451 curr->records = NULL;
452 currentkey = curr;
453 }
454
455 bool CRegistry::LookupKey(const char *name) {
456 tKey *curr;
457 curr = root;
458 while (curr) {
459 if (!stricmp(name, curr->name)) {
460 // found a match
461 currentkey = curr;
462 return true;
463 }
464 curr = curr->next;
465 }
466 return false;
467 }
468
469 tRecord *CRegistry::LookupRecord(const char *record, void *data) {
470 if (!currentkey)
471 return NULL;
472
473 tRecord *curr;
474 curr = currentkey->records;
475 while (curr) {
476 if (!stricmp(record, curr->name)) {
477 // found the record
478 switch (curr->type) {
479 case REGT_STRING:
480 char *st;
481 st = (char *)curr->data;
482 strcpy((char *)data, st);
483 break;
484 case REGT_DWORD:
485 int *dw;
486 dw = (int *)curr->data;
487 *((int *)data) = *dw;
488 break;
489 };
490 return curr;
491 }
492 curr = curr->next;
493 }
494 return NULL;
495 }
496
497 int CRegistry::GetDataSize(const char *record) {
498 if (!currentkey)
499 return false;
500 tRecord *curr;
501 curr = currentkey->records;
502 while (curr) {
503 if (!stricmp(record, curr->name)) {
504 // found the record
505 switch (curr->type) {
506 case REGT_STRING:
507 return strlen((char *)curr->data) + 1;
508 break;
509 case REGT_DWORD:
510 return sizeof(int);
511 break;
512 };
513 return 0;
514 }
515 curr = curr->next;
516 }
517 return 0;
518 }
519
520 bool CRegistry::CreateRecord(const char *name, char type, void *data) {
521 if (!currentkey)
522 return false;
523 tRecord *curr;
524
525 // first see if the record exists under this key
526 int datasize = GetDataSize(name);
527 if (datasize) {
528 char *olddata = (char *)malloc(datasize);
529 if (olddata) {
530 curr = LookupRecord(name, olddata);
531 if (curr) {
532 // ok we have an old value, replace it!
533 mprintf(0, "Replacing %s\n", name);
534 if (curr->data)
535 free(curr->data);
536 free(olddata);
537 curr->type = type;
538
539 switch (type) {
540 case REGT_STRING:
541 curr->data = malloc(strlen((char *)data) + 1);
542 strcpy((char *)curr->data, (char *)data);
543 break;
544 case REGT_DWORD:
545 curr->data = malloc(sizeof(int));
546 *((int *)curr->data) = *((int *)data);
547 break;
548 }
549 return true;
550 }
551 }
552 }
553
554 // it is a new record
555 if (currentkey->records) {
556 curr = currentkey->records;
557 while (curr->next)
558 curr = curr->next;
559
560 curr->next = (tRecord *)malloc(sizeof(tRecord));
561 if (!curr->next)
562 return false;
563 curr = curr->next;
564 } else {
565 currentkey->records = (tRecord *)malloc(sizeof(tRecord));
566 if (!currentkey->records)
567 return false;
568 curr = currentkey->records;
569 }
570
571 curr->next = NULL;
572 strcpy(curr->name, name);
573 switch (type) {
574 case REGT_STRING:
575 curr->data = malloc(strlen((char *)data) + 1);
576 curr->type = REGT_STRING;
577 strcpy((char *)curr->data, (char *)data);
578 break;
579 case REGT_DWORD:
580 curr->data = malloc(sizeof(int));
581 curr->type = REGT_DWORD;
582 *((int *)curr->data) = *((int *)data);
583 break;
584 };
585 return true;
586 }