📄 config.c
字号:
* Does the work of loading a config file. */static void load_config_file(CONFIG **config, char *filename, char *savefile){ int length = file_size(filename); if (length > 0) { FILE *f = fopen(filename, "r"); if (f) { char *tmp = (char *) malloc(length); fread(tmp, 1, length, f); fclose(f); set_config(config, tmp, length, savefile); free(tmp); } else set_config(config, NULL, 0, savefile); } else set_config(config, NULL, 0, savefile);}/* set_config_file: * Sets the file to be used for all future configuration operations. */void set_config_file(char *filename){ load_config_file(&config[0], filename, filename);}/* set_config_data: * Sets the block of data to be used for all future configuration * operations. */void set_config_data(char *data, int length){ set_config(&config[0], data, length, NULL);}/* override_config_file: * Sets the file that will override all future configuration operations. */void override_config_file(char *filename){ load_config_file(&config_override, filename, NULL);}/* override_config_data: * Sets the block of data that will override all future configuration * operations. */void override_config_data(char *data, int length){ set_config(&config_override, data, length, NULL);}/* push_config_state: * Pushes the current config state onto the stack. */void push_config_state(){ int i; if (config[MAX_CONFIGS-1]) destroy_config(config[MAX_CONFIGS-1]); for (i=MAX_CONFIGS-1; i>0; i--) config[i] = config[i-1]; config[0] = NULL;}/* pop_config_state: * Pops the current config state off the stack. */void pop_config_state(){ int i; if (config[0]) destroy_config(config[0]); for (i=0; i<MAX_CONFIGS-1; i++) config[i] = config[i+1]; config[MAX_CONFIGS-1] = NULL;}/* prettify_section_name: * Helper for ensuring that a section name is enclosed by [ ] braces. */static void prettify_section_name(char *in, char *out){ if (in) { if (in[0] != '[') strcpy(out, "["); else out[0] = 0; strcat(out, in); if (out[strlen(out)-1] != ']') strcat(out, "]"); } else out[0] = 0;}/* find_config_string: * Helper for finding an entry in the configuration file. */static CONFIG_ENTRY *find_config_string(CONFIG *config, char *section, char *name, CONFIG_ENTRY **prev){ CONFIG_ENTRY *p; int in_section = TRUE; char section_name[256]; prettify_section_name(section, section_name); if (config) { p = config->head; if (prev) *prev = NULL; while (p) { if (p->name) { if ((p->name[0] == '[') && (p->name[strlen(p->name)-1] == ']')) { /* change section */ in_section = (strcasecmp(section_name, p->name) == 0); } if ((in_section) || (name[0] == '[')) { /* is this the one? */ if (strcasecmp(p->name, name) == 0) return p; } } if (prev) *prev = p; p = p->next; } } return NULL;}/* get_config_string: * Reads a string from the configuration file. */char *get_config_string(char *section, char *name, char *def){ CONFIG_ENTRY *p; init_config(TRUE); p = find_config_string(config_override, section, name, NULL); if (!p) p = find_config_string(config[0], section, name, NULL); if (p) return (p->data ? p->data : ""); else return def;}/* get_config_int: * Reads an integer from the configuration file. */int get_config_int(char *section, char *name, int def){ char *s = get_config_string(section, name, NULL); if ((s) && (*s)) return strtol(s, NULL, 0); return def;}/* get_config_hex: * Reads a hexadecimal integer from the configuration file. */int get_config_hex(char *section, char *name, int def){ char *s = get_config_string(section, name, NULL); int i; if ((s) && (*s)) { i = strtol(s, NULL, 16); if ((i == 0x7FFFFFFF) && (strcasecmp(s, "7FFFFFFF") != 0)) i = -1; return i; } return def;}/* get_config_float: * Reads a float from the configuration file. */float get_config_float(char *section, char *name, float def){ char *s = get_config_string(section, name, NULL); if ((s) && (*s)) return atof(s); return def;}/* get_config_argv: * Reads an argc/argv style token list from the configuration file. */char **get_config_argv(char *section, char *name, int *argc){ #define MAX_ARGV 16 static char buf[256]; static char *argv[MAX_ARGV]; int pos, ac; char *s = get_config_string(section, name, NULL); if (!s) { *argc = 0; return NULL; } strcpy(buf, s); pos = 0; ac = 0; while ((ac<MAX_ARGV) && (buf[pos]) && (buf[pos] != '#')) { while ((buf[pos]) && (isspace((int)buf[pos]))) pos++; if ((buf[pos]) && (buf[pos] != '#')) { argv[ac++] = buf+pos; while ((buf[pos]) && (!isspace((int)buf[pos]))) pos++; if (buf[pos]) buf[pos++] = 0; } } *argc = ac; return argv;}/* insert_variable: * Helper for inserting a new variable into a configuration file. */static CONFIG_ENTRY *insert_variable(CONFIG_ENTRY *p, char *name, char *data){ CONFIG_ENTRY *n = (CONFIG_ENTRY *) malloc(sizeof(CONFIG_ENTRY)); if (name) { n->name = (char *) malloc(strlen(name)+1); strcpy(n->name, name); } else n->name = NULL; if (data) { n->data = (char *) malloc(strlen(data)+1); strcpy(n->data, data); } else n->data = NULL; if (p) { n->next = p->next; p->next = n; } else { n->next = NULL; config[0]->head = n; } return n;}/* set_config_string: * Writes a string to the configuration file. */void set_config_string(char *section, char *name, char *val){ CONFIG_ENTRY *p, *prev; char section_name[256]; init_config(TRUE); if (config[0]) { p = find_config_string(config[0], section, name, &prev); if (p) { if ((val) && (*val)) { /* modify existing variable */ if (p->data) free(p->data); p->data = (char *) malloc(strlen(val)+1); strcpy(p->data, val); } else { /* delete variable */ if (p->name) free(p->name); if (p->data) free(p->data); if (prev) prev->next = p->next; else config[0]->head = p->next; free(p); } } else { if ((val) && (*val)) { /* add a new variable */ prettify_section_name(section, section_name); if (section_name[0]) { p = find_config_string(config[0], NULL, section_name, &prev); if (!p) { /* create a new section */ p = config[0]->head; while ((p) && (p->next)) p = p->next; if ((p) && (p->data) && (*p->data)) p = insert_variable(p, NULL, NULL); p = insert_variable(p, section_name, NULL); } /* append to the end of the section */ while ((p) && (p->next) && (((p->next->name) && (*p->next->name)) || ((p->next->data) && (*p->next->data)))) p = p->next; p = insert_variable(p, name, val); } else { /* global variable */ p = config[0]->head; insert_variable(NULL, name, val); config[0]->head->next = p; } } } config[0]->dirty = TRUE; }}/* set_config_int: * Writes an integer to the configuration file. */void set_config_int(char *section, char *name, int val){ char buf[32]; sprintf(buf, "%d", val); set_config_string(section, name, buf);}/* set_config_hex: * Writes a hexadecimal integer to the configuration file. */void set_config_hex(char *section, char *name, int val){ if (val >= 0) { char buf[32]; sprintf(buf, "%X", val); set_config_string(section, name, buf); } else set_config_string(section, name, "-1");}/* set_config_float: * Writes a float to the configuration file. */void set_config_float(char *section, char *name, float val){ char buf[32]; sprintf(buf, "%f", val); set_config_string(section, name, buf);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -