Mercurial > hg > octave-nkf
changeset 19756:701b43ce4467
properly close audio files
* audioread.cc (Faudioread, Faudiowrite, Faudioinfo):
Check that file was opened. Use unwind_protect to close file.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 09 Jan 2015 14:11:19 -0500 |
parents | 0b068273340f |
children | e75df9e43e63 |
files | libinterp/dldfcn/audioread.cc |
diffstat | 1 files changed, 35 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/dldfcn/audioread.cc +++ b/libinterp/dldfcn/audioread.cc @@ -28,6 +28,7 @@ #include <map> #include "oct-locbuf.h" +#include "unwind-prot.h" #include "defun-dld.h" #include "error.h" @@ -40,6 +41,14 @@ #include <sndfile.h> #endif +#ifdef HAVE_SNDFILE +static void +safe_close (SNDFILE *file) +{ + sf_close (file); +} +#endif + DEFUN_DLD (audioread, args, , "-*- texinfo -*-\n\ @deftypefn {Loadable Function} {[@var{y}, @var{fs}] =} audioread (@var{filename})\n\ @@ -83,6 +92,16 @@ info.format = 0; SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); + if (! file) + { + error ("audioread: failed to open input file %s", filename.c_str ()); + return retval; + } + + unwind_protect frame; + + frame.add_fcn (safe_close, file); + OCTAVE_LOCAL_BUFFER (float, data, info.frames * info.channels); sf_read_float (file, data, info.frames * info.channels); @@ -363,16 +382,18 @@ } } - const char *out = filename.c_str (); - - SNDFILE *file = sf_open (out, SFM_WRITE, &info); + SNDFILE *file = sf_open (filename.c_str (), SFM_WRITE, &info); if (! file) { - error ("audiowrite: failed to open output file %s", out); + error ("audiowrite: failed to open output file %s", filename.c_str ()); return retval; } + unwind_protect frame; + + frame.add_fcn (safe_close, file); + if (title != "") sf_set_string (file, SF_STR_TITLE, title.c_str ()); @@ -440,6 +461,16 @@ info.format = 0; SNDFILE *file = sf_open (filename.c_str (), SFM_READ, &info); + if (! file) + { + error ("audioinfo: failed to open file %s", filename.c_str ()); + return retval; + } + + unwind_protect frame; + + frame.add_fcn (safe_close, file); + octave_scalar_map result; result.assign ("Filename", filename);