Commit d017697b authored by Mark Olesen's avatar Mark Olesen
Browse files

regExp class added - enhanced and with minor bugfixes

parent b94c3fb8
regexTest.C
EXE = $(FOAM_USER_APPBIN)/regexTest
EXE_LIBS = \
$(FOAM_LIBBIN)/libOSspecific.o
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
\*---------------------------------------------------------------------------*/
#include "IOstreams.H"
#include "IOobject.H"
#include "IFstream.H"
#include "regExp.H"
#include "List.H"
#include "Tuple2.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
List<Tuple2<string, string> > rawList(IFstream("testRegexps")());
Info<< "input list:" << rawList << endl;
IOobject::writeDivider(Info);
Info<< endl;
List<string> groups;
// report matches:
forAll(rawList, elemI)
{
const string& pat = rawList[elemI].first();
const string& str = rawList[elemI].second();
regExp re(pat);
Info<< str << " =~ m/" << pat.c_str() << "/ == ";
if (re.match(str, groups))
{
Info<< "true";
if (re.ngroups())
{
Info<< groups;
}
}
else
{
Info<< "false";
if (re.match(str, true))
{
Info<< " partial match";
}
}
Info << endl;
}
Info<< endl;
return 0;
}
// ************************************************************************* //
/*-------------------------------*- C++ -*---------------------------------*\
| ========= |
| \\ / OpenFOAM |
| \\ / |
| \\ / The Open Source CFD Toolbox |
| \\/ http://www.OpenFOAM.org |
\*-------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// pattern, string
(
( "a.*" "abc" )
( "a.*" "bac" )
( "a.*" "abcd" )
( "a.*" "def" )
( "d(.*)f" "def" )
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -2,6 +2,7 @@ signals/sigFpe.C
signals/sigSegv.C
signals/sigInt.C
signals/sigQuit.C
regExp.C
timer.C
fileStat.C
Unix.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include <sys/types.h>
#include "regExp.H"
#include "label.H"
#include "string.H"
#include "List.H"
#include "IOstreams.H"
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
void Foam::regExp::compile(const char* pat) const
{
clear();
preg_ = new regex_t;
if (regcomp(preg_, pat, REG_EXTENDED) != 0)
{
FatalErrorIn
(
"regExp::compile(const char*)"
) << "Failed to compile regular expression '" << pat << "'"
<< exit(FatalError);
}
}
void Foam::regExp::clear() const
{
if (preg_)
{
regfree(preg_);
delete preg_;
preg_ = 0;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::regExp::regExp()
:
preg_(0)
{}
Foam::regExp::regExp(const string& pat)
:
preg_(0)
{
compile(pat.c_str());
}
Foam::regExp::regExp(const char* pat)
:
preg_(0)
{
compile(pat);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::regExp::~regExp()
{
clear();
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
int Foam::regExp::ngroups() const
{
return preg_ ? preg_->re_nsub : 0;
}
bool Foam::regExp::match
(
const string& str,
bool partialMatch
) const
{
if (preg_ && str.size())
{
size_t nmatch = 1;
regmatch_t pmatch[1];
// match and also verify that the entire string was matched
if
(
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&&
(
partialMatch
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
)
{
return true;
}
}
return false;
}
bool Foam::regExp::match
(
const string& str,
List<string>& groups,
bool partialMatch
) const
{
if (preg_ && str.size())
{
size_t nmatch = ngroups() + 1;
regmatch_t pmatch[nmatch];
// match and also verify that the entire string was matched
if
(
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&&
(
partialMatch
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
)
{
groups.setSize(ngroups());
label groupI = 0;
for (size_t matchI = 1; matchI < nmatch; matchI++)
{
if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
{
groups[groupI] = str.substr
(
pmatch[matchI].rm_so,
pmatch[matchI].rm_eo - pmatch[matchI].rm_so
);
}
else
{
groups[groupI].clear();
}
groupI++;
}
return true;
}
}
groups.clear();
return false;
}
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
void Foam::regExp::operator=(const string& pat)
{
compile(pat.c_str());
}
void Foam::regExp::operator=(const char* pat)
{
compile(pat);
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::regExp
Description
Wrapper around regular expressions.
The beginning-of-line and end-of-line anchors are added by default.
SourceFiles
regExp.C
\*---------------------------------------------------------------------------*/
#ifndef regExp_H
#define regExp_H
#include <regex.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class string;
template<class T> class List;
/*---------------------------------------------------------------------------*\
Class regExp Declaration
\*---------------------------------------------------------------------------*/
class regExp
{
// Private data
//- Precompiled regular expression
mutable regex_t* preg_;
// Private member functions
//- release allocated space
void clear() const;
//- compile into a regular expression
void compile(const char*) const;
//- Disallow default bitwise copy construct
regExp(const regExp&);
//- Disallow default bitwise assignment
void operator=(const regExp&);
public:
// Constructors
//- Construct null
regExp();
//- Construct from string
regExp(const string&);
//- Construct from character array
regExp(const char*);
// Destructor
~regExp();
// Member functions
//- Return the number of (groups)
int ngroups() const;
//- Return true if matches, partial matches are optional
bool match
(
const string&,
bool partialMatch=false
) const;
//- Return true if matches and sets sub-groups matched,
// partial matches are optional
bool match
(
const string&,
List<string>& groups,
bool partialMatch=false
) const;
// Member Operators
//- Assign from a string
void operator=(const string&);
//- Assign from a character array
void operator=(const char*);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment