static void __config_write_setting(const config_setting_t *setting, FILE *stream, int depth, unsigned short tab_width) { int output_format = setting->config->output_format; if(depth > 1) __config_indent(stream, depth, tab_width); if(setting->name) { char separator; fputs(setting->name, stream); if(SHOULD_USE_COLON(output_format) && SHOULD_USE_ASSIGN(output_format)) separator = (setting->type == CONFIG_TYPE_GROUP ? ':' : '='); else separator = SHOULD_USE_COLON(output_format) ? ':' : '='; fprintf(stream, " %c ", separator); } __config_write_value(&(setting->value), setting->type, config_setting_get_format(setting), depth, tab_width, stream); if(depth > 0) { if (SHOULD_APPEND_SEMICOLON(output_format)) fputc(';', stream); else if (SHOULD_APPEND_COMMA(output_format)) fputc(':', stream); fputc('\n', stream); } }
static void __config_write_setting(const config_t *config, const config_setting_t *setting, FILE *stream, int depth) { char group_assign_char = __config_has_option( config, CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS) ? ':' : '='; char nongroup_assign_char = __config_has_option( config, CONFIG_OPTION_COLON_ASSIGNMENT_FOR_NON_GROUPS) ? ':' : '='; if(depth > 1) __config_indent(stream, depth, config->tab_width); if(setting->name) { fputs(setting->name, stream); fprintf(stream, " %c ", ((setting->type == CONFIG_TYPE_GROUP) ? group_assign_char : nongroup_assign_char)); } __config_write_value(config, &(setting->value), setting->type, config_setting_get_format(setting), depth, stream); if(depth > 0) { if(__config_has_option(config, CONFIG_OPTION_SEMICOLON_SEPARATORS)) fputc(';', stream); fputc('\n', stream); } }
static void __config_write_setting (const config_setting_t * setting, FILE * stream, int depth, unsigned short tab_width) { if (depth > 1) __config_indent (stream, depth, tab_width); if (setting->name) { fputs (setting->name, stream); fprintf (stream, " %c ", (setting->type == CONFIG_TYPE_GROUP ? ':' : '=')); } __config_write_value (&(setting->value), setting->type, config_setting_get_format (setting), depth, tab_width, stream); if (depth > 0) { fputc (';', stream); fputc ('\n', stream); } }
static void __config_write_value(const config_value_t *value, int type, int format, int depth, unsigned short tab_width, FILE *stream) { char fbuf[64]; switch(type) { /* boolean */ case CONFIG_TYPE_BOOL: fputs(value->ival ? "true" : "false", stream); break; /* int */ case CONFIG_TYPE_INT: switch(format) { case CONFIG_FORMAT_HEX: fprintf(stream, "0x%X", value->ival); break; case CONFIG_FORMAT_DEFAULT: default: fprintf(stream, "%d", value->ival); break; } break; /* 64-bit int */ case CONFIG_TYPE_INT64: switch(format) { case CONFIG_FORMAT_HEX: fprintf(stream, "0x" INT64_HEX_FMT "L", value->llval); break; case CONFIG_FORMAT_DEFAULT: default: fprintf(stream, INT64_FMT "L", value->llval); break; } break; /* float */ case CONFIG_TYPE_FLOAT: { char *q; snprintf(fbuf, sizeof(fbuf) - 3, "%.*g", FLOAT_PRECISION, value->fval); /* check for exponent */ q = strchr(fbuf, 'e'); if(! q) { /* no exponent */ if(! strchr(fbuf, '.')) /* no decimal point */ strcat(fbuf, ".0"); else { /* has decimal point */ char *p; for(p = fbuf + strlen(fbuf) - 1; p > fbuf; --p) { if(*p != '0') { *(++p) = '\0'; break; } } } } fputs(fbuf, stream); break; } /* string */ case CONFIG_TYPE_STRING: { char *p; fputc('\"', stream); if(value->sval) { for(p = value->sval; *p; p++) { int c = (int)*p & 0xFF; switch(c) { case '\"': case '\\': fputc('\\', stream); fputc(c, stream); break; case '\n': fputs("\\n", stream); break; case '\r': fputs("\\r", stream); break; case '\f': fputs("\\f", stream); break; case '\t': fputs("\\t", stream); break; default: if(c >= ' ') fputc(c, stream); else fprintf(stream, "\\x%02X", c); } } } fputc('\"', stream); break; } /* list */ case CONFIG_TYPE_LIST: { config_list_t *list = value->list; fprintf(stream, "( "); if(list) { int len = list->length; config_setting_t **s; for(s = list->elements; len--; s++) { __config_write_value(&((*s)->value), (*s)->type, config_setting_get_format(*s), depth + 1, tab_width, stream); if(len) fputc(',', stream); fputc(' ', stream); } } fputc(')', stream); break; } /* array */ case CONFIG_TYPE_ARRAY: { config_list_t *list = value->list; fprintf(stream, "[ "); if(list) { int len = list->length; config_setting_t **s; for(s = list->elements; len--; s++) { __config_write_value(&((*s)->value), (*s)->type, config_setting_get_format(*s), depth + 1, tab_width, stream); if(len) fputc(',', stream); fputc(' ', stream); } } fputc(']', stream); break; } /* group */ case CONFIG_TYPE_GROUP: { config_list_t *list = value->list; if(depth > 0) { #ifdef K_AND_R_STYLE /* Horrendous, but many people like it. */ fputc(' ', stream); #else fputc('\n', stream); if(depth > 1) __config_indent(stream, depth, tab_width); #endif fprintf(stream, "{\n"); } if(list) { int len = list->length; config_setting_t **s; for(s = list->elements; len--; s++) __config_write_setting(*s, stream, depth + 1, tab_width); } if(depth > 1) __config_indent(stream, depth, tab_width); if(depth > 0) fputc('}', stream); break; } default: /* this shouldn't happen, but handle it gracefully... */ fputs("???", stream); break; } }