KiCad PCB EDA Suite
REGEX_VALIDATOR Class Reference

Custom validator that checks verifies that a string exactly matches a regular expression. More...

#include <validators.h>

Inheritance diagram for REGEX_VALIDATOR:

Public Member Functions

 REGEX_VALIDATOR (const wxString &aRegEx, wxString *aValue=NULL)
 
 REGEX_VALIDATOR (const wxString &aRegEx, int aFlags, wxString *aValue=NULL)
 
 REGEX_VALIDATOR (const REGEX_VALIDATOR &aOther)
 
virtual wxObject * Clone () const override
 
bool Validate (wxWindow *aParent) override
 

Protected Member Functions

void compileRegEx (const wxString &aRegEx, int aFlags)
 

Compiles and stores a regular expression

More...
 

Protected Attributes

wxString m_regExString
 

Original regular expression (for copy constructor)

More...
 
int m_regExFlags
 

Original compilation flags (for copy constructor)

More...
 
wxRegEx m_regEx
 

Compiled regex

More...
 

Detailed Description

Custom validator that checks verifies that a string exactly matches a regular expression.

Definition at line 116 of file validators.h.

Constructor & Destructor Documentation

◆ REGEX_VALIDATOR() [1/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const wxString &  aRegEx,
wxString *  aValue = NULL 
)
inline
Parameters
aRegExis a regular expression to validate strings.
aValueis a pointer to a wxString containing the value to validate.

Definition at line 123 of file validators.h.

124  : wxTextValidator( wxFILTER_NONE, aValue )
125  {
126  compileRegEx( aRegEx, wxRE_DEFAULT );
127  }
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:258

References compileRegEx().

Referenced by Clone().

◆ REGEX_VALIDATOR() [2/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const wxString &  aRegEx,
int  aFlags,
wxString *  aValue = NULL 
)
inline
Parameters
aRegExis a regular expression to validate strings.
aFlagsare compilation flags (normally wxRE_DEFAULT).
aValueis a pointer to a wxString containing the value to validate.

Definition at line 134 of file validators.h.

135  : wxTextValidator( wxFILTER_NONE, aValue )
136  {
137  compileRegEx( aRegEx, aFlags );
138  }
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:258

References compileRegEx().

◆ REGEX_VALIDATOR() [3/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const REGEX_VALIDATOR aOther)
inline

Definition at line 140 of file validators.h.

140  : wxTextValidator( aOther )
141  {
142  compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
143  }
wxString m_regExString
Original regular expression (for copy constructor)
Definition: validators.h:157
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:258
int m_regExFlags
Original compilation flags (for copy constructor)
Definition: validators.h:160

References compileRegEx(), m_regExFlags, and m_regExString.

Member Function Documentation

◆ Clone()

virtual wxObject* REGEX_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 145 of file validators.h.

146  {
147  return new REGEX_VALIDATOR( *this );
148  }
REGEX_VALIDATOR(const wxString &aRegEx, wxString *aValue=NULL)
Definition: validators.h:123

References REGEX_VALIDATOR().

◆ compileRegEx()

void REGEX_VALIDATOR::compileRegEx ( const wxString &  aRegEx,
int  aFlags 
)
protected

Compiles and stores a regular expression

Definition at line 258 of file validators.cpp.

259 {
260  if( !m_regEx.Compile( aRegEx, aFlags ) )
261  {
262  throw std::runtime_error( "REGEX_VALIDATOR: Invalid regular expression: "
263  + aRegEx.ToStdString() );
264  }
265 
266  m_regExString = aRegEx;
267  m_regExFlags = aFlags;
268 }
wxString m_regExString
Original regular expression (for copy constructor)
Definition: validators.h:157
wxRegEx m_regEx
Compiled regex
Definition: validators.h:163
int m_regExFlags
Original compilation flags (for copy constructor)
Definition: validators.h:160

References m_regEx, m_regExFlags, and m_regExString.

Referenced by REGEX_VALIDATOR().

◆ Validate()

bool REGEX_VALIDATOR::Validate ( wxWindow *  aParent)
override

Definition at line 220 of file validators.cpp.

221 {
222  // If window is disabled, simply return
223  if( !m_validatorWindow->IsEnabled() )
224  return true;
225 
226  wxTextEntry* const textEntry = GetTextEntry();
227 
228  if( !textEntry )
229  return false;
230 
231  bool valid = true;
232  const wxString& value = textEntry->GetValue();
233 
234  if( m_regEx.Matches( value ) )
235  {
236  size_t start, len;
237  m_regEx.GetMatch( &start, &len );
238 
239  if( start != 0 || len != value.Length() ) // whole string must match
240  valid = false;
241  }
242  else // no match at all
243  {
244  valid = false;
245  }
246 
247  if( !valid )
248  {
249  m_validatorWindow->SetFocus();
250  DisplayError( aParent, wxString::Format( _( "Incorrect value: %s" ), value ) );
251  return false;
252  }
253 
254  return true;
255 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
wxRegEx m_regEx
Compiled regex
Definition: validators.h:163
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:31

References _, DisplayError(), Format(), and m_regEx.

Member Data Documentation

◆ m_regEx

wxRegEx REGEX_VALIDATOR::m_regEx
protected

Compiled regex

Definition at line 163 of file validators.h.

Referenced by compileRegEx(), and Validate().

◆ m_regExFlags

int REGEX_VALIDATOR::m_regExFlags
protected

Original compilation flags (for copy constructor)

Definition at line 160 of file validators.h.

Referenced by compileRegEx(), and REGEX_VALIDATOR().

◆ m_regExString

wxString REGEX_VALIDATOR::m_regExString
protected

Original regular expression (for copy constructor)

Definition at line 157 of file validators.h.

Referenced by compileRegEx(), and REGEX_VALIDATOR().


The documentation for this class was generated from the following files: