Back to index

nux  3.0.0
Validator.cpp
Go to the documentation of this file.
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   }
00032 
00033   Validator::~Validator()
00034   {
00035 
00036   }
00037 
00038   bool Validator::InitRegExp()
00039   {
00040 #if defined(NUX_OS_WINDOWS)
00041     regex_ = _regexp_str.GetTCharPtr();
00042     return true;
00043 #else
00044     const char *error;
00045     int   erroffset;
00046     _regexp = pcre_compile(
00047       _regexp_str.GetTCharPtr(),    /* the pattern */
00048       PCRE_MULTILINE,
00049       &error,         /* for error message */
00050       &erroffset,     /* for error offset */
00051       0);             /* use default character tables */
00052 
00053     if (!_regexp)
00054     {
00055       nuxDebugMsg("[IntegerValidator::IntegerValidator] Invalid regular expression: %s", _regexp_str.GetTCharPtr());
00056       return false;
00057     }    
00058     return true;
00059 #endif
00060   }
00061 
00062   Validator::State Validator::Validate(const char* str) const
00063   {
00064 #if defined(NUX_OS_WINDOWS)
00065     if (str == NULL)
00066       return Validator::Invalid;
00067     std::string search_string = str;
00068 
00069     if (std::regex_match(search_string.begin(), search_string.end(), regex_))
00070     {
00071       return Validator::Acceptable;
00072     }
00073     return Validator::Acceptable;
00074 #else
00075     if (_regexp == 0)
00076       return Validator::Invalid;
00077 
00078     int out_vector [10];
00079     unsigned int offset = 0;
00080     int len = (int) strlen(str);
00081 
00082     // See the "PCRE DISCUSSION OF STACK USAGE" and why it maybe necessary to limit the stack usage.
00083     pcre_extra extra;
00084     extra.flags = PCRE_EXTRA_MATCH_LIMIT_RECURSION;
00085     extra.match_limit_recursion = 2000; 
00086 
00087     int rc = pcre_exec(_regexp, &extra, str, len, offset, 0, out_vector, 10);
00088     if (rc <= -1)
00089     {
00090       return Validator::Invalid;
00091     }
00092     return Validator::Acceptable;
00093 #endif
00094   }
00095 
00096 }