]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
update
authorMathieu Rene <mrene@avgs.ca>
Thu, 5 Mar 2009 04:22:09 +0000 (04:22 +0000)
committerMathieu Rene <mrene@avgs.ca>
Thu, 5 Mar 2009 04:22:09 +0000 (04:22 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12440 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_xml_config.h
src/switch_xml_config.c

index 317b4294152a37848e17a30277113587940ac90c..016aa42b534a8c0dd6cc64b48f813caab1aa6810 100644 (file)
@@ -17,7 +17,7 @@
  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  *
  * The Initial Developer of the Original Code is
- * Anthony Minessale II <anthm@freeswitch.org>
+ * Mathieu Rene <mathieu.rene@gmail.com>
  * Portions created by the Initial Developer are Copyright (C)
  * the Initial Developer. All Rights Reserved.
  *
 
 /*! \brief Type of value to parse */
 typedef enum {
-       SWITCH_CONFIG_INT,              /*< (ptr=int* default=int data=NULL) Integer */
-       SWITCH_CONFIG_STRING,   /*< (ptr=[char* or char ** (for alloc)] default=char* data=switch_xml_config_string_options_t*) Zero-terminated C-string */
-       SWITCH_CONFIG_YESNO,    /*< (ptr=switch_bool_t* default=switch_bool_t data=NULL) Yes and no */
-       SWITCH_CONFIG_CUSTOM,   /*< (ptr=<custom function data> default=<custom function data> data=switch_xml_config_callback_t) Custom, get value through function pointer  */
-       SWITCH_CONFIG_ENUM,     /*< (ptr=int* default=int data=switch_xml_config_enum_item_t*) */
-       SWITCH_CONFIG_FLAG,             /*< (ptr=int32_t* default=switch_bool_t data=int (flag index) */
-       SWITCH_CONFIG_FLAGARRAY,/*< (ptr=int8_t* default=switch_bool_t data=int (flag index) */
+       SWITCH_CONFIG_INT,                      /*< (ptr=int* default=int data=NULL) Integer */
+       SWITCH_CONFIG_STRING,           /*< (ptr=[char* or char ** (for alloc)] default=char* data=switch_xml_config_string_options_t*) Zero-terminated C-string */
+       SWITCH_CONFIG_YESNO,            /*< (ptr=switch_bool_t* default=switch_bool_t data=NULL) Yes and no */
+       SWITCH_CONFIG_CUSTOM,           /*< (ptr=<custom function data> default=<custom function data> data=switch_xml_config_callback_t) Custom, get value through function pointer  */
+       SWITCH_CONFIG_ENUM,             /*< (ptr=int* default=int data=switch_xml_config_enum_item_t*) */
+       SWITCH_CONFIG_FLAG,                     /*< (ptr=int32_t* default=switch_bool_t data=int (flag index) */
+       SWITCH_CONFIG_FLAGARRAY,        /*< (ptr=int8_t* default=switch_bool_t data=int (flag index) */
        
        /* No more past that line */
        SWITCH_CONFIG_LAST
 } switch_xml_config_type_t;
 
 typedef struct {
-       char *key;                              /*< The item's key or NULL if this is the last one in the list */
-       int value;                              /*< The item's value */
+       char *key;                                      /*< The item's key or NULL if this is the last one in the list */
+       switch_size_t value;            /*< The item's value */
 } switch_xml_config_enum_item_t;
 
 typedef struct {
@@ -57,21 +57,28 @@ typedef struct {
        int length;                                     /*< Length of the char array, or 0 if memory has to be allocated dynamically*/
 } switch_xml_config_string_options_t;
 
+struct switch_xml_config_item;
+typedef struct switch_xml_config_item switch_xml_config_item_t;
+
+typedef switch_status_t (*switch_xml_config_callback_t)(switch_xml_config_item_t *data);
+
 /*!
  * \brief A configuration instruction read by switch_xml_config_parse 
 */
-typedef struct {
-       char *key;                                      /*< The key of the element, or NULL to indicate the end of the list */
+struct switch_xml_config_item {
+       char *key;                                              /*< The key of the element, or NULL to indicate the end of the list */
        switch_xml_config_type_t type;  /*< The type of variable */
-       switch_bool_t reloadable;       /*< True if the var can be changed on reload */
-       void *ptr;                                      /*< Ptr to the var to be changed */
-       void *defaultvalue;             /*< Default value */
-       void *data;                             /*< Custom data (depending on the type) */
-} switch_xml_config_item_t;
+       switch_bool_t reloadable;               /*< True if the var can be changed on reload */
+       void *ptr;                                              /*< Ptr to the var to be changed */
+       void *defaultvalue;                     /*< Default value */
+       void *data;                                     /*< Custom data (depending on the type) */
+       switch_xml_config_callback_t function;  /*< Callback (for type CUSTOM) */
+} ;
 
-typedef switch_status_t (*switch_xml_config_callback_t)(switch_xml_config_item_t *data);
 
-#define SWITCH_CONFIG_END { NULL, SWITCH_CONFIG_LAST, 0, NULL ,NULL, NULL }
+#define SWITCH_CONFIG_ITEM (_key, _type, _reloadable, _ptr, _defaultvalue, _data)      { _key, _type, _reloadable, _ptr, _defaultvalue, _data, NULL }
+#define SWITCH_CONFIG_ITEM_CALLBACK (_key, _type, _reloadable, _ptr, _defaultvalue, _data)     { _key, _type, _reloadable, _ptr, _defaultvalue, NULL, _data }
+#define SWITCH_CONFIG_ITEM_END () { NULL, SWITCH_CONFIG_LAST, 0, NULL ,NULL, NULL }
 
 /*! 
  * \brief Parses all the xml elements, following a ruleset defined by an array of switch_xml_config_item_t 
index 3996f9e365f46ea0a11575c98cbd26730b5e7814..1e015162be8f4768a69983d435e50bb0c135652f 100644 (file)
  *
  */
 
+//#define SWITCH_XML_CONFIG_TEST
+
 #include <switch.h>
 
-switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml_config_item_t *options)
+SWITCH_DECLARE(switch_status_t) switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml_config_item_t *options)
 {
        switch_xml_config_item_t *item;
        switch_xml_t node;
        switch_event_t *event;
+       int file_count = 0, matched_count = 0;
+       
        switch_event_create(&event, SWITCH_EVENT_REQUEST_PARAMS);
        switch_assert(event);
-       int file_count = 0, matched_count = 0;
+
 
        for (node = xml; node; node = node->next) {
                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, switch_xml_attr_soft(node, "name"), switch_xml_attr_soft(node, "value"));
@@ -63,27 +67,39 @@ switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml
                                                } else {
                                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s]\n", 
                                                                value, item->key);
