--- cfg_main.c 2002/07/10 12:00:23 1.1
+++ cfg_main.c 2002/07/10 14:46:28 1.2
@@ -31,32 +31,168 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
+#include <errno.h>
#include "cfg.h"
+#include "cfg_fmt.h"
+#include "cfg_main.h"
#include "cfg_data.h"
+#include "cfg_node.h"
+#include "cfg_syn.h"
cfg_rc_t cfg_create(cfg_t **cfg)
{
+ cfg_rc_t rc;
+
+ if (cfg == NULL)
+ return CFG_ERR_ARG;
+ if ((*cfg = malloc(sizeof(cfg_t))) == NULL)
+ return CFG_ERR_SYS;
+ (*cfg)->root = NULL;
+ (*cfg)->szError[0] = '\0';
+ (*cfg)->szErrorInfo[0] = '\0';
+ (*cfg)->rcErrorInfo = CFG_OK;
+ if ((rc = cfg_grid_create(&((*cfg)->grid_nodes), sizeof(cfg_node_t), 512)) != CFG_OK)
+ return rc;
+ if ((rc = cfg_grid_create(&((*cfg)->grid_tokens), 8192, 1)) != CFG_OK) {
+ cfg_grid_destroy((*cfg)->grid_nodes);
+ return rc;
+ }
return CFG_OK;
}
cfg_rc_t cfg_destroy(cfg_t *cfg)
{
+ if (cfg == NULL)
+ return CFG_ERR_ARG;
+ cfg_grid_destroy(cfg->grid_nodes);
+ cfg_grid_destroy(cfg->grid_tokens);
+ free(cfg);
+ return CFG_OK;
+}
+
+cfg_rc_t cfg_error_info(cfg_t *cfg, cfg_rc_t rv, const char *fmt, ...)
+{
+ va_list ap;
+
+ /* argument sanity check */
+ if (cfg == NULL || rv == CFG_OK || fmt == NULL)
+ return CFG_ERR_ARG;
+
+ /* remember error information */
+ va_start(ap, fmt);
+ cfg_fmt_vsprintf(cfg->szErrorInfo, sizeof(cfg->szErrorInfo), fmt, ap);
+ cfg->rcErrorInfo = rv;
+ va_end(ap);
+
return CFG_OK;
}
-cfg_rc_t cfg_error(cfg_t *cfg, cfg_rc_t rc, char *err_ptr, size_t err_len)
+cfg_rc_t cfg_error(cfg_t *cfg, cfg_rc_t rv, char **error)
{
+ char *sz;
+ char *cpBuf;
+ int nBuf;
+ int n;
+
+ /* argument sanity check */
+ if (cfg == NULL || error == NULL)
+ return CFG_ERR_ARG;
+
+ /* start at begin of buffer */
+ cpBuf = cfg->szError;
+ nBuf = sizeof(cfg->szError);
+
+ /* translate result value into corresponding string */
+ if (rv == CFG_OK) sz = "everything ok";
+ else if (rv == CFG_ERR_ARG) sz = "invalid argument";
+ else if (rv == CFG_ERR_USE) sz = "invalid use";
+ else if (rv == CFG_ERR_MEM) sz = "no more memory available";
+ else if (rv == CFG_ERR_SYS) sz = "operating system error";
+ else if (rv == CFG_ERR_FMT) sz = "formatting error";
+ else if (rv == CFG_ERR_INT) sz = "internal error";
+ else if (rv == CFG_ERR_SYN) sz = "syntax error";
+ else if (rv == CFG_ERR_NDE) sz = "node reference error";
+ else sz = "unknown error";
+ n = cfg_fmt_sprintf(cpBuf, nBuf, "%s", sz);
+ cpBuf += n;
+ nBuf -= n;
+
+ /* optionally annotate with error information */
+ if (rv == cfg->rcErrorInfo && cfg->szErrorInfo[0] != '\0') {
+ n = cfg_fmt_sprintf(cpBuf, nBuf, "; %s", cfg->szErrorInfo);
+ cpBuf += n;
+ nBuf -= n;
+ }
+
+ /* optionally annotate with operating system error information */
+ if (rv == CFG_ERR_SYS) {
+ n = cfg_fmt_sprintf(cpBuf, nBuf, "; %s (%d)", strerror(errno), errno);
+ cpBuf += n;
+ nBuf -= n;
+ }
+
+ /* return pointer to internal buffer */
+ *error = cfg->szError;
+
return CFG_OK;
}
cfg_rc_t cfg_import(cfg_t *cfg, cfg_node_t *node, cfg_fmt_t fmt, const char *in_ptr, size_t in_len)
{
+ cfg_rc_t rv;
+ char error[1024];
+ cfg_node_t *root;
+
+ if (cfg == NULL || in_ptr == NULL || in_len == 0)
+ return CFG_ERR_ARG;
+ if (node == NULL)
+ node = cfg->root;
+ if (fmt == CFG_FMT_CFG) {
+ if ((rv = cfg_syn_import(cfg, &root, in_ptr, in_len, error, sizeof(error))) != CFG_OK) {
+ cfg_error_info(cfg, rv, "%s", error);
+ return rv;
+ }
+ }
+ /* FIXME: CFG_FMT_XML */
+ else
+ return CFG_ERR_INT;
+
+ /* FIXME merge: cfg->root[node] := cfg->root[node] + root */
+ cfg->root = root;
+
return CFG_OK;
}
-cfg_rc_t cfg_export(cfg_t *cfg, cfg_node_t *node, cfg_fmt_t fmt, char *ex_ptr, size_t ex_len)
+cfg_rc_t cfg_export(cfg_t *cfg, cfg_node_t *node, cfg_fmt_t fmt, char **ex_ptr, size_t ex_len)
{
+ cfg_rc_t rv;
+ char *output;
+ size_t n;
+
+ if (cfg == NULL || ex_ptr == NULL)
+ return CFG_ERR_ARG;
+ if (node == NULL)
+ node = cfg->root;
+ if (fmt == CFG_FMT_CFG) {
+ if ((rv = cfg_syn_export(cfg, node, &output)) != CFG_OK)
+ return rv;
+ }
+ /* FIXME: CFG_FMT_XML */
+ else
+ return CFG_ERR_INT;
+
+ /* provide output */
+ if (ex_len == 0)
+ *ex_ptr = output;
+ else {
+ n = strlen(output);
+ if (n >= ex_len)
+ return CFG_ERR_MEM;
+ memmove(*ex_ptr, output, n+1);
+ free(output);
+ }
+
return CFG_OK;
}
|