typedef struct file_string_context file_string_context_t;
-static int next_file(switch_file_handle_t *handle)
+static switch_status_t next_file(switch_file_handle_t *handle)
{
file_string_context_t *context = handle->private_info;
char *file;
}
if (context->index >= context->argc) {
- return 0;
+ return SWITCH_STATUS_FALSE;
}
file = switch_core_sprintf(handle->memory_pool, "%s%s%s", prefix, SWITCH_PATH_SEPARATOR, context->argv[context->index]);
}
- if (switch_core_file_open(&context->fh,
- file, handle->channels, handle->samplerate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) {
+ if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
+ char *path = switch_core_strdup(handle->memory_pool, file);
+ char *p;
+
+ if ((p = strrchr(path, *SWITCH_PATH_SEPARATOR))) {
+ *p = '\0';
+ if (switch_dir_make_recursive(path, SWITCH_DEFAULT_DIR_PERMS, handle->memory_pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error creating %s\n", path);
+ return SWITCH_STATUS_FALSE;
+ }
+
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error finding the folder path section in '%s'\n", path);
+ }
+
+ }
+ if (switch_core_file_open(&context->fh, file, handle->channels, handle->samplerate, handle->flags, NULL) != SWITCH_STATUS_SUCCESS) {
goto top;
}
}
}
- return 1;
+ return SWITCH_STATUS_SUCCESS;
}
file_string_context_t *context;
char *file_dup;
- if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "This format does not support writing!\n");
- return SWITCH_STATUS_FALSE;
- }
-
context = switch_core_alloc(handle->memory_pool, sizeof(*context));
file_dup = switch_core_strdup(handle->memory_pool, path);
handle->private_info = context;
- return next_file(handle) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+ return next_file(handle);
}
static switch_status_t file_string_file_close(switch_file_handle_t *handle)
}
if (status != SWITCH_STATUS_SUCCESS) {
- if (!next_file(handle)) {
- return SWITCH_STATUS_FALSE;
+ if ((status = next_file(handle)) != SWITCH_STATUS_SUCCESS) {
+ return status;
}
*len = llen;
status = switch_core_file_read(&context->fh, data, len);
}
- return SWITCH_STATUS_SUCCESS;
+ return status;
+}
+
+static switch_status_t file_string_file_write(switch_file_handle_t *handle, void *data, size_t *len)
+{
+ file_string_context_t *context = handle->private_info;
+ switch_status_t status;
+ size_t llen = *len;
+
+ status = switch_core_file_write(&context->fh, data, len);
+
+ if (status != SWITCH_STATUS_SUCCESS) {
+ if ((status = next_file(handle)) != SWITCH_STATUS_SUCCESS) {
+ return status;
+ }
+ *len = llen;
+ status = switch_core_file_write(&context->fh, data, len);
+ }
+ return status;
}
+
/* Registration */
static char *file_string_supported_formats[SWITCH_MAX_CODECS] = { 0 };
file_interface->file_open = file_string_file_open;
file_interface->file_close = file_string_file_close;
file_interface->file_read = file_string_file_read;
+ file_interface->file_write = file_string_file_write;
file_interface->file_seek = file_string_file_seek;