LCOV - code coverage report
Current view: top level - advgetopt - validator_email.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 36 100.0 %
Date: 2022-07-15 17:40:56 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             : // libtld
      37             : //
      38             : #include    <libtld/tld.h>
      39             : 
      40             : 
      41             : // last include
      42             : //
      43             : #include    <snapdev/poison.h>
      44             : 
      45             : 
      46             : 
      47             : 
      48             : namespace advgetopt
      49             : {
      50             : 
      51             : 
      52             : 
      53             : namespace
      54             : {
      55             : 
      56             : 
      57             : 
      58           2 : class validator_email_factory
      59             :     : public validator_factory
      60             : {
      61             : public:
      62           2 :     validator_email_factory()
      63           2 :     {
      64           2 :         validator::register_validator(*this);
      65           2 :     }
      66             : 
      67           4 :     virtual std::string get_name() const override
      68             :     {
      69           4 :         return std::string("email");
      70             :     }
      71             : 
      72           5 :     virtual std::shared_ptr<validator> create(string_list_t const & data) const override
      73             :     {
      74           5 :         return std::make_shared<validator_email>(data);
      75             :     }
      76             : };
      77             : 
      78           2 : validator_email_factory         g_validator_email_factory;
      79             : 
      80             : 
      81             : 
      82             : } // no name namespace
      83             : 
      84             : 
      85             : 
      86             : 
      87             : 
      88           5 : validator_email::validator_email(string_list_t const & param_list)
      89             : {
      90             :     // at this time the tld library does not offer support for
      91             :     // flags or anything
      92             :     //
      93           5 :     if(param_list.size() > 1)
      94             :     {
      95           2 :         cppthread::log << cppthread::log_level_t::error
      96           1 :                        << "validator_email() supports zero or one parameter."
      97           2 :                        << cppthread::end;
      98           1 :         return;
      99             :     }
     100             : 
     101           4 :     if(param_list.size() == 1)
     102             :     {
     103           3 :         if(param_list[0] == "single")
     104             :         {
     105           1 :             f_multiple = false;
     106             :         }
     107           2 :         else if(param_list[0] == "multiple")
     108             :         {
     109           1 :             f_multiple = true;
     110             :         }
     111             :         else
     112             :         {
     113           2 :             cppthread::log << cppthread::log_level_t::error
     114           1 :                            << "validator_email(): unknown parameter \""
     115           1 :                            << param_list[0]
     116           1 :                            << "\"."
     117           2 :                            << cppthread::end;
     118             :         }
     119             :     }
     120             : }
     121             : 
     122             : 
     123             : /** \brief Return the name of this validator.
     124             :  *
     125             :  * This function returns "email".
     126             :  *
     127             :  * \return "email".
     128             :  */
     129           3 : std::string validator_email::name() const
     130             : {
     131           3 :     return std::string("email");
     132             : }
     133             : 
     134             : 
     135             : /** \brief Check the value to make sure emails are considered valid.
     136             :  *
     137             :  * This function is used to verify the value for a valid email.
     138             :  *
     139             :  * \param[in] value  The value to be validated.
     140             :  *
     141             :  * \return true on a match.
     142             :  */
     143          23 : bool validator_email::validate(std::string const & value) const
     144             : {
     145          46 :     tld_email_list list;
     146          23 :     if(list.parse(value, 0) != TLD_RESULT_SUCCESS)
     147             :     {
     148          12 :         return false;
     149             :     }
     150             : 
     151          11 :     if(f_multiple)
     152             :     {
     153           3 :         return list.count() > 0;
     154             :     }
     155           8 :     return list.count() == 1;
     156             : }
     157             : 
     158             : 
     159             : 
     160           6 : } // namespace advgetopt
     161             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13