help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: glob(pattern)


From: Thorsten Meyer
Subject: Re: glob(pattern)
Date: Thu, 18 Feb 2010 20:15:54 +0100
User-agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706)

John W. Eaton wrote:
> On 17-Feb-2010, Thorsten Meyer wrote:
>
> | John W. Eaton wrote:
> | > On 16-Feb-2010, Ben Abbott wrote:
> | >
> | > | On Feb 16, 2010, at 1:00 AM, Aditya Bhagwat wrote:
> | > |
> | > | > Dear Ben,
> | > | >
> | > | >> Did you try  ...
> | > | >>
> | > | >>    glob ('.*')
> | > | >>
> | > | >> Ben
> | > | >
> | > | > Yep, I did. In fact, that's what isn't working (or not the 
> "regexp"-way I
> | > | > would expect it to).
> | >
> | > The glob function uses filename globbing rules, not regexp rules.
> | > Yes, the documentation should explain what "filename globbing rules"
> | > are.  The "Pattern Matching" section of the bash manual has an
> | > explanation.  Would someone please adapt that (or another suitable)
> | > description and post a patch for the glob docstring?
> | 
> | Here is a patch to document filename globbing rules.
>
> I didn't receive a patch in the message or as an attachment.
>
> | the glob function.
> | I don't like that test yet: when the asserts fail, the cleanup section will 
> not
> | be executed and afterwards the working directory or the octave session will 
> be
> | the temporary directory of the test (I tried with a dummy assert (1, 2)).
> | I tried to use an unwind_protect block but that does not seem to work 
> within a test.
> | Yet for the test itself to be robust, I need a clean directory to start 
> with.
> | 
> | Is there another way to provide a robust cleanup for a test?
>
> I'm not sure.  Send the patch to the maintainers mailing list first
> and then we can discuss it there.
>
>   
sorry. I forgot the patch. Here it is.

Thorsten
# HG changeset patch
# User Thorsten Meyer <address@hidden>
# Date 1266440495 -3600

document glob patterns

diff -r 76c0570417f6 src/ChangeLog
--- a/src/ChangeLog     Sat Feb 06 20:52:32 2010 +0100
+++ b/src/ChangeLog     Wed Feb 17 22:01:35 2010 +0100
@@ -0,0 +1,4 @@
+2010-02-17  Thorsten Meyer  <address@hidden>
+
+       * dirfns.cc (Fglob): Document globbing patterns, add test.
+
diff -r 76c0570417f6 src/dirfns.cc
--- a/src/dirfns.cc     Sat Feb 06 20:52:32 2010 +0100
+++ b/src/dirfns.cc     Wed Feb 17 22:01:35 2010 +0100
@@ -548,19 +548,49 @@
 DEFUN (glob, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} glob (@var{pattern})\n\
-Given an array of strings (as a char array or a cell array) in\n\
+Given an array of pattern strings (as a char array or a cell array) in\n\
 @var{pattern}, return a cell array of file names that match any of\n\
-them, or an empty cell array if no patterns match.  Tilde expansion\n\
+them, or an empty cell array if no patterns match.  The pattern strings are \n\
+interpreted as filename globbing patterns (as they are used by unix shells).\n\
+Within a pattern\n\
address@hidden @code\n\
address@hidden *\n\
+matches any string, including the null string,\n\
address@hidden ?\n\
+matches any single character, and\n\
address@hidden [...]\n\
+matches any of the enclosed characters.\n\
address@hidden table\n\
+\n\
+Tilde expansion\n\
 is performed on each of the patterns before looking for matching file\n\
-names.  For example,\n\
+names.  For example:\n\
 \n\
 @example\n\
 @group\n\
-glob (\"/vm*\")\n\
-     @result{} \"/vmlinuz\"\n\
+ls\n\
+     @result{}\n\
+        file1  file2  file3  myfile1 myfile1b \n\
+glob (\"*file1\")\n\
+     @result{}\n\
+        @{\n\
+          [1,1] = file1\n\
+          [2,1] = myfile1\n\
+        @}\n\
+glob (\"myfile?\")\n\
+     @result{}\n\
+        @{\n\
+          [1,1] = myfile1\n\
+        @}\n\
+glob (\"file[12]\")\n\
+     @result{}\n\
+        @{\n\
+          [1,1] = file1\n\
+          [2,1] = file2\n\
+        @}\n\
 @end group\n\
 @end example\n\
address@hidden, ls, stat, readdir}\n\
address@hidden, ls, stat, readdir, regexp}\n\
 @end deftypefn")
 {
   octave_value retval;
@@ -584,6 +614,37 @@
   return retval;
 }
 
+/*
+%!test
+%!  tmpdir = tmpnam;
+%!  filename = {"file1", "file2", "file3", "myfile1", "myfile1b"};
+%!  if (mkdir (tmpdir))
+%!    cwd = pwd;
+%!    cd (tmpdir);
+%!    if strcmp (pwd, tmpdir)
+%!      a = 0;
+%!      for n = 1:5
+%!        save (filename{n}, "a");
+%!      endfor
+%!      success = 1;
+%!    else
+%!      error ("Couldn't change to working dir");
+%!    endif
+%!  else
+%!    error ("Couldn't create test files");
+%!  endif
+%!  assert (glob ("*file1"), {"file1"; "myfile1"});
+%!  assert (glob ("myfile?"), {"myfile1"});
+%!  assert (glob ("file[12]"), {"file1"; "file2"});
+%!  if (success)
+%!    for n = 1:5
+%!      delete (filename{n});
+%!    endfor
+%!    cd (cwd);
+%!    rmdir (tmpdir);
+%!  endif
+*/
+
 DEFUN (fnmatch, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string})\n\

reply via email to

[Prev in Thread] Current Thread [Next in Thread]