Statistics
| Branch: | Revision:

root / src / common / matchexpression.cc @ e1750c09

History | View | Annotate | Download (3.63 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  MATCHEXPRESSION.CC - part of
3
//                 OMNeT++/OMNEST
4
//              Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include "matchexpression.h"
18
#include "patternmatcher.h"
19
#include "commonutil.h"
20
21
USING_NAMESPACE
22
23
24
MatchExpression::Elem::Elem(PatternMatcher *pattern, const char *fieldname)
25
{
26
    type = fieldname==NULL ? PATTERN : FIELDPATTERN;
27
    this->fieldname = fieldname==NULL ? "" : fieldname;
28
    this->pattern = pattern;
29
}
30
31
MatchExpression::Elem::~Elem()
32
{
33
    if (type==PATTERN || type==FIELDPATTERN)
34
        delete pattern;
35
}
36
37
void MatchExpression::Elem::operator=(const Elem& other)
38
{
39
    type = other.type;
40
    if (type==PATTERN || type==FIELDPATTERN)
41
    {
42
        fieldname = other.fieldname;
43
        pattern = new PatternMatcher(*(other.pattern));
44
    }
45
}
46
47
MatchExpression::MatchExpression()
48
{
49
}
50
51
MatchExpression::MatchExpression(const char *pattern, bool dottedpath, bool fullstring, bool casesensitive)
52
{
53
    setPattern(pattern, dottedpath, fullstring, casesensitive);
54
}
55
56
void MatchExpression::setPattern(const char *pattern, bool dottedpath, bool fullstring, bool casesensitive)
57
{
58
    this->dottedpath = dottedpath;
59
    this->fullstring = fullstring;
60
    this->casesensitive = casesensitive;
61
62
    elems.clear();
63
64
    Assert(pattern);
65
    if (*pattern)
66
        parsePattern(elems, pattern, dottedpath, fullstring, casesensitive);
67
}
68
69
bool MatchExpression::matches(const Matchable *object)
70
{
71
    if (elems.empty())
72
        return false;
73
74
    const int stksize = 20;
75
    bool stk[stksize];
76
77
    int tos = -1;
78
    for (int i = 0; i < (int)elems.size(); i++)
79
    {
80
        Elem& e = elems[i];
81
        const char *attr;
82
        switch (e.type)
83
        {
84
          case Elem::PATTERN:
85
            if (tos>=stksize-1)
86
                throw opp_runtime_error("MatchExpression: malformed expression: stack overflow");
87
            attr = object->getDefaultAttribute();
88
            stk[++tos] = attr==NULL ? false : e.pattern->matches(attr);
89
            break;
90
          case Elem::FIELDPATTERN:
91
            if (tos>=stksize-1)
92
                throw opp_runtime_error("MatchExpression: malformed expression: stack overflow");
93
            attr = object->getAttribute(e.fieldname.c_str());
94
            stk[++tos] = attr==NULL ? false : e.pattern->matches(attr);
95
            break;
96
          case Elem::OR:
97
            if (tos<1)
98
                throw opp_runtime_error("MatchExpression: malformed expression: stack underflow");
99
            stk[tos-1] = stk[tos-1] || stk[tos];
100
            tos--;
101
            break;
102
          case Elem::AND:
103
            if (tos<1)
104
                throw opp_runtime_error("MatchExpression: malformed expression: stack underflow");
105
            stk[tos-1] = stk[tos-1] && stk[tos];
106
            tos--;
107
            break;
108
          case Elem::NOT:
109
            if (tos<0)
110
                throw opp_runtime_error("MatchExpression: malformed expression: stack underflow");
111
            stk[tos] = !stk[tos];
112
            break;
113
          default:
114
            throw opp_runtime_error("MatchExpression: malformed expression: unknown element type");
115
       }
116
    }
117
    if (tos!=0)
118
        throw opp_runtime_error("MatchExpression: malformed expression: %d items left on stack", tos);
119
120
    return stk[tos];
121
}