git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12447 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Mathieu Rene 2009-03-05 05:32:50 +00:00
parent 578481e655
commit 6ed3089a48
2 changed files with 71 additions and 47 deletions

View File

@ -60,7 +60,7 @@ typedef struct {
struct switch_xml_config_item; struct switch_xml_config_item;
typedef struct switch_xml_config_item switch_xml_config_item_t; 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); typedef switch_status_t (*switch_xml_config_callback_t)(switch_xml_config_item_t *data, switch_bool_t changed);
/*! /*!
* \brief A configuration instruction read by switch_xml_config_parse * \brief A configuration instruction read by switch_xml_config_parse
@ -72,7 +72,8 @@ struct switch_xml_config_item {
void *ptr; /*< Ptr to the var to be changed */ void *ptr; /*< Ptr to the var to be changed */
void *defaultvalue; /*< Default value */ void *defaultvalue; /*< Default value */
void *data; /*< Custom data (depending on the type) */ void *data; /*< Custom data (depending on the type) */
switch_xml_config_callback_t function; /*< Callback (for type CUSTOM) */ switch_xml_config_callback_t function; /*< Callback to be called after the var is parsed */
void *functiondata; /*< Custom data passed to the callback */
} ; } ;

View File

@ -52,6 +52,8 @@ SWITCH_DECLARE(switch_status_t) switch_xml_config_parse(switch_xml_t xml, int re
for (item = instructions; item->key; item++) { for (item = instructions; item->key; item++) {
const char *value = switch_event_get_header(event, item->key); const char *value = switch_event_get_header(event, item->key);
switch_bool_t changed = SWITCH_FALSE;
switch_xml_config_callback_t callback = (switch_xml_config_callback_t)item->function;
if (reload && !item->reloadable) { if (reload && !item->reloadable) {
continue; continue;
@ -61,45 +63,53 @@ SWITCH_DECLARE(switch_status_t) switch_xml_config_parse(switch_xml_t xml, int re
case SWITCH_CONFIG_INT: case SWITCH_CONFIG_INT:
{ {
int *dest = (int*)item->ptr; int *dest = (int*)item->ptr;
int intval;
if (value) { if (value) {
if (switch_is_number(value)) { if (switch_is_number(value)) {
*dest = atoi(value); intval = atoi(value);
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s]\n", intval = (int)(intptr_t)item->defaultvalue;
value, item->key); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s] setting default [%d]\n",
*dest = (int)(intptr_t)item->defaultvalue; value, item->key, intval);
} }
} else { } else {
*dest = (int)(intptr_t)item->defaultvalue; intval = (int)(intptr_t)item->defaultvalue;
}
if (*dest != intval) {
*dest = intval;
changed = SWITCH_TRUE;
} }
} }
break; break;
case SWITCH_CONFIG_STRING: case SWITCH_CONFIG_STRING:
{ {
switch_xml_config_string_options_t *string_options = (switch_xml_config_string_options_t*)item->data; switch_xml_config_string_options_t *string_options = (switch_xml_config_string_options_t*)item->data;
const char *newstring = NULL;
if (string_options->length > 0) { if (string_options->length > 0) {
/* We have a preallocated buffer */ /* We have a preallocated buffer */
char *dest = (char*)item->ptr; char *dest = (char*)item->ptr;
if (value) { if (value) {
switch_copy_string(dest, value, string_options->length); newstring = value;
} else if (item->defaultvalue){ } else if (item->defaultvalue) {
switch_copy_string(dest, item->defaultvalue, string_options->length); newstring = item->defaultvalue;
}
if (newstring && strncasecmp(dest, newstring, string_options->length)) {
switch_copy_string(dest, newstring, string_options->length);
} }
} else { } else {
char **dest = (char**)item->ptr; char **dest = (char**)item->ptr;
if (string_options->pool) { const char *newstring = value ? value : (char*)item->defaultvalue;
if (value) {
*dest = switch_core_strdup(string_options->pool, value); if (newstring && strcasecmp(*dest, newstring)) {
} else if (item->defaultvalue) { if (string_options->pool) {
*dest = switch_core_strdup(string_options->pool, (char*)item->defaultvalue); *dest = switch_core_strdup(string_options->pool, newstring);
} } else {
} else { switch_safe_free(*dest);
switch_safe_free(*dest); /* Free the destination if its not NULL */ *dest = strdup(newstring);
if (value) {
*dest = strdup(value);
} else if(item->defaultvalue) {
*dest = strdup((char*)item->defaultvalue);
} }
changed = SWITCH_TRUE;
} }
} }
} }
@ -107,38 +117,41 @@ SWITCH_DECLARE(switch_status_t) switch_xml_config_parse(switch_xml_t xml, int re
case SWITCH_CONFIG_YESNO: case SWITCH_CONFIG_YESNO:
{ {
switch_bool_t *dest = (switch_bool_t*)item->ptr; switch_bool_t *dest = (switch_bool_t*)item->ptr;
if (value) { switch_bool_t newval = value ? !!switch_true(value) : (switch_bool_t)(intptr_t)item->defaultvalue;
*dest = !!switch_true(value);
} else { if (*dest != newval) {
*dest = (switch_bool_t)(intptr_t)item->defaultvalue; *dest = newval;
changed = SWITCH_TRUE;
} }
} }
break; break;
case SWITCH_CONFIG_CUSTOM: case SWITCH_CONFIG_CUSTOM:
{
switch_xml_config_callback_t callback = (switch_xml_config_callback_t)item->function;
callback(item);
}
break; break;
case SWITCH_CONFIG_ENUM: case SWITCH_CONFIG_ENUM:
{ {
switch_xml_config_enum_item_t *enum_options = (switch_xml_config_enum_item_t*)item->data; switch_xml_config_enum_item_t *enum_options = (switch_xml_config_enum_item_t*)item->data;
int *dest = (int*)item->ptr; int *dest = (int*)item->ptr;
int newval;
if (value) { if (value) {
for (;enum_options->key; enum_options++) { for (;enum_options->key; enum_options++) {
if (!strcasecmp(value, enum_options->key)) { if (!strcasecmp(value, enum_options->key)) {
*dest = enum_options->value; newval = enum_options->value;
break; break;
} }
} }
if (!enum_options->key) { /* if (!found) */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s]\n",
value, item->key);
}
} else { } else {
*dest = (int)(intptr_t)item->defaultvalue; newval = (int)(intptr_t)item->defaultvalue;
}
if (!enum_options->key) { /* if (!found) */
newval = (int)(intptr_t)item->defaultvalue;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s]\n", value, item->key);
}
if (*dest != newval) {
changed = SWITCH_TRUE;
*dest = newval;
} }
} }
break; break;
@ -146,14 +159,18 @@ SWITCH_DECLARE(switch_status_t) switch_xml_config_parse(switch_xml_t xml, int re
{ {
int32_t *dest = (int32_t*)item->ptr; int32_t *dest = (int32_t*)item->ptr;
int index = (int)(intptr_t)item->data; int index = (int)(intptr_t)item->data;
int8_t currentval = !!(*dest & index);
int8_t newval = 0;
if (value) { if (value) {
if (switch_true(value)) { newval = switch_true(value);
*dest |= (1 << index);
} else {
*dest &= ~(1 << index);
}
} else { } else {
if ((switch_bool_t)(intptr_t)item->defaultvalue) { newval = (switch_bool_t)(intptr_t)item->defaultvalue;
}
if (newval != currentval) {
changed = SWITCH_TRUE;
if (newval) {
*dest |= (1 << index); *dest |= (1 << index);
} else { } else {
*dest &= ~(1 << index); *dest &= ~(1 << index);
@ -165,15 +182,21 @@ SWITCH_DECLARE(switch_status_t) switch_xml_config_parse(switch_xml_t xml, int re
{ {
int8_t *dest = (int8_t*)item->ptr; int8_t *dest = (int8_t*)item->ptr;
int index = (int)(intptr_t)item->data; int index = (int)(intptr_t)item->data;
if (value) { int newval = value ? !!switch_true(value) : (int8_t)((intptr_t)item->defaultvalue);
dest[index] = !!switch_true(value); if (dest[index] != newval) {
} else { changed = SWITCH_TRUE;
dest[index] = (int8_t)((intptr_t)item->defaultvalue); dest[index] = newval;
} }
} }
break; break;
case SWITCH_CONFIG_LAST: case SWITCH_CONFIG_LAST:
break; break;
default:
break;
}
if (callback) {
callback(item, changed);
} }
} }