advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
validator_duration.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
31// self
32//
34
36
37
38// cppthread
39//
40#include <cppthread/log.h>
41
42
43// last include
44//
45#include <snapdev/poison.h>
46
47
48
49namespace advgetopt
50{
51
52
53
54namespace
55{
56
57
58
60 : public validator_factory
61{
62public:
64 {
65 validator::register_validator(*this);
66 }
67
68 virtual std::string get_name() const override
69 {
70 return std::string("duration");
71 }
72
73 virtual std::shared_ptr<validator> create(string_list_t const & data) const override
74 {
75 return std::make_shared<validator_duration>(data);
76 }
77};
78
80
81
82
83} // no name namespace
84
85
86
87
88
113{
114 for(auto r : flag_list)
115 {
116 if(r == "small")
117 {
119 }
120 else if(r == "large")
121 {
123 }
124 else
125 {
126 cppthread::log << cppthread::log_level_t::error
127 << r
128 << " is not a valid flag for the duration validator."
129 << cppthread::end;
130 continue;
131 }
132 }
133}
134
135
142std::string validator_duration::name() const
143{
144 return std::string("duration");
145}
146
147
160bool validator_duration::validate(std::string const & value) const
161{
162 double result(0);
163 return convert_string(value, f_flags, result);
164}
165
166
205 std::string const & value
206 , flag_t flags
207 , double & result)
208{
209 result = 0.0;
210
211 // TODO: use libutf8 to walk the string and skip all UTF-8 spaces
212 //
213 char const * s(value.c_str());
214 while(isspace(*s))
215 {
216 ++s;
217 }
218 if(*s == '\0')
219 {
220 return false;
221 }
222
223 double r(0.0);
224 while(*s != '\0')
225 {
226 // get the number
227 //
228 char const * number(s);
229 if(*s == '+'
230 || *s == '-')
231 {
232 ++s;
233 }
234 while(*s >= '0' && *s <= '9')
235 {
236 ++s;
237 }
238 if(*s == '.')
239 {
240 do
241 {
242 ++s;
243 }
244 while(*s >= '0' && *s <= '9');
245 if(*s == '.')
246 {
247 // this would otherwise allow "2..8 year" which is probably
248 // a mistake (i.e. not "2.s" and ".8 year")
249 //
250 return false;
251 }
252 }
253 double n(0.0);
254#pragma GCC diagnostic push
255#pragma GCC diagnostic ignored "-Wrestrict"
257 (*number == '.' ? "0" : "") + std::string(number, s - number)
258 , n))
259 {
260 return false;
261 }
262#pragma GCC diagnostic pop
263
264 while(isspace(*s))
265 {
266 ++s;
267 }
268
269 // get the suffix
270 //
271 std::string suffix;
272 for(;; ++s)
273 {
274 if(*s >= 'A'
275 && *s <= 'Z')
276 {
277 // make lowercase as we're at it
278 //
279 suffix += *s + 0x20;
280 }
281 else if(*s >= 'a'
282 && *s <= 'z')
283 {
284 suffix += *s;
285 }
286 else
287 {
288 break;
289 }
290 }
291
292 double factor(1.0);
293 if(!suffix.empty()) // empty == "seconds"
294 {
295 switch(suffix[0])
296 {
297 case 'd':
298 if(suffix == "d"
299 || suffix == "day"
300 || suffix == "days")
301 {
302 factor = 86400.0;
303 }
304 else
305 {
306 return false;
307 }
308 break;
309
310 case 'h':
311 if(suffix == "h"
312 || suffix == "hour"
313 || suffix == "hours")
314 {
315 factor = 3600.0;
316 }
317 else
318 {
319 return false;
320 }
321 break;
322
323 case 'm':
324 if(suffix == "m")
325 {
326 if((flags & VALIDATOR_DURATION_LONG) != 0)
327 {
328 factor = 86400.0 * 30.0; // 1 month
329 }
330 else
331 {
332 factor = 60.0; // 1 minute
333 }
334 }
335 else if(suffix == "minute"
336 || suffix == "minutes")
337 {
338 factor = 60.0;
339 }
340 else if(suffix == "month"
341 || suffix == "months")
342 {
343 factor = 86400.0 * 30.0;
344 }
345 else
346 {
347 return false;
348 }
349 break;
350
351 case 's':
352 if(suffix != "s"
353 && suffix != "second"
354 && suffix != "seconds")
355 {
356 return false;
357 }
358 break;
359
360 case 'w':
361 if(suffix == "w"
362 || suffix == "week"
363 || suffix == "weeks")
364 {
365 factor = 86400.0 * 7.0;
366 }
367 else
368 {
369 return false;
370 }
371 break;
372
373 case 'y':
374 if(suffix == "y"
375 || suffix == "year"
376 || suffix == "years")
377 {
378 factor = 86400.0 * 365.0;
379 }
380 else
381 {
382 return false;
383 }
384 break;
385
386 default:
387 return false;
388
389 }
390 }
391
392 // TODO: catch ERANGE errors
393 //
394 r += n * factor;
395
396 while(isspace(*s))
397 {
398 ++s;
399 }
400 }
401
402 result = r;
403
404 return true;
405}
406
407
408
409} // namespace advgetopt
410// 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, double &result)
Convert a string to a double value.
static bool convert_string(std::string const &duration, flag_t flags, double &result)
Convert a string to a double value representing a duration.
virtual std::string name() const override
Return the name of this validator.
virtual bool validate(std::string const &value) const override
Determine whether value is a valid duration.
validator_duration(string_list_t const &data)
Initialize the duration validator.
static constexpr flag_t VALIDATOR_DURATION_LONG
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 validators which can be used to verify the parameters.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.