OSSP CVS Repository

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

Check-in Number: 2217
Date: 2002-Jul-04 08:16:13 (local)
2002-Jul-04 06:16:13 (UTC)
User:rse
Branch:
Comment: split out cfg_data_t stuff into seperate object to get better abstraction for this
Tickets:
Inspections:
Files:
ossp-pkg/cfg/Makefile      1.1 -> 1.2     1 inserted, 1 deleted
ossp-pkg/cfg/cfg.h      1.1 -> 1.2     1 inserted, 0 deleted
ossp-pkg/cfg/cfg_data.c      added-> 1.1
ossp-pkg/cfg/cfg_data.h      added-> 1.1
ossp-pkg/cfg/cfg_node.c      1.1 -> 1.2     9 inserted, 27 deleted
ossp-pkg/cfg/cfg_node.h      1.1 -> 1.2     1 inserted, 27 deleted

ossp-pkg/cfg/Makefile 1.1 -> 1.2

--- Makefile     2002/07/03 13:25:34     1.1
+++ Makefile     2002/07/04 06:16:13     1.2
@@ -15,7 +15,7 @@
 FLEX   = flex-beta
 
 LIB    = libcfg.a
-OBJ    = cfg_grid.o cfg_node.o cfg_fmt.o cfg_syn.o cfg_syn_parse.o cfg_syn_scan.o cfg_util.o
+OBJ    = cfg_grid.o cfg_data.o cfg_node.o cfg_fmt.o cfg_syn.o cfg_syn_parse.o cfg_syn_scan.o cfg_util.o
 TST    = cfg_test
 
 all: $(LIB) $(TST)


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

