glob-match.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_glob_match_h)
00024 #define octave_glob_match_h 1
00025 
00026 #include <string>
00027 
00028 #include "Array.h"
00029 #include "str-vec.h"
00030 
00031 class
00032 OCTAVE_API
00033 glob_match
00034 {
00035 public:
00036 
00037   enum opts
00038   {
00039     pathname = 1,  // No wildcard can ever match '/'.
00040     noescape = 2,  // Backslashes don't quote special chars.
00041     period = 4     // Leading '.' is matched only explicitly.
00042   };
00043 
00044   glob_match (const std::string& p,
00045               unsigned int xopts = pathname|noescape|period)
00046     : pat (p), fnmatch_flags (opts_to_fnmatch_flags (xopts)) { }
00047 
00048   glob_match (const string_vector& p = string_vector (),
00049               unsigned int xopts = pathname|noescape|period)
00050     : pat (p), fnmatch_flags (opts_to_fnmatch_flags (xopts)) { }
00051 
00052   glob_match (const glob_match& gm)
00053     : pat (gm.pat), fnmatch_flags (gm.fnmatch_flags) { }
00054 
00055   glob_match& operator = (const glob_match& gm)
00056   {
00057     if (this != &gm)
00058       {
00059         pat = gm.pat;
00060         fnmatch_flags = gm.fnmatch_flags;
00061       }
00062     return *this;
00063   }
00064 
00065   ~glob_match (void) { }
00066 
00067   void set_pattern (const std::string& p) { pat = p; }
00068 
00069   void set_pattern (const string_vector& p) { pat = p; }
00070 
00071   bool match (const std::string& str) const;
00072 
00073   Array<bool> match (const string_vector& str) const
00074   {
00075     int n = str.length ();
00076 
00077     Array<bool> retval (dim_vector (n, 1));
00078 
00079     for (int i = 0; i < n; i++)
00080       retval(i) = match (str[i]);
00081 
00082     return retval;
00083   }
00084 
00085   // We forward to glob_internal here to avoid problems with gnulib's
00086   // glob.h defining glob to be rpl_glob.
00087 
00088   string_vector glob (void) const;
00089 
00090 private:
00091 
00092   // Globbing pattern(s).
00093   string_vector pat;
00094 
00095   // Option flags.
00096   int fnmatch_flags;
00097 
00098   int opts_to_fnmatch_flags (unsigned int xopts) const;
00099 };
00100 
00101 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines