[Freeswitch-branches] [commit] r3323 - in freeswitch/branches/knhor/trunk/src: . include
Freeswitch SVN
knhor at freeswitch.org
Sat Nov 11 21:58:45 EST 2006
Author: knhor
Date: Sat Nov 11 21:58:45 2006
New Revision: 3323
Modified:
freeswitch/branches/knhor/trunk/src/include/switch_ivr.h
freeswitch/branches/knhor/trunk/src/switch_ivr.c
Log:
flesh out the new functions
Modified: freeswitch/branches/knhor/trunk/src/include/switch_ivr.h
==============================================================================
--- freeswitch/branches/knhor/trunk/src/include/switch_ivr.h (original)
+++ freeswitch/branches/knhor/trunk/src/include/switch_ivr.h Sat Nov 11 21:58:45 2006
@@ -403,17 +403,24 @@
SWITCH_DECLARE(switch_status_t) switch_ivr_transfer_variable(switch_core_session_t *sessa, switch_core_session_t *sessb, char *var);
-struct switch_ivr_digit_stream_parser {
-};
+struct switch_ivr_digit_stream_parser;
typedef struct switch_ivr_digit_stream_parser switch_ivr_digit_stream_parser_t;
/*!
\brief Create a digit stream parser object
+ \param pool the pool to use for the new hash
\param parser a pointer to the object pointer
\return SWITCH_STATUS_SUCCESS if all is well
*/
-SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_ivr_digit_stream_parser_t **parser);
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_memory_pool_t *pool, switch_ivr_digit_stream_parser_t **parser);
/*!
+ \brief Destroy a digit stream parser object
+ \param parser a pointer to the object pointer
+ \return SWITCH_STATUS_SUCCESS if all is well
+*/
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t **parser);
+
+/*!
\brief Set a digit string to action mapping
\param parser a pointer to the parser object created by switch_ivr_digit_stream_parser_new
\param digits a string of digits to associate with an action
@@ -437,6 +444,20 @@
\return 0 if no match found or non-zero action that was associated with a given digit string when matched
*/
SWITCH_DECLARE(int) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, char digit);
+
+/*!
+ \brief Reset the collected digit stream to nothing
+ \param parser a pointer to the parser object created by switch_ivr_digit_stream_parser_new
+ \return SWITCH_STATUS_SUCCESS if all is well
+*/
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_reset(switch_ivr_digit_stream_parser_t *parser);
+
+/*!
+ \brief Set a digit string terminator
+ \param parser a pointer to the parser object created by switch_ivr_digit_stream_parser_new
+ \return SWITCH_STATUS_SUCCESS if all is well
+*/
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_terminator(switch_ivr_digit_stream_parser_t *parser, char digit);
/** @} */
Modified: freeswitch/branches/knhor/trunk/src/switch_ivr.c
==============================================================================
--- freeswitch/branches/knhor/trunk/src/switch_ivr.c (original)
+++ freeswitch/branches/knhor/trunk/src/switch_ivr.c Sat Nov 11 21:58:45 2006
@@ -3237,28 +3237,145 @@
}
struct switch_ivr_digit_stream_parser {
+ int pool_auto_created;
+ switch_memory_pool_t *pool;
+ switch_hash_t *hash;
+ char *digits;
+ char terminator;
};
-SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_ivr_digit_stream_parser_t **parser)
-{ switch_status_t status = SWITCH_STATUS_SUCCESS;
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_memory_pool_t *pool, switch_ivr_digit_stream_parser_t **parser)
+{ switch_status_t status = SWITCH_STATUS_FALSE;
+ if(parser != NULL) {
+ int pool_auto_created = 0;
+
+ // if the caller didn't provide a pool, make one
+ if (pool == NULL) {
+ switch_core_new_memory_pool(&pool);
+ if (pool != NULL) {
+ pool_auto_created = 1;
+ }
+ }
+
+ // if we have a pool, make a parser object
+ if (pool != NULL) {
+ *parser = (switch_ivr_digit_stream_parser_t *)switch_core_alloc(pool,sizeof(switch_ivr_digit_stream_parser_t));
+ }
+
+ // if we have parser object, initialize it for the caller
+ if (*parser != NULL) {
+ memset(*parser,0,sizeof(switch_ivr_digit_stream_parser_t));
+ (*parser)->pool_auto_created = pool_auto_created;
+ (*parser)->pool = pool;
+ switch_core_hash_init(&(*parser)->hash,(*parser)->pool);
+
+ status = SWITCH_STATUS_SUCCESS;
+ } else {
+ status = SWITCH_STATUS_MEMERR;
+ // clean up the pool if we created it
+ if (pool != NULL && pool_auto_created) {
+ switch_core_destroy_memory_pool(&pool);
+ }
+ }
+ }
+
return status;
}
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t **parser)
+{ switch_status_t status = SWITCH_STATUS_FALSE;
+
+ if (parser != NULL && *parser != NULL) {
+ if ((*parser)->hash != NULL) {
+ switch_core_hash_destroy((*parser)->hash);
+ (*parser)->hash = NULL;
+ }
+ // free the memory pool if we created it
+ if ((*parser)->pool_auto_created && (*parser)->pool != NULL) {
+ status = switch_core_destroy_memory_pool(&(*parser)->pool);
+ }
+ // clean up for the caller
+ *parser = NULL;
+ }
+
+ return status;
+}
+
SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_event(switch_ivr_digit_stream_parser_t *parser, char *digits, int action)
-{ switch_status_t status = SWITCH_STATUS_SUCCESS;
+{ switch_status_t status = SWITCH_STATUS_FALSE;
+ if (parser != NULL && digits != NULL && *digits && parser->hash != NULL) {
+ status = switch_core_hash_insert_dup(parser->hash,digits,(void *)action);
+ }
+ if (status != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "unable to add hash for '%s' action: %d\n",digits,action);
+ }
+
return status;
}
SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_del_event(switch_ivr_digit_stream_parser_t *parser, char *digits)
-{ switch_status_t status = SWITCH_STATUS_SUCCESS;
+{ switch_status_t status = SWITCH_STATUS_FALSE;
+ if (parser != NULL && digits != NULL && *digits) {
+ status = switch_core_hash_delete(parser->hash,digits);
+ }
+
+ if (status != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "unable to del hash for '%s'\n",digits);
+ }
+
return status;
}
SWITCH_DECLARE(int) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, char digit)
{ int result = 0;
+ if (parser != NULL && digit != '\0') {
+ size_t len = (parser->digits != NULL ? strlen(parser->digits) : 0);
+
+ // if it's not a terminator digit, add it to the collected digits
+ if (parser->terminator != digit) {
+ parser->digits = realloc(parser->digits,len+2);
+ *(parser->digits+(len++)) = digit;
+ *(parser->digits+len) = '\0';
+ }
+
+ // if we have digits to test
+ if (len) {
+ result = (int)switch_core_hash_find(parser->hash,parser->digits);
+ // if we matched the digit string, or this digit is the terminator
+ // reset the collected digits for next digit string
+ if (result != 0 || parser->terminator == digit) {
+ free(parser->digits);
+ parser->digits = NULL;
+ }
+ }
+ }
+
return result;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_reset(switch_ivr_digit_stream_parser_t *parser)
+{ switch_status_t status = SWITCH_STATUS_FALSE;
+
+ if (parser != NULL && parser->digits != NULL) {
+ free(parser->digits);
+ parser->digits = NULL;
+ status = SWITCH_STATUS_SUCCESS;
+ }
+
+ return status;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_terminator(switch_ivr_digit_stream_parser_t *parser, char digit)
+{ switch_status_t status = SWITCH_STATUS_FALSE;
+
+ if (parser != NULL) {
+ parser->terminator = digit;
+ status = SWITCH_STATUS_SUCCESS;
+ }
+
+ return status;
}
More information about the Freeswitch-branches
mailing list