Statistics
| Branch: | Revision:

root / src / common / matchexpression.h @ master

History | View | Annotate | Download (4.96 KB)

1
//==========================================================================
2
//  MATCHEXPRESSION.H - 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

    
18
#ifndef __MATCHEXPRESSION_H__
19
#define __MATCHEXPRESSION_H__
20

    
21
#include <string>
22
#include <vector>
23
#include "commondefs.h"
24
#include "exception.h"
25

    
26
NAMESPACE_BEGIN
27

    
28
class PatternMatcher;
29

    
30

    
31
/**
32
 * Matches various fields of an object. By default, a pattern must match
33
 * the "default field" of the object, which will usually be its name.
34
 * Other fields can be matched with the fieldname(pattern) syntax.
35
 * These elements can be combined with the AND, OR, NOT operators, accepted in
36
 * both lowercase and uppercase. AND has higher precedence than OR, but
37
 * parentheses can be used to change the evaluation order.
38
 *
39
 * Patterns are those accepted by PatternMatcher, that is, "*", "?",
40
 * character ranges as "{a-z}", numeric ranges as "{0..999}", or bracketed
41
 * numeric ranges as "[0..999]" (e.g. "*[90..100] matching "foo[95]")
42
 * are accepted.
43
 *
44
 * Pattern examples:
45
 *  - "" matches nothing
46
 *  - "node*"
47
 *  - "node* or host*"
48
 *  - "node* or host* or className(StandardHost*)"
49
 *  - "packet-* and className(PPPFrame)"
50
 *  - "className(TCPSegment) and not kind({0..2}) and SYN or data-*"
51
 *  - "className(TCPSegment) or byteLength({4096..})
52
 */
53
class COMMON_API MatchExpression
54
{
55
  public:
56
    /**
57
     * Objects to be matched must implement this interface
58
     */
59
    class COMMON_API Matchable
60
    {
61
      public:
62
        /**
63
         * Return the default string to match. The returned pointer will not
64
         * be cached by the caller, so it is OK to return a pointer to some
65
         * internal buffer which gets overwritten by subsequent
66
         * getDefaultAttribute() / getAttribute() calls.
67
         */
68
        virtual const char *getDefaultAttribute() const = 0;
69

    
70
        /**
71
         * Return the default string to match. Should return NULL if attribute
72
         * doesn't exist. The returned pointer will not be cached by the caller,
73
         * so it is OK to return a pointer to some internal buffer which gets
74
         * overwritten by subsequent getDefaultAttribute() / getAttribute() calls.
75
         */
76
        virtual const char *getAttribute(const char *name) const = 0;
77

    
78
        /**
79
         * Virtual destructor, for good manners...
80
         */
81
        virtual ~Matchable() {}
82
    };
83

    
84
    /**
85
     * One element in a (reverse Polish) expression
86
     */
87
    class COMMON_API Elem
88
    {
89
      public:
90
        friend class MatchExpression;
91
        enum Type {UNDEF, AND, OR, NOT, PATTERN, FIELDPATTERN};
92
      private:
93
        Type type;
94
        std::string fieldname;
95
        PatternMatcher *pattern;
96
      public:
97
        /** Ctor for AND, OR, NOT */
98
        Elem(Type type=UNDEF)  {this->type = type;}
99

    
100
        /** The given field of the object must match pattern */
101
        Elem(PatternMatcher *pattern, const char *fieldname=NULL);
102

    
103
        /** Copy ctor */
104
        Elem(const Elem& other)  {type=UNDEF; operator=(other);}
105

    
106
        /** Dtor */
107
        ~Elem();
108

    
109
        /** Assignment */
110
        void operator=(const Elem& other);
111
    };
112

    
113
  protected:
114
    // options
115
    bool dottedpath;
116
    bool fullstring;
117
    bool casesensitive;
118

    
119
    // stores the expression
120
    std::vector<Elem> elems;
121

    
122
  protected:
123
    // internal: access to the parser
124
    static void parsePattern(std::vector<MatchExpression::Elem>& elems, const char *pattern,
125
                             bool dottedpath, bool fullstring, bool casesensitive);
126

    
127
  public:
128
    /**
129
     * Constructor
130
     */
131
    MatchExpression();
132

    
133
    /**
134
     * Constructor, accepts the same args as setPattern().
135
     */
136
    MatchExpression(const char *pattern, bool dottedpath, bool fullstring, bool casesensitive);
137

    
138
    /**
139
     * Sets the pattern to be used by subsequent calls to matches(). See the
140
     * general class description for the meaning of the rest of the arguments.
141
     * Throws cException if the pattern is bogus.
142
     */
143
    void setPattern(const char *pattern, bool dottedpath, bool fullstring, bool casesensitive);
144

    
145
    /**
146
     * Returns true if the line matches the pattern with the given settings.
147
     * See setPattern().
148
     */
149
    bool matches(const Matchable *object);
150
};
151

    
152

    
153
/**
154
 * Wrapper to make a string matchable with MatchExpression.
155
 */
156
class COMMON_API MatchableString : public MatchExpression::Matchable
157
{
158
  private:
159
    std::string str;
160
  public:
161
    MatchableString(const char *s) {str = s;}
162
    virtual const char *getDefaultAttribute() const {return str.c_str();}
163
    virtual const char *getAttribute(const char *name) const {return NULL;}
164
};
165

    
166
NAMESPACE_END
167

    
168

    
169
#endif
170

    
171