1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include <sys/types.h>
32 #include "IOstreams.H"
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 Foam::regExp::regExp()
42 Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
46 set(pattern, ignoreCase);
50 Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
54 set(pattern.c_str(), ignoreCase);
58 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
60 Foam::regExp::~regExp()
66 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
68 void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
72 // avoid NULL pointer and zero-length patterns
73 if (pattern && *pattern)
77 int cflags = REG_EXTENDED;
83 if (regcomp(preg_, pattern, cflags) != 0)
87 "regExp::set(const char*)"
88 ) << "Failed to compile regular expression '" << pattern << "'"
95 void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
97 return set(pattern.c_str(), ignoreCase);
101 bool Foam::regExp::clear() const
116 std::string::size_type Foam::regExp::find(const std::string& str) const
118 if (preg_ && str.size())
121 regmatch_t pmatch[1];
123 if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
125 return pmatch[0].rm_so;
133 bool Foam::regExp::match(const std::string& str) const
135 if (preg_ && str.size())
138 regmatch_t pmatch[1];
140 // also verify that the entire string was matched
141 // pmatch[0] is the entire match
144 regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
145 && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
156 bool Foam::regExp::match(const string& str, List<string>& groups) const
158 if (preg_ && str.size())
160 size_t nmatch = ngroups() + 1;
161 regmatch_t pmatch[nmatch];
163 // also verify that the entire string was matched
164 // pmatch[0] is the entire match
165 // pmatch[1..] are the (...) sub-groups
168 regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
169 && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
172 groups.setSize(ngroups());
175 for (size_t matchI = 1; matchI < nmatch; matchI++)
177 if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
179 groups[groupI] = str.substr
181 pmatch[matchI].rm_so,
182 pmatch[matchI].rm_eo - pmatch[matchI].rm_so
187 groups[groupI].clear();
201 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
203 void Foam::regExp::operator=(const char* pat)
209 void Foam::regExp::operator=(const std::string& pat)
215 // ************************************************************************* //