|
|
|
@@ -12,15 +12,20 @@
|
|
|
|
|
*
|
|
|
|
|
* @see https://lowenware.com/
|
|
|
|
|
*/
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <cStuff/file.h>
|
|
|
|
|
#include <cStuff/config.h>
|
|
|
|
|
|
|
|
|
|
static const char
|
|
|
|
|
m_mixed_line_indent[] = "Line indent contains tabs and spaces"
|
|
|
|
|
, m_mixed_block_indent[] = "Block was initiated with different indent"
|
|
|
|
|
, m_incomplete_key[] = "Incomplete key"
|
|
|
|
|
, m_parser_error[] = "Parser error"
|
|
|
|
|
, m_indent_error[] = "Indent error"
|
|
|
|
|
, m_out_of_memory[] = "Out of memory"
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
@@ -29,7 +34,7 @@ struct config_path {
|
|
|
|
|
int size;
|
|
|
|
|
int indent;
|
|
|
|
|
int level;
|
|
|
|
|
bool is_block;
|
|
|
|
|
bool is_node;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct config_ctx {
|
|
|
|
@@ -50,13 +55,12 @@ config_get_indent(char *line)
|
|
|
|
|
if (c == ' ' || c == '\t') {
|
|
|
|
|
if (!chr) {
|
|
|
|
|
chr = c;
|
|
|
|
|
}
|
|
|
|
|
if (chr == c) {
|
|
|
|
|
result++;
|
|
|
|
|
} else {
|
|
|
|
|
if (chr == c) {
|
|
|
|
|
result++;
|
|
|
|
|
} else {
|
|
|
|
|
/* mixed indent */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
/* mixed indent */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
@@ -73,30 +77,35 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
|
|
|
|
|
{
|
|
|
|
|
const char *err = NULL;
|
|
|
|
|
CStuffConfigEvent evt = CSTUFF_CONFIG_NONE;
|
|
|
|
|
ssize_t i;
|
|
|
|
|
int k_len = 0, eq = -1, v_len = 0, last_ch = -1, tab = 0;
|
|
|
|
|
char c, *key, *val = NULL;
|
|
|
|
|
ssize_t i = 0;
|
|
|
|
|
int k_len = 0, v_len = 0, indent = 0;
|
|
|
|
|
char c, *key = NULL, *val = NULL;
|
|
|
|
|
|
|
|
|
|
cfg->line_num++;
|
|
|
|
|
fprintf(stderr, "cfg:%d ", cfg->line_num);
|
|
|
|
|
|
|
|
|
|
if ((indent = config_get_indent(line)) != -1) {
|
|
|
|
|
fprintf(stderr, "indent=%d ", indent);
|
|
|
|
|
key = &line[indent];
|
|
|
|
|
|
|
|
|
|
for (i = indent; i < length; i++) {
|
|
|
|
|
c = line[i];
|
|
|
|
|
if (islanum(c) || c == '_' || c == '-') {
|
|
|
|
|
if (isalnum(c) || c == '_' || c == '-') {
|
|
|
|
|
k_len++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
k_len++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (i < length) {
|
|
|
|
|
switch ((c = line[i++])) {
|
|
|
|
|
case ':':
|
|
|
|
|
evt = CSTUFF_CONFIG_NODE;
|
|
|
|
|
fprintf(stderr, "node! ");
|
|
|
|
|
continue;
|
|
|
|
|
case '=':
|
|
|
|
|
evt = CSTUFF_CONFIG_PAIR;
|
|
|
|
|
fprintf(stderr, "pair! ");
|
|
|
|
|
continue;
|
|
|
|
|
case ' ':
|
|
|
|
|
case '\t':
|
|
|
|
@@ -114,8 +123,7 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
|
|
|
|
|
err = m_incomplete_key;
|
|
|
|
|
}
|
|
|
|
|
} else if (evt == CSTUFF_CONFIG_PAIR) {
|
|
|
|
|
int l = i;
|
|
|
|
|
val = &line[i];
|
|
|
|
|
val = &line[i - 1];
|
|
|
|
|
while (i < length) {
|
|
|
|
|
switch (line[i++]) {
|
|
|
|
|
case ';':
|
|
|
|
@@ -125,7 +133,7 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
|
|
|
|
|
case '\t':
|
|
|
|
|
continue;
|
|
|
|
|
default:
|
|
|
|
|
v_len = &line[i - 1];
|
|
|
|
|
v_len = (int) (&line[i] - val) - 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
@@ -135,30 +143,32 @@ config_read_line(char *line, ssize_t length, struct cstuff_config *cfg)
|
|
|
|
|
err = m_parser_error;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
evt = cSTUFF_CONFIG_ERROR;
|
|
|
|
|
evt = CSTUFF_CONFIG_ERROR;
|
|
|
|
|
err = m_mixed_line_indent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Key ! */
|
|
|
|
|
key[k_len] = 0;
|
|
|
|
|
if (key)
|
|
|
|
|
key[k_len] = 0;
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
switch ((cfg->evt = evt)) {
|
|
|
|
|
case CSTUFF_CONFIG_PAIR:
|
|
|
|
|
cfg->data.pair.key = key;
|
|
|
|
|
cfg->data.pair.value = val;
|
|
|
|
|
cfg->data.pair.value_len = v_len;
|
|
|
|
|
cfg->data.pair.val = val;
|
|
|
|
|
cfg->data.pair.val_len = v_len;
|
|
|
|
|
cfg->data.pair.key_len = k_len;
|
|
|
|
|
val[v_len] = 0;
|
|
|
|
|
return indent;
|
|
|
|
|
case CSTUFF_CONFIG_NODE:
|
|
|
|
|
cfg->data.pair.node = key;
|
|
|
|
|
cfg->data.pair.node_len = k_len;
|
|
|
|
|
cfg->data.node.name = key;
|
|
|
|
|
cfg->data.node.name_len = k_len;
|
|
|
|
|
return indent;
|
|
|
|
|
case CSTUFF_CONFIG_ERROR:
|
|
|
|
|
default:
|
|
|
|
|
cfg->data.error.char_num = i;
|
|
|
|
|
cfg->data.error.line_text = line;
|
|
|
|
|
cfg->data.error.err_text = err;
|
|
|
|
|
cfg->data.error.line_txt = line;
|
|
|
|
|
cfg->data.error.err_txt = err;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -168,7 +178,8 @@ static void
|
|
|
|
|
config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
|
|
|
|
|
{
|
|
|
|
|
struct config_path *path = &ctx->path;
|
|
|
|
|
const char *key = ctx->cfg.data.pair.key, ptr;
|
|
|
|
|
const char *key = ctx->cfg.data.pair.key;
|
|
|
|
|
char *ptr;
|
|
|
|
|
int i, len, level, k_len = ctx->cfg.data.pair.key_len;
|
|
|
|
|
|
|
|
|
|
if (line_indent) {
|
|
|
|
@@ -183,11 +194,14 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
|
|
|
|
|
level = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (level > path->level && !path->is_node ||
|
|
|
|
|
level - path->level != path->indent) {
|
|
|
|
|
if (level > path->level && (!path->is_node ||
|
|
|
|
|
level - path->level != path->indent)) {
|
|
|
|
|
fprintf(stderr, "cfg:%d ((%d > %d && !%d) || %d - %d != %d)\n",
|
|
|
|
|
ctx->cfg.line_num, level,
|
|
|
|
|
path->level, path->is_node & 0xFF, level, path->level, path->indent);
|
|
|
|
|
ctx->cfg.evt = CSTUFF_CONFIG_ERROR;
|
|
|
|
|
ctx->cfg.data.error.line_text = NULL;
|
|
|
|
|
ctx->cfg.data.error.err_text = m_indent_error;
|
|
|
|
|
ctx->cfg.data.error.line_txt = NULL;
|
|
|
|
|
ctx->cfg.data.error.err_txt = m_indent_error;
|
|
|
|
|
ctx->cfg.data.error.char_num = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@@ -195,7 +209,8 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
|
|
|
|
|
ptr = ctx->path.key;
|
|
|
|
|
for (i = 0; i < level; i++) {
|
|
|
|
|
int l = strcspn(ptr, ".");
|
|
|
|
|
ptr += (l + 1);
|
|
|
|
|
ptr += l;
|
|
|
|
|
fprintf(stderr, "l = %d\n", l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = (ptr - ctx->path.key) + 1 + k_len + 1;
|
|
|
|
@@ -203,8 +218,8 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
|
|
|
|
|
char *new_key;
|
|
|
|
|
if (!(new_key = realloc(ctx->path.key, len))) {
|
|
|
|
|
ctx->cfg.evt = CSTUFF_CONFIG_ERROR;
|
|
|
|
|
ctx->cfg.data.error.line_text = NULL;
|
|
|
|
|
ctx->cfg.data.error.err_text = m_out_of_memory;
|
|
|
|
|
ctx->cfg.data.error.line_txt = NULL;
|
|
|
|
|
ctx->cfg.data.error.err_txt = m_out_of_memory;
|
|
|
|
|
ctx->cfg.data.error.char_num = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@@ -212,9 +227,14 @@ config_set_path_key(struct config_ctx *ctx, int line_indent, bool is_node)
|
|
|
|
|
ctx->path.size = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*(ptr++) = '.';
|
|
|
|
|
if (level) {
|
|
|
|
|
*(ptr++) = '.';
|
|
|
|
|
} else {
|
|
|
|
|
*ptr = 0;
|
|
|
|
|
}
|
|
|
|
|
strcpy(ptr, key);
|
|
|
|
|
path->level = level;
|
|
|
|
|
path->is_node = is_node;
|
|
|
|
|
|
|
|
|
|
ctx->cfg.data.pair.key = ctx->path.key;
|
|
|
|
|
}
|
|
|
|
@@ -224,16 +244,19 @@ static int
|
|
|
|
|
config_get_line(char *line, ssize_t length, void *p_ctx)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
struct config_ctx *ctx = (struct config_ctx *)ctx;
|
|
|
|
|
struct config_ctx *ctx = (struct config_ctx *)p_ctx;
|
|
|
|
|
|
|
|
|
|
memset(&ctx->cfg.data, 0, sizeof (ctx->cfg.data));
|
|
|
|
|
if ((result = config_read_line(line, length, &ctx->cfg)) != -1) {
|
|
|
|
|
bool is_node = (ctx->cfg.evt == CSTUFF_CONFIG_NODE);
|
|
|
|
|
/* result == indent */
|
|
|
|
|
config_set_path_key(ctx, indent, is_node);
|
|
|
|
|
|
|
|
|
|
result = ctx->callback(&ctx->cfg, ctx->u_ptr);
|
|
|
|
|
config_set_path_key(ctx, result, is_node);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "cfg: read line failed %d\n", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = ctx->callback(&ctx->cfg, ctx->u_ptr);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -250,14 +273,16 @@ cstuff_config_parse(const char *file, CStuffConfigCallback callback, void *u_ptr
|
|
|
|
|
return CSTUFF_MALLOC_ERROR;
|
|
|
|
|
} else {
|
|
|
|
|
ctx.path.size = CSTUFF_CONFIG_PATH_SIZE;
|
|
|
|
|
ctx.path.indent = 0;
|
|
|
|
|
ctx.path.in_chr = 0;
|
|
|
|
|
}
|
|
|
|
|
ctx.u_ptr = u_ptr;
|
|
|
|
|
ctx.callback = callback;
|
|
|
|
|
|
|
|
|
|
rc = cstuff_file_get_lines(file, config_get_line, (void *)&ctx);
|
|
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
|
fprintf(stderr, "cfg: get lines failed %d (%s)\n", rc, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ctx.path.key)
|
|
|
|
|
free(ctx.path.key);
|
|
|
|
|
return rc;
|
|
|
|
|