nux-1.14.0
Validator.cpp
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "Validator.h"
00025 
00026 namespace nux
00027 {
00028 
00029   Validator::Validator()
00030   {
00031     _regexp = 0;
00032   }
00033 
00034   Validator::~Validator()
00035   {
00036 
00037   }
00038 
00039   bool Validator::InitRegExp ()
00040   {
00041     const char *error;
00042     int   erroffset;
00043     _regexp = pcre_compile (
00044       _regexp_str.GetTCharPtr (),    /* the pattern */
00045       PCRE_MULTILINE,
00046       &error,         /* for error message */
00047       &erroffset,     /* for error offset */
00048       0);             /* use default character tables */
00049 
00050     if (!_regexp)
00051     {
00052       nuxDebugMsg (TEXT("[IntegerValidator::IntegerValidator] Invalid regular expression: %s"), _regexp_str.GetTCharPtr ());
00053       return false;
00054     }    
00055     return true;
00056   }
00057 
00058   Validator::State Validator::Validate (const TCHAR *str) const
00059   {
00060     if (_regexp == 0)
00061       return Validator::Invalid;
00062 
00063     int out_vector [10];
00064     unsigned int offset = 0;
00065     int len = (int) strlen (str);
00066 
00067     // See the "PCRE DISCUSSION OF STACK USAGE" and why it maybe necessary to limit the stack usage.
00068     pcre_extra extra;
00069     extra.flags = PCRE_EXTRA_MATCH_LIMIT_RECURSION;
00070     extra.match_limit_recursion = 2000; 
00071 
00072     int rc = pcre_exec(_regexp, &extra, str, len, offset, 0, out_vector, 10);
00073     if (rc <= -1)
00074     {
00075       return Validator::Invalid;
00076     }
00077 
00078     return Validator::Acceptable;
00079   }
00080 
00081 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends