ossp-pkg/cfg/cfg_test.c
1.1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "cfg_util.h"
#include "cfg_node.h"
#include "cfg_syn.h"
typedef struct {
int line;
int col;
FILE *fp;
} cb_ctx_t;
static void cb_out(cb_ctx_t *ctx, char *str)
{
int i;
fprintf(stderr, "cb_out <%s>\n", str);
fprintf(ctx->fp, "%s", str);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == '\n') {
ctx->line++;
ctx->col = 0;
}
else {
ctx->col++;
}
}
}
static void cb_fct(void *_ctx, cfg_node_t *node)
{
cb_ctx_t *ctx = (cb_ctx_t *)_ctx;
cfg_node_type_t type;
cfg_node_t *node2;
cfg_rc_t rc;
char *token;
fprintf(stderr, "cb_fct enter\n");
rc = cfg_node_get(node, CFG_NODE_ATTR_TYPE, &type);
fprintf(stderr, "rc=%d type=%d\n", rc, type);
if (type == CFG_NODE_TYPE_SEQ) {
cb_out(ctx, " {\n");
}
else if (type == CFG_NODE_TYPE_DIR) {
cb_out(ctx, ";\n");
cfg_node_get(node, CFG_NODE_ATTR_RBROTH, &node2);
if (node2 == NULL)
cb_out(ctx, "};\n");
}
else if (type == CFG_NODE_TYPE_TOK) {
cfg_node_get(node, CFG_NODE_ATTR_TOKEN, &token);
if (token != NULL) {
cb_out(ctx, token);
cfg_node_get(node, CFG_NODE_ATTR_RBROTH, &node2);
if (node2 != NULL)
cb_out(ctx, " ");
}
}
fprintf(stderr, "cb_fct leave\n");
}
int main(int argc, char *argv[])
{
cfg_rc_t rc;
char *buf_ptr;
size_t buf_size;
cfg_node_t *node;
char err_buf[1024];
if (argc != 2) {
fprintf(stderr, "USAGE: %s <file>\n", argv[0]);
exit(1);
}
if ((rc = cfg_util_readfile(argv[1], &buf_ptr, &buf_size)) != CFG_OK) {
fprintf(stderr, "ERROR: reading file \"%s\"\n", argv[1]);
exit(1);
}
if ((rc = cfg_syn(NULL, &node, err_buf, sizeof(err_buf), buf_ptr)) != CFG_OK) {
fprintf(stderr, "ERROR: %s\n", err_buf);
}
else {
cb_ctx_t ctx;
ctx.line = 0;
ctx.col = 0;
ctx.fp = stdout;
cfg_node_apply(node, cb_fct, &ctx);
}
free(buf_ptr);
return 0;
}