File: registry.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (484 lines) | stat: -rw-r--r-- 12,142 bytes parent folder | download | duplicates (2)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/Main/linux/registry.cpp $
 * $Revision: 1.4 $
 * $Date: 2004/02/25 00:04:06 $
 * $Author: ryan $
 *
 * Linux registry routines
 *
 * $Log: registry.cpp,v $
 * Revision 1.4  2004/02/25 00:04:06  ryan
 * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete).
 *
 * Revision 1.3  2000/06/24 01:15:15  icculus
 * patched to compile.
 *
 * Revision 1.2  2000/04/28 20:19:29  icculus
 * Minor mprintf update
 *
 * Revision 1.1.1.1  2000/04/18 00:00:39  icculus
 * initial checkin
 *
 *
 * 8     8/20/99 1:59p Jeff
 * removed mprintfs...
 *
 * 7     7/14/99 9:09p Jeff
 * added comment header
 *
 * $NoKeywords: $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// #include "local_malloc.h"
#include "registry.h"
#include "mono.h"

#if defined(_WIN32)
#define strcasecmp stricmp
#endif

// Convert a string that represents a hex value into an int
int hextoi(char *p) {
  int value = 0;
  while ((p) && (*p) && isalnum(*p)) {
    *p = toupper(*p);
    if ((*p >= '0') && (*p <= '9'))
      value = (value * 16) + ((*p) - '0');
    else if ((*p >= 'A') && (*p <= 'F'))
      value = (value * 16) + ((*p) - 'A');
    else
      return value;

    p++;
  }
  return value;
}

// Removes whitespace from the start of the given string.
// Returns a pointer to the first non-white character
char *SkipWhite(char *p) {
  while (isspace(*p))
    p++;
  return p;
}

// Parses a quoted string
// Returns true if got string ok, else false
char *ParseString(char *p, char *buf, int bufsize, char sdelim, char edelim) {
  char *save_p;

  p = SkipWhite(p);

  save_p = p;

  if (*p != sdelim) {
    return NULL;
  } else
    p++; // skip initial quote

  // Copy chars until endquote or out of space
  while (*p && (*p != edelim) && --bufsize)
    *buf++ = *p++;

  // Check for buffer overflow
  if (bufsize <= 0) {
    return NULL;
  }

  // Check for missing endquote
  if (!*p) {
    return NULL;
  }

  // Write terminator
  *buf = 0;

  // Return new pointer (move over a char for end quote)
  return p + 1;
}

// Parses a sequence of non-space characters
char *ParseToken(char *p, char *buf, int bufsize) {
  char *save_p;
  p = SkipWhite(p);
  save_p = p;

  while (!isspace(*p) && (*p != ',') && *p && --bufsize)
    *buf++ = *p++;

  *buf = 0;

  // Check for buffer overflow
  if (bufsize <= 0) {
    return NULL;
  }

  return p;
}
#define PARSE_KEY(buf)                                                                                                 \
  do {                                                                                                                 \
    ptr = ParseString(ptr, buf, sizeof(buf), '[', ']');                                                                \
  } while (0)
#define PARSE_STRING(buf)                                                                                              \
  do {                                                                                                                 \
    ptr = ParseString(ptr, buf, sizeof(buf), '"', '"');                                                                \
  } while (0)
#define PARSE_TOKEN(buf)                                                                                               \
  do {                                                                                                                 \
    ptr = ParseToken(ptr, buf, sizeof(buf));                                                                           \
  } while (0)

CRegistry::CRegistry(const char *str) {
  currentkey = root = NULL;
  strcpy(name, str);
}

CRegistry::~CRegistry() { Destroy(); }

void CRegistry::GetSystemName(char *n) { strcpy(n, name); }

void CRegistry::SetSystemName(const char *n) { strcpy(name, n); }

void CRegistry::Destroy(void) {
  tKey *curr, *next;
  curr = next = root;
  while (curr) {
    next = curr->next;
    DestroyKey(curr);
    curr = next;
  }
  root = NULL;
}

