Make ADPCM frames standard
[asterisk/asterisk.git] / file.c
diff --git a/file.c b/file.c
index 4213a23..9cdaec5 100755 (executable)
--- a/file.c
+++ b/file.c
@@ -274,7 +274,7 @@ static int copy(char *infile, char *outfile)
 static char *build_filename(char *filename, char *ext)
 {
        char *fn;
-       char tmp[AST_CONFIG_MAX_PATH];
+       char tmp[AST_CONFIG_MAX_PATH]="";
        snprintf(tmp,sizeof(tmp)-1,"%s/%s",(char *)ast_config_AST_VAR_DIR,"sounds");
        fn = malloc(strlen(tmp) + strlen(filename) + strlen(ext) + 10);
        if (fn) {
@@ -287,6 +287,22 @@ static char *build_filename(char *filename, char *ext)
        
 }
 
+static int exts_compare(char *exts, char *type)
+{
+       char *stringp = NULL, *ext;
+       char tmp[256];
+
+       strncpy(tmp, exts, sizeof(tmp) - 1);
+       stringp = tmp;
+       while ((ext = strsep(&stringp, "|"))) {
+               if (!strcmp(ext, type)) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 #define ACTION_EXISTS 1
 #define ACTION_DELETE 2
 #define ACTION_RENAME 3
@@ -319,7 +335,7 @@ static int ast_filehelper(char *filename, char *filename2, char *fmt, int action
        }
        f = formats;
        while(f) {
-               if (!fmt || !strcasecmp(f->name, fmt)) {
+               if (!fmt || exts_compare(f->exts, fmt)) {
                        char *stringp=NULL;
                        exts = strdup(f->exts);
                        /* Try each kind of extension */
@@ -376,7 +392,7 @@ static int ast_filehelper(char *filename, char *filename2, char *fmt, int action
                                                                                        chan->vstream = s;
                                                                        } else {
                                                                                close(ret);
-                                                                               ast_log(LOG_WARNING, "Unable to open fd on %s\n", filename);
+                                                                               ast_log(LOG_WARNING, "Unable to open fd on %s\n", fn);
                                                                        }
                                                                } else
                                                                        ast_log(LOG_WARNING, "Couldn't open file %s\n", fn);
@@ -499,6 +515,15 @@ struct ast_filestream *ast_openvstream(struct ast_channel *chan, char *filename,
        return NULL;
 }
 
+struct ast_frame *ast_readframe(struct ast_filestream *s)
+{
+       struct ast_frame *f = NULL;
+       int whennext = 0;       
+       if (s && s->fmt)
+               f = s->fmt->read(s, &whennext);
+       return f;
+}
+
 static int ast_readaudio_callback(void *data)
 {
        struct ast_filestream *s = data;
@@ -726,10 +751,58 @@ int ast_streamfile(struct ast_channel *chan, char *filename, char *preflang)
        return -1;
 }
 
+struct ast_filestream *ast_readfile(char *filename, char *type, char *comment, int flags, int check, mode_t mode)
+{
+       int fd,myflags = 0;
+       struct ast_format *f;
+       struct ast_filestream *fs=NULL;
+       char *fn;
+       char *ext;
+       if (ast_mutex_lock(&formatlock)) {
+               ast_log(LOG_WARNING, "Unable to lock format list\n");
+               return NULL;
+       }
+       f = formats;
+       while(f) {
+               if (exts_compare(f->exts, type)) {
+                       char *stringp=NULL;
+                       /* XXX Implement check XXX */
+                       ext = strdup(f->exts);
+                       stringp=ext;
+                       ext = strsep(&stringp, "|");
+                       fn = build_filename(filename, ext);
+                       fd = open(fn, flags | myflags);
+                       if (fd >= 0) {
+                               errno = 0;
+                               if ((fs = f->open(fd))) {
+                                       fs->trans = NULL;
+                                       fs->fmt = f;
+                                       fs->flags = flags;
+                                       fs->mode = mode;
+                                       fs->filename = strdup(filename);
+                                       fs->vfs = NULL;
+                               } else {
+                                       ast_log(LOG_WARNING, "Unable to open %s\n", fn);
+                                       close(fd);
+                                       unlink(fn);
+                               }
+                       } else if (errno != EEXIST)
+                               ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
+                       free(fn);
+                       free(ext);
+                       break;
+               }
+               f = f->next;
+       }
+       ast_mutex_unlock(&formatlock);
+       if (!f) 
+               ast_log(LOG_WARNING, "No such format '%s'\n", type);
+       return fs;
+}
 
 struct ast_filestream *ast_writefile(char *filename, char *type, char *comment, int flags, int check, mode_t mode)
 {
-       int fd,myflags;
+       int fd,myflags = 0;
        struct ast_format *f;
        struct ast_filestream *fs=NULL;
        char *fn;
@@ -738,19 +811,22 @@ struct ast_filestream *ast_writefile(char *filename, char *type, char *comment,
                ast_log(LOG_WARNING, "Unable to lock format list\n");
                return NULL;
        }
-       myflags = 0;
        /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
-       if (!(flags & O_APPEND)) myflags = O_TRUNC;
+       if (!(flags & O_APPEND)) 
+               myflags = O_TRUNC;
+       
+       myflags |= O_WRONLY | O_CREAT;
+
        f = formats;
        while(f) {
-               if (!strcasecmp(f->name, type)) {
+               if (exts_compare(f->exts, type)) {
                        char *stringp=NULL;
                        /* XXX Implement check XXX */
                        ext = strdup(f->exts);
                        stringp=ext;
                        ext = strsep(&stringp, "|");
                        fn = build_filename(filename, ext);
-                       fd = open(fn, flags | myflags | O_WRONLY | O_CREAT, mode);
+                       fd = open(fn, flags | myflags, mode);
                        if (fd >= 0) {
                                errno = 0;
                                if ((fs = f->rewrite(fd, comment))) {