-                                                       *dest = (int)item->defaultvalue;
+                                                       *dest = (int)(intptr_t)item->defaultvalue;
                                                }
                                        } else {
-                                               *dest = (int)item->defaultvalue;
+                                               *dest = (int)(intptr_t)item->defaultvalue;
                                        }
                                }
                                break;
                        case SWITCH_CONFIG_STRING:
                                {
-                                       switch_xml_config_string_options_t *options = (switch_xml_config_string_options_t*)item->data;
-                                       if (options->length > 0) {
+                                       switch_xml_config_string_options_t *string_options = (switch_xml_config_string_options_t*)item->data;
+                                       if (string_options->length > 0) {
                                                /* We have a preallocated buffer */
                                                char *dest = (char*)item->ptr;
-                                               strncpy(dest, value, options->length);
+                                               if (value) {
+                                                       switch_copy_string(dest, value, string_options->length);
+                                               } else if (options->defaultvalue){
+                                                       switch_copy_string(dest, value, string_options->length);
+                                               }
                                        } else {
                                                char **dest = (char**)item->ptr;
-                                               if (options->pool) {
-                                                       *dest = switch_core_strdup(options->pool, value);
+                                               if (string_options->pool) {
+                                                       if (value) {
+                                                               *dest = switch_core_strdup(string_options->pool, value);
+                                                       } else if (item->defaultvalue) {
+                                                               *dest = switch_core_strdup(string_options->pool, (char*)item->defaultvalue);
+                                                       }
                                                } else {
                                                        switch_safe_free(*dest); /* Free the destination if its not NULL */
-                                                       *dest = strdup(value);
+                                                       if (value) {
+                                                               *dest = strdup(value);
+                                                       } else if(item->defaultvalue) {
+                                                               *dest = strdup((char*)item->defaultvalue);
+                                                       }
                                                }
                                        }
                                }
@@ -94,18 +110,15 @@ switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml
                                        if (value) {
                                                *dest = !!switch_true(value);
                                        } else {
-                                               *dest = (switch_bool_t)item->defaultvalue;
+                                               *dest = (switch_bool_t)(intptr_t)item->defaultvalue;
                                        }
                                }
                                break;
                        case SWITCH_CONFIG_CUSTOM: 
-#if 0
-                               {
-                                       
-                                       switch_xml_config_callback_t callback = (switch_xml_config_callback_t)item->data;
+                               {       
+                                       switch_xml_config_callback_t callback = (switch_xml_config_callback_t)item->function;
                                        callback(item);
                                }
-#endif
                                break;
                        case SWITCH_CONFIG_ENUM:
                                {
@@ -125,7 +138,7 @@ switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml
                                                                value, item->key);
                                                }
                                        } else {
-                                               *dest = (int)item->defaultvalue;
+                                               *dest = (int)(intptr_t)item->defaultvalue;
                                        }
                                }
                                break;
