changeset 10138:805a83ecd3da

avoid conflict between glob.h definition of glob and glob_match::glob function
author John W. Eaton <jwe@octave.org>
date Wed, 20 Jan 2010 01:57:41 -0500
parents cf6a01e0e93f
children 93c74edcc3e3
files liboctave/ChangeLog liboctave/Makefile.am liboctave/glob-match.cc liboctave/glob-match.h liboctave/oct-glob.cc liboctave/oct-glob.h
diffstat 6 files changed, 215 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- 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  <jwe@octave.org>
+
+	* 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  <highegg@gmail.com>
 
 	* CMatrix.cc (xgemm): Use octave_idx_type rather than int.
--- 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 \
--- 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 <fnmatch.h>
-#include <glob.h>
 
-#include <iostream>
-#include <string>
-
-#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<bool>
-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]);
+  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++ ***
--- 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 <string>
 
 #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<bool> match (const string_vector&);
+  Array<bool> match (const string_vector& str)
+  {
+    int n = str.length ();
+
+    Array<bool> 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
--- /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
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string>
+
+#include <fnmatch.h>
+#include <glob.h>
+
+#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 ();
+}
--- /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
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#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