# HG changeset patch # User John W. Eaton # Date 1263970661 18000 # Node ID 805a83ecd3da5205ad5704885f19f8d0b2391439 # Parent cf6a01e0e93fc94bf70d03294044b335f4816d5c avoid conflict between glob.h definition of glob and glob_match::glob function diff -r cf6a01e0e93f -r 805a83ecd3da liboctave/ChangeLog --- a/liboctave/ChangeLog Wed Jan 20 03:30:06 2010 +0100 +++ b/liboctave/ChangeLog Wed Jan 20 01:57:41 2010 -0500 @@ -1,3 +1,23 @@ +2010-01-20 John W. Eaton + + * Makefile.am (INCS): Include oct-glob.h in the list. + (LIBOCTAVE_CXX_SOURCES): Include oct-glob.cc in the list. + * oct-glob.h, oct-glob.cc: New files. + * glob-match.cc: (glob_match::match): Move internals to oct-glob.cc. + (glob_match::glob_internal): Move internals to oct-glob.cc. + (single_match_exists): Move to oct-glob.cc. + (glob_match::opts_to_fnmatch_flags): New function. + * glob-match.h Include oct-glob.h. + (glob_match::glob): Call octave_glob to do the real work. + (glob_match::glob_internal): Delete decl. + (glob_match::match (const string_vector&)): Move here from + glob-match.cc. + (glob_match::match (const std::string&)): Call octave_fnmatch to + do the real work. + (glob_match::fnmatch_flags): Rename from flags. + (glob_match::opts_to_fnmatch_flags): New function. + (glob_match::glob_match): Use it. + 2010-01-17 Jaroslav Hajek * CMatrix.cc (xgemm): Use octave_idx_type rather than int. diff -r cf6a01e0e93f -r 805a83ecd3da liboctave/Makefile.am --- a/liboctave/Makefile.am Wed Jan 20 03:30:06 2010 +0100 +++ b/liboctave/Makefile.am Wed Jan 20 01:57:41 2010 -0500 @@ -215,6 +215,7 @@ oct-env.h \ oct-fftw.h \ oct-getopt.h \ + oct-glob.h \ oct-group.h \ oct-inttypes.h \ oct-locbuf.h \ @@ -422,6 +423,7 @@ oct-alloc.cc \ oct-env.cc \ oct-fftw.cc \ + oct-glob.cc \ oct-group.cc \ oct-locbuf.cc \ oct-md5.cc \ diff -r cf6a01e0e93f -r 805a83ecd3da liboctave/glob-match.cc --- a/liboctave/glob-match.cc Wed Jan 20 03:30:06 2010 +0100 +++ b/liboctave/glob-match.cc Wed Jan 20 01:57:41 2010 -0500 @@ -1,6 +1,6 @@ /* -Copyright (C) 1996, 1997, 2000, 2005, 2006, 2007, 2009 John W. Eaton +Copyright (C) 1996, 1997, 2000, 2005, 2006, 2007, 2009, 2010 John W. Eaton This file is part of Octave. @@ -25,109 +25,26 @@ #endif #include -#include -#include -#include - -#include "file-stat.h" #include "glob-match.h" -#include "str-vec.h" -bool -glob_match::match (const std::string& s) +int +glob_match::opts_to_fnmatch_flags (unsigned int xopts) const { - int npat = pat.length (); - - const char *str = s.c_str (); - - int fnmatch_flags = 0; - - if (flags & pathname) - fnmatch_flags |= FNM_PATHNAME; + int retval = 0; - if (flags & noescape) - fnmatch_flags |= FNM_NOESCAPE; - - if (flags & period) - fnmatch_flags |= FNM_PERIOD; - - for (int i = 0; i < npat; i++) - if (fnmatch (pat(i).c_str (), str, fnmatch_flags) != FNM_NOMATCH) - return true; + if (xopts & pathname) + retval |= FNM_PATHNAME; - return false; -} + if (xopts & noescape) + retval |= FNM_NOESCAPE; -Array -glob_match::match (const string_vector& s) -{ - int n = s.length (); - - Array retval (n); - - for (int i = 0; i < n; i++) - retval(i) = match (s[i]); + if (xopts & period) + retval |= FNM_PERIOD; return retval; } -static bool -single_match_exists (const std::string& file) -{ - file_stat s (file); - - return s.exists (); -} - -string_vector -glob_match::glob_internal (void) -{ - string_vector retval; - - int npat = pat.length (); - - int k = 0; - - for (int i = 0; i < npat; i++) - { - std::string xpat = pat(i); - - if (! xpat.empty ()) - { - glob_t glob_info; - - int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info); - - if (! err) - { - int n = glob_info.gl_pathc; - - const char * const *matches = glob_info.gl_pathv; - - // FIXME -- we shouldn't have to check to see if - // a single match exists, but it seems that glob() won't - // check for us unless the pattern contains globbing - // characters. Hmm. - - if (n > 1 - || (n == 1 - && single_match_exists (std::string (matches[0])))) - { - retval.resize (k+n); - - for (int j = 0; j < n; j++) - retval[k++] = matches[j]; - } - - globfree (&glob_info); - } - } - } - - return retval.sort (); -} - /* ;;; Local Variables: *** ;;; mode: C++ *** diff -r cf6a01e0e93f -r 805a83ecd3da liboctave/glob-match.h --- a/liboctave/glob-match.h Wed Jan 20 03:30:06 2010 +0100 +++ b/liboctave/glob-match.h Wed Jan 20 01:57:41 2010 -0500 @@ -26,6 +26,7 @@ #include #include "Array.h" +#include "oct-glob.h" #include "str-vec.h" class @@ -35,31 +36,32 @@ public: enum opts - { - pathname = 1, // No wildcard can ever match `/'. - noescape = 2, // Backslashes don't quote special chars. - period = 4 // Leading `.' is matched only explicitly. - }; + { + pathname = 1, // No wildcard can ever match `/'. + noescape = 2, // Backslashes don't quote special chars. + period = 4 // Leading `.' is matched only explicitly. + }; glob_match (const std::string& p, - unsigned int f = pathname|noescape|period) - : pat (p), flags (f) { } + unsigned int xopts = pathname|noescape|period) + : pat (p), fnmatch_flags (opts_to_fnmatch_flags (xopts)) { } glob_match (const string_vector& p = string_vector (), - unsigned int f = pathname|noescape|period) - : pat (p), flags (f) { } + unsigned int xopts = pathname|noescape|period) + : pat (p), fnmatch_flags (opts_to_fnmatch_flags (xopts)) { } - glob_match (const glob_match& gm) : pat (gm.pat), flags (gm.flags) { } + glob_match (const glob_match& gm) + : pat (gm.pat), fnmatch_flags (gm.fnmatch_flags) { } glob_match& operator = (const glob_match& gm) - { - if (this != &gm) - { - pat = gm.pat; - flags = gm.flags; - } - return *this; - } + { + if (this != &gm) + { + pat = gm.pat; + fnmatch_flags = gm.fnmatch_flags; + } + return *this; + } ~glob_match (void) { } @@ -67,14 +69,27 @@ void set_pattern (const string_vector& p) { pat = p; } - bool match (const std::string&); + bool match (const std::string& str) + { + return octave_fnmatch (pat, str, fnmatch_flags); + } - Array match (const string_vector&); + Array match (const string_vector& str) + { + int n = str.length (); + + Array retval (n); + + for (int i = 0; i < n; i++) + retval(i) = match (str[i]); + + return retval; + } // We forward to glob_internal here to avoid problems with gnulib's // glob.h defining glob to be rpl_glob. - string_vector glob (void) { return glob_internal (); } + string_vector glob (void) { return octave_glob (pat); } private: @@ -82,9 +97,9 @@ string_vector pat; // Option flags. - unsigned int flags; + int fnmatch_flags; - string_vector glob_internal (void); + int opts_to_fnmatch_flags (unsigned int xopts) const; }; #endif diff -r cf6a01e0e93f -r 805a83ecd3da liboctave/oct-glob.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/oct-glob.cc Wed Jan 20 01:57:41 2010 -0500 @@ -0,0 +1,110 @@ +/* + +Copyright (C) 2010 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include +#include + +#include "oct-glob.h" +#include "file-stat.h" + +// These functions are defined here and not in glob_match.cc so that we +// can include the glob.h file from gnulib, which defines glob to +// be rpl_glob. If we include glob.h in glob_match.cc, then it +// transforms the glob_match::glob function to be glob_match::rpl_glob, +// which is not what we want... + +static bool +single_match_exists (const std::string& file) +{ + file_stat s (file); + + return s.exists (); +} + +bool +octave_fnmatch (const string_vector& pat, const std::string& str, + int fnmatch_flags) +{ + int npat = pat.length (); + + const char *cstr = str.c_str (); + + for (int i = 0; i < npat; i++) + if (fnmatch (pat(i).c_str (), cstr, fnmatch_flags) != FNM_NOMATCH) + return true; + + return false; +} + +string_vector +octave_glob (const string_vector& pat) +{ + string_vector retval; + + int npat = pat.length (); + + int k = 0; + + for (int i = 0; i < npat; i++) + { + std::string xpat = pat(i); + + if (! xpat.empty ()) + { + glob_t glob_info; + + int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info); + + if (! err) + { + int n = glob_info.gl_pathc; + + const char * const *matches = glob_info.gl_pathv; + + // FIXME -- we shouldn't have to check to see if + // a single match exists, but it seems that glob() won't + // check for us unless the pattern contains globbing + // characters. Hmm. + + if (n > 1 + || (n == 1 + && single_match_exists (std::string (matches[0])))) + { + retval.resize (k+n); + + for (int j = 0; j < n; j++) + retval[k++] = matches[j]; + } + + globfree (&glob_info); + } + } + } + + return retval.sort (); +} diff -r cf6a01e0e93f -r 805a83ecd3da liboctave/oct-glob.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liboctave/oct-glob.h Wed Jan 20 01:57:41 2010 -0500 @@ -0,0 +1,35 @@ +/* + +Copyright (C) 2010 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +. + +*/ + +#if !defined (octave_glob_h) +#define octave_glob_h 1 + +#include "str-vec.h" + +extern bool +octave_fnmatch (const string_vector& pat, const std::string& str, + int fnmatch_flags); + +extern string_vector +octave_glob (const string_vector&); + +#endif