2 #include "osregistry.h"
13 const char *Osreg_company_name = "Volition";
15 const char *Osreg_class_name = "FreeSpaceClass";
17 const char *Osreg_class_name = "Freespace2Class";
20 const char *Osreg_app_name = "FreeSpaceDemo";
21 const char *Osreg_title = "FreeSpace Demo";
22 #define PROFILE_NAME "FreeSpaceDemo.ini"
23 #elif defined(FS2_DEMO)
24 const char *Osreg_app_name = "FreeSpace2Demo";
25 const char *Osreg_title = "Freespace 2 Demo";
26 #define PROFILE_NAME "FreeSpace2Demo.ini"
27 #elif defined(OEM_BUILD)
28 const char *Osreg_app_name = "FreeSpace2OEM";
29 const char *Osreg_title = "Freespace 2 OEM";
30 #define PROFILE_NAME "FreeSpace2OEM.ini"
31 #elif defined(MAKE_FS1)
32 const char *Osreg_app_name = "FreeSpace";
33 const char *Osreg_title = "FreeSpace";
34 #define PROFILE_NAME "FreeSpace.ini"
36 const char *Osreg_app_name = "FreeSpace2";
37 const char *Osreg_title = "Freespace 2";
38 #define PROFILE_NAME "FreeSpace2.ini"
41 #define DEFAULT_SECTION "Default"
44 typedef struct KeyValue
49 struct KeyValue *next;
52 typedef struct Section
56 struct KeyValue *pairs;
60 typedef struct Profile
62 struct Section *sections;
65 static char *read_line_from_file(CFILE *fp)
67 char *buf, *buf_start;
71 buf = (char *)malloc(buflen);
80 if (cfgets(buf_start, 80, fp) == NULL) {
81 if (buf_start == buf) {
90 len = strlen(buf_start);
92 if (buf_start[len-1] == '\n') {
98 buf = (char *)realloc(buf, buflen);
100 /* be sure to skip over the proper amount of nulls */
101 buf_start = buf+(buflen-80)-(buflen/80)+1;
108 static char *trim_string(char *str)
116 /* kill any comment */
117 ptr = strchr(str, ';');
120 ptr = strchr(str, '#');
130 while ((ptr > str) && isspace(*ptr)) {
140 while (*ptr && isspace(*ptr)) {
147 static Profile *profile_read(const char *file)
149 CFILE *fp = cfopen(file, "rt", CFILE_NORMAL, CF_TYPE_ROOT);
153 Profile *profile = (Profile *)malloc(sizeof(Profile));
154 profile->sections = NULL;
156 Section **sp_ptr = &(profile->sections);
159 KeyValue **kvp_ptr = NULL;
162 while ((str = read_line_from_file(fp)) != NULL) {
163 char *ptr = trim_string(str);
168 char *pend = strchr(ptr, ']');
170 // if (pend[1]) { /* trailing garbage! */ }
175 sp = (Section *)malloc(sizeof(Section));
178 sp->name = strdup(ptr);
182 sp_ptr = &(sp->next);
184 kvp_ptr = &(sp->pairs);
185 } // else { /* null name! */ }
186 } // else { /* incomplete section name! */ }
192 ptr = strchr(ptr, '=');
198 } // else { /* random garbage! */ }
200 if (key && *key && value /* && *value */) {
202 KeyValue *kvp = (KeyValue *)malloc(sizeof(KeyValue));
204 kvp->key = strdup(key);
205 kvp->value = strdup(value);
210 kvp_ptr = &(kvp->next);
211 } // else { /* key/value with no section! */
212 } // else { /* malformed key/value entry! */ }
213 } // else it's just a comment or empty string
224 static void profile_free(Profile *profile)
229 Section *sp = profile->sections;
232 KeyValue *kvp = sp->pairs;
234 while (kvp != NULL) {
253 static Profile *profile_update(Profile *profile, const char *section, const char *key, const char *value)
255 if (profile == NULL) {
256 profile = (Profile *)malloc(sizeof(Profile));
258 profile->sections = NULL;
263 Section **sp_ptr = &(profile->sections);
264 Section *sp = profile->sections;
266 if (strcmp(section, sp->name) == 0) {
267 KeyValue **kvp_ptr = &(sp->pairs);
270 while (kvp != NULL) {
271 if (strcmp(key, kvp->key) == 0) {
275 *kvp_ptr = kvp->next;
280 kvp->value = strdup(value);
287 kvp_ptr = &(kvp->next);
293 kvp = (KeyValue *)malloc(sizeof(KeyValue));
295 kvp->key = strdup(key);
296 kvp->value = strdup(value);
305 sp_ptr = &(sp->next);
309 /* section not found */
310 sp = (Section *)malloc(sizeof(Section));
312 sp->name = strdup(section);
314 kvp = (KeyValue *)malloc(sizeof(KeyValue));
316 kvp->key = strdup(key);
317 kvp->value = strdup(value);
326 static const char *profile_get_value(Profile *profile, const char *section, const char *key)
331 Section *sp = profile->sections;
333 if (strcmp(section, sp->name) == 0) {
334 KeyValue *kvp = sp->pairs;
336 while (kvp != NULL) {
337 if (strcmp(key, kvp->key) == 0) {
351 static void profile_save(Profile *profile, const char *file)
355 char tmp[MAX_PATH] = "";
356 char tmp2[MAX_PATH] = "";
361 fp = cfopen(file, "wt", CFILE_NORMAL, CF_TYPE_ROOT);
365 Section *sp = profile->sections;
367 sprintf(tmp, NOX("[%s]\n"), sp->name);
370 KeyValue *kvp = sp->pairs;
371 while (kvp != NULL) {
372 sprintf(tmp2, NOX("%s=%s\n"), kvp->key, kvp->value);
377 cfwrite_char('\n', fp);
385 static char tmp_string_data[1024];
387 const char *os_config_read_string(const char *section, const char *name, const char *default_value)
389 Profile *p = profile_read(PROFILE_NAME);
392 section = DEFAULT_SECTION;
394 const char *ptr = profile_get_value(p, section, name);
396 strncpy(tmp_string_data, ptr, 1023);
397 default_value = tmp_string_data;
402 return default_value;
405 unsigned int os_config_read_uint(const char *section, const char *name, unsigned int default_value)
407 Profile *p = profile_read(PROFILE_NAME);
410 section = DEFAULT_SECTION;
412 const char *ptr = profile_get_value(p, section, name);
414 default_value = atoi(ptr);
419 return default_value;
422 void os_config_write_string(const char *section, const char *name, const char *value)
424 Profile *p = profile_read(PROFILE_NAME);
427 section = DEFAULT_SECTION;
429 p = profile_update(p, section, name, value);
430 profile_save(p, PROFILE_NAME);
434 void os_config_write_uint(const char *section, const char *name, unsigned int value)
438 snprintf(buf, 20, "%u", value);
440 Profile *p = profile_read(PROFILE_NAME);
443 section = DEFAULT_SECTION;
445 p = profile_update(p, section, name, buf);
446 profile_save(p, PROFILE_NAME);