2 #include "osregistry.h"
11 char *Osreg_company_name = "Volition";
12 char *Osreg_class_name = "Freespace2Class";
14 char *Osreg_app_name = "FreeSpace2Demo";
15 char *Osreg_title = "Freespace 2 Demo";
16 #define PROFILE_NAME "FreeSpace2Demo.ini"
17 #elif defined(OEM_BUILD)
18 char *Osreg_app_name = "FreeSpace2OEM";
19 char *Osreg_title = "Freespace 2 OEM";
20 #define PROFILE_NAME "FreeSpace2OEM.ini"
22 char *Osreg_app_name = "FreeSpace2";
23 char *Osreg_title = "Freespace 2";
24 #define PROFILE_NAME "FreeSpace2.ini"
27 #define DEFAULT_SECTION "Default"
29 typedef struct KeyValue
37 typedef struct Section
45 typedef struct Profile
50 static char *read_line_from_file(FILE *fp)
52 char *buf, *buf_start;
56 buf = (char *)malloc(buflen);
64 if (fgets(buf_start, 80, fp) == NULL)
67 len = strlen(buf_start);
69 if (buf_start[len-1] == '\n') {
75 buf = (char *)realloc(buf, buflen);
77 /* be sure to skip over the proper amount of nulls */
78 buf_start = buf+(buflen-80)-(buflen/80)+1;
85 static char *trim_string(char *str)
92 /* kill any comment */
93 ptr = strchr(str, ';');
97 ptr = str+strlen(str)-1;
98 while (*ptr && isspace(*ptr)) {
108 while (*ptr && isspace(*ptr)) {
115 static Profile *profile_read(char *file)
117 FILE *fp = fopen(file, "r");
121 Profile *profile = (Profile *)malloc(sizeof(Profile));
122 profile->sections = NULL;
124 Section **sp_ptr = &(profile->sections);
127 KeyValue **kvp_ptr = NULL;
130 while ((str = read_line_from_file(fp)) != NULL) {
131 char *ptr = trim_string(str);
136 char *pend = strchr(ptr, ']');
138 // if (pend[1]) { /* trailing garbage! */ }
143 sp = (Section *)malloc(sizeof(Section));
146 sp->name = strdup(ptr);
150 sp_ptr = &(sp->next);
152 kvp_ptr = &(sp->pairs);
153 } // else { /* null name! */ }
154 } // else { /* incomplete section name! */ }
160 ptr = strchr(ptr, '=');
166 } // else { /* random garbage! */ }
168 if (key && *key && value /* && *value */) {
170 KeyValue *kvp = (KeyValue *)malloc(sizeof(KeyValue));
172 kvp->key = strdup(key);
173 kvp->value = strdup(value);
178 kvp_ptr = &(kvp->next);
179 } // else { /* key/value with no section! */
180 } // else { /* malformed key/value entry! */ }
181 } // else it's just a comment or empty string
192 static void profile_free(Profile *profile)
197 Section *sp = profile->sections;
200 KeyValue *kvp = sp->pairs;
202 while (kvp != NULL) {
221 static Profile *profile_update(Profile *profile, char *section, char *key, char *value)
223 if (profile == NULL) {
224 profile = (Profile *)malloc(sizeof(Profile));
226 profile->sections = NULL;
231 Section **sp_ptr = &(profile->sections);
232 Section *sp = profile->sections;
234 if (strcmp(section, sp->name) == 0) {
235 KeyValue **kvp_ptr = &(sp->pairs);
238 while (kvp != NULL) {
239 if (strcmp(key, kvp->key) == 0) {
242 kvp->value = strdup(value);
248 kvp_ptr = &(kvp->next);
253 kvp = (KeyValue *)malloc(sizeof(KeyValue));
255 kvp->key = strdup(key);
256 kvp->value = strdup(value);
264 sp_ptr = &(sp->next);
268 /* section not found */
269 sp = (Section *)malloc(sizeof(Section));
271 sp->name = strdup(section);
273 kvp = (KeyValue *)malloc(sizeof(KeyValue));
275 kvp->key = strdup(key);
276 kvp->value = strdup(value);
285 static char *profile_get_value(Profile *profile, char *section, char *key)
290 Section *sp = profile->sections;
292 if (strcmp(section, sp->name) == 0) {
293 KeyValue *kvp = sp->pairs;
295 while (kvp != NULL) {
296 if (strcmp(key, kvp->key) == 0) {
310 static void profile_save(Profile *profile, char *file)
317 fp = fopen(file, "w");
321 Section *sp = profile->sections;
323 fprintf(fp, "[%s]\n", sp->name);
325 KeyValue *kvp = sp->pairs;
326 while (kvp != NULL) {
327 fprintf(fp, "%s=%s\n", kvp->key, kvp->value);
339 static char tmp_string_data[1024];
341 char *os_config_read_string(char *section, char *name, char *default_value)
343 Profile *p = profile_read(PROFILE_NAME);
346 section = DEFAULT_SECTION;
348 char *ptr = profile_get_value(p, section, name);
350 strncpy(tmp_string_data, ptr, 1023);
351 default_value = tmp_string_data;
356 return default_value;
359 unsigned int os_config_read_uint(char *section, char *name, unsigned int default_value)
361 Profile *p = profile_read(PROFILE_NAME);
364 section = DEFAULT_SECTION;
366 char *ptr = profile_get_value(p, section, name);
368 default_value = atoi(ptr);
373 return default_value;
376 void os_config_write_string(char *section, char *name, char *value)
378 Profile *p = profile_read(PROFILE_NAME);
381 section = DEFAULT_SECTION;
383 p = profile_update(p, section, name, value);
384 profile_save(p, PROFILE_NAME);
388 void os_config_write_uint(char *section, char *name, unsigned int value)
392 snprintf(buf, 20, "%u", value);
394 Profile *p = profile_read(PROFILE_NAME);
397 section = DEFAULT_SECTION;
399 p = profile_update(p, section, name, buf);
400 profile_save(p, PROFILE_NAME);