Mercurial > hg > octave-avbm
changeset 2496:9823f8bfd1a5
[project @ 1996-11-11 03:17:10 by jwe]
author | jwe |
---|---|
date | Mon, 11 Nov 1996 03:17:12 +0000 |
parents | 29cd3862a9dc |
children | 6f7bb8b60579 |
files | NEWS doc/interpreter/system.texi liboctave/oct-glob.cc liboctave/oct-glob.h src/ChangeLog src/dirfns.cc |
diffstat | 6 files changed, 59 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/NEWS +++ b/NEWS @@ -184,6 +184,8 @@ umask -- set permission mask for file creation stat -- get information about a file lstat -- get information about a symbolic link + glob -- perform filename globbing + fnmatch -- match strings with filename globbing patterns more -- turn the pager on or off gammaln -- alias for lgamma
--- a/doc/interpreter/system.texi +++ b/doc/interpreter/system.texi @@ -359,6 +359,12 @@ matching file names. @end deftypefn +@deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string}) +Return 1 or zero for each element of @var{string} that matches any of +the elements of the string array @var{pattern}, using the rules of +filename pattern matching. +@end deftypefn + @node Interacting with the OS, Password Database Functions, Filesystem Utilities, System Utilities @section Interacting with the OS
--- a/liboctave/oct-glob.cc +++ b/liboctave/oct-glob.cc @@ -36,39 +36,28 @@ #include "str-vec.h" bool -glob_match::match (const string& s, match_type mt) +glob_match::match (const string& s) { int npat = pat.length (); const char *str = s.c_str (); - if (mt == all) - { - for (int i = 0; i < npat; i++) - if (fnmatch (pat(i).c_str (), str, flags) == FNM_NOMATCH) - return false; - + for (int i = 0; i < npat; i++) + if (fnmatch (pat(i).c_str (), str, flags) != FNM_NOMATCH) return true; - } - else - { - for (int i = 0; i < npat; i++) - if (fnmatch (pat(i).c_str (), str, flags) != FNM_NOMATCH) - return true; - return false; - } + return false; } Array<bool> -glob_match::match (const string_vector& s, match_type mt) +glob_match::match (const string_vector& s) { int n = s.length (); Array<bool> retval (n); for (int i = 0; i < n; i++) - retval(i) = match (s[i], mt); + retval(i) = match (s[i]); return retval; }
--- a/liboctave/oct-glob.h +++ b/liboctave/oct-glob.h @@ -40,12 +40,6 @@ period = 4 // Leading `.' is matched only explicitly. }; - enum match_type - { - any = 2, // Match any pattern. - all = 2 // Must match all patterns. - }; - glob_match (const string& p = string (), unsigned int f = pathname|noescape|period) : pat (p), flags (f) { } @@ -72,9 +66,9 @@ void set_pattern (const string_vector& p) { pat = p; } - bool match (const string&, match_type mt = any); + bool match (const string&); - Array<bool> match (const string_vector&, match_type mt = any); + Array<bool> match (const string_vector&); string_vector glob (void);
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,7 @@ Sun Nov 10 16:58:07 1996 John W. Eaton <jwe@bevo.che.wisc.edu> + * dirfns.cc (Ffnmatch): New function. + * octave.cc (intern_argv): Use new string_vector constructor. * ov-str-mat.cc (octave_char_matrix_str::all_strings): Have
--- a/src/dirfns.cc +++ b/src/dirfns.cc @@ -588,9 +588,9 @@ DEFUN (glob, args, , "glob (PATTERN)\n\ \n\ -Given an array of strings in PATTERN, return the list of file names -that any of them, or an empty string if no patterns match. Tilde -expansion is performed on each of the patterns before looking for +Given an array of strings in PATTERN, return the list of file names\n\ +that any of them, or an empty string if no patterns match. Tilde\n\ +expansion is performed on each of the patterns before looking for\n\ matching file names.") { octave_value retval; @@ -619,6 +619,44 @@ return retval; } +DEFUN (fnmatch, args, , + "fnmatch (PATTERN, STRING)\n\ +\n\ +Return 1 or zero for each element of STRING that matches any of the\n\ +elements of the string array PATTERN, using the rules of filename\n\ +pattern matching.") +{ + octave_value retval; + + if (args.length () == 2) + { + string_vector pat = args(0).all_strings (); + string_vector str = args(1).all_strings (); + + if (error_state) + gripe_wrong_type_arg ("fnmatch", args(0)); + else + { + glob_match pattern (oct_tilde_expand (pat)); + + Array<bool> tmp = pattern.match (str); + + int n = tmp.length (); + + ColumnVector result (n); + + for (int i = 0; i < n; i++) + result(i) = tmp(i); + + retval = octave_value (result, true); + } + } + else + print_usage ("fnmatch"); + + return retval; +} + static int pwd (void) {