OSSP CVS Repository

ossp - Check-in [2270]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 2270
Date: 2002-Jul-10 16:46:28 (local)
2002-Jul-10 14:46:28 (UTC)
User:rse
Branch:
Comment: Step 2 in API overhauling and implementation filling.
Tickets:
Inspections:
Files:
ossp-pkg/cfg/cfg.h      1.6 -> 1.7     10 inserted, 11 deleted
ossp-pkg/cfg/cfg_main.c      1.1 -> 1.2     138 inserted, 2 deleted
ossp-pkg/cfg/cfg_main.h      1.1 -> 1.2     9 inserted, 0 deleted
ossp-pkg/cfg/cfg_node.c      1.5 -> 1.6     4 inserted, 2 deleted
ossp-pkg/cfg/cfg_syn.c      1.11 -> 1.12     8 inserted, 6 deleted
ossp-pkg/cfg/cfg_syn.h      1.3 -> 1.4     1 inserted, 1 deleted
ossp-pkg/cfg/cfg_test.c      1.4 -> 1.5     36 inserted, 13 deleted

ossp-pkg/cfg/cfg.h 1.6 -> 1.7

--- cfg.h        2002/07/10 14:14:05     1.6
+++ cfg.h        2002/07/10 14:46:28     1.7
@@ -40,11 +40,12 @@
     CFG_OK = 0,               /* everything ok */
     CFG_ERR_ARG,              /* invalid argument */
     CFG_ERR_USE,              /* invalid use */
-    CFG_ERR_SYS,              /* operating system error (see errno) */
-    CFG_ERR_SYN,              /* syntax parsing error */
+    CFG_ERR_MEM,              /* no more memory available */
+    CFG_ERR_SYS,              /* operating system error */
     CFG_ERR_FMT,              /* formatting error */
-    CFG_ERR_GOT,              /* goto error */
-    CFG_ERR_INT               /* internal error */
+    CFG_ERR_INT,              /* internal error */
+    CFG_ERR_SYN,              /* syntax error */
+    CFG_ERR_NDE               /* node reference error */
 } cfg_rc_t;
 
 /* configuration format */
@@ -117,11 +118,11 @@
 /* configuration handling */
 cfg_rc_t   cfg_create      (cfg_t **cfg);
 cfg_rc_t   cfg_destroy     (cfg_t  *cfg);
-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 rc, char **error);
 
 /* configuration import/export */
 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   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);
 
 /* node handling */
 cfg_rc_t   cfg_node_create (cfg_t  *cfg, cfg_node_t **node);
@@ -132,12 +133,10 @@
 cfg_rc_t   cfg_node_set    (cfg_t  *cfg, cfg_node_t  *node, cfg_node_attr_t attr, ...);
 cfg_rc_t   cfg_node_get    (cfg_t  *cfg, cfg_node_t  *node, cfg_node_attr_t attr, ...);
 
-/* node traversing */
+/* node traversing/locating */
 cfg_rc_t   cfg_node_root   (cfg_t  *cfg, cfg_node_t **node);
-cfg_rc_t   cfg_node_goto   (cfg_t  *cfg, cfg_node_t **node, const char *spec);
-
-/* node locating */
-cfg_rc_t   cfg_node_select (cfg_t  *cfg, cfg_node_t  *node, const char *dotpath, cfg_node_t **node2);
+cfg_rc_t   cfg_node_goto   (cfg_t  *cfg, cfg_node_t  *node, const char *spec, cfg_node_t **node2);
+cfg_rc_t   cfg_node_select (cfg_t  *cfg, cfg_node_t  *node, const char *spec, cfg_node_t **node2);
 cfg_rc_t   cfg_node_find   (cfg_t  *cfg, cfg_node_t  *node, cfg_rc_t (*cb_fct_cmp)(cfg_t *, cfg_node_t *, void *), void *cb_ctx_cmp, cfg_node_t **cont);
 cfg_rc_t   cfg_node_apply  (cfg_t  *cfg, cfg_node_t  *node, cfg_rc_t (*cb_fct_cmp)(cfg_t *, cfg_node_t *, void *), void *cb_ctx_cmp, cfg_rc_t (*cb_fct_cb)(cfg_t *, cfg_node_t *, void *), void *cb_ctx_cb);
 cfg_rc_t   cfg_node_cmp    (cfg_t  *cfg, cfg_node_t  *node, void *token);


ossp-pkg/cfg/cfg_main.c 1.1 -> 1.2

--- 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;
 }
 


ossp-pkg/cfg/cfg_main.h 1.1 -> 1.2

--- cfg_main.h   2002/07/10 12:00:23     1.1
+++ cfg_main.h   2002/07/10 14:46:28     1.2
@@ -32,10 +32,19 @@
 #define __CFG_MAIN_H__
 
 #include "cfg.h"
+#include "cfg_node.h"
+#include "cfg_grid.h"
 
 struct cfg_st {
     cfg_node_t *root;
+    cfg_grid_t *grid_nodes;
+    cfg_grid_t *grid_tokens;
+    char        szError[512];
+    char        szErrorInfo[256];
+    cfg_rc_t    rcErrorInfo;
 };
 
+cfg_rc_t cfg_error_info(cfg_t *cfg, cfg_rc_t rv, const char *fmt, ...);
+
 #endif /* __CFG_MAIN_H__ */
 


ossp-pkg/cfg/cfg_node.c 1.5 -> 1.6

