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 = SDL_strchr(str, ';');
120 ptr = SDL_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 = SDL_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 = SDL_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 char tmp_string_data[1024];
353 static void profile_save(Profile *profile, const char *file)
360 fp = cfopen(file, "wt", CFILE_NORMAL, CF_TYPE_ROOT);
364 Section *sp = profile->sections;
366 SDL_snprintf(tmp_string_data, sizeof(tmp_string_data), NOX("[%s]\n"), sp->name);
367 cfputs(tmp_string_data, fp);
369 KeyValue *kvp = sp->pairs;
370 while (kvp != NULL) {
371 SDL_snprintf(tmp_string_data, sizeof(tmp_string_data), NOX("%s=%s\n"), kvp->key, kvp->value);
372 cfputs(tmp_string_data, fp);
376 cfwrite_char('\n', fp);
384 const char *os_config_read_string(const char *section, const char *name, const char *default_value)
386 Profile *p = profile_read(PROFILE_NAME);
389 section = DEFAULT_SECTION;
391 const char *ptr = profile_get_value(p, section, name);
393 SDL_strlcpy(tmp_string_data, ptr, sizeof(tmp_string_data));
394 default_value = tmp_string_data;
399 return default_value;
402 unsigned int os_config_read_uint(const char *section, const char *name, unsigned int default_value)
404 Profile *p = profile_read(PROFILE_NAME);
407 section = DEFAULT_SECTION;
409 const char *ptr = profile_get_value(p, section, name);
411 default_value = atoi(ptr);
416 return default_value;
419 void os_config_write_string(const char *section, const char *name, const char *value)
421 Profile *p = profile_read(PROFILE_NAME);
424 section = DEFAULT_SECTION;
426 p = profile_update(p, section, name, value);
427 profile_save(p, PROFILE_NAME);
431 void os_config_write_uint(const char *section, const char *name, unsigned int value)
435 SDL_snprintf(buf, 20, "%u", value);
437 Profile *p = profile_read(PROFILE_NAME);
440 section = DEFAULT_SECTION;
442 p = profile_update(p, section, name, buf);
443 profile_save(p, PROFILE_NAME);