Revision
14933
Author
sathieu
Date
2009-09-21 09:11:08 -0500 (Mon, 21 Sep 2009)

Log Message

mod_tts_commandline: various memory fixes, corrected indentation

Modified Paths

Diff

Modified: freeswitch/trunk/src/mod/asr_tts/mod_tts_commandline/mod_tts_commandline.c (14932 => 14933)


--- freeswitch/trunk/src/mod/asr_tts/mod_tts_commandline/mod_tts_commandline.c	2009-09-21 09:43:47 UTC (rev 14932)
+++ freeswitch/trunk/src/mod/asr_tts/mod_tts_commandline/mod_tts_commandline.c	2009-09-21 14:11:08 UTC (rev 14933)
@@ -47,10 +47,10 @@
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_command, globals.command);
 
 struct tts_commandline_data {
-	switch_file_handle_t fh;
 	char *voice_name;
 	int rate;
 	char *file;
+	switch_file_handle_t *fh;
 };
 
 typedef struct tts_commandline_data tts_commandline_t;
@@ -68,9 +68,9 @@
 	switch_assert(dest);
 
 #ifdef WIN32
-    dest[n++] = '"';
+	dest[n++] = '"';
 #else
-    dest[n++] = '\'';
+	dest[n++] = '\'';
 #endif
 
 	for (i = 0; i < string_len; i++) {
@@ -82,7 +82,7 @@
 			break;
 #else
 		case '\'':
-            /* We replace ' by '\'' */
+			/* We replace ' by '\'' */
 			dest_len+=3;
 			tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
 			switch_assert(tmp);
@@ -98,17 +98,17 @@
 		}
   }
   
-dest_len++;
-tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
-switch_assert(tmp);
-dest = tmp;
+	dest_len += 2; /* +2 for the closing quote and the null character */
+	tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
+	switch_assert(tmp);
+	dest = tmp;
 #ifdef WIN32
-    dest[n++] = '"';
+	dest[n++] = '"';
 #else
-    dest[n++] = '\'';
+	dest[n++] = '\'';
 #endif
-
-	dest[dest_len] = 0;
+	dest[n++] = 0;
+	switch_assert(n == dest_len);
 	return dest;
 }
 
@@ -134,7 +134,7 @@
 	}
 
 	if (switch_strlen_zero(globals.command)) {
-	    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No command set, please edit %s\n", cf);   
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No command set, please edit %s\n", cf);   
 	}
 
 	return SWITCH_STATUS_SUCCESS;
@@ -160,12 +160,14 @@
 	info->voice_name = switch_core_strdup(sh->memory_pool, voice_name);
 	info->rate = rate;
 
+	/* Construct temporary file name with a new UUID */
 	switch_uuid_get(&uuid);
 	switch_uuid_format(uuid_str, &uuid);
-
 	switch_snprintf(outfile, sizeof(outfile), "%s%s.tmp.wav", SWITCH_GLOBAL_dirs.temp_dir, uuid_str);
-	info->file = outfile;
+	info->file = switch_core_strdup(sh->memory_pool, outfile);
 	
+	info->fh = (switch_file_handle_t *) switch_core_alloc(sh->memory_pool, sizeof(switch_file_handle_t));
+	
 	sh->private_info = info;
   
 	return SWITCH_STATUS_SUCCESS;
@@ -207,11 +209,11 @@
 	   return SWITCH_STATUS_FALSE;
 	}
 
-	if (switch_core_file_open(&info->fh,
-	                        info->file,
-	                        0, //number_of_channels,
-	                        0, //samples_per_second,
-	                        SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
+	if (switch_core_file_open(info->fh,
+							info->file,
+							0, //number_of_channels,
+							0, //samples_per_second,
+							SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
 	   switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to open file: %s\n", info->file);
 	   return SWITCH_STATUS_FALSE;
 	}
@@ -228,15 +230,15 @@
 	
 	size_t my_datalen = *datalen / 2;
 	
-	if (switch_core_file_read(&info->fh, data, &my_datalen) != SWITCH_STATUS_SUCCESS) {
-	    *datalen = my_datalen * 2;
-	    return SWITCH_STATUS_FALSE;
+	if (switch_core_file_read(info->fh, data, &my_datalen) != SWITCH_STATUS_SUCCESS) {
+		*datalen = my_datalen * 2;
+		return SWITCH_STATUS_FALSE;
 	}
 	*datalen = my_datalen * 2;
 	if(datalen == 0) {
-	    return SWITCH_STATUS_BREAK;
+		return SWITCH_STATUS_BREAK;
 	} else {
-	    return SWITCH_STATUS_SUCCESS;
+		return SWITCH_STATUS_SUCCESS;
 	}
 }
 
@@ -245,9 +247,9 @@
 	tts_commandline_t *info = (tts_commandline_t *) sh->private_info;
 	assert(info != NULL);
 	
-	switch_core_file_close(&info->fh);
+	switch_core_file_close(info->fh);
 	if (unlink(info->file) != 0) {
-	    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Sound file [%s] delete failed\n", info->file);
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Sound file [%s] delete failed\n", info->file);
 	}
 }
 
@@ -257,7 +259,7 @@
 	assert(info != NULL);
 
 	if (!strcasecmp(param, "voice")) {
-	    info->voice_name = switch_core_strdup(sh->memory_pool, val);
+		info->voice_name = switch_core_strdup(sh->memory_pool, val);
 	}
 }