changeset 19299:d0f02639f560

delete.m: Allow multiple file names to delete. * delete.m: Use varargin to accept multiple file names to delete. Add new BIST tests for deleting a file and deleting a graphics handle.
author Rik <rik@octave.org>
date Sat, 18 Oct 2014 20:49:34 -0700
parents f0dcb44826f6
children 217c455eac8f
files scripts/miscellaneous/delete.m
diffstat 1 files changed, 49 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/delete.m	Sat Oct 18 18:10:52 2014 -0700
+++ b/scripts/miscellaneous/delete.m	Sat Oct 18 20:49:34 2014 -0700
@@ -18,37 +18,47 @@
 
 ## -*- texinfo -*-
 ## @deftypefn  {Function File} {} delete (@var{file})
+## @deftypefnx {Function File} {} delete (@var{file1}, @var{file2}, @dots{})
 ## @deftypefnx {Function File} {} delete (@var{handle})
 ## Delete the named file or graphics handle.
 ##
-## Deleting graphics objects is the proper way to remove
+## @var{file} may contain globbing patterns such as @samp{*}.  Multiple files
+## to be deleted may be specified in the same function call.
+##
+## @var{handle} may be a scalar or vector of graphic handles to delete.
+##
+## Programming Note: Deleting graphics objects is the proper way to remove
 ## features from a plot without clearing the entire figure.
-## @seealso{clf, cla, unlink}
+## @seealso{clf, cla, unlink, rmdir}
 ## @end deftypefn
 
 ## Author: jwe
 
-function delete (arg)
+function delete (varargin)
 
-  if (nargin != 1)
+  if (nargin == 0)
     print_usage ();
   endif
 
-  if (ischar (arg))
-    files = glob (arg);
-    if (isempty (files))
-      warning ("delete: no such file: %s", arg);
-    endif
-    for i = 1:length (files)
-      file = files{i};
-      [err, msg] = unlink (file);
-      if (err)
-        warning ("delete: %s: %s", file, msg);
+  if (iscellstr (varargin))
+    for arg = varargin
+      files = glob (arg{1});
+      if (isempty (files))
+        warning ("delete: no such file: %s", arg{1});
       endif
+      for i = 1:length (files)
+        file = files{i};
+        [err, msg] = unlink (file);
+        if (err)
+          warning ("delete: %s: %s", file, msg);
+        endif
+      endfor
     endfor
-  elseif (all (ishandle (arg(:))))
+
+  elseif (isscalar (varargin) && all (ishandle (varargin{1}(:))))
     ## Delete a graphics object.
-    __go_delete__ (arg);
+    __go_delete__ (varargin{1});
+
   else
     error ("delete: first argument must be a filename or graphics handle");
   endif
@@ -56,8 +66,30 @@
 endfunction
 
 
+%!test
+%! unwind_protect
+%!   file = tempname;
+%!   tmp_var = pi;
+%!   save (file, "tmp_var");
+%!   assert (exist (file, "file"));
+%!   delete (file);
+%!   assert (! exist (file, "file"));
+%! unwind_protect_cleanup
+%!   unlink (file);
+%! end_unwind_protect
+
+%!test
+%! unwind_protect
+%!   hf = figure ("visible", "off"); 
+%!   hl = plot (1:10);
+%!   assert (get (gca, "children"), hl); 
+%!   delete (hl);
+%!   assert (get (gca, "children"), zeros (0,1)); 
+%! unwind_protect_cleanup
+%!   close (hf);
+%! end_unwind_protect
+
 %% Test input validation
 %!error delete ()
-%!error delete (1, 2)
 %!error <first argument must be a filename> delete (struct ())