KiCad PCB EDA Suite
eda_pattern_match.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2017 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
29 #ifndef EDA_PATTERN_MATCH_H
30 #define EDA_PATTERN_MATCH_H
31 
32 #include <vector>
33 #include <map>
34 #include <memory>
35 #include <wx/wx.h>
36 #include <wx/string.h>
37 #include <wx/regex.h>
38 
39 static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
40 
41 /*
42  * Interface for a pattern matcher, for which there are several implementations
43  */
45 {
46 public:
47  virtual ~EDA_PATTERN_MATCH() {}
48 
53  virtual bool SetPattern( const wxString& aPattern ) = 0;
54 
58  virtual wxString const& GetPattern() const = 0;
59 
64  virtual int Find( const wxString& aCandidate ) const = 0;
65 };
66 
67 
68 /*
69  * Match simple substring
70  */
72 {
73 public:
74  virtual bool SetPattern( const wxString& aPattern ) override;
75  virtual wxString const& GetPattern() const override;
76  virtual int Find( const wxString& aCandidate ) const override;
77 
78 protected:
79  wxString m_pattern;
80 };
81 
82 
83 /*
84  * Match regular expression
85  */
87 {
88 public:
89  virtual bool SetPattern( const wxString& aPattern ) override;
90  virtual wxString const& GetPattern() const override;
91  virtual int Find( const wxString& aCandidate ) const override;
92 
93 protected:
94  wxString m_pattern;
95  wxRegEx m_regex;
96 };
97 
98 
100 {
101 public:
102  virtual bool SetPattern( const wxString& aPattern ) override;
103  virtual wxString const& GetPattern() const override;
104  virtual int Find( const wxString& aCandidate ) const override;
105 
106 protected:
108 };
109 
110 
112 {
113 public:
114  virtual bool SetPattern( const wxString& aPattern ) override;
115 };
116 
117 
132 {
133 public:
134  virtual bool SetPattern( const wxString& aPattern ) override;
135  virtual wxString const& GetPattern() const override;
136  virtual int Find( const wxString& aCandidate ) const override;
137  int FindOne( const wxString& aCandidate ) const;
138 
139 protected:
140 
141  enum RELATION { LT, LE, EQ, GE, GT, NONE };
142 
143  wxString m_pattern;
144  wxString m_key;
146  double m_value;
147 
148  static wxRegEx m_regex_description;
149  static wxRegEx m_regex_search;
150  static const std::map<wxString, double> m_units;
151 };
152 
153 
155 {
156 public:
157  EDA_COMBINED_MATCHER( const wxString &aPattern );
158 
159  /*
160  * Look in all existing matchers, return the earliest match of any of
161  * the existing.
162  *
163  * @param aTerm term to look for
164  * @param aMatchersTriggered out: number of matcher that found the term
165  * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
166  *
167  * @return true if any matchers found the term
168  */
169  bool Find( const wxString &aTerm, int& aMatchersTriggered, int& aPosition );
170 
171  wxString const& GetPattern() const;
172 
173 private:
174  // Add matcher if it can compile the pattern.
175  void AddMatcher( const wxString &aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
176 
177  std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
178  wxString m_pattern;
179 };
180 
181 #endif // EDA_PATTERN_MATCH_H
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
virtual bool SetPattern(const wxString &aPattern)=0
Set the pattern against which candidates will be matched.
virtual ~EDA_PATTERN_MATCH()
virtual int Find(const wxString &aCandidate) const =0
Return the location of a match iff a given candidate string matches the set pattern.
static const int EDA_PATTERN_NOT_FOUND
virtual wxString const & GetPattern() const =0
Return the pattern passed to SetPattern().
static const std::map< wxString, double > m_units