2 #include "osregistry.h"
13 char *Osreg_company_name = "Volition";
14 char *Osreg_class_name = "Freespace2Class";
16 char *Osreg_app_name = "FreeSpace2Demo";
17 char *Osreg_title = "Freespace 2 Demo";
18 char *Osreg_user_dir = ".freespace_demo";
19 #define PROFILE_NAME "FreeSpace2Demo.ini"
20 #elif defined(OEM_BUILD)
21 char *Osreg_app_name = "FreeSpace2OEM";
22 char *Osreg_title = "Freespace 2 OEM";
23 #define PROFILE_NAME "FreeSpace2OEM.ini"
25 char *Osreg_app_name = "FreeSpace2";
26 char *Osreg_title = "Freespace 2";
27 char *Osreg_user_dir = ".freespace2";
28 #define PROFILE_NAME "FreeSpace2.ini"
31 #define DEFAULT_SECTION "Default"
33 typedef struct KeyValue
38 struct KeyValue *next;
41 typedef struct Section
45 struct KeyValue *pairs;
49 typedef struct Profile
51 struct Section *sections;
54 static char *read_line_from_file(CFILE *fp)
56 char *buf, *buf_start;
60 buf = (char *)malloc(buflen);
69 if (cfgets(buf_start, 80, fp) == NULL) {
70 if (buf_start == buf) {
79 len = strlen(buf_start);
81 if (buf_start[len-1] == '\n') {
87 buf = (char *)realloc(buf, buflen);
89 /* be sure to skip over the proper amount of nulls */
90 buf_start = buf+(buflen-80)-(buflen/80)+1;
97 static char *trim_string(char *str)
105 /* kill any comment */
106 ptr = strchr(str, ';');
109 ptr = strchr(str, '#');
119 while ((ptr > str) && isspace(*ptr)) {
129 while (*ptr && isspace(*ptr)) {
136 static Profile *profile_read(char *file)
138 CFILE *fp = cfopen(file, "rt", CFILE_NORMAL, CF_TYPE_ROOT);
142 Profile *profile = (Profile *)malloc(sizeof(Profile));
143 profile->sections = NULL;
145 Section **sp_ptr = &(profile->sections);
148 KeyValue **kvp_ptr = NULL;
151 while ((str = read_line_from_file(fp)) != NULL) {
152 char *ptr = trim_string(str);
157 char *pend = strchr(ptr, ']');
159 // if (pend[1]) { /* trailing garbage! */ }
164 sp = (Section *)malloc(sizeof(Section));
167 sp->name = strdup(ptr);
171 sp_ptr = &(sp->next);
173 kvp_ptr = &(sp->pairs);
174 } // else { /* null name! */ }
175 } // else { /* incomplete section name! */ }
181 ptr = strchr(ptr, '=');
187 } // else { /* random garbage! */ }
189 if (key && *key && value /* && *value */) {
191 KeyValue *kvp = (KeyValue *)malloc(sizeof(KeyValue));
193 kvp->key = strdup(key);
194 kvp->value = strdup(value);
199 kvp_ptr = &(kvp->next);
200 } // else { /* key/value with no section! */
201 } // else { /* malformed key/value entry! */ }
202 } // else it's just a comment or empty string
213 static void profile_free(Profile *profile)
218 Section *sp = profile->sections;
221 KeyValue *kvp = sp->pairs;
223 while (kvp != NULL) {
242 static Profile *profile_update(Profile *profile, char *section, char *key, char *value)
244 if (profile == NULL) {
245 profile = (Profile *)malloc(sizeof(Profile));
247 profile->sections = NULL;
252 Section **sp_ptr = &(profile->sections);
253 Section *sp = profile->sections;
255 if (strcmp(section, sp->name) == 0) {
256 KeyValue **kvp_ptr = &(sp->pairs);
259 while (kvp != NULL) {
260 if (strcmp(key, kvp->key) == 0) {
264 *kvp_ptr = kvp->next;
269 kvp->value = strdup(value);
276 kvp_ptr = &(kvp->next);
282 kvp = (KeyValue *)malloc(sizeof(KeyValue));
284 kvp->key = strdup(key);
285 kvp->value = strdup(value);
294 sp_ptr = &(sp->next);
298 /* section not found */
299 sp = (Section *)malloc(sizeof(Section));
301 sp->name = strdup(section);
303 kvp = (KeyValue *)malloc(sizeof(KeyValue));
305 kvp->key = strdup(key);
306 kvp->value = strdup(value);
315 static char *profile_get_value(Profile *profile, char *section, char *key)
320 Section *sp = profile->sections;
322 if (strcmp(section, sp->name) == 0) {
323 KeyValue *kvp = sp->pairs;
325 while (kvp != NULL) {
326 if (strcmp(key, kvp->key) == 0) {
340 static void profile_save(Profile *profile, char *file)
344 char tmp[MAX_PATH] = "";
345 char tmp2[MAX_PATH] = "";
350 fp = cfopen(file, "wt", CFILE_NORMAL, CF_TYPE_ROOT);
354 Section *sp = profile->sections;
356 sprintf(tmp, NOX("[%s]\n"), sp->name);
359 KeyValue *kvp = sp->pairs;
360 while (kvp != NULL) {
361 sprintf(tmp2, NOX("%s=%s\n"), kvp->key, kvp->value);
366 cfwrite_char('\n', fp);
374 static char tmp_string_data[1024];
376 char *os_config_read_string(char *section, char *name, char *default_value)
378 Profile *p = profile_read(PROFILE_NAME);
381 section = DEFAULT_SECTION;
383 char *ptr = profile_get_value(p, section, name);
385 strncpy(tmp_string_data, ptr, 1023);
386 default_value = tmp_string_data;
391 return default_value;
394 unsigned int os_config_read_uint(char *section, char *name, unsigned int default_value)
396 Profile *p = profile_read(PROFILE_NAME);
399 section = DEFAULT_SECTION;
401 char *ptr = profile_get_value(p, section, name);
403 default_value = atoi(ptr);
408 return default_value;
411 void os_config_write_string(char *section, char *name, char *value)
413 Profile *p = profile_read(PROFILE_NAME);
416 section = DEFAULT_SECTION;
418 p = profile_update(p, section, name, value);
419 profile_save(p, PROFILE_NAME);
423 void os_config_write_uint(char *section, char *name, unsigned int value)
427 snprintf(buf, 20, "%u", value);
429 Profile *p = profile_read(PROFILE_NAME);
432 section = DEFAULT_SECTION;
434 p = profile_update(p, section, name, buf);
435 profile_save(p, PROFILE_NAME);