Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
dynashell_regex.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Dynamixel
4 //
5 // Program: dynashell
6 //
7 // File: dynashell_regex.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-01-12 10:56:06 -0700 (Mon, 12 Jan 2015) $
12  * $Rev: 3845 $
13  *
14  * \brief The Dynamixel Shell Regular Expression Class.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2012-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  *
26  * Unless otherwise stated explicitly, all materials contained are copyrighted
27  * and may not be used without RoadNarrows LLC's written consent,
28  * except as provided in these terms and conditions or in the copyright
29  * notice (documents and software) or other proprietary notice provided with
30  * the relevant materials.
31  *
32  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
33  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
34  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
35  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
36  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
40  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
41  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
42  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
43  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
44  *
45  * @EulaEnd@
46  */
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #ifndef _DYNASHELL_REGEX_H
50 #define _DYNASHELL_REGEX_H
51 
52 #include <sys/types.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <regex.h>
57 #include <string>
58 
59 #include <rnr/rnrconfig.h>
60 
61 using namespace std;
62 
63 // ----------------------------------------------------------------------------
64 // Class RegEx
65 // ----------------------------------------------------------------------------
66 
67 /*!
68  * \brief Regular Express Class.
69  */
70 class RegEx
71 {
72 public:
73  /*!
74  * \brief Default constructor.
75  */
77  {
78  m_bIsValid = false;
79  memset(&m_regex, 0, sizeof(m_regex));
80  }
81 
82  /*!
83  * \brief String initialization constructor.
84  */
85  RegEx(const string &strRegEx)
86  {
87  m_strRegEx = strRegEx;
88  Compile();
89  }
90 
91  /*!
92  * \brief Null-terminated string initialization constructor.
93  */
94  RegEx(const char *sRegEx)
95  {
96  if( sRegEx != NULL )
97  {
98  m_strRegEx = sRegEx;
99  Compile();
100  }
101  else
102  {
103  m_bIsValid = false;
104  }
105  }
106 
107  /*!
108  * \brief Copy constructor.
109  */
110  RegEx(const RegEx &src)
111  {
112  m_strRegEx = src.m_strRegEx;
113  Compile();
114  }
115 
116  /*!
117  * \brief Default destructor.
118  */
119  virtual ~RegEx()
120  {
121  regfree(&m_regex);
122  }
123 
124  /*!
125  * \brief Match pattern against regular expression.
126  *
127  * \param strPat String pattern.
128  *
129  * \return Returns true if the pattern matches the expression,
130  * false otherwise.
131  */
132  bool Match(const string &strPat)
133  {
134  return Match(strPat.c_str());
135  }
136 
137  /*!
138  * \brief Match pattern against regular expression.
139  *
140  * \param sPat Null-terminated string pattern.
141  *
142  * \return Returns true if the pattern matches the expression,
143  * false otherwise.
144  */
145  bool Match(const char *sPat)
146  {
147  if( m_bIsValid && (regexec(&m_regex, sPat, 0, NULL, 0) == 0) )
148  {
149  return true;
150  }
151  else
152  {
153  return false;
154  }
155  }
156 
157  /*!
158  * \brief Get this object's regular expression.
159  *
160  * \return If a regular expressing exist, then the expression string is
161  * returned. Else NULL is returned.
162  */
163  const char *GetRegEx() const
164  {
165  return m_strRegEx.length() > 0? m_strRegEx.c_str(): NULL;
166  }
167 
168  /*!
169  * \brief Test if in valid state.
170  *
171  * \return Returns true or false;
172  */
173  bool IsValid()
174  {
175  return m_bIsValid;
176  }
177 
178  /*!
179  * \brief Assignment copy operator.
180  *
181  * \param rhs Regular expression object.
182  *
183  * \return This regular expression object.
184  */
185  RegEx &operator=(const RegEx &rhs)
186  {
187  regfree(&m_regex);
188  m_strRegEx = rhs.m_strRegEx;
189  Compile();
190  return *this;
191  }
192 
193  /*!
194  * \brief Assignment operator.
195  *
196  * \param rhs String regular expression.
197  *
198  * \return This regular expression object.
199  */
200  RegEx &operator=(const string &rhs)
201  {
202  regfree(&m_regex);
203  m_strRegEx = rhs;
204  Compile();
205  return *this;
206  }
207 
208  /*!
209  * \brief Assignment operator.
210  *
211  * \param rhs Null-terminated string expression object.
212  *
213  * \return This regular expression object.
214  */
215  RegEx &operator=(const char *rhs)
216  {
217  regfree(&m_regex);
218  if( rhs != NULL )
219  {
220  m_strRegEx = rhs;
221  Compile();
222  }
223  else
224  {
225  m_strRegEx.clear();
226  m_bIsValid = false;
227  }
228  return *this;
229  }
230 
231 protected:
232  bool m_bIsValid; ///< expression is [not] valid
233  string m_strRegEx; ///< pre-compiled regular expression string
234  regex_t m_regex; ///< compiled reqular expression
235 
236  void Compile();
237 };
238 
239 #endif // _DYNASHELL_REGEX_H
regex_t m_regex
compiled reqular expression
bool Match(const char *sPat)
Match pattern against regular expression.
string m_strRegEx
pre-compiled regular expression string
const char * GetRegEx() const
Get this object&#39;s regular expression.
RegEx(const string &strRegEx)
String initialization constructor.
bool m_bIsValid
expression is [not] valid
RegEx & operator=(const char *rhs)
Assignment operator.
Regular Express Class.
RegEx(const char *sRegEx)
Null-terminated string initialization constructor.
bool Match(const string &strPat)
Match pattern against regular expression.
RegEx & operator=(const RegEx &rhs)
Assignment copy operator.
virtual ~RegEx()
Default destructor.
bool IsValid()
Test if in valid state.
RegEx()
Default constructor.
RegEx(const RegEx &src)
Copy constructor.
RegEx & operator=(const string &rhs)
Assignment operator.