diff libinterp/corefcn/dirfns.cc @ 22155:289409b2992d

Allow dir to accept [ and ] in arguments. (bug #47950) * liboctave/util/oct-glob.{cc,h} (windows_glob): New function * libinterp/corefcn/dirfns.cc (__wglob__): New wrapper for the above * dir.m: Call __wglob__ instead of glob.
author Lachlan Andrew <lachlanbis@gmail.com>
date Sat, 16 Jul 2016 14:03:34 +1000
parents 9203833cab7d
children bac0d6f07a3e
line wrap: on
line diff
--- a/libinterp/corefcn/dirfns.cc	Thu Jul 21 07:49:44 2016 -0400
+++ b/libinterp/corefcn/dirfns.cc	Sat Jul 16 14:03:34 2016 +1000
@@ -37,6 +37,7 @@
 #include "file-stat.h"
 #include "glob-match.h"
 #include "oct-env.h"
+#include "oct-glob.h"
 #include "pathsearch.h"
 #include "str-vec.h"
 
@@ -486,6 +487,73 @@
   return ovl (Cell (pattern.glob ()));
 }
 
+
+DEFUN (__wglob__, args, ,
+       doc: /* -*- texinfo -*-
+@deftypefn {} {} __wglob__ (@var{pattern})
+Windows-like glob for dir.
+
+Given an array of pattern strings (as a char array or a cell array) in
+@var{pattern}, return a cell array of filenames that match any of
+them, or an empty cell array if no patterns match.
+
+The pattern strings are interpreted as filename globbing patterns
+(roughly as they are used by Windows dir).
+
+Within a pattern
+
+@table @code
+@item *
+matches any string, including the null string,
+
+@item ?
+matches any single character, and
+
+@item *.*
+matches any string, even if no . is present.
+@end table
+
+Tilde expansion is performed on each of the patterns before looking for
+matching filenames.  For example:
+
+@example
+ls
+   @result{}
+      file1  file2  file3  myfile1 myfile1b
+glob ("*file1")
+   @result{}
+      @{
+        [1,1] = file1
+        [2,1] = myfile1
+      @}
+glob ("myfile?")
+   @result{}
+      @{
+        [1,1] = myfile1
+      @}
+glob ("*.*")
+   @result{}
+      @{
+        [1,1] = file1
+        [2,1] = file2
+        [3,1] = file3
+        [4,1] = myfile1
+        [5,1] = myfile1b
+      @}
+@end example
+@seealso{glob, dir}
+@end deftypefn */)
+{
+  if (args.length () == 0)
+    return ovl ();
+
+  string_vector pat = args(0).string_vector_value ();
+
+  string_vector pattern (octave::sys::file_ops::tilde_expand (pat));
+
+  return ovl (Cell (octave::sys::windows_glob (pattern)));
+}
+
 /*
 %!test
 %! tmpdir = tempname;