From 042347802b2b4972a11212df0c84921b91b71d64 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Wed, 20 Feb 2013 16:34:13 -0600
Subject: [PATCH] changing the params thing a bit, use {foo=bar} syntax instead

---
 src/include/switch_module_interfaces.h        |  1 +
 .../applications/mod_dptools/mod_dptools.c    |  1 +
 .../formats/mod_tone_stream/mod_tone_stream.c |  9 +--
 src/switch_core_file.c                        | 39 +++++++++----
 src/switch_ivr_play_say.c                     | 55 ++-----------------
 5 files changed, 42 insertions(+), 63 deletions(-)

diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h
index 650d16eb97..e4bab2221c 100644
--- a/src/include/switch_module_interfaces.h
+++ b/src/include/switch_module_interfaces.h
@@ -352,6 +352,7 @@ struct switch_file_handle {
 	char *spool_path;
 	const char *prefix;
 	int max_samples;
+	switch_event_t *params;
 };
 
 /*! \brief Abstract interface to an asr module */
diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c
index d90fb29d6a..d1bd034143 100755
--- a/src/mod/applications/mod_dptools/mod_dptools.c
+++ b/src/mod/applications/mod_dptools/mod_dptools.c
@@ -4498,6 +4498,7 @@ static switch_status_t next_file(switch_file_handle_t *handle)
 	handle->seekable = context->fh.seekable;
 	handle->speed = context->fh.speed;
 	handle->interval = context->fh.interval;