--- cfg_node.c   2002/07/10 12:00:23     1.5
+++ cfg_node.c   2002/07/10 14:46:28     1.6
@@ -40,6 +40,8 @@
 {
     cfg_node_t *n;
 
+    if (node == NULL)
+        return CFG_ERR_ARG;
     if ((n = malloc(sizeof(cfg_node_t))) == NULL)
         return CFG_ERR_SYS;
     n->parent   = NULL;
@@ -161,10 +163,10 @@
     return CFG_OK;
 }
 
-cfg_rc_t cfg_node_goto(cfg_t *cfg, cfg_node_t **node, const char *spec)
+cfg_rc_t cfg_node_goto(cfg_t *cfg, cfg_node_t  *node, const char *spec, cfg_node_t **node2)
 {
 
-    if (cfg == NULL || node == NULL || spec == NULL)
+    if (cfg == NULL || node == NULL || spec == NULL || node2 == NULL)
         return CFG_ERR_ARG;
     /* FIXME */
 #if 0


ossp-pkg/cfg/cfg_syn.c 1.11 -> 1.12

--- cfg_syn.c    2002/07/10 12:00:23     1.11
+++ cfg_syn.c    2002/07/10 14:46:28     1.12
@@ -50,14 +50,16 @@
 cfg_rc_t cfg_syn_import(
     cfg_t *cfg, 
     cfg_node_t **node, 
-    const char *input,
-    char *err_buf, size_t err_len)
+    const char *in_ptr,
+    size_t in_len,
+    char *err_buf, 
+    size_t err_len)
 {
     cfg_syn_ctx_t ctx;
     void *yyscan;
     
     /* argument sanity checking */
-    if (node == NULL || input == NULL)
+    if (node == NULL || in_ptr == NULL || in_len == 0)
         return CFG_ERR_ARG;
 
     /* initialize scanner */
@@ -66,9 +68,9 @@
 
     /* establish our own context which is passed 
        through the parser and scanner */
-    ctx.inputptr = input;
-    ctx.inputbuf = input;
-    ctx.inputlen = strlen(input);
+    ctx.inputptr = in_ptr;
+    ctx.inputbuf = in_ptr;
+    ctx.inputlen = in_len;
     ctx.cfg      = cfg;
     ctx.node     = NULL;
     ctx.rv       = CFG_OK;


ossp-pkg/cfg/cfg_syn.h 1.3 -> 1.4

--- cfg_syn.h    2002/07/05 15:32:42     1.3
+++ cfg_syn.h    2002/07/10 14:46:28     1.4
@@ -65,7 +65,7 @@
 #define last_column  last
 
 /* internal API */
-extern cfg_rc_t cfg_syn_import  (cfg_t *cfg, cfg_node_t **node, const char *input, char *err_buf, size_t err_len);
+extern cfg_rc_t cfg_syn_import  (cfg_t *cfg, cfg_node_t **node, const char *in_ptr, size_t in_len, char *err_buf, size_t err_len);
 extern cfg_rc_t cfg_syn_export  (cfg_t *cfg, cfg_node_t *node, char **output);
 extern cfg_rc_t cfg_syn_destroy (cfg_t *cfg, cfg_node_t *node);
 


ossp-pkg/cfg/cfg_test.c 1.4 -> 1.5

--- cfg_test.c   2002/07/05 18:32:37     1.4
+++ cfg_test.c   2002/07/10 14:46:28     1.5
@@ -12,30 +12,53 @@
 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];
+    char *im_ptr;
+    size_t im_size;
+    char *ex_ptr;
+    char *error;
+    cfg_t *cfg;
 
     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) {
+
+    if ((rc = cfg_util_readfile(argv[1], &im_ptr, &im_size)) != CFG_OK) {
         fprintf(stderr, "ERROR: reading file \"%s\"\n", argv[1]);
         exit(1);
     }
-    rc = cfg_syn_import(NULL, &node, buf_ptr, err_buf, sizeof(err_buf));
-    free(buf_ptr);
 
-    if (rc != CFG_OK)
-        fprintf(stderr, "ERROR: %s\n", err_buf);
-    else {
-        cfg_syn_export(NULL, node, &buf_ptr);
-        cfg_syn_destroy(NULL, node);
-        fprintf(stdout, "%s", buf_ptr);
+    if ((rc = cfg_create(&cfg)) != CFG_OK) {
+        cfg_error(cfg, rc, &error);
+        fprintf(stderr, "ERROR: cfg_create: %s\n", error);
+        free(im_ptr);
+        exit(1);
+    }
+
+    if ((rc = cfg_import(cfg, NULL, CFG_FMT_CFG, im_ptr, im_size)) != CFG_OK) {
+        cfg_error(cfg, rc, &error);
+        fprintf(stderr, "ERROR: cfg_import: %s\n", error);
+        free(im_ptr);
+        cfg_destroy(cfg);
+        exit(1);
     }
 
+
+    if ((rc = cfg_export(cfg, NULL, CFG_FMT_CFG, &ex_ptr, 0)) != CFG_OK) {
+        cfg_error(cfg, rc, &error);
+        fprintf(stderr, "ERROR: cfg_export: %s\n", error);
+        free(im_ptr);
+        cfg_destroy(cfg);
+        exit(1);
+    }
+
+    fprintf(stdout, "%s", ex_ptr);
+
+#if 0
+    cfg_syn_destroy(cfg, node);
+    cfg_destroy(cfg);
+#endif
+
     return 0;
 }
 

CVSTrac 2.0.1