Back to index

nux  3.0.0
IntegerValidator.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 "IntegerValidator.h"
00025 
00026 namespace nux
00027 {
00028 
00029   IntegerValidator::IntegerValidator(int Minimum, int Maximum)
00030     :   m_Minimum(Minimum)
00031     ,   m_Maximum(Maximum)
00032   {
00033     _regexp_str = "^[-+]?[0-9]+$";
00034 
00035     InitRegExp();
00036 
00037     if (m_Minimum > m_Maximum)
00038     {
00039       int temp = m_Minimum;
00040       m_Minimum = m_Maximum;
00041       m_Maximum = temp;
00042     }
00043   }
00044 
00045   IntegerValidator::IntegerValidator(const IntegerValidator &copy)
00046   {
00047     m_Minimum   = copy.m_Minimum;
00048     m_Minimum   = copy.m_Maximum;
00049     _regexp_str    = copy._regexp_str;
00050     InitRegExp();
00051   }
00052 
00053   IntegerValidator &IntegerValidator::operator= (const IntegerValidator &rhs)
00054   {
00055     if (&rhs != this)
00056     {
00057       m_Minimum   = rhs.m_Minimum;
00058       m_Minimum   = rhs.m_Maximum;
00059       _regexp_str    = rhs._regexp_str;
00060       InitRegExp();
00061     }
00062 
00063     return *this;
00064   }
00065 
00066   IntegerValidator::~IntegerValidator()
00067   {
00068 
00069   }
00070 
00071   Validator *IntegerValidator::Clone()  const
00072   {
00073     return new IntegerValidator(*this);
00074   }
00075 
00076   void IntegerValidator::SetMinimum(int value)
00077   {
00078     m_Minimum = value;
00079 
00080     if (m_Minimum > m_Maximum)
00081     {
00082       int temp = m_Minimum;
00083       m_Minimum = m_Maximum;
00084       m_Maximum = temp;
00085     }
00086   }
00087 
00088   int IntegerValidator::GetMinimum() const
00089   {
00090     return m_Minimum;
00091   }
00092 
00093   void IntegerValidator::SetMaximum(int value)
00094   {
00095     m_Maximum = value;
00096 
00097     if (m_Minimum > m_Maximum)
00098     {
00099       int temp = m_Minimum;
00100       m_Minimum = m_Maximum;
00101       m_Maximum = temp;
00102     }
00103   }
00104 
00105   int IntegerValidator::GetMaximum() const
00106   {
00107     return m_Maximum;
00108   }
00109 
00110   int IntegerValidator::GetClampedValue(int i) const
00111   {
00112     if (i < m_Minimum)
00113       return m_Minimum;
00114 
00115     if (i > m_Maximum)
00116       return m_Maximum;
00117 
00118     return i;
00119   }
00120 
00121   void IntegerValidator::Alternative(const char *str)
00122   {
00123     str = "0";
00124   }
00125 
00126   NString IntegerValidator::ToString(int i)
00127   {
00128     NString Prec("%d");
00129     return NString::Printf(Prec.GetTCharPtr(), i);
00130   }
00131 
00132   int IntegerValidator::ToInteger(const char *str)
00133   {
00134     if (Validate(str) == Acceptable)
00135       return CharToInteger(str);
00136     else
00137       return 0;
00138   }
00139 }
00140