regExp.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00060
00061 Foam::regExp::~regExp()
00062 {
00063 clear();
00064 }
00065
00066
00067
00068
00069 void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
00070 {
00071 clear();
00072
00073
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
00142
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
00165
00166
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
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