--- cfg.h        2002/07/03 13:25:34     1.1
+++ cfg.h        2002/07/04 06:16:13     1.2
@@ -39,6 +39,7 @@
 typedef enum {
     CFG_OK = 0,
     CFG_ERR_ARG,
+    CFG_ERR_USE,
     CFG_ERR_INT,
     CFG_ERR_SYS,
     CFG_ERR_SYN


ossp-pkg/cfg/cfg_data.c -> 1.1

*** /dev/null    Sat Nov 23 01:26:37 2024
--- -    Sat Nov 23 01:26:51 2024
***************
*** 0 ****
--- 1,248 ----
+ /*
+ **  OSSP cfg - Configuration Parsing
+ **  Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
+ **  Copyright (c) 1999-2002 The OSSP Project (http://www.ossp.org/)
+ **  Copyright (c) 2001-2002 Cable & Wireless Deutschland (http://www.cw.com/de/)
+ **
+ **  This file is part of OSSP cfg, a configuration parsing
+ **  library which can be found at http://www.ossp.org/pkg/lib/cfg/.
+ **
+ **  Permission to use, copy, modify, and distribute this software for
+ **  any purpose with or without fee is hereby granted, provided that
+ **  the above copyright notice and this permission notice appear in all
+ **  copies.
+ **
+ **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRCFG, STRICT LIABILITY,
+ **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ **  SUCH DAMAGE.
+ **
+ **  cfg_data.c: configuration annotational data
+ */
+ 
+ #include <stdlib.h>
+ #include <stdarg.h>
+ #include <string.h>
+ 
+ #include "cfg.h"
+ #include "cfg_data.h"
+ 
+ cfg_rc_t cfg_data_create(cfg_data_t **data)
+ {
+     cfg_rc_t rc;
+ 
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     if ((*data = (cfg_data_t *)malloc(sizeof(cfg_data_t))) == NULL)
+         return CFG_ERR_SYS;
+     if ((rc = cfg_data_init(*data)) != CFG_OK) {
+         free(*data);
+         return rc;
+     }
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_init(cfg_data_t *data)
+ {
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     data->type    = CFG_DATA_TYPE_INT;
+     data->value.i = 0;
+     data->ctrl    = cfg_data_ctrl_default;
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_kill(cfg_data_t *data)
+ {
+     cfg_rc_t rc;
+ 
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     if (data->ctrl != NULL)
+         if ((rc = data->ctrl(data, CFG_DATA_CTRL_DESTROY)) != CFG_OK)
+             return rc;
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_destroy(cfg_data_t *data)
+ {
+     cfg_rc_t rc;
+ 
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     if ((rc = cfg_data_kill(data)) != CFG_OK)
+         return rc;
+     free(data);
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_clone(cfg_data_t *data, cfg_data_t **clone)
+ {
+     cfg_rc_t rc;
+ 
+     if (data == NULL || clone == NULL)
+         return CFG_ERR_ARG;
+     if ((*clone = (cfg_data_t *)malloc(sizeof(cfg_data_t))) == NULL)
+         return CFG_ERR_SYS;
+     (*clone)->type = data->type;
+     (*clone)->ctrl = data->ctrl;
+     if ((rc = data->ctrl(data, CFG_DATA_CTRL_CLONE, *clone)) != CFG_OK) {
+         free(*clone);
+         return rc;
+     }
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_ctrl_default(cfg_data_t *data, cfg_data_ctrl_t ctrl, ...)
+ {
+     cfg_data_t *data2;
+     va_list ap;
+ 
+     va_start(ap, ctrl);
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     if (ctrl == CFG_DATA_CTRL_CLONE) {
+         if ((data2 = (cfg_data_t *)va_arg(ap, void *)) == NULL)
+             return CFG_ERR_ARG;
+         if (data->type == CFG_DATA_TYPE_STR) {
+             if (data->value.s != NULL)
+                 data2->value.s = strdup(data->value.s);
+             else
+                 data2->value.s = NULL;
+         }
+         else {
+             memcpy((void *)&(data2->value), 
+                    (void *)&(data->value), sizeof(cfg_data_value_t));
+         }
+     }
+     else if (ctrl == CFG_DATA_CTRL_DESTROY) {
+         if (data->type == CFG_DATA_TYPE_STR)
+             if (data->value.s != NULL)
+                 free(data->value.s);
+     }
+     va_end(ap);
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_set(cfg_data_t *data, cfg_data_id_t id, ...)
+ {
+     va_list ap;
+ 
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     va_start(ap, id);
+     switch (id) {
+         case CFG_DATA_TYPE: {
+             cfg_data_type_t t = (cfg_data_type_t)va_arg(ap, int);
+             if (!(   t == CFG_DATA_TYPE_PTR
+                   || t == CFG_DATA_TYPE_STR
+                   || t == CFG_DATA_TYPE_INT
+                   || t == CFG_DATA_TYPE_FLT))
+                 return CFG_ERR_ARG;
+             data->type = t;
+             break;
+         }
+         case CFG_DATA_VALUE: {
+             switch (data->type) {
+                 case CFG_DATA_TYPE_PTR: {
+                     data->value.p = (void *)va_arg(ap, void *); 
+                     break;
+                 }
+                 case CFG_DATA_TYPE_STR: {
+                     char *s = (char *)va_arg(ap, void *);
+                     if (data->value.s != NULL)
+                         free(data->value.s);
+                     if (s != NULL)
+                         s = strdup(s);
+                     data->value.s = s;
+                     break;
+                 }
+                 case CFG_DATA_TYPE_INT: {
+                     data->value.i = (int)va_arg(ap, int);
+                     break;
+                 }
+                 case CFG_DATA_TYPE_FLT: {
+                     data->value.f = (double)va_arg(ap, double);
+                     break;
+                 }
+             }
+             break;
+         }
+         case CFG_DATA_CTRL: {
+             cfg_data_ctrl_cb_t ctrl = (cfg_data_ctrl_cb_t)va_arg(ap, void (*)(void));
+             data->ctrl = ctrl;
+             break;
+         }
+     }
+     va_end(ap);
+     return CFG_OK;
+ }
+ 
+ cfg_rc_t cfg_data_get(cfg_data_t *data, cfg_data_id_t id, ...)
+ {
+     va_list ap;
+ 
+     if (data == NULL)
+         return CFG_ERR_ARG;
+     va_start(ap, id);
+     switch (id) {
+         case CFG_DATA_TYPE: {
+             cfg_data_type_t *pt = (cfg_data_type_t *)va_arg(ap, int *);
+             if (pt == NULL)
+                 return CFG_ERR_ARG;
+             *pt = data->type;
+             break;
+         }
+         case CFG_DATA_VALUE: {
+             switch (data->type) {
+                 case CFG_DATA_TYPE_PTR: {
+                     void **p = (void **)va_arg(ap, void *);
+                     if (p == NULL)
+                         return CFG_ERR_ARG;
+                     *p = data->value.p;
+                     break;
+                 }
+                 case CFG_DATA_TYPE_STR: {
+                     char **p = (char **)va_arg(ap, void *);
+                     if (p == NULL)
+                         return CFG_ERR_ARG;
+                     *p = data->value.s;
+                     break;
+                 }
+                 case CFG_DATA_TYPE_INT: {
+                     int *p = (int *)va_arg(ap, void *);
+                     if (p == NULL)
+                         return CFG_ERR_ARG;
+                     *p = data->value.i;
+                     break;
+                 }
+                 case CFG_DATA_TYPE_FLT: {
+                     double *p = (double *)va_arg(ap, void *);
+                     if (p == NULL)
+                         return CFG_ERR_ARG;
+                     *p = data->value.f;
+                     break;
+                 }
+             }
+             break;
+         }
+         case CFG_DATA_CTRL: {
+             cfg_data_ctrl_cb_t *pcb = (cfg_data_ctrl_cb_t *)va_arg(ap, void *);
+             if (pcb == NULL)
+                 return CFG_ERR_ARG;
+             *pcb = data->ctrl;
+             break;
+         }
+     }
+     va_end(ap);
+     return CFG_OK;
+ }
+ 


ossp-pkg/cfg/cfg_data.h -> 1.1

*** /dev/null    Sat Nov 23 01:26:37 2024
--- -    Sat Nov 23 01:26:51 2024
***************
*** 0 ****
--- 1,82 ----
+ /*
+ **  OSSP cfg - Configuration Parsing
+ **  Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
+ **  Copyright (c) 1999-2002 The OSSP Project (http://www.ossp.org/)
+ **  Copyright (c) 2001-2002 Cable & Wireless Deutschland (http://www.cw.com/de/)
+ **
+ **  This file is part of OSSP cfg, a configuration parsing
+ **  library which can be found at http://www.ossp.org/pkg/lib/cfg/.
+ **
+ **  Permission to use, copy, modify, and distribute this software for
+ **  any purpose with or without fee is hereby granted, provided that
+ **  the above copyright notice and this permission notice appear in all
+ **  copies.
+ **
+ **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRCFG, STRICT LIABILITY,
+ **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ **  SUCH DAMAGE.
+ **
+ **  cfg_data.h: configuration annotational data
+ */
+ 
+ #ifndef __CFG_DATA_H__
+ #define __CFG_DATA_H__
+ 
+ #include "cfg.h"
+ 
+ typedef enum {
+     CFG_DATA_TYPE_PTR,  /* "void *" */
+     CFG_DATA_TYPE_STR,  /* "char *" */
+     CFG_DATA_TYPE_INT,  /* "int"    */
+     CFG_DATA_TYPE_FLT   /* "double" */
+ } cfg_data_type_t;
+ 
+ typedef union {
+     void  *p;
+     char  *s;
+     int    i;
+     double f;
+ } cfg_data_value_t;
+ 
+ typedef enum {
+     CFG_DATA_CTRL_CLONE,
+     CFG_DATA_CTRL_DESTROY
+ } cfg_data_ctrl_t;
+ 
+ typedef enum {
+     CFG_DATA_TYPE,
+     CFG_DATA_VALUE,
+     CFG_DATA_CTRL
+ } cfg_data_id_t;
+ 
+ struct cfg_data_st;
+ typedef struct cfg_data_st cfg_data_t;
+ 
+ typedef cfg_rc_t (*cfg_data_ctrl_cb_t)(cfg_data_t *, cfg_data_ctrl_t, ...);
+ 
+ struct cfg_data_st {
+     cfg_data_type_t    type;   /* data type    */
+     cfg_data_value_t   value;  /* data value   */
+     cfg_data_ctrl_cb_t ctrl;   /* data control */
+ };
+ 
+ extern cfg_rc_t  cfg_data_create       (cfg_data_t **data);
+ extern cfg_rc_t  cfg_data_init         (cfg_data_t  *data);
+ extern cfg_rc_t  cfg_data_kill         (cfg_data_t  *data);
+ extern cfg_rc_t  cfg_data_destroy      (cfg_data_t  *data);
+ extern cfg_rc_t  cfg_data_clone        (cfg_data_t  *data, cfg_data_t **clone);
+ extern cfg_rc_t  cfg_data_set          (cfg_data_t  *data, cfg_data_id_t id, ...);
+ extern cfg_rc_t  cfg_data_get          (cfg_data_t  *data, cfg_data_id_t id, ...);
+ extern cfg_rc_t  cfg_data_ctrl_default (cfg_data_t  *data, cfg_data_ctrl_t ctrl, ...);
+ 
+ #endif /* __CFG_DATA_H__ */
+ 


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

--- cfg_node.c   2002/07/03 13:25:34     1.1
+++ cfg_node.c   2002/07/04 06:16:13     1.2
@@ -46,8 +46,7 @@
     n->rbroth   = NULL;
     n->child1   = NULL;
     n->token    = NULL;
-    n->data.t   = CFG_DATA_TYPE_LONG;
-    n->data.u.l = 0;
+    cfg_data_init(&n->data);
     *node = n;
     return CFG_OK;
 }
@@ -81,16 +80,7 @@
             break;
         }
         case CFG_NODE_ATTR_DATA: {
-            node->data.t = (cfg_data_type_t)va_arg(ap, cfg_data_type_t);
-            switch (node->data.t) {
-                case CFG_DATA_TYPE_PTR:    node->data.u.p = (void *)va_arg(ap, void *); break;
-                case CFG_DATA_TYPE_CHAR:   node->data.u.c = (char  )va_arg(ap, int   ); break;
-                case CFG_DATA_TYPE_INT:    node->data.u.i = (int   )va_arg(ap, int   ); break;
-                case CFG_DATA_TYPE_LONG:   node->data.u.l = (long  )va_arg(ap, long  ); break;
-                case CFG_DATA_TYPE_FLOAT:  node->data.u.f = (float )va_arg(ap, double); break;
-                case CFG_DATA_TYPE_DOUBLE: node->data.u.d = (double)va_arg(ap, double); break;
-                default: return CFG_ERR_ARG;
-            }
+            return CFG_ERR_USE;
         }
         default:
             return CFG_ERR_ARG;
@@ -108,22 +98,22 @@
     va_start(ap, attr);
     switch (attr) {
         case CFG_NODE_ATTR_TYPE: {
-            cfg_node_type_t *type = (cfg_node_type_t *)va_arg(ap, cfg_node_type_t *);
+            cfg_node_type_t *type = (cfg_node_type_t *)va_arg(ap, void *);
             *type = node->type;
             break;
         }
         case CFG_NODE_ATTR_PARENT: {
-            cfg_node_t **n = (cfg_node_t **)va_arg(ap, cfg_node_t **);
+            cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
             *n = node->parent;
             break;
         }
         case CFG_NODE_ATTR_RBROTH: {
-            cfg_node_t **n = (cfg_node_t **)va_arg(ap, cfg_node_t **);
+            cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
             *n = node->rbroth;
             break;
         }
         case CFG_NODE_ATTR_CHILD1: {
-            cfg_node_t **n = (cfg_node_t **)va_arg(ap, cfg_node_t **);
+            cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
             *n = node->child1;
             break;
         }
@@ -133,17 +123,9 @@
             break;
         }
         case CFG_NODE_ATTR_DATA: {
-            cfg_data_type_t *type = (cfg_data_type_t *)va_arg(ap, cfg_data_type_t *);
-            *type = node->data.t;
-            switch (node->data.t) {
-                case CFG_DATA_TYPE_PTR:    *((void  **)va_arg(ap, void  **)) = node->data.u.p; break;
-                case CFG_DATA_TYPE_CHAR:   *((char   *)va_arg(ap, int    *)) = node->data.u.c; break;
-                case CFG_DATA_TYPE_INT:    *((int    *)va_arg(ap, int    *)) = node->data.u.i; break;
-                case CFG_DATA_TYPE_LONG:   *((long   *)va_arg(ap, long   *)) = node->data.u.l; break;
-                case CFG_DATA_TYPE_DOUBLE: *((double *)va_arg(ap, double *)) = node->data.u.d; break;
-                case CFG_DATA_TYPE_FLOAT:  *((float  *)va_arg(ap, double *)) = node->data.u.f; break;
-                default: return CFG_ERR_ARG;
-            }
+            cfg_data_t **data = (cfg_data_t **)va_arg(ap, void *);
+            *data = &(node->data);
+            break;
         }
         default:
             return CFG_ERR_ARG;


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

--- cfg_node.h   2002/07/03 13:25:34     1.1
+++ cfg_node.h   2002/07/04 06:16:13     1.2
@@ -32,33 +32,7 @@
 #define __CFG_NODE_H__
 
 #include "cfg.h"
-
-typedef enum {
-    CFG_DATA_TYPE_PTR,
-    CFG_DATA_TYPE_CHAR,
-    CFG_DATA_TYPE_INT,
-    CFG_DATA_TYPE_LONG,
-    CFG_DATA_TYPE_FLOAT,
-    CFG_DATA_TYPE_DOUBLE
-} cfg_data_type_t;
-
-typedef union {
-    void  *p;
-    char   c;
-    int    i;
-    long   l;
-    float  f;
-    double d;
-} cfg_data_union_t;
-
-typedef struct {
-    cfg_data_type_t t;       /* data type       */
-    cfg_data_union_t u;      /* data storage    */
-    void (*d)(               /* data destructor */
-        cfg_data_type_t t,
-        cfg_data_union_t *u
-    );
-} cfg_data_t;
+#include "cfg_data.h"
 
 struct cfg_node_st {
     cfg_node_type_t type;    /* type of node */

CVSTrac 2.0.1