1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_glob_match_h)
27 #define octave_glob_match_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 
33 #include "Array.h"
34 #include "str-vec.h"
35 
36 class
37 OCTAVE_API
38 glob_match
39 {
40 public:
41 
42   enum opts
43   {
44     pathname = 1,  // No wildcard can ever match '/'.
45     noescape = 2,  // Backslashes don't quote special chars.
46     period = 4     // Leading '.' is matched only explicitly.
47   };
48 
49   glob_match (const std::string& p,
50               unsigned int xopts = pathname | noescape | period)
m_pat(p)51     : m_pat (p), m_fnmatch_flags (opts_to_fnmatch_flags (xopts))
52   { }
53 
54   glob_match (const string_vector& p = string_vector (),
55               unsigned int xopts = pathname | noescape | period)
m_pat(p)56     : m_pat (p), m_fnmatch_flags (opts_to_fnmatch_flags (xopts))
57   { }
58 
59   glob_match (const glob_match& gm) = default;
60 
61   glob_match& operator = (const glob_match& gm) = default;
62 
63   ~glob_match (void) = default;
64 
set_pattern(const std::string & p)65   void set_pattern (const std::string& p) { m_pat = p; }
66 
set_pattern(const string_vector & p)67   void set_pattern (const string_vector& p) { m_pat = p; }
68 
69   bool match (const std::string& str) const;
70 
match(const string_vector & str)71   Array<bool> match (const string_vector& str) const
72   {
73     int n = str.numel ();
74 
75     Array<bool> retval (dim_vector (n, 1));
76 
77     for (int i = 0; i < n; i++)
78       retval(i) = match (str[i]);
79 
80     return retval;
81   }
82 
83   // We forward to glob_internal here to avoid problems with gnulib's
84   // glob.h defining glob to be rpl_glob.
85 
86   string_vector glob (void) const;
87 
88 private:
89 
90   // Globbing pattern(s).
91   string_vector m_pat;
92 
93   // Option flags.
94   int m_fnmatch_flags;
95 
96   int opts_to_fnmatch_flags (unsigned int xopts) const;
97 };
98 
99 #endif
100