FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <sys/types.h>
00027 
00028 #include "regExp.H"
00029 #include <OpenFOAM/label.H>
00030 #include <OpenFOAM/string.H>
00031 #include <OpenFOAM/List.H>
00032 #include <OpenFOAM/IOstreams.H>
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 Foam::regExp::regExp()
00037 :
00038     preg_(0)
00039 {}
00040 
00041 
00042 Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
00043 :
00044     preg_(0)
00045 {
00046     set(pattern, ignoreCase);
00047 }
00048 
00049 
00050 Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
00051 :
00052     preg_(0)
00053 {
00054     set(pattern.c_str(), ignoreCase);
00055 }
00056 
00057 
00058 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00059 
00060 Foam::regExp::~regExp()
00061 {
00062     clear();
00063 }
00064 
00065 
00066 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00067 
00068 void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
00069 {
00070     clear();
00071 
00072     // avoid NULL pointer and zero-length patterns
00073     if (pattern && *pattern)
00074     {
00075         preg_ = new regex_t;
00076 
00077         int cflags = REG_EXTENDED;
00078         if (ignoreCase)
00079         {
00080             cflags |= REG_ICASE;
00081         }
00082 
00083         if (regcomp(preg_, pattern, cflags) != 0)
00084         {
00085             FatalErrorIn
00086             (
00087                 "regExp::set(const char*)"
00088             )   << "Failed to compile regular expression '" << pattern << "'"
00089                 << exit(FatalError);
00090         }
00091     }
00092 }
00093 
00094 
00095 void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
00096 {
00097     return set(pattern.c_str(), ignoreCase);
00098 }
00099 
00100 
00101 bool Foam::regExp::clear() const
00102 {
00103     if (preg_)
00104     {
00105         regfree(preg_);
00106         delete preg_;
00107         preg_ = 0;
00108 
00109         return true;
00110     }
00111 
00112     return false;
00113 }
00114 
00115 
00116 std::string::size_type Foam::regExp::find(const std::string& str) const
00117 {
00118     if (preg_ && str.size())
00119     {
00120         size_t nmatch = 1;
00121         regmatch_t pmatch[1];
00122 
00123         if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
00124         {
00125             return pmatch[0].rm_so;
00126         }
00127     }
00128 
00129     return string::npos;
00130 }
00131 
00132 
00133 bool Foam::regExp::match(const std::string& str) const
00134 {
00135     if (preg_ && str.size())
00136     {
00137         size_t nmatch = 1;
00138         regmatch_t pmatch[1];
00139 
00140         // also verify that the entire string was matched
00141         // pmatch[0] is the entire match
00142         if
00143         (
00144             regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
00145          && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
00146         )
00147         {
00148             return true;
00149         }
00150     }
00151 
00152     return false;
00153 }
00154 
00155 
00156 bool Foam::regExp::match(const string& str, List<string>& groups) const
00157 {
00158     if (preg_ && str.size())
00159     {
00160         size_t nmatch = ngroups() + 1;
00161         regmatch_t pmatch[nmatch];
00162 
00163         // also verify that the entire string was matched
00164         // pmatch[0] is the entire match
00165         // pmatch[1..] are the (...) sub-groups
00166         if
00167         (
00168             regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
00169          && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
00170         )
00171         {
00172             groups.setSize(ngroups());
00173             label groupI = 0;
00174 
00175             for (size_t matchI = 1; matchI < nmatch; matchI++)
00176             {
00177                 if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
00178                 {
00179                     groups[groupI] = str.substr
00180                     (
00181                         pmatch[matchI].rm_so,
00182                         pmatch[matchI].rm_eo - pmatch[matchI].rm_so
00183                     );
00184                 }
00185                 else
00186                 {
00187                     groups[groupI].clear();
00188                 }
00189                 groupI++;
00190             }
00191 
00192             return true;
00193         }
00194     }
00195 
00196     groups.clear();
00197     return false;
00198 }
00199 
00200 
00201 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
00202 
00203 void Foam::regExp::operator=(const char* pat)
00204 {
00205     set(pat);
00206 }
00207 
00208 
00209 void Foam::regExp::operator=(const std::string& pat)
00210 {
00211     set(pat);
00212 }
00213 
00214 
00215 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines