OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

regExp.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include <sys/types.h>
00028 
00029 #include "regExp.H"
00030 #include "label.H"
00031 #include "string.H"
00032 #include "List.H"
00033 #include "IOstreams.H"
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 Foam::regExp::regExp()
00038 :
00039     preg_(0)
00040 {}
00041 
00042 
00043 Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
00044 :
00045     preg_(0)
00046 {
00047     set(pattern, ignoreCase);
00048 }
00049 
00050 
00051 Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
00052 :
00053     preg_(0)
00054 {
00055     set(pattern.c_str(), ignoreCase);
00056 }
00057 
00058 
00059 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00060 
00061 Foam::regExp::~regExp()
00062 {
00063     clear();
00064 }
00065 
00066 
00067 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00068 
00069 void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
00070 {
00071     clear();
00072 
00073     // avoid NULL pointer and zero-length patterns
00074     if (pattern && *pattern)
00075     {
00076         preg_ = new regex_t;
00077 
00078         int cflags = REG_EXTENDED;
00079         if (ignoreCase)
00080         {
00081             cflags |= REG_ICASE;
00082         }
00083 
00084         if (regcomp(preg_, pattern, cflags) != 0)
00085         {
00086             FatalErrorIn
00087             (
00088                 "regExp::set(const char*)"
00089             )   << "Failed to compile regular expression '" << pattern << "'"
00090                 << exit(FatalError);
00091         }
00092     }
00093 }
00094 
00095 
00096 void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
00097 {
00098     return set(pattern.c_str(), ignoreCase);
00099 }
00100 
00101 
00102 bool Foam::regExp::clear() const
00103 {
00104     if (preg_)
00105     {
00106         regfree(preg_);
00107         delete preg_;
00108         preg_ = 0;
00109 
00110         return true;
00111     }
00112 
00113     return false;
00114 }
00115 
00116 
00117 std::string::size_type Foam::regExp::find(const std::string& str) const
00118 {
00119     if (preg_ && str.size())
00120     {
00121         size_t nmatch = 1;
00122         regmatch_t pmatch[1];
00123 
00124         if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
00125         {
00126             return pmatch[0].rm_so;
00127         }
00128     }
00129 
00130     return string::npos;
00131 }
00132 
00133 
00134 bool Foam::regExp::match(const std::string& str) const
00135 {
00136     if (preg_ && str.size())
00137     {
00138         size_t nmatch = 1;
00139         regmatch_t pmatch[1];
00140 
00141         // also verify that the entire string was matched
00142         // pmatch[0] is the entire match
00143         if
00144         (
00145             regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
00146          && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
00147         )
00148         {
00149             return true;
00150         }
00151     }
00152 
00153     return false;
00154 }
00155 
00156 
00157 bool Foam::regExp::match(const string& str, List<string>& groups) const
00158 {
00159     if (preg_ && str.size())
00160     {
00161         size_t nmatch = ngroups() + 1;
00162         regmatch_t pmatch[nmatch];
00163 
00164         // also verify that the entire string was matched
00165         // pmatch[0] is the entire match
00166         // pmatch[1..] are the (...) sub-groups
00167         if
00168         (
00169             regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
00170          && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
00171         )
00172         {
00173             groups.setSize(ngroups());
00174             label groupI = 0;
00175 
00176             for (size_t matchI = 1; matchI < nmatch; matchI++)
00177             {
00178                 if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
00179                 {
00180                     groups[groupI] = str.substr
00181                     (
00182                         pmatch[matchI].rm_so,
00183                         pmatch[matchI].rm_eo - pmatch[matchI].rm_so
00184                     );
00185                 }
00186                 else
00187                 {
00188                     groups[groupI].clear();
00189                 }
00190                 groupI++;
00191             }
00192 
00193             return true;
00194         }
00195     }
00196 
00197     groups.clear();
00198     return false;
00199 }
00200 
00201 
00202 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
00203 
00204 void Foam::regExp::operator=(const char* pat)
00205 {
00206     set(pat);
00207 }
00208 
00209 
00210 void Foam::regExp::operator=(const std::string& pat)
00211 {
00212     set(pat);
00213 }
00214 
00215 
00216 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd