Switch to unified view

a b/src/utils/strmatcher.h
1
/* Copyright (C) 2012 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#ifndef _STRMATCHER_H_INCLUDED_
18
#define _STRMATCHER_H_INCLUDED_
19
20
#include <string>
21
22
// Encapsulating simple wildcard/regexp string matching.
23
24
// Matcher class. Interface to either wildcard or regexp yes/no matcher
25
class StrMatcher {
26
public:
27
    StrMatcher(const std::string& exp) 
28
    : m_sexp(exp)
29
    {
30
    }
31
    virtual ~StrMatcher() {};
32
    virtual bool match(const std::string &val) const = 0;
33
    virtual std::string::size_type baseprefixlen() const = 0;
34
    virtual bool setExp(const std::string& newexp) 
35
    {
36
  m_sexp = newexp;
37
  return true;
38
    }
39
    virtual bool ok() const
40
    {
41
  return true;
42
    }
43
    virtual const std::string& exp()
44
    {
45
  return m_sexp;
46
    }
47
    virtual StrMatcher *clone() = 0;
48
    const string& getreason() 
49
    {
50
  return m_reason;
51
    }
52
protected:
53
    std::string m_sexp;
54
    std::string m_reason;
55
};
56
57
class StrWildMatcher : public StrMatcher {
58
public:
59
    StrWildMatcher(const std::string& exp)
60
    : StrMatcher(exp)
61
    {
62
    }
63
    virtual ~StrWildMatcher()
64
    {
65
    }
66
    virtual bool match(const std::string& val) const;
67
    virtual std::string::size_type baseprefixlen() const;
68
    virtual StrWildMatcher *clone()
69
    {
70
  return new StrWildMatcher(m_sexp);
71
    }
72
};
73
74
class StrRegexpMatcher : public StrMatcher {
75
public:
76
    StrRegexpMatcher(const std::string& exp);
77
    virtual bool setExp(const std::string& newexp);
78
    virtual ~StrRegexpMatcher();
79
    virtual bool match(const std::string& val) const;
80
    virtual std::string::size_type baseprefixlen() const;
81
    virtual bool ok() const;
82
    virtual StrRegexpMatcher *clone()
83
    {
84
  return new StrRegexpMatcher(m_sexp);
85
    }
86
    const string& getreason() 
87
    {
88
  return m_reason;
89
    }
90
private:
91
    void *m_compiled;
92
    bool m_errcode;
93
};
94
95
#endif /* _STRMATCHER_H_INCLUDED_ */