Line data Source code
1 : // Copyright (c) 2006-2024 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 keywords validator. 22 : * 23 : * The keywords validator allows us to check words in a very simple manner 24 : * (compared to the regular expression validator). It is also likely going 25 : * to be faster. 26 : */ 27 : 28 : // self 29 : // 30 : #include "advgetopt/validator_keywords.h" 31 : 32 : 33 : // cppthread 34 : // 35 : #include <cppthread/log.h> 36 : 37 : 38 : // last include 39 : // 40 : #include <snapdev/poison.h> 41 : 42 : 43 : 44 : 45 : namespace advgetopt 46 : { 47 : 48 : 49 : 50 : namespace 51 : { 52 : 53 : 54 : 55 : class validator_keywords_factory 56 : : public validator_factory 57 : { 58 : public: 59 2 : validator_keywords_factory() 60 2 : { 61 2 : validator::register_validator(*this); 62 2 : } 63 : 64 4 : virtual std::string get_name() const override 65 : { 66 4 : return std::string("keywords"); 67 : } 68 : 69 4 : virtual std::shared_ptr<validator> create(string_list_t const & data) const override 70 : { 71 4 : return std::make_shared<validator_keywords>(data); 72 : } 73 : }; 74 : 75 : validator_keywords_factory g_validator_keywords_factory; 76 : 77 : 78 : 79 : } // no name namespace 80 : 81 : 82 : 83 : 84 : 85 4 : validator_keywords::validator_keywords(string_list_t const & keywords_list) 86 : { 87 4 : if(keywords_list.empty()) 88 : { 89 4 : cppthread::log << cppthread::log_level_t::error 90 2 : << "validator_keywords() requires at least one parameter." 91 4 : << cppthread::end; 92 2 : return; 93 : } 94 : 95 4 : std::copy( 96 : keywords_list.begin() 97 : , keywords_list.end() 98 2 : , std::inserter(f_keywords, f_keywords.begin())); 99 0 : } 100 : 101 : 102 : /** \brief Return the name of this validator. 103 : * 104 : * This function returns "keywords". 105 : * 106 : * \return "keywords". 107 : */ 108 1 : std::string validator_keywords::name() const 109 : { 110 1 : return std::string("keywords"); 111 : } 112 : 113 : 114 : /** \brief Check the value against a list of keywords. 115 : * 116 : * This function is used to match the value of an argument against a 117 : * list of keywords. It returns true when it does match. 118 : * 119 : * \param[in] value The value to be validated. 120 : * 121 : * \return true on a match. 122 : */ 123 129 : bool validator_keywords::validate(std::string const & value) const 124 : { 125 129 : return f_keywords.find(value) != f_keywords.end(); 126 : } 127 : 128 : 129 : 130 : } // namespace advgetopt 131 : // vim: ts=4 sw=4 et