advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
validator_length.cpp
Go to the documentation of this file.
1// Copyright (c) 2006-2025 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
28// self
29//
31
33
34
35// cppthread
36//
37#include <cppthread/log.h>
38
39
40// libutf8
41//
42#include <libutf8/libutf8.h>
43
44
45// snapdev
46//
47#include <snapdev/trim_string.h>
48
49
50// last include
51//
52#include <snapdev/poison.h>
53
54
55
56
57namespace advgetopt
58{
59
60
61
62namespace
63{
64
65
66
68 : public validator_factory
69{
70public:
72 {
73 validator::register_validator(*this);
74 }
75
76 virtual std::string get_name() const override
77 {
78 return std::string("length");
79 }
80
81 virtual std::shared_ptr<validator> create(string_list_t const & data) const override
82 {
83 return std::make_shared<validator_length>(data);
84 }
85};
86
88
89
90
91} // no name namespace
92
93
94
95
96
98{
100 for(auto r : length_list)
101 {
102 std::string::size_type const pos(r.find("..."));
103 if(pos == std::string::npos)
104 {
106 {
107 cppthread::log << cppthread::log_level_t::error
108 << r
109 << " is not a valid standalone value for your ranges;"
110 " it must only be digits, optionally preceeded by a sign (+ or -)"
111 " and not overflow an int64_t value."
112 << cppthread::end;
113 continue;
114 }
115 range.f_maximum = range.f_minimum;
116 }
117 else
118 {
119 std::string const min_value(snapdev::trim_string(r.substr(0, pos)));
120 if(!min_value.empty())
121 {
123 {
124 cppthread::log << cppthread::log_level_t::error
125 << min_value
126 << " is not a valid value for your range's start;"
127 " it must only be digits, optionally preceeded by a sign (+ or -)"
128 " and not overflow an int64_t value."
129 << cppthread::end;
130 continue;
131 }
132 }
133
134 std::string const max_value(snapdev::trim_string(r.substr(pos + 3)));
135 if(!max_value.empty())
136 {
138 {
139 cppthread::log << cppthread::log_level_t::error
140 << max_value
141 << " is not a valid value for your range's end;"
142 " it must only be digits, optionally preceeded by a sign (+ or -)"
143 " and not overflow an int64_t value."
144 << cppthread::end;
145 continue;
146 }
147 }
148
149 if(range.f_minimum > range.f_maximum)
150 {
151 cppthread::log << cppthread::log_level_t::error
152 << min_value
153 << " has to be smaller or equal to "
154 << max_value
155 << "; you have an invalid range."
156 << cppthread::end;
157 continue;
158 }
159 }
160 f_allowed_lengths.push_back(range);
161 }
162}
163
164
171std::string validator_length::name() const
172{
173 return std::string("length");
174}
175
176
185bool validator_length::validate(std::string const & value) const
186{
187 if(f_allowed_lengths.empty())
188 {
189 return true;
190 }
191
192 // get the number of characters assuming the input string is UTF-8
193 //
194 std::int64_t const length(static_cast<std::int64_t>(libutf8::u8length(value)));
195 for(auto f : f_allowed_lengths)
196 {
197 if(length >= f.f_minimum
198 && length <= f.f_maximum)
199 {
200 return true;
201 }
202 }
203 return false;
204}
205
206
207
208} // namespace advgetopt
209// vim: ts=4 sw=4 et
virtual std::shared_ptr< validator > create(string_list_t const &data) const override
static bool convert_string(std::string const &number, std::int64_t &result)
Convert a string to an std::int64_t value.
virtual bool validate(std::string const &value) const override
Check the value against a list of length ranges.
virtual std::string name() const override
Return the name of this validator.
range_t::vector_t f_allowed_lengths
validator_length(string_list_t const &data)
The advgetopt environment to parse command line options.
Definition version.h:37
constexpr flag_t option_flags_merge()
Definition flags.h:87
std::vector< std::string > string_list_t
Definition utils.h:41
Declaration of validators which can be used to verify the parameters.
Declaration of the length validator.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.