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, ';');
96 ptr = strchr(str, '#');
100 ptr = str+strlen(str)-1;
101 while (ptr >= str && isspace(*ptr)) {
111 while (*ptr && isspace(*ptr)) {
118 static Profile *profile_read(char *file)
120 FILE *fp = fopen(file, "r");
124 Profile *profile = (Profile *)malloc(sizeof(Profile));
125 profile->sections = NULL;
127 Section **sp_ptr = &(profile->sections);
130 KeyValue **kvp_ptr = NULL;
133 while ((str = read_line_from_file(fp)) != NULL) {
134 char *ptr = trim_string(str);
139 char *pend = strchr(ptr, ']');
141 // if (pend[1]) { /* trailing garbage! */ }
146 sp = (Section *)malloc(sizeof(Section));
149 sp->name = strdup(ptr);
153 sp_ptr = &(sp->next);
155 kvp_ptr = &(sp->pairs);
156 } // else { /* null name! */ }
157 } // else { /* incomplete section name! */ }
163 ptr = strchr(ptr, '=');
169 } // else { /* random garbage! */ }
171 if (key && *key && value /* && *value */) {
173 KeyValue *kvp = (KeyValue *)malloc(sizeof(KeyValue));
175 kvp->key = strdup(key);
176 kvp->value = strdup(value);
181 kvp_ptr = &(kvp->next);
182 } // else { /* key/value with no section! */
183 } // else { /* malformed key/value entry! */ }
184 } // else it's just a comment or empty string
195 static void profile_free(Profile *profile)
200 Section *sp = profile->sections;
203 KeyValue *kvp = sp->pairs;
205 while (kvp != NULL) {
224 static Profile *profile_update(Profile *profile, char *section, char *key, char *value)
226 if (profile == NULL) {
227 profile = (Profile *)malloc(sizeof(Profile));
229 profile->sections = NULL;
234 Section **sp_ptr = &(profile->sections);
235 Section *sp = profile->sections;
237 if (strcmp(section, sp->name) == 0) {
238 KeyValue **kvp_ptr = &(sp->pairs);
241 while (kvp != NULL) {
242 if (strcmp(key, kvp->key) == 0) {
245 kvp->value = strdup(value);
251 kvp_ptr = &(kvp->next);
256 kvp = (KeyValue *)malloc(sizeof(KeyValue));
258 kvp->key = strdup(key);
259 kvp->value = strdup(value);
267 sp_ptr = &(sp->next);
271 /* section not found */
272 sp = (Section *)malloc(sizeof(Section));
274 sp->name = strdup(section);
276 kvp = (KeyValue *)malloc(sizeof(KeyValue));
278 kvp->key = strdup(key);
279 kvp->value = strdup(value);
288 static char *profile_get_value(Profile *profile, char *section, char *key)
293 Section *sp = profile->sections;
295 if (strcmp(section, sp->name) == 0) {
296 KeyValue *kvp = sp->pairs;
298 while (kvp != NULL) {
299 if (strcmp(key, kvp->key) == 0) {
313 static void profile_save(Profile *profile, char *file)
320 fp = fopen(file, "w");
324 Section *sp = profile->sections;
326 fprintf(fp, "[%s]\n", sp->name);
328 KeyValue *kvp = sp->pairs;
329 while (kvp != NULL) {
330 fprintf(fp, "%s=%s\n", kvp->key, kvp->value);
342 static char tmp_string_data[1024];
344 char *os_config_read_string(char *section, char *name, char *default_value)
346 Profile *p = profile_read(PROFILE_NAME);
349 section = DEFAULT_SECTION;
351 char *ptr = profile_get_value(p, section, name);
353 strncpy(tmp_string_data, ptr, 1023);
354 default_value = tmp_string_data;
359 return default_value;
362 unsigned int os_config_read_uint(char *section, char *name, unsigned int default_value)
364 Profile *p = profile_read(PROFILE_NAME);
367 section = DEFAULT_SECTION;
369 char *ptr = profile_get_value(p, section, name);
371 default_value = atoi(ptr);
376 return default_value;
379 void os_config_write_string(char *section, char *name, char *value)
381 Profile *p = profile_read(PROFILE_NAME);
384 section = DEFAULT_SECTION;
386 p = profile_update(p, section, name, value);
387 profile_save(p, PROFILE_NAME);
391 void os_config_write_uint(char *section, char *name, unsigned int value)
395 snprintf(buf, 20, "%u", value);
397 Profile *p = profile_read(PROFILE_NAME);
400 section = DEFAULT_SECTION;
402 p = profile_update(p, section, name, buf);
403 profile_save(p, PROFILE_NAME);