void CRegistry::DestroyKey(tKey *key) {
  tRecord *curr, *next;
  curr = next = key->records;
  while (curr) {
    next = curr->next;
    DestroyRecord(curr);
    curr = next;
  }
  free(key);
}

void CRegistry::DestroyRecord(tRecord *record) {
  if (record->data) {
    free(record->data);
    record->data = NULL;
  }
  free(record);
}

void CRegistry::Export() {
  tKey *curr, *next;
  curr = next = root;
  FILE *file;
  file = fopen(name, "wt");
  if (!file)
    return;
  while (curr) {
    next = curr->next;
    ExportKey(curr, file);
    curr = next;
  }
  fclose(file);
}

void CRegistry::ExportKey(tKey *key, FILE *file) {
  tRecord *curr, *next;
  curr = next = key->records;
  // write out name
  char buffer[256+16];
  snprintf(buffer, sizeof(buffer), "[%s]\n", key->name);
  fputs(buffer, file);
  while (curr) {
    next = curr->next;
    ExportRecord(curr, file);
    curr = next;
  }
}

void CRegistry::ExportRecord(tRecord *record, FILE *file) {
  int *dw;
  char *st;
  char buffer[512];
  switch (record->type) {
  case REGT_STRING: {
    st = (char *)record->data;
    snprintf(buffer, sizeof(buffer), "\"%s\"=\"%s\"\n", record->name, st);
  } break;
  case REGT_DWORD: {
    dw = (int *)record->data;
    snprintf(buffer, sizeof(buffer), "\"%s\"=dword:%X\n", record->name, *dw);
  } break;
  };
  fputs(buffer, file);
}

bool CRegistry::Import() {
  char buffer[500];
  char newbuff[500];
  FILE *file;
  char *ptr;
  file = fopen(name, "rt");
  if (!file) {
    mprintf(0, "REGISTRY: Unable to import %s\n", name);
    return false;
  }
  mprintf(0, "REGISTRY: Importing %s\n", name);
  Destroy();

  bool oktocreate;
  // loop till we are done
  while (!feof(file)) {
    oktocreate = true;
    char type = REGT_STRING;

    // read in the string
    fgets(buffer, 500, file);
    ptr = buffer;
    if (feof(file)) // we are at the end of the file so there isn't data to continue
      oktocreate = false;
    // see what we read, a key or record, if the first character is a [ than a key " is record
    if (oktocreate) {
      if (buffer[0] == '[') {
        // Create a key!
        PARSE_KEY(newbuff);
        // mprintf(0,"Found Key: |%s|\n",newbuff);
        CreateKey(newbuff);
      } else if (buffer[0] == '\"') {
        // Create a record
        // see what type of record by looking at whats after the =
        char *p;
        p = buffer;
        bool done = false;
        type = REGT_STRING;
        while (!done) {
          if ((!p) || (!*p))
            done = true;
          if ((p) && (*p == '=')) {
            if (*(p + 1) == 'd')
              type = REGT_DWORD;
            else
              type = REGT_STRING;
            done = true;
          }
          p++;
        }
        // now we "SHOULD" know the type, parse the info
        char data[300];
        int idata;
        switch (type) {
        case REGT_STRING:
          PARSE_STRING(newbuff);

          // rcg06212000 getting a NULL ptr in here...added a check.
          if (ptr == NULL)
            continue;

          ptr++; // blow by =
          PARSE_STRING(data);
          if (!CreateRecord(newbuff, REGT_STRING, data)) {
            // mprintf(0,"Unable to create String record: %s\n",newbuff);
          } else {
            // mprintf(0,"Created String record %s = %s\n",newbuff,data);
          }
          break;
        case REGT_DWORD:
          PARSE_STRING(newbuff);
          ptr += 7; // blow by =dword:
          PARSE_TOKEN(data);
          idata = hextoi(data);
          if (!CreateRecord(newbuff, REGT_DWORD, &idata)) {
            // mprintf(0,"Unable to create dword record: %s\n",newbuff);
          } else {
            // mprintf(0,"Created dword record %s = %X\n",newbuff,idata);
          }
          break;
        };
      } else {
        // mprintf(0,"Expected [ or \"\n");
      }
    }
  }
  fclose(file);
  return true;
}

