[Freeswitch-svn] [commit] r3601 - in freeswitch/trunk/src: . include

Freeswitch SVN mikej at freeswitch.org
Sun Dec 10 17:22:28 EST 2006


Author: mikej
Date: Sun Dec 10 17:22:28 2006
New Revision: 3601

Modified:
   freeswitch/trunk/src/include/switch_ivr.h
   freeswitch/trunk/src/switch_ivr.c

Log:
separate the stream context from the parser context

merged from knhor branch, thanks Neal.

Modified: freeswitch/trunk/src/include/switch_ivr.h
==============================================================================
--- freeswitch/trunk/src/include/switch_ivr.h	(original)
+++ freeswitch/trunk/src/include/switch_ivr.h	Sun Dec 10 17:22:28 2006
@@ -406,8 +406,12 @@
 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;
 typedef struct switch_ivr_digit_stream_parser switch_ivr_digit_stream_parser_t;
+struct switch_ivr_digit_stream;
+typedef struct switch_ivr_digit_stream switch_ivr_digit_stream_t;
 /*!
   \brief Create a digit stream parser object
   \param pool the pool to use for the new hash
@@ -418,12 +422,27 @@
 
 /*!
   \brief Destroy a digit stream parser object
-  \param parser a pointer to the object pointer
+  \param parser a pointer to the parser object
   \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);
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t *parser);
 
 /*!
+  \brief Create a new digit stream object
+  \param parser a pointer to the parser object created by switch_ivr_digit_stream_parser_new
+  \param stream a pointer to the stream object pointer
+  \return NULL if no match found or consumer data that was associated with a given digit string when matched
+*/
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_new(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t **stream);
+
+/*!
+  \brief Destroys a digit stream object
+  \param stream a pointer to the stream object
+  \return NULL if no match found or consumer data that was associated with a given digit string when matched
+*/
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_destroy(switch_ivr_digit_stream_t *stream);
+
+/*!
   \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
@@ -446,14 +465,14 @@
   \param digit a digit to collect and test against the map of digit strings
   \return NULL if no match found or consumer data that was associated with a given digit string when matched
 */
-SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, char digit);
+SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t *stream, 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
+  \param stream a pointer to the parser stream object created by switch_ivr_digit_stream_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);
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_reset(switch_ivr_digit_stream_t *stream);
 
 /*!
   \brief Set a digit string terminator
@@ -462,6 +481,10 @@
   \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/trunk/src/switch_ivr.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr.c	(original)
+++ freeswitch/trunk/src/switch_ivr.c	Sun Dec 10 17:22:28 2006
@@ -3495,11 +3495,14 @@
 	int pool_auto_created;
 	switch_memory_pool_t *pool;
 	switch_hash_t *hash;
-	char *digits;
-	char terminator;
 	switch_size_t maxlen;
 	switch_size_t minlen;
+	char terminator;
 	unsigned int digit_timeout_ms;
+};
+
+struct switch_ivr_digit_stream {
+	char *digits;
 	switch_time_t last_digit_time;
 };
 
@@ -3544,25 +3547,50 @@
 	return status;
 }
 
-SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t **parser)
+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;
+	if (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);
+		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_new(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t **stream)
+{	switch_status_t status = SWITCH_STATUS_FALSE;
+
+	// if we have a paser object memory pool and a stream object pointer that is null
+	if (parser != NULL && parser->pool && stream != NULL && *stream == NULL) {
+		*stream = (switch_ivr_digit_stream_t *)switch_core_alloc(parser->pool,sizeof(switch_ivr_digit_stream_t));
+		if (*stream != NULL) {
+			memset(*stream,0,sizeof(switch_ivr_digit_stream_t));
+			status = SWITCH_STATUS_SUCCESS;
+		}
+	}
+
+	return status;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_destroy(switch_ivr_digit_stream_t *stream)
+{	switch_status_t status = SWITCH_STATUS_FALSE;
+
+	if (stream == NULL && stream->digits != NULL) {
+		free(stream->digits);
+		stream->digits = NULL;
+		status = SWITCH_STATUS_SUCCESS;
+	}
+
+	return status;
+}
+
 SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_event(switch_ivr_digit_stream_parser_t *parser, char *digits, void *data)
 {	switch_status_t status = SWITCH_STATUS_FALSE;
 
@@ -3613,11 +3641,11 @@
 	return status;
 }
 
-SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, char digit)
+SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t *stream, char digit)
 {	void *result = NULL;
 
-	if (parser != NULL) {
-		switch_size_t len = (parser->digits != NULL ? strlen(parser->digits) : 0);
+	if (parser != NULL && stream != NULL) {
+		switch_size_t len = (stream->digits != NULL ? strlen(stream->digits) : 0);
 
 		// handle new digit arrivals
 		if(digit != '\0') {
@@ -3627,37 +3655,37 @@
 				// if collected digits length >= the max length of the keys
 				// in the hash table, then left shift the digit string
 				if (len > 0 && parser->maxlen != 0 && len >= parser->maxlen) {
-					char *src = parser->digits + 1;
-					char *dst = parser->digits;
+					char *src = stream->digits + 1;
+					char *dst = stream->digits;
 
 					while (*src) {
 						*(dst++) = *(src++);
 					}
 					*dst = digit;
 				} else {
-					parser->digits = realloc(parser->digits,len+2);
-					*(parser->digits+(len++)) = digit;
-					*(parser->digits+len) = '\0';
-					parser->last_digit_time = switch_time_now() / 1000;
+					stream->digits = realloc(stream->digits,len+2);
+					*(stream->digits+(len++)) = digit;
+					*(stream->digits+len) = '\0';
+					stream->last_digit_time = switch_time_now() / 1000;
 				}
 			}
 		}
 
 		// don't allow collected digit string testing if there are varying sized keys until timeout
 		if ( parser->maxlen - parser->minlen > 0
-			&& (switch_time_now() / 1000) - parser->last_digit_time < parser->digit_timeout_ms
+			&& (switch_time_now() / 1000) - stream->last_digit_time < parser->digit_timeout_ms
 		) {
 			len = 0;
 		}
 
 		// if we have digits to test
 		if (len) {
-			result = switch_core_hash_find(parser->hash,parser->digits);
+			result = switch_core_hash_find(parser->hash, stream->digits);
 			// if we matched the digit string, or this digit is the terminator
 			// reset the collected digits for next digit string
 			if (result != NULL || parser->terminator == digit) {
-				free(parser->digits);
-				parser->digits = NULL;
+				free(stream->digits);
+				stream->digits = NULL;
 			}
 		}
 }
@@ -3665,12 +3693,13 @@
 	return result;
 }
 
-SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_reset(switch_ivr_digit_stream_parser_t *parser)
+SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_reset(switch_ivr_digit_stream_t *stream)
 {	switch_status_t status = SWITCH_STATUS_FALSE;
 
-	if (parser != NULL && parser->digits != NULL) {
-		free(parser->digits);
-		parser->digits = NULL;
+	if (stream != NULL && stream->digits != NULL) {
+		free(stream->digits);
+		stream->digits = NULL;
+		stream->last_digit_time = 0;
 		status = SWITCH_STATUS_SUCCESS;
 	}
 



More information about the Freeswitch-svn mailing list