@@ -140,7 +153,7 @@ switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml
                                                        *dest &= ~(1 << index);
                                                }
                                        } else {
-                                               if ((switch_bool_t)item->defaultvalue) {
+                                               if ((switch_bool_t)(intptr_t)item->defaultvalue) {
                                                        *dest |= (1 << index);
                                                } else {
                                                        *dest &= ~(1 << index);
@@ -155,7 +168,7 @@ switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml
                                        if (value) {
                                                dest[index] = !!switch_true(value);                                             
                                        } else {
-                                               dest[index] = (int8_t)((int32_t)item->defaultvalue);
+                                               dest[index] = (int8_t)((intptr_t)item->defaultvalue);
                                        }
                                }
                                break;
@@ -174,7 +187,7 @@ switch_status_t switch_xml_config_parse(switch_xml_t xml, int reload, switch_xml
 }
 
 
-#if 0
+#if SWITCH_XML_CONFIG_TEST
 typedef enum {
        MYENUM_TEST1 = 1,
        MYENUM_TEST2 = 2,
@@ -190,7 +203,7 @@ static struct {
 } globals;
 
 
-void switch_xml_config_test()
+SWITCH_DECLARE(void) switch_xml_config_test()
 {
        char *cf = "test.conf";
        switch_xml_t cfg, xml, settings;
@@ -204,10 +217,10 @@ void switch_xml_config_test()
        };
        
        switch_xml_config_item_t instructions[] = {
-                       { "db_host", SWITCH_CONFIG_STRING, SWITCH_TRUE, &globals.stringalloc, "blah", &config_opt_stringalloc },
-                       { "db_user", SWITCH_CONFIG_STRING, SWITCH_TRUE, globals.string,           "dflt", &config_opt_buffer },
-                       { "test",        SWITCH_CONFIG_ENUM, SWITCH_FALSE, &globals.enumm,  (void*)MYENUM_TEST1, enumm_options },
-                       SWITCH_CONFIG_END
+                       SWITCH_CONFIG_ITEM("db_host", SWITCH_CONFIG_STRING, SWITCH_TRUE, &globals.stringalloc, "blah", &config_opt_stringalloc),
+                       SWITCH_CONFIG_ITEM("db_user", SWITCH_CONFIG_STRING, SWITCH_TRUE, globals.string, "dflt", &config_opt_buffer ),
+                       SWITCH_CONFIG_ITEM("test", SWITCH_CONFIG_ENUM, SWITCH_FALSE, &globals.enumm,  (void*)MYENUM_TEST1, enumm_options ),
+                       SWITCH_CONFIG_ITEM_END()
        };
 
        if (!(xml = switch_xml_open_cfg("blaster.conf", &cfg, NULL))) {