|
INET Framework for OMNeT++/OMNEST
|
Glob-style pattern matching class, adopted to special OMNeT++ requirements.
More...
#include <PatternMatcher.h>
|
| | PatternMatcher () |
| | Constructor. More...
|
| |
| | PatternMatcher (const char *pattern, bool dottedpath, bool fullstring, bool casesensitive) |
| | Constructor. More...
|
| |
| | ~PatternMatcher () |
| | Destructor. More...
|
| |
| void | setPattern (const char *pattern, bool dottedpath, bool fullstring, bool casesensitive) |
| | Sets the pattern to be used by subsequent calls to matches(). More...
|
| |
| bool | matches (const char *line) const |
| | Returns true if the line matches the pattern with the given settings. More...
|
| |
| const char * | patternPrefixMatches (const char *line, int suffixoffset) |
| | Similar to matches(): it returns non-nullptr iif (1) the pattern ends in a string literal (and not, say, '*' or '**') which contains the line suffix (which begins at suffixoffset characters of line) and (2) pattern matches the whole line, except that (3) in matching the pattern's last string literal, it is also accepted if line is shorter than the pattern. More...
|
| |
| std::string | debugStr () |
| | Returns the internal representation of the pattern as a string. More...
|
| |
| void | dump () |
| | Prints the internal representation of the pattern on the standard output. More...
|
| |
Glob-style pattern matching class, adopted to special OMNeT++ requirements.
One instance represents a pattern to match.
Pattern syntax:
- ? : matches any character except '.'
- * : matches zero or more characters except '.'
- ** : matches zero or more character (any character)
- {a-z} : matches a character in range a-z
- {^a-z} : matches a character NOT in range a-z
- {32..255} : any number (ie. sequence of digits) in range 32..255 (e.g. "99")
- [32..255] : any number in square brackets in range 32..255 (e.g. "[99]")
- backslash \ : takes away the special meaning of the subsequent character
The "except '.'" phrases in the above rules apply only in "dottedpath" mode (see below).
There are three option switches (see setPattern() method):
- dottedpath: dottedpath=yes is the mode used in omnetpp.ini for matching module parameters, like this: "**.mac[*].retries=9". In this mode mode, '*' cannot "eat" dot, so it can only match one component (module name) in the path. '**' can be used to match more components. (This is similar to e.g. Java Ant's usage of the asterisk.) In dottedpath=false mode, '*' will match anything.
- fullstring: selects between full string and substring match. The pattern "ate" will match "whatever" in substring mode, but not in full string mode.
- case sensitive: selects between case sensitive and case insensitive mode.
Rule details:
- sets, negated sets: They can contain several character ranges and also enumeration of characters. For example: "{_a-zA-Z0-9}","{xyzc-f}". To include '-' in the set, put it at a position where it cannot be interpreted as character range, for example: "{a-z-}" or "{-a-z}". If you want to include '}' in the set, it must be the first character: "{}a-z}", or as a negated set: "{^}a-z}". A backslash is always taken as literal backslash (and NOT as escape character) within set definitions. When doing case-insensitive match, avoid ranges that include both alpha (a-zA-Z) and non-alpha characters, because they might cause funny results.
- numeric ranges: only nonnegative integers can be matched. The start or the end of the range (or both) can be omitted: "{10..}", "{..99}" or "{..}" are valid numeric ranges (the last one matches any number). The specification must use exactly two dots. Caveat: "*{17..19}" will match "a17","117" and "963217" as well.
◆ ElemType
| Enumerator |
|---|
| LITERALSTRING | |
| ANYCHAR | |
| COMMONCHAR | |
| SET | |
| NEGSET | |
| NUMRANGE | |
| ANYSEQ | |
| COMMONSEQ | |
| END | |
◆ PatternMatcher() [1/2]
| inet::PatternMatcher::PatternMatcher |
( |
| ) |
|
◆ PatternMatcher() [2/2]
| inet::PatternMatcher::PatternMatcher |
( |
const char * |
pattern, |
|
|
bool |
dottedpath, |
|
|
bool |
fullstring, |
|
|
bool |
casesensitive |
|
) |
| |
◆ ~PatternMatcher()
| inet::PatternMatcher::~PatternMatcher |
( |
| ) |
|
◆ containsWildcards()
| bool inet::PatternMatcher::containsWildcards |
( |
const char * |
pattern | ) |
|
|
static |
Utility function to determine whether a given string contains wildcards.
If it does not, a simple strcmp() might be a faster option than using PatternMatcher.
◆ debugStr()
| std::string inet::PatternMatcher::debugStr |
( |
| ) |
|
|
inline |
Returns the internal representation of the pattern as a string.
May be useful for debugging purposes.
◆ debugStrFrom()
| std::string inet::PatternMatcher::debugStrFrom |
( |
int |
from | ) |
|
|
private |
179 for (
size_t k = from;
k <
pattern.size();
k++) {
183 result = result +
"\"" +
e.literalstring +
"\"";
195 result = result +
"SET(" +
e.setchars +
")";
199 result = result +
"NEGSET(" +
e.setchars +
")";
204 sprintf(buf,
"%ld..%ld",
e.fromnum,
e.tonum);
◆ doMatch()
| bool inet::PatternMatcher::doMatch |
( |
const char * |
line, |
|
|
int |
patternpos, |
|
|
int |
suffixlen |
|
) |
| const |
|
private |
249 len =
e.literalstring.length();
251 if (suffixlen > 0 &&
k == (
int)
pattern.size() - 2)
254 if (
iscasesensitive ? strncmp(
s,
e.literalstring.c_str(), len) : strncasecmp(
s,
e.literalstring.c_str(), len))
266 if (!*
s || *
s ==
'.')
293 if ((
e.fromnum >= 0 && num <
e.fromnum) || (
e.tonum >= 0 && num >
e.tonum))
304 return opp_stringendswith(
s,
pattern[
k + 1].literalstring.c_str());
320 if (!*
s || *
s ==
'.')
Referenced by matches(), and patternPrefixMatches().
◆ dump()
| void inet::PatternMatcher::dump |
( |
| ) |
|
|
inline |
Prints the internal representation of the pattern on the standard output.
May be useful for debugging purposes.
167 { printf(
"%s",
debugStr().c_str()); }
◆ isInSet()
| bool inet::PatternMatcher::isInSet |
( |
char |
c, |
|
|
const char * |
set |
|
) |
| const |
|
private |
230 ASSERT((strlen(set) & 1) == 0);
234 if (
c >= *set &&
c <= *(set + 1))
Referenced by doMatch().
◆ matches()
| bool inet::PatternMatcher::matches |
( |
const char * |
line | ) |
const |
◆ parseLiteralString()
| void inet::PatternMatcher::parseLiteralString |
( |
const char *& |
s, |
|
|
Elem & |
e |
|
) |
| |
|
private |
134 while (*
s && *
s !=
'?' && *
s !=
'{' && *
s !=
'*') {
138 e.literalstring += *(++
s);
140 e.literalstring += *
s;
Referenced by setPattern().
◆ parseNumRange() [1/2]
| void inet::PatternMatcher::parseNumRange |
( |
const char *& |
s, |
|
|
Elem & |
e |
|
) |
| |
|
private |
◆ parseNumRange() [2/2]
| bool inet::PatternMatcher::parseNumRange |
( |
const char *& |
str, |
|
|
char |
closingchar, |
|
|
long & |
lo, |
|
|
long & |
up |
|
) |
| |
|
private |
156 const char *
s = str + 1;
162 if (*
s !=
'.' || *(
s + 1) !=
'.')
170 if (*
s != closingchar)
◆ parseSet()
| void inet::PatternMatcher::parseSet |
( |
const char *& |
s, |
|
|
Elem & |
e |
|
) |
| |
|
private |
104 const char *sbeg =
s;
105 while (*
s && (*
s !=
'}' ||
s == sbeg)) {
108 if (*(
s + 1) ==
'-' && *(
s + 2) && *(
s + 2) !=
'}') {
116 range[0] = range[1] = *
s;
127 throw cRuntimeError(
"unmatched '}' in expression");
Referenced by setPattern().
◆ patternPrefixMatches()
| const char * inet::PatternMatcher::patternPrefixMatches |
( |
const char * |
line, |
|
|
int |
suffixoffset |
|
) |
| |
Similar to matches(): it returns non-nullptr iif (1) the pattern ends in a string literal (and not, say, '*' or '**') which contains the line suffix (which begins at suffixoffset characters of line) and (2) pattern matches the whole line, except that (3) in matching the pattern's last string literal, it is also accepted if line is shorter than the pattern.
If the above conditions hold, it returns the rest of the pattern. The returned pointer is valid until the next call to this method.
This method is used by cIniFile's getEntriesWithPrefix(), used e.g. to find RNG mapping entries for a module. For that, we have to find all ini file entries (keys) like "net.host1.gen.rng-NN" where NN=0,1,2,... In cIniFile, every entry is a pattern ("**.host*.gen.rng-1", "**.*.gen.rng-0", etc.). So we'd invoke patternPrefixMatches("net.host1.gen.rng-", 13) (i.e. suffix=".rng-") to find those entries (patterns) which can expand to "net.host1.gen.rng-0", "net.host1.gen.rng-1", etc.
See matches().
367 throw cRuntimeError(
"PatternMatcher: patternPrefixMatches() doesn't support case-insensitive match");
378 const char *pattstring =
e.literalstring.c_str();
379 const char *p = strstr(pattstring, line + suffixoffset);
382 p += strlen(line + suffixoffset);
384 int pattsuffixlen =
e.literalstring.size() - (p - pattstring);
387 return doMatch(line, 0, pattsuffixlen) ?
rest.c_str() :
nullptr;
◆ setPattern()
| void inet::PatternMatcher::setPattern |
( |
const char * |
pattern, |
|
|
bool |
dottedpath, |
|
|
bool |
fullstring, |
|
|
bool |
casesensitive |
|
) |
| |
Sets the pattern to be used by subsequent calls to matches().
See the general class description for the meaning of the rest of the arguments. Throws cException if the pattern is bogus.
64 if (*(
s + 1) ==
'*') {
Referenced by PatternMatcher().
◆ iscasesensitive
| bool inet::PatternMatcher::iscasesensitive = false |
|
private |
◆ pattern
| std::vector<Elem> inet::PatternMatcher::pattern |
|
private |
◆ rest
| std::string inet::PatternMatcher::rest |
|
private |
The documentation for this class was generated from the following files:
const value< double, compose< units::m, pow< units::s, -1 > > > c(299792458)
@ COMMONSEQ
Definition: PatternMatcher.h:80
@ LITERALSTRING
Definition: PatternMatcher.h:73
std::string debugStr()
Returns the internal representation of the pattern as a string.
Definition: PatternMatcher.h:161
@ ANYSEQ
Definition: PatternMatcher.h:79
void parseNumRange(const char *&s, Elem &e)
const value< double, units::C > e(1.602176487e-19)
void setPattern(const char *pattern, bool dottedpath, bool fullstring, bool casesensitive)
Sets the pattern to be used by subsequent calls to matches().
Definition: PatternMatcher.cc:32
char opp_toupper(unsigned char c)
Definition: PatternMatcher.cc:17
bool doMatch(const char *line, int patternpos, int suffixlen) const
Definition: PatternMatcher.cc:241
@ NUMRANGE
Definition: PatternMatcher.h:78
std::string debugStrFrom(int from)
Definition: PatternMatcher.cc:176
value< double, units::s > s
Definition: Units.h:1235
bool isInSet(char c, const char *set) const
Definition: PatternMatcher.cc:228
bool opp_isdigit(unsigned char c)
Definition: PatternMatcher.cc:16
std::vector< Elem > pattern
Definition: PatternMatcher.h:91
@ ANYCHAR
Definition: PatternMatcher.h:74
std::string rest
Definition: PatternMatcher.h:94
bool iscasesensitive
Definition: PatternMatcher.h:92
const double k
Definition: Qam1024Modulation.cc:14
void parseLiteralString(const char *&s, Elem &e)
Definition: PatternMatcher.cc:131
@ COMMONCHAR
Definition: PatternMatcher.h:75
void parseSet(const char *&s, Elem &e)
Definition: PatternMatcher.cc:95
@ SET
Definition: PatternMatcher.h:76
removed DscpReq Ipv4ControlInfo up
Definition: IUdp-gates.txt:14
@ NEGSET
Definition: PatternMatcher.h:77
@ END
Definition: PatternMatcher.h:81