diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2003-10-07 22:05:06 +0000 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2003-10-07 22:05:06 +0000 |
commit | f0ddea931355509f4d3b1cead0e92bc8240df712 (patch) | |
tree | 8ac4d79a3e1b2a6373d8d6e4c0206c257458b6b9 /src | |
parent | 954f1b98bfc6d4862ba1cefd33eea7201124d258 (diff) | |
download | weechat-f0ddea931355509f4d3b1cead0e92bc8240df712.zip |
New function to modify config parameters ; code cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 283 |
1 files changed, 146 insertions, 137 deletions
diff --git a/src/config.c b/src/config.c index 479031a75..6b0ef97b2 100644 --- a/src/config.c +++ b/src/config.c @@ -526,6 +526,80 @@ get_pos_array_values (char **array, char *string) } /* + * config_option_set_value: set new value for an option + * return: 0 if success + * -1 if error (bad value) + */ + +int +config_option_set_value (t_config_option *option, char *value) +{ + int int_value; + + switch (option->option_type) + { + case OPTION_TYPE_BOOLEAN: + if (strcasecmp (value, "on") == 0) + *(option->ptr_int) = BOOL_TRUE; + else if (strcasecmp (value, "off") == 0) + *(option->ptr_int) = BOOL_FALSE; + else + return -1; + break; + case OPTION_TYPE_INT: + int_value = atoi (value); + if ((int_value < option->min) || (int_value > option->max)) + return -1; + *(option->ptr_int) = int_value; + break; + case OPTION_TYPE_INT_WITH_STRING: + int_value = get_pos_array_values (option->array_values, value); + if (int_value < 0) + return -1; + *(option->ptr_int) = int_value; + break; + case OPTION_TYPE_COLOR: + if (!gui_assign_color (option->ptr_int, value)) + return -1; + break; + case OPTION_TYPE_STRING: + if (*(option->ptr_string)) + free (*(option->ptr_string)); + *(option->ptr_string) = strdup (value); + break; + } + return 0; +} + +/* + * config_set_value: set new value for an option (found by name) + * return: 0 if success + * -1 if bad value for option + * -2 if option is not found + */ + +int +config_set_value (char *option_name, char *value) +{ + int i, j; + + for (i = 0; i < CONFIG_NUMBER_SECTIONS; i++) + { + if ((i != CONFIG_SECTION_ALIAS) && (i != CONFIG_SECTION_SERVER)) + { + for (j = 0; weechat_options[i][j].option_name; j++) + { + /* if option found, assign value and exit */ + if (strcasecmp (weechat_options[i][j].option_name, option_name) == 0) + return config_option_set_value (&weechat_options[i][j], value); + } + } + } + /* option not found */ + return -2; +} + +/* * config_allocate_server: allocate a new server */ @@ -639,7 +713,7 @@ config_read () { char *filename; FILE *file; - int section, line_number, i, option_number, int_value; + int section, line_number, i, option_number; int server_found; char line[1024], *ptr_line, *pos, *pos2; @@ -679,58 +753,44 @@ config_read () { pos = strchr (line, ']'); if (pos == NULL) - { gui_printf (NULL, _("%s %s, line %d: invalid syntax, missing \"]\"\n"), WEECHAT_WARNING, filename, line_number); - fclose (file); - free (filename); - return -2; - } - pos[0] = '\0'; - pos = ptr_line + 1; - section = CONFIG_SECTION_NONE; - for (i = 0; config_sections[i].section_name; i++) + else { - if (strcmp (config_sections[i].section_name, pos) == 0) + pos[0] = '\0'; + pos = ptr_line + 1; + section = CONFIG_SECTION_NONE; + for (i = 0; config_sections[i].section_name; i++) { - section = i; - break; + if (strcmp (config_sections[i].section_name, pos) == 0) + { + section = i; + break; + } } - } - if (section == CONFIG_SECTION_NONE) - { - gui_printf (NULL, - _("%s %s, line %d: unknown section identifier (\"%s\")\n"), - WEECHAT_WARNING, filename, line_number, pos); - fclose (file); - free (filename); - return -2; - } - if (server_found) - { - /* if server already started => create it */ - if (!config_allocate_server (filename, line_number)) + if (section == CONFIG_SECTION_NONE) + gui_printf (NULL, + _("%s %s, line %d: unknown section identifier (\"%s\")\n"), + WEECHAT_WARNING, filename, line_number, pos); + else { - fclose (file); - free (filename); - return -2; + if (server_found) + { + /* if server already started => create it */ + config_allocate_server (filename, line_number); + } + server_found = (section == CONFIG_SECTION_SERVER) ? 1 : 0; } } - server_found = (section == CONFIG_SECTION_SERVER) ? 1 : 0; } else { pos = strchr (line, '='); if (pos == NULL) - { gui_printf (NULL, _("%s %s, line %d: invalid syntax, missing \"=\"\n"), WEECHAT_WARNING, filename, line_number); - fclose (file); - free (filename); - return -2; - } else { pos[0] = '\0'; @@ -762,113 +822,62 @@ config_read () } } if (option_number < 0) - { gui_printf (NULL, _("%s %s, line %d: invalid option \"%s\"\n"), WEECHAT_WARNING, filename, line_number, ptr_line); - fclose (file); - free (filename); - return -2; - } - switch (weechat_options[section] - [option_number].option_type) + else { - case OPTION_TYPE_BOOLEAN: - if (strcasecmp (pos, "on") == 0) - *weechat_options[section] - [option_number].ptr_int = BOOL_TRUE; - else if (strcasecmp (pos, "off") == 0) - *weechat_options[section] - [option_number].ptr_int = BOOL_FALSE; - else - { - gui_printf (NULL, - _("%s %s, line %d: invalid value for" - "option '%s'\n" - "Expected: boolean value: " - "'off' or 'on'\n"), - WEECHAT_WARNING, filename, - line_number, ptr_line); - fclose (file); - free (filename); - return -2; - } - break; - case OPTION_TYPE_INT: - int_value = atoi (pos); - if ((int_value < - weechat_options[section] - [option_number].min) - || (int_value > - weechat_options[section] - [option_number].max)) - { - gui_printf (NULL, - _("%s %s, line %d: invalid value for " - "option '%s'\n" - "Expected: integer between %d " - "and %d\n"), - WEECHAT_WARNING, filename, - line_number, ptr_line, - weechat_options[section][option_number].min, - weechat_options[section][option_number].max); - fclose (file); - free (filename); - return -2; - } - *weechat_options[section][option_number].ptr_int = - int_value; - break; - case OPTION_TYPE_INT_WITH_STRING: - int_value = get_pos_array_values ( - weechat_options[section][option_number].array_values, - pos); - if (int_value < 0) - { - gui_printf (NULL, - _("%s %s, line %d: invalid value for " - "option '%s'\n" - "Expected: one of these strings: "), - WEECHAT_WARNING, filename, - line_number, ptr_line); - i = 0; - while (weechat_options[section][option_number].array_values[i]) - { - gui_printf (NULL, "\"%s\" ", - weechat_options[section][option_number].array_values[i]); - i++; - } - gui_printf (NULL, "\n"); - fclose (file); - free (filename); - return -2; - } - *weechat_options[section][option_number].ptr_int = - int_value; - break; - case OPTION_TYPE_COLOR: - if (!gui_assign_color ( - weechat_options[section][option_number].ptr_int, - pos)) + if (config_option_set_value (&weechat_options[section][option_number], pos) < 0) + { + switch (weechat_options[section] + [option_number].option_type) { - gui_printf (NULL, - _("%s %s, line %d: invalid color " - "name for option '%s'\n"), - WEECHAT_WARNING, filename, - line_number, - ptr_line); - fclose (file); - free (filename); - return -2; + case OPTION_TYPE_BOOLEAN: + gui_printf (NULL, + _("%s %s, line %d: invalid value for" + "option '%s'\n" + "Expected: boolean value: " + "'off' or 'on'\n"), + WEECHAT_WARNING, filename, + line_number, ptr_line); + break; + case OPTION_TYPE_INT: + gui_printf (NULL, + _("%s %s, line %d: invalid value for " + "option '%s'\n" + "Expected: integer between %d " + "and %d\n"), + WEECHAT_WARNING, filename, + line_number, ptr_line, + weechat_options[section][option_number].min, + weechat_options[section][option_number].max); + break; + case OPTION_TYPE_INT_WITH_STRING: + gui_printf (NULL, + _("%s %s, line %d: invalid value for " + "option '%s'\n" + "Expected: one of these strings: "), + WEECHAT_WARNING, filename, + line_number, ptr_line); + i = 0; + while (weechat_options[section][option_number].array_values[i]) + { + gui_printf (NULL, "\"%s\" ", + weechat_options[section][option_number].array_values[i]); + i++; + } + gui_printf (NULL, "\n"); + break; + case OPTION_TYPE_COLOR: + gui_printf (NULL, + _("%s %s, line %d: invalid color " + "name for option '%s'\n"), + WEECHAT_WARNING, filename, + line_number, + ptr_line); + break; } - break; - case OPTION_TYPE_STRING: - if (*weechat_options[section] - [option_number].ptr_string) - free (*weechat_options[section][option_number].ptr_string); - *weechat_options[section][option_number].ptr_string = - strdup (pos); - break; + } } } } |