Author: knhor Date: Sun Nov 12 09:50:39 2006 New Revision: 3329 Modified: freeswitch/branches/knhor/trunk/src/switch_ivr.c Log: automatically keep the digit collection to a maximum length determined by the largest digit string added to the hash 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 Sun Nov 12 09:50:39 2006 @@ -3242,6 +3242,7 @@ switch_hash_t *hash; char *digits; char terminator; + int maxlen; }; SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_memory_pool_t *pool, switch_ivr_digit_stream_parser_t **parser) @@ -3306,7 +3307,12 @@ { switch_status_t status = SWITCH_STATUS_FALSE; if (parser != NULL && digits != NULL && *digits && parser->hash != NULL) { + int len; + status = switch_core_hash_insert_dup(parser->hash,digits,(void *)action); + if (status == SWITCH_STATUS_SUCCESS && parser->terminator == '\0' && (len = strlen(digits)) > parser->maxlen) { + parser->maxlen = len; + } } 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); @@ -3336,10 +3342,22 @@ 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 (digit != parser->terminator) { + // if collected digits length >= the max length of the keys + // in the hash table, then left shift the digit string + if ( len > 1 && parser->maxlen != 0 && len >= parser->maxlen) { + char *src = parser->digits + 1; + char *dst = parser->digits; + + while (*src) { + *(dst++) = *(src++); + } + *dst = digit; + } else { + parser->digits = realloc(parser->digits,len+2); + *(parser->digits+(len++)) = digit; + *(parser->digits+len) = '\0'; + } } // if we have digits to test @@ -3374,6 +3392,7 @@ if (parser != NULL) { parser->terminator = digit; + parser->maxlen = 0; status = SWITCH_STATUS_SUCCESS; }