//------------------------------------------------------------------------------ void settings_set_str(settings_t* s, const char* name, const char* value) { const setting_decl_t* decl = settings_get_decl_by_name(s, name); if (decl != NULL) { set_value(s, decl, value); } }
//------------------------------------------------------------------------------ void settings_set_int(settings_t* s, const char* name, int value) { const setting_decl_t* decl = settings_get_decl_by_name(s, name); if (decl != NULL) { char buffer[32]; itoa(value, buffer, 10); set_value(s, decl, buffer); } }
//------------------------------------------------------------------------------ static const char* get_decl_default(settings_t* s, const char* name) { const setting_decl_t* decl = settings_get_decl_by_name(s, name); if (decl != NULL) { return decl->default_value; } return ""; }
//------------------------------------------------------------------------------ static int get_decl_index(settings_t* s, const char* name) { const setting_decl_t* decl = settings_get_decl_by_name(s, name); if (decl != NULL) { return (int)(decl - s->decls); } return -1; }
//------------------------------------------------------------------------------ static int set_value(const char* key, const char* value) { const setting_decl_t* decl = settings_get_decl_by_name(g_settings, key); if (decl == NULL) { printf("ERROR: Setting '%s' not found.\n", key); return 1; } settings_set(g_settings, key, value); printf("Settings '%s' set to '%s'\n", key, settings_get_str(g_settings, key)); return 0; }
//------------------------------------------------------------------------------ void settings_set(settings_t* s, const char* name, const char* value) { const setting_decl_t* decl = settings_get_decl_by_name(s, name); if (decl != NULL) { if (decl_is_string_type(decl)) { set_value(s, decl, value); } else { int i; i = atoi(value); settings_set_int(s, name, i); } } }
//------------------------------------------------------------------------------ static int print_value(const char* key) { const setting_decl_t* decl = settings_get_decl_by_name(g_settings, key); if (decl == NULL) { printf("ERROR: Setting '%s' not found.\n", key); return 1; } printf(" Name: %s\n", decl->name); printf("Friendly name: %s\n", decl->friendly_name); printf("Current value: %s\n", settings_get_str(g_settings, key)); puts(""); wrapped_write(stdout, "", decl->description, 78); return 0; }
//------------------------------------------------------------------------------ int settings_load(settings_t* s, const char* file) { int i; FILE* in; char* data; char* line; // Open the file. in = fopen(file, "rb"); if (in == NULL) { return 0; } // Buffer the file. fseek(in, 0, SEEK_END); i = ftell(in); fseek(in, 0, SEEK_SET); data = malloc(i + 1); fread(data, i, 1, in); fclose(in); data[i] = '\0'; // Split at new lines. line = strtok(data, "\n\r"); while (line != NULL && *line) { char* c; // Skip line's leading whitespace. while (isspace(*line)) { ++line; } c = strchr(line, '='); if (c != NULL && *line != '#') { char* d; const setting_decl_t* decl; *c++ = '\0'; // Trim whitespace. d = c - 2; while (d >= line && isspace(*d)) { --d; } *(d + 1) = '\0'; while (*c && isspace(*c)) { ++c; } decl = settings_get_decl_by_name(s, line); if (decl != NULL) { set_value(s, decl, c); } } line = strtok(NULL, "\n\r"); } free(data); return 1; }