# HG changeset patch # User Matti Hamalainen # Date 1577266963 -7200 # Node ID 836e16f27b345c8c4d5516d504f844f705afe5a1 # Parent 1dbd9259c3b85ba8d9ac60d265d84265da1eb4c1 Cleanup. diff -r 1dbd9259c3b8 -r 836e16f27b34 th_config.c --- a/th_config.c Wed Dec 25 11:05:15 2019 +0200 +++ b/th_config.c Wed Dec 25 11:42:43 2019 +0200 @@ -11,6 +11,7 @@ #include #include + #define SET_MAX_BUF (8192) @@ -159,11 +160,11 @@ /* Add new section */ -int th_cfg_add_section(th_cfgitem_t **cfg, const char *name, th_cfgitem_t *data) +int th_cfg_add_section(th_cfgitem_t **cfg, const char *name, th_cfgitem_t *sect) { th_cfgitem_t *node; - node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) data); + node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) sect); if (node == NULL) return -1; @@ -207,7 +208,7 @@ #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#') -static int th_cfg_read_sect(th_ioctx *ctx, th_cfgitem_t *cfg, int nesting) +static int th_cfg_read_sect(th_ioctx *fh, th_cfgitem_t *cfg, int nesting) { th_cfgitem_t *item = NULL; char *tmpStr = NULL; @@ -231,12 +232,12 @@ if (c == -1) { // Get next character - switch (c = thfgetc(ctx)) + switch (c = thfgetc(fh)) { case EOF: if (parseMode != PM_IDLE) { - th_io_error(ctx, THERR_OUT_OF_DATA, "Unexpected end of file.\n"); + th_io_error(fh, THERR_OUT_OF_DATA, "Unexpected end of file.\n"); parseMode = PM_ERROR; } else @@ -244,7 +245,7 @@ break; case '\n': - ctx->line++; + fh->line++; } } @@ -280,7 +281,7 @@ goto out; else { - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Invalid nesting sequence encountered.\n"); parseMode = PM_ERROR; } @@ -295,7 +296,7 @@ else { // Error! Invalid character found - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Unexpected character '%c'.\n", c); parseMode = PM_ERROR; } @@ -324,7 +325,7 @@ else { // Error! Key name string too long! - th_io_error(ctx, THERR_INVALID_DATA, "Config key name too long!"); + th_io_error(fh, THERR_INVALID_DATA, "Config key name too long!"); parseMode = PM_ERROR; } c = -1; @@ -333,7 +334,7 @@ { // Error! Invalid character found tmpStr[strPos] = 0; - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Unexpected character '%c' in key name '%s'.\n", c, tmpStr); parseMode = PM_ERROR; @@ -394,7 +395,7 @@ else { // Error! No configuration key by this name found - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "No such configuration setting ('%s')\n", tmpStr); parseMode = PM_ERROR; @@ -405,7 +406,7 @@ else { // Error! '=' expected! - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Unexpected character '%c', assignation '=' was expected.\n", c); parseMode = PM_ERROR; @@ -469,14 +470,14 @@ if (c != '{') { // Error! Section start '{' expected! - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Unexpected character '%c', section start '{' was expected.\n", c); parseMode = PM_ERROR; } else { - int res = th_cfg_read_sect(ctx, item->v.section, nesting + 1); + int res = th_cfg_read_sect(fh, item->v.section, nesting + 1); c = -1; if (res > 0) validError = TRUE; @@ -532,7 +533,7 @@ else { // Error! String too long! - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "String too long! Maximum is %d characters.", SET_MAX_BUF); parseMode = PM_ERROR; @@ -547,7 +548,7 @@ if (isStart && item->type == ITEM_UINT && c == '-') { // Error! Negative values not allowed for unsigned ints - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Negative value specified for %s, unsigned value expected.", item->name); parseMode = PM_ERROR; @@ -607,7 +608,7 @@ else { // Error! Unexpected character. - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Unexpected character '%c' for numeric setting '%s'.", c, item->name); parseMode = PM_ERROR; @@ -616,7 +617,7 @@ if (isError) { // Error! String too long! - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "String too long! Maximum is %d characters.", SET_MAX_BUF); parseMode = PM_ERROR; @@ -656,7 +657,7 @@ if (isError) { - th_io_error(ctx, THERR_INVALID_DATA, + th_io_error(fh, THERR_INVALID_DATA, "Invalid boolean value for '%s'.\n", item->name); parseMode = PM_ERROR; @@ -681,52 +682,51 @@ } -int th_cfg_read(th_ioctx *ctx, th_cfgitem_t *cfg) +int th_cfg_read(th_ioctx *fh, th_cfgitem_t *cfg) { - if (ctx == NULL || cfg == NULL) + if (fh == NULL || cfg == NULL) return -1; - return th_cfg_read_sect(ctx, cfg, 0); + return th_cfg_read_sect(fh, cfg, 0); } /* Write a configuration into file */ -static void th_print_indent(th_ioctx *ctx, int nesting) +static void th_print_indent(th_ioctx *fh, int nesting) { - int i; - for (i = 0; i < nesting * 2; i++) - thfputc(' ', ctx); + for (int i = 0; i < nesting * 2; i++) + thfputc(' ', fh); } -static int th_cfg_write_sect(th_ioctx *ctx, const th_cfgitem_t *item, int nesting) +static int th_cfg_write_sect(th_ioctx *fh, const th_cfgitem_t *item, int nesting) { while (item != NULL) { if (item->type == ITEM_COMMENT) { - th_print_indent(ctx, nesting); - if (thfprintf(ctx, "# %s\n", + th_print_indent(fh, nesting); + if (thfprintf(fh, "# %s\n", (item->name != NULL) ? item->name : "") < 0) return -1; } else if (item->name != NULL) { - th_print_indent(ctx, nesting); + th_print_indent(fh, nesting); switch (item->type) { case ITEM_STRING: if (*(item->v.val_str) == NULL) { - if (thfprintf(ctx, "#%s = \"\"\n", + if (thfprintf(fh, "#%s = \"\"\n", item->name) < 0) return -3; } else { - if (thfprintf(ctx, "%s = \"%s\"\n", + if (thfprintf(fh, "%s = \"%s\"\n", item->name, *(item->v.val_str)) < 0) return -3; } @@ -735,7 +735,7 @@ case ITEM_STRING_LIST: if (*(item->v.list) == NULL) { - if (thfprintf(ctx, + if (thfprintf(fh, "#%s = \"\", \"\"\n", item->name) < 0) return -3; } @@ -743,46 +743,46 @@ { th_llist_t *node = *(item->v.list); size_t n = th_llist_length(node); - if (thfprintf(ctx, "%s = ", item->name) < 0) + if (thfprintf(fh, "%s = ", item->name) < 0) return -3; for (; node != NULL; node = node->next) { if (node->data != NULL) - thfprintf(ctx, "\"%s\"", (char *) node->data); + thfprintf(fh, "\"%s\"", (char *) node->data); if (--n > 0) { - thfprintf(ctx, ",\n"); - th_print_indent(ctx, nesting); + thfprintf(fh, ",\n"); + th_print_indent(fh, nesting); } } - if (thfprintf(ctx, "\n") < 0) + if (thfprintf(fh, "\n") < 0) return -3; } break; case ITEM_INT: - if (thfprintf(ctx, "%s = %i\n", + if (thfprintf(fh, "%s = %i\n", item->name, *(item->v.val_int)) < 0) return -4; break; case ITEM_UINT: - if (thfprintf(ctx, "%s = %d\n", + if (thfprintf(fh, "%s = %d\n", item->name, *(item->v.val_uint)) < 0) return -5; break; case ITEM_FLOAT: - if (thfprintf(ctx, "%s = %1.5f\n", + if (thfprintf(fh, "%s = %1.5f\n", item->name, *(item->v.val_float)) < 0) return -5; break; case ITEM_BOOL: - if (thfprintf(ctx, "%s = %s\n", item->name, + if (thfprintf(fh, "%s = %s\n", item->name, *(item->v.val_bool) ? "yes" : "no") < 0) return -6; break; @@ -790,18 +790,18 @@ case ITEM_SECTION: { int res; - if (thfprintf(ctx, "%s = {\n", item->name) < 0) + if (thfprintf(fh, "%s = {\n", item->name) < 0) return -7; - res = th_cfg_write_sect(ctx, item->v.section, nesting + 1); + res = th_cfg_write_sect(fh, item->v.section, nesting + 1); if (res != 0) return res; - if (thfprintf(ctx, "}\n\n") < 0) + if (thfprintf(fh, "}\n\n") < 0) return -8; } break; case ITEM_HEX_TRIPLET: - if (thfprintf(ctx, "%s = \"%06x\"\n", + if (thfprintf(fh, "%s = \"%06x\"\n", item->name, *(item->v.val_int)) < 0) return -6; break; @@ -814,15 +814,15 @@ } -int th_cfg_write(th_ioctx *ctx, const th_cfgitem_t *cfg) +int th_cfg_write(th_ioctx *fh, const th_cfgitem_t *cfg) { - if (ctx == NULL || cfg == NULL) + if (fh == NULL || cfg == NULL) return -1; - thfprintf(ctx, "# Configuration written by %s %s\n\n", + thfprintf(fh, "# Configuration written by %s %s\n\n", th_prog_desc, th_prog_version); - return th_cfg_write_sect(ctx, cfg, 0); + return th_cfg_write_sect(fh, cfg, 0); } diff -r 1dbd9259c3b8 -r 836e16f27b34 th_config.h --- a/th_config.h Wed Dec 25 11:05:15 2019 +0200 +++ b/th_config.h Wed Dec 25 11:42:43 2019 +0200 @@ -57,12 +57,12 @@ /* Functions */ -int th_cfg_read(th_ioctx *ctx, th_cfgitem_t *cfg); +int th_cfg_read(th_ioctx *fh, th_cfgitem_t *cfg /*, TODO XXX const int flags (for controlling things like "error out on unknown items or ignore" etc */); void th_cfg_free(th_cfgitem_t *cfg); -int th_cfg_write(th_ioctx *ctx, const th_cfgitem_t *cfg); +int th_cfg_write(th_ioctx *fh, const th_cfgitem_t *cfg); -int th_cfg_add_section(th_cfgitem_t **cfg, const char *name, th_cfgitem_t *data); int th_cfg_add_comment(th_cfgitem_t **cfg, const char *comment); +int th_cfg_add_section(th_cfgitem_t **cfg, const char *name, th_cfgitem_t *sect); int th_cfg_add_int(th_cfgitem_t **cfg, const char *name, int *data, int defValue); int th_cfg_add_uint(th_cfgitem_t **cfg, const char *name, unsigned int *data, unsigned int defValue);