FreeFOAM The Cross-Platform CFD Toolkit
regExp.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::regExp
26 
27 Description
28  Wrapper around POSIX extended regular expressions.
29 
30 SeeAlso
31  The manpage regex(7) for more information about POSIX regular expressions.
32  These differ somewhat from @c Perl and @c sed regular expressions.
33 
34 SourceFiles
35  regExp.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef regExp_H
40 #define regExp_H
41 
42 #include <regex.h>
43 #include <string>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declaration of classes
51 class string;
52 template<class T> class List;
53 
54 /*---------------------------------------------------------------------------*\
55  Class regExp Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 class regExp
59 {
60  // Private data
61 
62  //- Precompiled regular expression
63  mutable regex_t* preg_;
64 
65  // Private member functions
66 
67  //- Disallow default bitwise copy construct
68  regExp(const regExp&);
69 
70  //- Disallow default bitwise assignment
71  void operator=(const regExp&);
72 
73 public:
74 
75  //- Is character a regular expression meta-character?
76  // any character: '.' \n
77  // quantifiers: '*', '+', '?' \n
78  // grouping: '(', '|', ')' \n
79  // range: '[', ']' \n
80  //
81  // Don't bother checking for '{digit}' bounds
82  inline static bool meta(char c)
83  {
84  return
85  (
86  (c == '.') // any character
87  || (c == '*' || c == '+' || c == '?') // quantifiers
88  || (c == '(' || c == ')' || c == '|') // grouping/branching
89  || (c == '[' || c == ']') // range
90  );
91  }
92 
93 
94  // Constructors
95 
96  //- Construct null
97  regExp();
98 
99  //- Construct from character array, optionally ignoring case
100  regExp(const char*, const bool ignoreCase=false);
101 
102  //- Construct from std::string (or string), optionally ignoring case
103  regExp(const std::string&, const bool ignoreCase=false);
104 
105  // Destructor
106 
107  ~regExp();
108 
109 
110  // Member functions
111 
112  //- Access
113 
114  //- Return true if a precompiled expression does not exist
115  inline bool empty() const
116  {
117  return !preg_;
118  }
119 
120  //- Does a precompiled expression exist?
121  inline bool exists() const
122  {
123  return preg_ ? true : false;
124  }
125 
126  //- Return the number of (groups)
127  inline int ngroups() const
128  {
129  return preg_ ? preg_->re_nsub : 0;
130  }
131 
132 
133  //- Editing
134 
135  //- Compile pattern into a regular expression, optionally ignoring case
136  void set(const char*, const bool ignoreCase=false) const;
137 
138  //- Compile pattern into a regular expression, optionally ignoring case
139  void set(const std::string&, const bool ignoreCase=false) const;
140 
141 
142  //- Release precompiled expression.
143  // Returns true if precompiled expression existed before clear
144  bool clear() const;
145 
146 
147  //- Searching
148 
149  //- Find position within string.
150  // Returns the index where it begins or string::npos if not found
151  std::string::size_type find(const std::string& str) const;
152 
153  //- Return true if it matches the entire string
154  // The begin-of-line (^) and end-of-line ($) anchors are implicit
155  bool match(const std::string&) const;
156 
157  //- Return true if it matches and sets the sub-groups matched
158  // The begin-of-line (^) and end-of-line ($) anchors are implicit
159  bool match(const string&, List<string>& groups) const;
160 
161  //- Return true if the regex was found in within string
162  bool search(const std::string& str) const
163  {
164  return std::string::npos != find(str);
165  }
166 
167 
168  // Member Operators
169 
170  //- Assign and compile pattern from a character array
171  // Always case sensitive
172  void operator=(const char*);
173 
174  //- Assign and compile pattern from string
175  // Always case sensitive
176  void operator=(const std::string&);
177 
178 };
179 
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 } // End namespace Foam
184 
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
186 
187 #endif
188 
189 // ************************ vim: set sw=4 sts=4 et: ************************ //