+	handle->max_samples = 0;
 
 	if (switch_test_flag((&context->fh), SWITCH_FILE_NATIVE)) {
 		switch_set_flag(handle, SWITCH_FILE_NATIVE);
diff --git a/src/mod/formats/mod_tone_stream/mod_tone_stream.c b/src/mod/formats/mod_tone_stream/mod_tone_stream.c
index 45f3ee65fd..fe3080f4c5 100644
--- a/src/mod/formats/mod_tone_stream/mod_tone_stream.c
+++ b/src/mod/formats/mod_tone_stream/mod_tone_stream.c
@@ -136,10 +136,11 @@ static switch_status_t tone_stream_file_open(switch_file_handle_t *handle, const
 	switch_buffer_create_dynamic(&audio_buffer, 1024, 1024, 0);
 	switch_assert(audio_buffer);
 
-	if ((tmp = strstr(tonespec, ";loops="))) {
-		*tmp = '\0';
-		loops = atoi(tmp + 7);
-		switch_buffer_set_loops(audio_buffer, loops);
+	if (handle->params) {
+		if ((tmp = switch_event_get_header(handle->params, "loops"))) {
+			loops = atoi(tmp);
+			switch_buffer_set_loops(audio_buffer, loops);		
+		}
 	}
 
 	if (!handle->samplerate) {
diff --git a/src/switch_core_file.c b/src/switch_core_file.c
index 7bc8ccc4b0..0f6de164a8 100644
--- a/src/switch_core_file.c
+++ b/src/switch_core_file.c
@@ -47,7 +47,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
 	char *rhs = NULL;
 	const char *spool_path = NULL;
 	int is_stream = 0;
-	char *fp = NULL, *params = NULL;
+	char *fp = NULL;
+	switch_event_t *params = NULL;
 	int to = 0;
 
 	if (switch_test_flag(fh, SWITCH_FILE_OPEN)) {
@@ -55,6 +56,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
 		return SWITCH_STATUS_FALSE;
 	}
 
+	fh->samples_in = 0;
+
 	if (!fh->samplerate) {
 		if (!(fh->samplerate = rate)) {
 			fh->samplerate = 8000;
@@ -78,25 +81,24 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
 		switch_set_flag(fh, SWITCH_FILE_FLAG_FREE_POOL);
 	}
 
-	if (strchr(file_path, ';')) {
+	if (*file_path == '{') {
 		char *timeout;
-
+		char *new_fp;
 		fp = switch_core_strdup(fh->memory_pool, file_path);
-		file_path = fp;
 
-		if ((params = strchr(fp, ';'))) {
-			*params++ = '\0';
-
-			if ((timeout = switch_find_parameter(params, "timeout", fh->memory_pool))) {
+		if (switch_event_create_brackets(fp, '{', '}', ',', &fh->params, &new_fp, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
+			if ((timeout = switch_event_get_header(fh->params, "timeout"))) {
 				if ((to = atoi(timeout)) < 1) {
 					to = 0;
 				}
 			}
-
+		} else {
+			new_fp = fp;
 		}
+
+		file_path = new_fp;
 	}
 
-	
 	if (switch_directory_exists(file_path, fh->memory_pool) == SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "File [%s] is a directory not a file.\n", file_path);
 		status = SWITCH_STATUS_GENERR;
@@ -179,6 +181,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
 
 	file_path = fh->spool_path ? fh->spool_path : fh->file_path;
 
+	if (params) {
+		fh->params = params;
+	}
 
 	if ((status = fh->file_interface->file_open(fh, file_path)) != SWITCH_STATUS_SUCCESS) {
 		if (fh->spool_path) {
@@ -231,6 +236,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
 
 	switch_clear_flag(fh, SWITCH_FILE_OPEN);
 
+	if (fh->params) {
+		switch_event_destroy(&fh->params);
+	}
+
+	fh->samples_in = 0;
+	fh->max_samples = 0;
+	
 	if (switch_test_flag(fh, SWITCH_FILE_FLAG_FREE_POOL)) {
 		switch_core_destroy_memory_pool(&fh->memory_pool);
 	}
@@ -585,6 +597,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_close(switch_file_handle_t *fh)
 		return SWITCH_STATUS_FALSE;
 	}
 
+	if (fh->params) {
+		switch_event_destroy(&fh->params);
+	}
+
+	fh->samples_in = 0;
+	fh->max_samples = 0;
+
 	if (fh->buffer) {
 		switch_buffer_destroy(&fh->buffer);
 	}
diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c
index 2b50b4cf55..3ff6893e44 100644
--- a/src/switch_ivr_play_say.c
+++ b/src/switch_ivr_play_say.c
@@ -1001,38 +1001,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_release_file_handle(switch_core_sessi
 #define FILE_BLOCKSIZE 1024 * 8
 #define FILE_BUFSIZE 1024 * 64
 
-static void add_playback_vars_to_event(switch_core_session_t *session, switch_event_t *event, char *vars)
-{
-	char *tmp;
-
-	if (!session || !event || !vars)
-		return;
-
-	if ((tmp = switch_core_session_strdup(session, vars))) {
-		char *argv[128] = { 0 };
-		int argc, i;
-
-		if (!(argc = switch_separate_string(tmp, ',', argv, (sizeof(argv) / sizeof(argv[0])))))
-			return;
-
-		for (i = 0; i < argc; i++) {
-			char *var, *val;
-
-			if ((var = strchr(argv[i], '='))) {
-				*var = '\0';
-				val = var+1;
-				var = argv[i];
-
-				if (var && *var && val && *val) {
-					if ((var = switch_core_session_sprintf(session, "playback_variable_%s", var))) {
-						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, var, val);
-					}
-				}
-			}
-		}
-	}
-}
-
 SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *session, switch_file_handle_t *fh, const char *file, switch_input_args_t *args)
 {
 	switch_channel_t *channel = switch_core_session_get_channel(session);
@@ -1072,7 +1040,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
 	switch_bool_t timeout_as_success = SWITCH_FALSE;
 	const char *var;
 	int more_data = 0;
-	char *playback_vars, *tmp;
 	switch_event_t *event;
 	uint32_t test_native = 0, last_native = 0;
 
@@ -1241,20 +1208,6 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
 			}
 		}
 
-		/* Try to parse extra parameters for this playback (parameters within {} at the end of the filename */
-		playback_vars = NULL;
-		if ((tmp = strchr(file, '{'))) {
-			char *tfile, *e;
-			
-			if ((tfile = switch_core_session_strdup(session, tmp))) {
-				if ((e = switch_find_end_paren(tfile, '{', '}')) && *(e + 1) == '\0') {
-					*tmp = '\0';
-					*e = '\0';
-					playback_vars = tfile+1;
-				}
-			}
-		}
-
 		if ((prebuf = switch_channel_get_variable(channel, "stream_prebuffer"))) {
 			int maybe = atoi(prebuf);
 			if (maybe > 0) {
@@ -1408,7 +1361,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
 				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Type", "tone_stream");
 			}
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-File-Path", file);
-			add_playback_vars_to_event(session, event, playback_vars); 
+			if (fh->params) {
+				switch_event_merge(event, fh->params);
+			}
 			switch_event_fire(&event);
 		}
 
@@ -1744,7 +1699,9 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_play_file(switch_core_session_t *sess
 			} else {
 				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Playback-Status", "done");
 			}
-			add_playback_vars_to_event(session, event, playback_vars); 
+			if (fh->params) {
+				switch_event_merge(event, fh->params);
+			}
 			switch_event_fire(&event);
 		}