void CRegistry::CreateKey(const char *name) {
  tKey *curr;
  if (LookupKey(name)) {
    // mprintf(0,"Key: %s already exists\n",name);
    return;
  }
  if (!root) {
    root = (tKey *)malloc(sizeof(tKey));
    if (!root)
      return;
    curr = root;
  } else {
    curr = root;
    while (curr->next) {
      curr = curr->next;
    }
    curr->next = (tKey *)malloc(sizeof(tKey));
    if (!curr->next)
      return;
    curr = curr->next;
  }
  curr->next = NULL;
  strcpy(curr->name, name);
  curr->records = NULL;
  currentkey = curr;
}

bool CRegistry::LookupKey(const char *name) {
  tKey *curr;
  curr = root;
  while (curr) {
    if (!strcasecmp(name, curr->name)) {
      // found a match
      currentkey = curr;
      return true;
    }
    curr = curr->next;
  }
  return false;
}

tRecord *CRegistry::LookupRecord(const char *record, void *data) {
  if (!currentkey)
    return NULL;

  tRecord *curr;
  curr = currentkey->records;
  while (curr) {
    if (!strcasecmp(record, curr->name)) {
      // found the record
      switch (curr->type) {
      case REGT_STRING:
        char *st;
        st = (char *)curr->data;
        strcpy((char *)data, st);
        break;
      case REGT_DWORD:
        int *dw;
        dw = (int *)curr->data;
        *((int *)data) = *dw;
        break;
      };
      return curr;
    }
    curr = curr->next;
  }
  return NULL;
}

int CRegistry::GetDataSize(const char *record) {
  if (!currentkey)
    return false;
  tRecord *curr;
  curr = currentkey->records;
  while (curr) {
    if (!strcasecmp(record, curr->name)) {
      // found the record
      switch (curr->type) {
      case REGT_STRING:
        return strlen((char *)curr->data) + 1;
        break;
      case REGT_DWORD:
        return sizeof(int);
        break;
      };
      return 0;
    }
    curr = curr->next;
  }
  return 0;
}

bool CRegistry::CreateRecord(const char *name, char type, void *data) {
  if (!currentkey)
    return false;
  tRecord *curr;
  // first see if the record exists under this key
  int datasize = GetDataSize(name);
  if (datasize) {
    char *olddata = (char *)malloc(datasize);
    if (olddata) {
      curr = LookupRecord(name, olddata);
      if (curr) {
        // ok we have an old value, replace it!
        // mprintf(0,"Replacing %s\n",name);
        if (curr->data)
          free(curr->data);
        free(olddata);
        curr->type = type;
        switch (type) {
        case REGT_STRING:
          curr->data = malloc(strlen((char *)data) + 1);
          strcpy((char *)curr->data, (char *)data);
          break;
        case REGT_DWORD:
          curr->data = malloc(sizeof(int));
          *((int *)curr->data) = *((int *)data);
          break;
        }
        return true;
      }
    }
  }

  // it is a new record
  if (currentkey->records) {
    curr = currentkey->records;
    while (curr->next)
      curr = curr->next;
    curr->next = (tRecord *)malloc(sizeof(tRecord));
    if (!curr->next)
      return false;
    curr = curr->next;
  } else {
    currentkey->records = (tRecord *)malloc(sizeof(tRecord));
    if (!currentkey->records)
      return false;
    curr = currentkey->records;
  }
  curr->next = NULL;
  strcpy(curr->name, name);
  switch (type) {
  case REGT_STRING:
    curr->data = malloc(strlen((char *)data) + 1);
    curr->type = REGT_STRING;
    strcpy((char *)curr->data, (char *)data);
    break;
  case REGT_DWORD:
    curr->data = malloc(sizeof(int));
    curr->type = REGT_DWORD;
    *((int *)curr->data) = *((int *)data);
    break;
  };
  return true;
}