LCOV - code coverage report
Current view: top level - advgetopt - validator_email.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 37 37 100.0 %
Date: 2022-07-08 21:04:10 Functions: 9 10 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/advgetopt
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : 
      20             : /** \file
      21             :  * \brief Implementation of the email validator.
      22             :  *
      23             :  * The email validator allows us to check the input as an email address.
      24             :  */
      25             : 
      26             : // self
      27             : //
      28             : #include    "advgetopt/validator_email.h"
      29             : 
      30             : 
      31             : // cppthread
      32             : //
      33             : #include    <cppthread/log.h>
      34             : 
      35             : 
      36             : // last include
      37             : //
      38             : #include    <snapdev/poison.h>
      39             : 
      40             : 
      41             : 
      42             : 
      43             : namespace advgetopt
      44             : {
      45             : 
      46             : 
      47             : 
      48             : namespace
      49             : {
      50             : 
      51             : 
      52             : 
      53           2 : class validator_email_factory
      54             :     : public validator_factory
      55             : {
      56             : public:
      57           2 :     validator_email_factory()
      58           2 :     {
      59           2 :         validator::register_validator(*this);
      60           2 :     }
      61             : 
      62           4 :     virtual std::string get_name() const override
      63             :     {
      64           4 :         return std::string("email");
      65             :     }
      66             : 
      67           5 :     virtual std::shared_ptr<validator> create(string_list_t const & data) const override
      68             :     {
      69           5 :         return std::make_shared<validator_email>(data);
      70             :     }
      71             : };
      72             : 
      73           2 : validator_email_factory         g_validator_email_factory;
      74             : 
      75             : 
      76             : 
      77             : } // no name namespace
      78             : 
      79             : 
      80             : 
      81             : 
      82             : 
      83           5 : validator_email::validator_email(string_list_t const & param_list)
      84             : {
      85             :     // at this time the tld library does not offer support for
      86             :     // flags or anything
      87             :     //
      88           5 :     if(param_list.size() > 1)
      89             :     {
      90           2 :         cppthread::log << cppthread::log_level_t::error
      91           1 :                        << "validator_email() supports zero or one parameter."
      92           2 :                        << cppthread::end;
      93           1 :         return;
      94             :     }
      95             : 
      96           4 :     if(param_list.size() == 1)
      97             :     {
      98           3 :         if(param_list[0] == "single")
      99             :         {
     100           1 :             f_multiple = false;
     101             :         }
     102           2 :         else if(param_list[0] == "multiple")
     103             :         {
     104           1 :             f_multiple = true;
     105             :         }
     106             :         else
     107             :         {
     108           2 :             cppthread::log << cppthread::log_level_t::error
     109           1 :                            << "validator_email(): unknown parameter \""
     110           1 :                            << param_list[0]
     111           1 :                            << "\"."
     112           2 :                            << cppthread::end;
     113           1 :             return;
     114             :         }
     115             :     }
     116             : }
     117             : 
     118             : 
     119             : /** \brief Return the name of this validator.
     120             :  *
     121             :  * This function returns "email".
     122             :  *
     123             :  * \return "email".
     124             :  */
     125           3 : std::string validator_email::name() const
     126             : {
     127           3 :     return std::string("email");
     128             : }
     129             : 
     130             : 
     131             : /** \brief Check the value to make sure emails are considered valid.
     132             :  *
     133             :  * This function is used to verify the value for a valid 
     134             :  *
     135             :  * \param[in] value  The value to be validated.
     136             :  *
     137             :  * \return true on a match.
     138             :  */
     139          23 : bool validator_email::validate(std::string const & value) const
     140             : {
     141          46 :     tld_email_list list;
     142          23 :     if(list.parse(value, 0) != TLD_RESULT_SUCCESS)
     143             :     {
     144          12 :         return false;
     145             :     }
     146             : 
     147          11 :     if(f_multiple)
     148             :     {
     149           3 :         return list.count() > 0;
     150             :     }
     151           8 :     return list.count() == 1;
     152             : }
     153             : 
     154             : 
     155             : 
     156           6 : } // namespace advgetopt
     157             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13