initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OSspecific / POSIX / regExp.C
blob44b68eb13d107cd2d6fcf832a4c45bca13cf0e9c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include <sys/types.h>
29 #include "regExp.H"
30 #include "label.H"
31 #include "string.H"
32 #include "List.H"
33 #include "IOstreams.H"
35 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
37 Foam::regExp::regExp()
39     preg_(0)
43 Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
45     preg_(0)
47     set(pattern, ignoreCase);
51 Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
53     preg_(0)
55     set(pattern.c_str(), ignoreCase);
59 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
61 Foam::regExp::~regExp()
63     clear();
67 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
69 void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
71     clear();
73     // avoid NULL pointer and zero-length patterns
74     if (pattern && *pattern)
75     {
76         preg_ = new regex_t;
78         int cflags = REG_EXTENDED;
79         if (ignoreCase)
80         {
81             cflags |= REG_ICASE;
82         }
84         if (regcomp(preg_, pattern, cflags) != 0)
85         {
86             FatalErrorIn
87             (
88                 "regExp::set(const char*)"
89             )   << "Failed to compile regular expression '" << pattern << "'"
90                 << exit(FatalError);
91         }
92     }
96 void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
98     return set(pattern.c_str(), ignoreCase);
102 bool Foam::regExp::clear() const
104     if (preg_)
105     {
106         regfree(preg_);
107         delete preg_;
108         preg_ = 0;
110         return true;
111     }
113     return false;
117 std::string::size_type Foam::regExp::find(const std::string& str) const
119     if (preg_ && str.size())
120     {
121         size_t nmatch = 1;
122         regmatch_t pmatch[1];
124         if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
125         {
126             return pmatch[0].rm_so;
127         }
128     }
130     return string::npos;
134 bool Foam::regExp::match(const std::string& str) const
136     if (preg_ && str.size())
137     {
138         size_t nmatch = 1;
139         regmatch_t pmatch[1];
141         // also verify that the entire string was matched
142         // pmatch[0] is the entire match
143         if
144         (
145             regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
146          && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
147         )
148         {
149             return true;
150         }
151     }
153     return false;
157 bool Foam::regExp::match(const string& str, List<string>& groups) const
159     if (preg_ && str.size())
160     {
161         size_t nmatch = ngroups() + 1;
162         regmatch_t pmatch[nmatch];
164         // also verify that the entire string was matched
165         // pmatch[0] is the entire match
166         // pmatch[1..] are the (...) sub-groups
167         if
168         (
169             regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
170          && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
171         )
172         {
173             groups.setSize(ngroups());
174             label groupI = 0;
176             for (size_t matchI = 1; matchI < nmatch; matchI++)
177             {
178                 if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
179                 {
180                     groups[groupI] = str.substr
181                     (
182                         pmatch[matchI].rm_so,
183                         pmatch[matchI].rm_eo - pmatch[matchI].rm_so
184                     );
185                 }
186                 else
187                 {
188                     groups[groupI].clear();
189                 }
190                 groupI++;
191             }
193             return true;
194         }
195     }
197     groups.clear();
198     return false;
202 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
204 void Foam::regExp::operator=(const char* pat)
206     set(pat);
210 void Foam::regExp::operator=(const std::string& pat)
212     set(pat);
216 // ************************************************************************* //