2 #include "osregistry.h"
13 char *Osreg_company_name = "Volition";
15 char *Osreg_class_name = "FreespaceClass";
17 char *Osreg_class_name = "Freespace2Class";
20 char *Osreg_app_name = "FreeSpaceDemo";
21 char *Osreg_title = "Freespace Demo";
23 char *Osreg_user_dir = ".freespace_demo";
25 char *Osreg_user_dir = "Library/Preferences/freespace_demo";
27 #define PROFILE_NAME "FreeSpaceDemo.ini"
28 #elif defined(FS2_DEMO)
29 char *Osreg_app_name = "FreeSpace2Demo";
30 char *Osreg_title = "Freespace 2 Demo";
32 char *Osreg_user_dir = ".freespace2_demo";
34 char *Osreg_user_dir = "Library/Preferences/freespace2_demo";
36 #define PROFILE_NAME "FreeSpace2Demo.ini"
37 #elif defined(OEM_BUILD)
38 char *Osreg_app_name = "FreeSpace2OEM";
39 char *Osreg_title = "Freespace 2 OEM";
40 #define PROFILE_NAME "FreeSpace2OEM.ini"
41 #elif defined(MAKE_FS1)
42 char *Osreg_app_name = "FreeSpace";
43 char *Osreg_title = "FreeSpace";
45 char *Osreg_user_dir = ".freespace";
47 char *Osreg_user_dir = "Library/Preferences/freespace";
49 #define PROFILE_NAME "FreeSpace.ini"
51 char *Osreg_app_name = "FreeSpace2";
52 char *Osreg_title = "Freespace 2";
54 char *Osreg_user_dir = ".freespace2";
56 char *Osreg_user_dir = "Library/Preferences/freespace2";
58 #define PROFILE_NAME "FreeSpace2.ini"
61 #define DEFAULT_SECTION "Default"
64 extern char full_path[1024];
67 typedef struct KeyValue
72 struct KeyValue *next;
75 typedef struct Section
79 struct KeyValue *pairs;
83 typedef struct Profile
85 struct Section *sections;
88 static char *read_line_from_file(CFILE *fp)
90 char *buf, *buf_start;
94 buf = (char *)malloc(buflen);
103 if (cfgets(buf_start, 80, fp) == NULL) {
104 if (buf_start == buf) {
113 len = strlen(buf_start);
115 if (buf_start[len-1] == '\n') {
116 buf_start[len-1] = 0;
121 buf = (char *)realloc(buf, buflen);
123 /* be sure to skip over the proper amount of nulls */
124 buf_start = buf+(buflen-80)-(buflen/80)+1;
131 static char *trim_string(char *str)
139 /* kill any comment */
140 ptr = strchr(str, ';');
143 ptr = strchr(str, '#');
153 while ((ptr > str) && isspace(*ptr)) {
163 while (*ptr && isspace(*ptr)) {
170 static Profile *profile_read(char *file)
172 CFILE *fp = cfopen(file, "rt", CFILE_NORMAL, CF_TYPE_ROOT);
176 Profile *profile = (Profile *)malloc(sizeof(Profile));
177 profile->sections = NULL;
179 Section **sp_ptr = &(profile->sections);
182 KeyValue **kvp_ptr = NULL;
185 while ((str = read_line_from_file(fp)) != NULL) {
186 char *ptr = trim_string(str);
191 char *pend = strchr(ptr, ']');
193 // if (pend[1]) { /* trailing garbage! */ }
198 sp = (Section *)malloc(sizeof(Section));
201 sp->name = strdup(ptr);
205 sp_ptr = &(sp->next);
207 kvp_ptr = &(sp->pairs);
208 } // else { /* null name! */ }
209 } // else { /* incomplete section name! */ }
215 ptr = strchr(ptr, '=');
221 } // else { /* random garbage! */ }
223 if (key && *key && value /* && *value */) {
225 KeyValue *kvp = (KeyValue *)malloc(sizeof(KeyValue));
227 kvp->key = strdup(key);
228 kvp->value = strdup(value);
233 kvp_ptr = &(kvp->next);
234 } // else { /* key/value with no section! */
235 } // else { /* malformed key/value entry! */ }
236 } // else it's just a comment or empty string
247 static void profile_free(Profile *profile)
252 Section *sp = profile->sections;
255 KeyValue *kvp = sp->pairs;
257 while (kvp != NULL) {
276 static Profile *profile_update(Profile *profile, char *section, char *key, char *value)
278 if (profile == NULL) {
279 profile = (Profile *)malloc(sizeof(Profile));
281 profile->sections = NULL;
286 Section **sp_ptr = &(profile->sections);
287 Section *sp = profile->sections;
289 if (strcmp(section, sp->name) == 0) {
290 KeyValue **kvp_ptr = &(sp->pairs);
293 while (kvp != NULL) {
294 if (strcmp(key, kvp->key) == 0) {
298 *kvp_ptr = kvp->next;
303 kvp->value = strdup(value);
310 kvp_ptr = &(kvp->next);
316 kvp = (KeyValue *)malloc(sizeof(KeyValue));
318 kvp->key = strdup(key);
319 kvp->value = strdup(value);
328 sp_ptr = &(sp->next);
332 /* section not found */
333 sp = (Section *)malloc(sizeof(Section));
335 sp->name = strdup(section);
337 kvp = (KeyValue *)malloc(sizeof(KeyValue));
339 kvp->key = strdup(key);
340 kvp->value = strdup(value);
349 static char *profile_get_value(Profile *profile, char *section, char *key)
354 Section *sp = profile->sections;
356 if (strcmp(section, sp->name) == 0) {
357 KeyValue *kvp = sp->pairs;
359 while (kvp != NULL) {
360 if (strcmp(key, kvp->key) == 0) {
374 static void profile_save(Profile *profile, char *file)
378 char tmp[MAX_PATH] = "";
379 char tmp2[MAX_PATH] = "";
384 fp = cfopen(file, "wt", CFILE_NORMAL, CF_TYPE_ROOT);
388 Section *sp = profile->sections;
390 sprintf(tmp, NOX("[%s]\n"), sp->name);
393 KeyValue *kvp = sp->pairs;
394 while (kvp != NULL) {
395 sprintf(tmp2, NOX("%s=%s\n"), kvp->key, kvp->value);
400 cfwrite_char('\n', fp);
408 static char tmp_string_data[1024];
410 char *os_config_read_string(char *section, char *name, char *default_value)
413 Profile *p = profile_read(PROFILE_NAME);
416 strcpy( p_name, full_path);
417 int f = strlen(full_path);
418 int n = strlen(PROFILE_NAME);
419 for (int i =0; i<n; i++)
421 p_name[f+i]=PROFILE_NAME[i];
424 Profile *p = profile_read(p_name);
427 section = DEFAULT_SECTION;
429 char *ptr = profile_get_value(p, section, name);
431 strncpy(tmp_string_data, ptr, 1023);
432 default_value = tmp_string_data;
437 return default_value;
440 unsigned int os_config_read_uint(char *section, char *name, unsigned int default_value)
443 Profile *p = profile_read(PROFILE_NAME);
446 strcpy( p_name, full_path);
447 int f = strlen(full_path);
448 int n = strlen(PROFILE_NAME);
449 for (int i =0; i<n; i++)
451 p_name[f+i]=PROFILE_NAME[i];
454 Profile *p = profile_read(p_name);
458 section = DEFAULT_SECTION;
460 char *ptr = profile_get_value(p, section, name);
462 default_value = atoi(ptr);
467 return default_value;
470 void os_config_write_string(char *section, char *name, char *value)
473 Profile *p = profile_read(PROFILE_NAME);
476 strcpy( p_name, full_path);
477 int f = strlen(full_path);
478 int n = strlen(PROFILE_NAME);
479 for (int i =0; i<n; i++)
481 p_name[f+i]=PROFILE_NAME[i];
484 Profile *p = profile_read(p_name);
488 section = DEFAULT_SECTION;
490 p = profile_update(p, section, name, value);
492 profile_save(p, PROFILE_NAME);
494 profile_save(p, p_name);
499 void os_config_write_uint(char *section, char *name, unsigned int value)
503 snprintf(buf, 20, "%u", value);
506 Profile *p = profile_read(PROFILE_NAME);
509 strcpy( p_name, full_path);
510 int f = strlen(full_path);
511 int n = strlen(PROFILE_NAME);
512 for (int i =0; i<n; i++)
514 p_name[f+i]=PROFILE_NAME[i];
517 Profile *p = profile_read(p_name);
520 section = DEFAULT_SECTION;
522 p = profile_update(p, section, name, buf);
524 profile_save(p, PROFILE_NAME);
526 profile_save(p, p_name);