2 #include "osregistry.h"
11 const char *Osreg_company_name = "Volition";
13 const char *Osreg_class_name = "FreeSpaceClass";
15 const char *Osreg_class_name = "FreeSpace2Class";
18 const char *Osreg_app_name = "FreeSpaceDemo";
19 const char *Osreg_title = "FreeSpace Demo";
20 #define PROFILE_NAME "FreeSpaceDemo.ini"
21 #elif defined(FS2_DEMO)
22 const char *Osreg_app_name = "FreeSpace2Demo";
23 const char *Osreg_title = "FreeSpace 2 Demo";
24 #define PROFILE_NAME "FreeSpace2Demo.ini"
25 #elif defined(OEM_BUILD)
26 const char *Osreg_app_name = "FreeSpace2OEM";
27 const char *Osreg_title = "FreeSpace 2 OEM";
28 #define PROFILE_NAME "FreeSpace2OEM.ini"
29 #elif defined(MAKE_FS1)
30 const char *Osreg_app_name = "FreeSpace";
31 const char *Osreg_title = "FreeSpace";
32 #define PROFILE_NAME "FreeSpace.ini"
34 const char *Osreg_app_name = "FreeSpace2";
35 const char *Osreg_title = "FreeSpace 2";
36 #define PROFILE_NAME "FreeSpace2.ini"
39 #define DEFAULT_SECTION "Default"
42 typedef struct KeyValue
47 struct KeyValue *next;
50 typedef struct Section
54 struct KeyValue *pairs;
58 typedef struct Profile
60 struct Section *sections;
63 static char *read_line_from_file(CFILE *fp)
65 char *buf, *buf_start;
69 buf = (char *)SDL_malloc(buflen);
78 if (cfgets(buf_start, 80, fp) == NULL) {
79 if (buf_start == buf) {
88 len = SDL_strlen(buf_start);
90 if (buf_start[len-1] == '\n') {
96 buf = (char *)SDL_realloc(buf, buflen);
98 /* be sure to skip over the proper amount of nulls */
99 buf_start = buf+(buflen-80)-(buflen/80)+1;
106 static char *trim_string(char *str)
114 /* kill any comment */
115 ptr = SDL_strchr(str, ';');
118 ptr = SDL_strchr(str, '#');
123 len = SDL_strlen(str);
128 while ((ptr > str) && SDL_isspace(*ptr)) {
138 while (*ptr && SDL_isspace(*ptr)) {
145 static Profile *profile_read(const char *file)
147 CFILE *fp = cfopen(file, "rt", CFILE_NORMAL, CF_TYPE_ROOT);
151 Profile *profile = (Profile *)SDL_malloc(sizeof(Profile));
152 profile->sections = NULL;
154 Section **sp_ptr = &(profile->sections);
157 KeyValue **kvp_ptr = NULL;
160 while ((str = read_line_from_file(fp)) != NULL) {
161 char *ptr = trim_string(str);
166 char *pend = SDL_strchr(ptr, ']');
168 // if (pend[1]) { /* trailing garbage! */ }
173 sp = (Section *)SDL_malloc(sizeof(Section));
176 sp->name = SDL_strdup(ptr);
180 sp_ptr = &(sp->next);
182 kvp_ptr = &(sp->pairs);
183 } // else { /* null name! */ }
184 } // else { /* incomplete section name! */ }
190 ptr = SDL_strchr(ptr, '=');
196 } // else { /* random garbage! */ }
198 if (key && *key && value /* && *value */) {
200 KeyValue *kvp = (KeyValue *)SDL_malloc(sizeof(KeyValue));
202 kvp->key = SDL_strdup(key);
203 kvp->value = SDL_strdup(value);
208 kvp_ptr = &(kvp->next);
209 } // else { /* key/value with no section! */
210 } // else { /* malformed key/value entry! */ }
211 } // else it's just a comment or empty string
222 static void profile_free(Profile *profile)
227 Section *sp = profile->sections;
230 KeyValue *kvp = sp->pairs;
232 while (kvp != NULL) {
236 SDL_free(kvp->value);
251 static Profile *profile_update(Profile *profile, const char *section, const char *key, const char *value)
253 if (profile == NULL) {
254 profile = (Profile *)SDL_malloc(sizeof(Profile));
256 profile->sections = NULL;
261 Section **sp_ptr = &(profile->sections);
262 Section *sp = profile->sections;
264 if (SDL_strcmp(section, sp->name) == 0) {
265 KeyValue **kvp_ptr = &(sp->pairs);
268 while (kvp != NULL) {
269 if (SDL_strcmp(key, kvp->key) == 0) {
270 SDL_free(kvp->value);
273 *kvp_ptr = kvp->next;
278 kvp->value = SDL_strdup(value);
285 kvp_ptr = &(kvp->next);
291 kvp = (KeyValue *)SDL_malloc(sizeof(KeyValue));
293 kvp->key = SDL_strdup(key);
294 kvp->value = SDL_strdup(value);
303 sp_ptr = &(sp->next);
307 /* section not found */
308 sp = (Section *)SDL_malloc(sizeof(Section));
310 sp->name = SDL_strdup(section);
312 kvp = (KeyValue *)SDL_malloc(sizeof(KeyValue));
314 kvp->key = SDL_strdup(key);
315 kvp->value = SDL_strdup(value);
324 static const char *profile_get_value(Profile *profile, const char *section, const char *key)
329 Section *sp = profile->sections;
331 if (SDL_strcmp(section, sp->name) == 0) {
332 KeyValue *kvp = sp->pairs;
334 while (kvp != NULL) {
335 if (SDL_strcmp(key, kvp->key) == 0) {
349 static char tmp_string_data[1024];
351 static void profile_save(Profile *profile, const char *file)
358 fp = cfopen(file, "wt", CFILE_NORMAL, CF_TYPE_ROOT);
362 Section *sp = profile->sections;
364 SDL_snprintf(tmp_string_data, SDL_arraysize(tmp_string_data), "[%s]\n", sp->name);
365 cfputs(tmp_string_data, fp);
367 KeyValue *kvp = sp->pairs;
368 while (kvp != NULL) {
369 SDL_snprintf(tmp_string_data, SDL_arraysize(tmp_string_data), "%s=%s\n", kvp->key, kvp->value);
370 cfputs(tmp_string_data, fp);
374 cfwrite_char('\n', fp);
382 const char *os_config_read_string(const char *section, const char *name, const char *default_value)
384 Profile *p = profile_read(PROFILE_NAME);
387 section = DEFAULT_SECTION;
389 const char *ptr = profile_get_value(p, section, name);
390 if ( (ptr != NULL) && SDL_strlen(ptr) ) {
391 SDL_strlcpy(tmp_string_data, ptr, SDL_arraysize(tmp_string_data));
392 default_value = tmp_string_data;
397 return default_value;
400 unsigned int os_config_read_uint(const char *section, const char *name, unsigned int default_value)
402 Profile *p = profile_read(PROFILE_NAME);
405 section = DEFAULT_SECTION;
407 const char *ptr = profile_get_value(p, section, name);
408 if ( (ptr != NULL) && SDL_strlen(ptr) ) {
409 default_value = SDL_atoi(ptr);
414 return default_value;
417 void os_config_write_string(const char *section, const char *name, const char *value)
419 Profile *p = profile_read(PROFILE_NAME);
422 section = DEFAULT_SECTION;
424 p = profile_update(p, section, name, value);
425 profile_save(p, PROFILE_NAME);
429 void os_config_write_uint(const char *section, const char *name, unsigned int value)
433 SDL_snprintf(buf, SDL_arraysize(buf), "%u", value);
435 Profile *p = profile_read(PROFILE_NAME);
438 section = DEFAULT_SECTION;
440 p = profile_update(p, section, name, buf);
441 profile_save(p, PROFILE_NAME);
445 // set default config options
446 // NOTE: this will * RESET CURRENT OPTIONS TO THEIR DEFAULTS *
447 void os_init_registry_stuff()
449 // NOTE: commented options are for reference to hidden/debug settings
452 os_config_write_string(NULL, "Language", "" /* DEFAULT_LANGUAGE */);
453 os_config_write_string(NULL, "LastPlayer", "");
454 os_config_write_uint(NULL, "ComputerSpeed", 2);
455 os_config_write_string(NULL, "ExtrasPath", "");
456 // os_config_write_uint(NULL, "LowMem", 0);
459 os_config_write_string("Video", "Renderer", "OpenGL");
460 os_config_write_uint("Video", "AntiAlias", 0);
461 os_config_write_uint("Video", "Fullscreen", 1);
462 os_config_write_string("Video", "Gamma", "1.8");
463 os_config_write_uint("Video", "ShowFPS", 0);
464 // os_config_write_uint("Video", "LowRes", 0);
465 // os_config_write_uint("Video", "PreloadTextures", 1);
466 // os_config_write_uint("Video", "ScaleMovies", 1);
469 os_config_write_string("Audio", "PlaybackDevice", "");
470 os_config_write_string("Audio", "CaptureDevice", "");
471 os_config_write_uint("Audio", "EFX", 0);
472 // os_config_write_uint("Audio", "LauncherSoundEnabled", 1);
474 // 'Controls' section
475 os_config_write_string("Controls", "CurrentJoystick", "");
476 os_config_write_uint("Controls", "EnableJoystickFF", 0);
477 os_config_write_uint("Controls", "EnableHitEffect", 0);
480 os_config_write_string("Network", "NetworkConnection", "LAN");
481 os_config_write_string("Network", "ConnectionSpeed", "Fast");
482 os_config_write_uint("Network", "ForcePort", 0);
485 os_config_write_string("PXO", "Login", "");
486 os_config_write_string("PXO", "Password", "");
487 os_config_write_string("PXO", "SquadName", "");
488 // os_config_write_uint("PXO", "Banners", 1);
489 // os_config_write_uint("PXO", "SkipVerify", 0);
492 os_config_write_uint("Version", "Major", FS_VERSION_MAJOR);
493 os_config_write_uint("Version", "Minor", FS_VERSION_MINOR);
494 os_config_write_uint("Version", "Build", FS_VERSION_BUILD);