Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : * Doug Barbieri doug@m2osw.com
25 : */
26 :
27 :
28 : /** \file
29 : * \brief Implementation of the option_info class.
30 : *
31 : * This is the implementation of the class used to define one command
32 : * line option.
33 : */
34 :
35 : // self
36 : //
37 : #include "advgetopt/option_info.h"
38 :
39 :
40 : // advgetopt lib
41 : //
42 : #include "advgetopt/exception.h"
43 :
44 :
45 : // cppthread lib
46 : //
47 : #include <cppthread/log.h>
48 :
49 :
50 : // libutf8 lib
51 : //
52 : #include <libutf8/libutf8.h>
53 :
54 :
55 : // boost lib
56 : //
57 : #include <boost/algorithm/string/replace.hpp>
58 :
59 :
60 : // last include
61 : //
62 : #include <snapdev/poison.h>
63 :
64 :
65 :
66 :
67 : namespace advgetopt
68 : {
69 :
70 :
71 :
72 :
73 :
74 :
75 :
76 : /** \brief Initialize a reference to an option_info object.
77 : *
78 : * This constructor creates a reference to the specified \p opt
79 : * option_info object.
80 : *
81 : * This gives you read and write access to the very first value held by
82 : * the \p opt object.
83 : *
84 : * \note
85 : * The option may not yet be defined in which case the default value is
86 : * used as the current value.
87 : *
88 : * \param[in] opt The option to create the reference of.
89 : */
90 159 : option_info_ref::option_info_ref(option_info::pointer_t opt)
91 159 : : f_opt(opt)
92 : {
93 159 : }
94 :
95 :
96 : /** \brief Retrieve the length of the option's value.
97 : *
98 : * This function checks the option's value and returns true if it is empty.
99 : *
100 : * \note
101 : * If the value is not currently defined, this function returns the
102 : * length of the default value.
103 : *
104 : * \return The length of the option's value.
105 : */
106 29 : bool option_info_ref::empty() const
107 : {
108 29 : if(f_opt->is_defined())
109 : {
110 15 : return f_opt->get_value().empty();
111 : }
112 :
113 14 : return true;
114 : }
115 :
116 :
117 : /** \brief Return the length of the option's value.
118 : *
119 : * This function returns the length of the option's value. This is the
120 : * number of bytes in the string.
121 : *
122 : * \note
123 : * If the value is not currently defined, this function returns the
124 : * length of the default value.
125 : *
126 : * \return The length of the option's value.
127 : */
128 18 : size_t option_info_ref::length() const
129 : {
130 18 : if(f_opt->is_defined())
131 : {
132 10 : return f_opt->get_value().length();
133 : }
134 :
135 8 : return f_opt->get_default().length();
136 : }
137 :
138 :
139 : /** \brief Return the length of the option's value.
140 : *
141 : * This function returns the length of the option's value. This is the
142 : * number of bytes in the string.
143 : *
144 : * \return The length of the option's value.
145 : */
146 9 : size_t option_info_ref::size() const
147 : {
148 9 : return length();
149 : }
150 :
151 :
152 : /** \brief Retrieve the referenced option as a long.
153 : *
154 : * This function attempts to retrieve the option value as a long integer.
155 : *
156 : * If the value is not yet defined, the function attempts to return the
157 : * default value converted to an integer. If that fails, the function
158 : * returns -1 after it emitted an error in the log.
159 : *
160 : * When the value is not defined and there is no default, the function
161 : * returns 0 (as if an empty string represented 0.)
162 : *
163 : * \return The value as a long or -1.
164 : */
165 29 : long option_info_ref::get_long() const
166 : {
167 29 : if(f_opt->is_defined())
168 : {
169 26 : return f_opt->get_long();
170 : }
171 :
172 3 : if(!f_opt->has_default())
173 : {
174 1 : return 0;
175 : }
176 :
177 : std::int64_t v;
178 2 : if(!validator_integer::convert_string(f_opt->get_default(), v))
179 : {
180 2 : cppthread::log << cppthread::log_level_t::error
181 1 : << "invalid default value for a number ("
182 1 : << f_opt->get_default()
183 1 : << ") in parameter --"
184 1 : << f_opt->get_name()
185 1 : << " at offset 0."
186 1 : << cppthread::end;
187 1 : return -1;
188 : }
189 :
190 1 : return v;
191 : }
192 :
193 :
194 : /** \brief Convert the reference to a string (a.k.a. read the value)
195 : *
196 : * This cast operator transforms the reference in a string which has
197 : * the contents of the option value.
198 : *
199 : * \return The option contents as a string.
200 : */
201 450 : option_info_ref::operator std::string () const
202 : {
203 450 : if(f_opt->is_defined())
204 : {
205 324 : return f_opt->get_value();
206 : }
207 :
208 126 : return f_opt->get_default();
209 : }
210 :
211 :
212 : /** \brief Set the option value to \p value.
213 : *
214 : * This assignment operator is used to change the value of the option.
215 : *
216 : * The input character is transform in a string and saved as such in the
217 : * option. If the character is '\0', then the value is cleared instead.
218 : *
219 : * \param[in] value The ISO-8859-1 character to save in the option_info object.
220 : *
221 : * \return A reference to this object_info_ref.
222 : */
223 5 : option_info_ref & option_info_ref::operator = (char value)
224 : {
225 10 : std::string v;
226 5 : if(value != '\0')
227 : {
228 3 : v += value;
229 : }
230 5 : f_opt->set_value(0, v);
231 10 : return *this;
232 : }
233 :
234 :
235 : /** \brief Set the option value to \p value.
236 : *
237 : * This assignment operator is used to change the value of the option.
238 : *
239 : * The input character is transform in a string and saved as such in the
240 : * option. If the character is '\0', then the value is cleared instead.
241 : *
242 : * \param[in] value The unicode character to save in the option_info object.
243 : *
244 : * \return A reference to this object_info_ref.
245 : */
246 4 : option_info_ref & option_info_ref::operator = (char32_t value)
247 : {
248 8 : std::string v;
249 4 : if(value != U'\0')
250 : {
251 2 : v = libutf8::to_u8string(value);
252 : }
253 4 : f_opt->set_value(0, v);
254 8 : return *this;
255 : }
256 :
257 :
258 : /** \brief Set the option value to \p value.
259 : *
260 : * This assignment operator can be used to change the value of the
261 : * option.
262 : *
263 : * \param[in] value The new value to save in the option_info object.
264 : *
265 : * \return A reference to this object_info_ref.
266 : */
267 5 : option_info_ref & option_info_ref::operator = (char const * value)
268 : {
269 5 : if(value == nullptr)
270 : {
271 1 : f_opt->set_value(0, std::string());
272 : }
273 : else
274 : {
275 4 : f_opt->set_value(0, value);
276 : }
277 5 : return *this;
278 : }
279 :
280 :
281 : /** \brief Set the option value to \p value.
282 : *
283 : * This assignment operator can be used to change the value of the
284 : * option.
285 : *
286 : * \param[in] value The new value to save in the option_info object.
287 : *
288 : * \return A reference to this object_info_ref.
289 : */
290 3 : option_info_ref & option_info_ref::operator = (std::string const & value)
291 : {
292 3 : f_opt->set_value(0, value);
293 3 : return *this;
294 : }
295 :
296 :
297 : /** \brief Set the value of this option to the value of another option.
298 : *
299 : * This assignment operator allows you to copy the value from reference
300 : * value to another.
301 : *
302 : * \param[in] value The other option to read the value from.
303 : *
304 : * \return A reference to this object_info_ref.
305 : */
306 4 : option_info_ref & option_info_ref::operator = (option_info_ref const & value)
307 : {
308 4 : f_opt->set_value(0, value);
309 4 : return *this;
310 : }
311 :
312 :
313 : /** \brief Append the character \p value to this option's value.
314 : *
315 : * This assignment operator can be used to append a character to the
316 : * existing value of the option.
317 : *
318 : * \note
319 : * The character is taken as an ISO-8859-1. If you want to use a Unicode
320 : * character, make sure to use a char32_t character.
321 : *
322 : * \param[in] value The character to append to the option_info's value.
323 : *
324 : * \return A reference to this object_info_ref.
325 : */
326 8 : option_info_ref & option_info_ref::operator += (char value)
327 : {
328 16 : std::string v;
329 8 : if(value != '\0')
330 : {
331 4 : v += value;
332 : }
333 8 : f_opt->set_value(0, static_cast<std::string>(*this) + v);
334 16 : return *this;
335 : }
336 :
337 :
338 : /** \brief Append the character \p value to this option's value.
339 : *
340 : * This assignment operator can be used to append a character to the
341 : * existing value of the option.
342 : *
343 : * \param[in] value The character to append to the option_info's value.
344 : *
345 : * \return A reference to this object_info_ref.
346 : */
347 6 : option_info_ref & option_info_ref::operator += (char32_t value)
348 : {
349 12 : std::string v;
350 6 : if(value != '\0')
351 : {
352 2 : v += libutf8::to_u8string(value);
353 : }
354 6 : f_opt->set_value(0, static_cast<std::string>(*this) + v);
355 12 : return *this;
356 : }
357 :
358 :
359 : /** \brief Append \p value to this option's value.
360 : *
361 : * This assignment operator can be used to append a string to the existing
362 : * value of the option.
363 : *
364 : * \param[in] value The value to append to the option_info's value.
365 : *
366 : * \return A reference to this object_info_ref.
367 : */
368 7 : option_info_ref & option_info_ref::operator += (char const * value)
369 : {
370 14 : std::string v;
371 7 : if(value != nullptr)
372 : {
373 5 : v = value;
374 : }
375 7 : f_opt->set_value(0, static_cast<std::string>(*this) + v);
376 14 : return *this;
377 : }
378 :
379 :
380 : /** \brief Append \p value to this option's value.
381 : *
382 : * This assignment operator is used to append a string to the existing
383 : * value of the option.
384 : *
385 : * \param[in] value The value to append to the option_info's value.
386 : *
387 : * \return A reference to this object_info_ref.
388 : */
389 5 : option_info_ref & option_info_ref::operator += (std::string const & value)
390 : {
391 5 : f_opt->set_value(0, static_cast<std::string>(*this) + value);
392 5 : return *this;
393 : }
394 :
395 :
396 : /** \brief Append the value of this \p value option to this option_info's value.
397 : *
398 : * This assignment operator is used to append a string to the existing
399 : * value of this option.
400 : *
401 : * \param[in] value The other option to read the value from.
402 : *
403 : * \return A reference to this object_info_ref.
404 : */
405 6 : option_info_ref & option_info_ref::operator += (option_info_ref const & value)
406 : {
407 24 : f_opt->set_value(0, static_cast<std::string>(*this)
408 24 : + static_cast<std::string>(value));
409 6 : return *this;
410 : }
411 :
412 :
413 : /** \brief Append the character \p value to this option's value.
414 : *
415 : * This operator is used to append a character to the value of the option
416 : * and returns the result as a string.
417 : *
418 : * \note
419 : * This version appends an ISO-8859-1 character. Make sure to use a
420 : * char32_t character to add a Unicode character.
421 : *
422 : * \param[in] value The character to append to the option_info's value.
423 : *
424 : * \return A string with the resulting concatenation.
425 : */
426 10 : std::string option_info_ref::operator + (char value) const
427 : {
428 10 : if(value == '\0')
429 : {
430 5 : return *this;
431 : }
432 5 : return static_cast<std::string>(*this) + value;
433 : }
434 :
435 :
436 : /** \brief Append the character \p value to this option's value.
437 : *
438 : * This operator is used to append a Unicode character to the value of the
439 : * option and returns the result as a string.
440 : *
441 : * \param[in] value The character to append to the option_info's value.
442 : *
443 : * \return A string with the resulting concatenation.
444 : */
445 12 : std::string option_info_ref::operator + (char32_t value) const
446 : {
447 12 : if(value == U'\0')
448 : {
449 6 : return *this;
450 : }
451 6 : return static_cast<std::string>(*this) + libutf8::to_u8string(value);
452 : }
453 :
454 :
455 : /** \brief Append \p value to this option's value.
456 : *
457 : * This operator is used to append a string to the value of the option
458 : * and return the result as a string.
459 : *
460 : * \param[in] value The string to append to the option_info's value.
461 : *
462 : * \return A string with the resulting concatenation.
463 : */
464 8 : std::string option_info_ref::operator + (char const * value) const
465 : {
466 8 : if(value == nullptr)
467 : {
468 2 : return *this;
469 : }
470 6 : return static_cast<std::string>(*this) + value;
471 : }
472 :
473 :
474 : /** \brief Append \p value to this option's value.
475 : *
476 : * This operator is used to append a string to the value of the option
477 : * and return the result as a string.
478 : *
479 : * \param[in] value The value to append to the option_info's value.
480 : *
481 : * \return A string with the resulting concatenation.
482 : */
483 5 : std::string option_info_ref::operator + (std::string const & value) const
484 : {
485 5 : return static_cast<std::string>(*this) + value;
486 : }
487 :
488 :
489 : /** \brief Append the value of this \p value option to this option_info's value.
490 : *
491 : * This operator is used to append two option references to each others and
492 : * return the concatenated string as the result.
493 : *
494 : * \param[in] value The other option to read the value from.
495 : *
496 : * \return A reference to this object_info_ref.
497 : */
498 10 : std::string option_info_ref::operator + (option_info_ref const & value) const
499 : {
500 20 : return static_cast<std::string>(*this)
501 30 : + static_cast<std::string>(value);
502 : }
503 :
504 :
505 : /** \brief Concatenate a character and an option reference value.
506 : *
507 : * This operator concatenates the \p value ISO-8859-1 character to the front
508 : * of the \p rhs reference.
509 : *
510 : * \param[in] value A character to add to the left of the referred value.
511 : * \param[in] rhs The referred value.
512 : *
513 : * \return The concatenated result.
514 : */
515 13 : std::string operator + (char value, option_info_ref const & rhs)
516 : {
517 13 : if(value == '\0')
518 : {
519 6 : return rhs;
520 : }
521 7 : return value + static_cast<std::string>(rhs);
522 : }
523 :
524 :
525 : /** \brief Concatenate a character and an option reference value.
526 : *
527 : * This operator concatenates the \p value Unicode character to the front
528 : * of the \p rhs reference.
529 : *
530 : * \param[in] value A character to add to the left of the referred value.
531 : * \param[in] rhs The referred value.
532 : *
533 : * \return The concatenated result.
534 : */
535 12 : std::string operator + (char32_t value, option_info_ref const & rhs)
536 : {
537 12 : if(value == U'\0')
538 : {
539 7 : return rhs;
540 : }
541 5 : return libutf8::to_u8string(value) + static_cast<std::string>(rhs);
542 : }
543 :
544 :
545 : /** \brief Concatenate a string and an option reference value.
546 : *
547 : * This operator concatenates the \p value string to the front
548 : * of the \p rhs reference.
549 : *
550 : * \param[in] value A character to add to the left of the referred value.
551 : * \param[in] rhs The referred value.
552 : *
553 : * \return The concatenated result.
554 : */
555 5 : std::string operator + (char const * value, option_info_ref const & rhs)
556 : {
557 5 : if(value == nullptr)
558 : {
559 2 : return rhs;
560 : }
561 3 : return value + static_cast<std::string>(rhs);
562 : }
563 :
564 :
565 : /** \brief Concatenate a string and an option reference value.
566 : *
567 : * This operator concatenates the \p value string to the front
568 : * of the \p rhs reference.
569 : *
570 : * \param[in] value A character to add to the left of the referred value.
571 : * \param[in] rhs The referred value.
572 : *
573 : * \return The concatenated result.
574 : */
575 3 : std::string operator + (std::string const & value, option_info_ref const & rhs)
576 : {
577 3 : return value + static_cast<std::string>(rhs);
578 : }
579 :
580 :
581 : /** \brief Check whether the value is an empty string or not.
582 : *
583 : * This function calls the empty function and returns the opposite result.
584 : *
585 : * \return true if the value is not an empty string.
586 : */
587 2 : option_info_ref::operator bool () const
588 : {
589 2 : return !empty();
590 : }
591 :
592 :
593 : /** \brief Check whether the value is an empty string or not.
594 : *
595 : * This function calls the empty function and returns the result.
596 : *
597 : * \return true if the value is an empty string.
598 : */
599 2 : bool option_info_ref::operator ! () const
600 : {
601 2 : return empty();
602 : }
603 :
604 :
605 : /** \brief Compare this option's value with the specified string.
606 : *
607 : * This operator compares this option's value with the specified string.
608 : *
609 : * \param[in] value A string to compare this option's value with.
610 : *
611 : * \return true if both are equal.
612 : */
613 61 : bool option_info_ref::operator == (char const * value) const
614 : {
615 61 : if(value == nullptr)
616 : {
617 2 : return empty();
618 : }
619 59 : return static_cast<std::string>(*this) == value;
620 : }
621 :
622 :
623 : /** \brief Compare this option's value with the specified string.
624 : *
625 : * This operator compares this option's value with the specified string.
626 : *
627 : * \param[in] value A string to compare this option's value with.
628 : *
629 : * \return true if both are equal.
630 : */
631 42 : bool option_info_ref::operator == (std::string const & value) const
632 : {
633 42 : return static_cast<std::string>(*this) == value;
634 : }
635 :
636 :
637 : /** \brief Compare this option's value with the value of option \p value.
638 : *
639 : * This operator compares this option's value with the value of the
640 : * option specified in \p value.
641 : *
642 : * \param[in] value A string to compare this option's value with.
643 : *
644 : * \return true if both are equal.
645 : */
646 11 : bool option_info_ref::operator == (option_info_ref const & value) const
647 : {
648 11 : return static_cast<std::string>(*this) == static_cast<std::string>(value);
649 : }
650 :
651 :
652 : /** \brief Compare this option's value with the specified string.
653 : *
654 : * This operator compares this option's value with the specified string.
655 : *
656 : * \param[in] value A string to compare this option's value with.
657 : *
658 : * \return true if both are not equal.
659 : */
660 8 : bool option_info_ref::operator != (char const * value) const
661 : {
662 8 : if(value == nullptr)
663 : {
664 2 : return !empty();
665 : }
666 6 : return static_cast<std::string>(*this) != value;
667 : }
668 :
669 :
670 : /** \brief Compare this option's value with the specified string.
671 : *
672 : * This operator compares this option's value with the specified string.
673 : *
674 : * \param[in] value A string to compare this option's value with.
675 : *
676 : * \return true if both are not equal.
677 : */
678 6 : bool option_info_ref::operator != (std::string const & value) const
679 : {
680 6 : return static_cast<std::string>(*this) != value;
681 : }
682 :
683 :
684 : /** \brief Compare this option's value with the value of option \p value.
685 : *
686 : * This operator compares this option's value with the value of the
687 : * option specified in \p value.
688 : *
689 : * \param[in] value A string to compare this option's value with.
690 : *
691 : * \return true if both are not equal.
692 : */
693 6 : bool option_info_ref::operator != (option_info_ref const & value) const
694 : {
695 6 : return static_cast<std::string>(*this) != static_cast<std::string>(value);
696 : }
697 :
698 :
699 : /** \brief Compare this option's value with the specified string.
700 : *
701 : * This operator compares this option's value with the specified string.
702 : *
703 : * \param[in] value A string to compare this option's value with.
704 : *
705 : * \return true if lhs is less than rhs.
706 : */
707 8 : bool option_info_ref::operator < (char const * value) const
708 : {
709 8 : if(value == nullptr)
710 : {
711 2 : return false;
712 : }
713 6 : return static_cast<std::string>(*this) < value;
714 : }
715 :
716 :
717 : /** \brief Compare this option's value with the specified string.
718 : *
719 : * This operator compares this option's value with the specified string.
720 : *
721 : * \param[in] value A string to compare this option's value with.
722 : *
723 : * \return true if lhs is less than rhs.
724 : */
725 6 : bool option_info_ref::operator < (std::string const & value) const
726 : {
727 6 : return static_cast<std::string>(*this) < value;
728 : }
729 :
730 :
731 : /** \brief Compare this option's value with the value of option \p value.
732 : *
733 : * This operator compares this option's value with the value of the
734 : * option specified in \p value.
735 : *
736 : * \param[in] value A string to compare this option's value with.
737 : *
738 : * \return true if lhs is less than rhs.
739 : */
740 6 : bool option_info_ref::operator < (option_info_ref const & value) const
741 : {
742 6 : return static_cast<std::string>(*this) < static_cast<std::string>(value);
743 : }
744 :
745 :
746 : /** \brief Compare this option's value with the specified string.
747 : *
748 : * This operator compares this option's value with the specified string.
749 : *
750 : * \param[in] value A string to compare this option's value with.
751 : *
752 : * \return true if lhs is less or equal than rhs.
753 : */
754 8 : bool option_info_ref::operator <= (char const * value) const
755 : {
756 8 : if(value == nullptr)
757 : {
758 2 : return empty();
759 : }
760 6 : return static_cast<std::string>(*this) <= value;
761 : }
762 :
763 :
764 : /** \brief Compare this option's value with the specified string.
765 : *
766 : * This operator compares this option's value with the specified string.
767 : *
768 : * \param[in] value A string to compare this option's value with.
769 : *
770 : * \return true if lhs is less or equal than rhs.
771 : */
772 6 : bool option_info_ref::operator <= (std::string const & value) const
773 : {
774 6 : return static_cast<std::string>(*this) <= value;
775 : }
776 :
777 :
778 : /** \brief Compare this option's value with the value of option \p value.
779 : *
780 : * This operator compares this option's value with the value of the
781 : * option specified in \p value.
782 : *
783 : * \param[in] value A string to compare this option's value with.
784 : *
785 : * \return true if lhs is less or equal than rhs.
786 : */
787 6 : bool option_info_ref::operator <= (option_info_ref const & value) const
788 : {
789 6 : return static_cast<std::string>(*this) <= static_cast<std::string>(value);
790 : }
791 :
792 :
793 : /** \brief Compare this option's value with the specified string.
794 : *
795 : * This operator compares this option's value with the specified string.
796 : *
797 : * \param[in] value A string to compare this option's value with.
798 : *
799 : * \return true if lhs is greater than rhs.
800 : */
801 8 : bool option_info_ref::operator > (char const * value) const
802 : {
803 8 : if(value == nullptr)
804 : {
805 2 : return !empty();
806 : }
807 6 : return static_cast<std::string>(*this) > value;
808 : }
809 :
810 :
811 : /** \brief Compare this option's value with the specified string.
812 : *
813 : * This operator compares this option's value with the specified string.
814 : *
815 : * \param[in] value A string to compare this option's value with.
816 : *
817 : * \return true if lhs is greater than rhs.
818 : */
819 6 : bool option_info_ref::operator > (std::string const & value) const
820 : {
821 6 : return static_cast<std::string>(*this) > value;
822 : }
823 :
824 :
825 : /** \brief Compare this option's value with the value of option \p value.
826 : *
827 : * This operator compares this option's value with the value of the
828 : * option specified in \p value.
829 : *
830 : * \param[in] value A string to compare this option's value with.
831 : *
832 : * \return true if lhs is greater than rhs.
833 : */
834 6 : bool option_info_ref::operator > (option_info_ref const & value) const
835 : {
836 6 : return static_cast<std::string>(*this) > static_cast<std::string>(value);
837 : }
838 :
839 :
840 : /** \brief Compare this option's value with the specified string.
841 : *
842 : * This operator compares this option's value with the specified string.
843 : *
844 : * \param[in] value A string to compare this option's value with.
845 : *
846 : * \return true if lhs is greater or equal than rhs.
847 : */
848 8 : bool option_info_ref::operator >= (char const * value) const
849 : {
850 8 : if(value == nullptr)
851 : {
852 2 : return true;
853 : }
854 6 : return static_cast<std::string>(*this) >= value;
855 : }
856 :
857 :
858 : /** \brief Compare this option's value with the specified string.
859 : *
860 : * This operator compares this option's value with the specified string.
861 : *
862 : * \param[in] value A string to compare this option's value with.
863 : *
864 : * \return true if lhs is greater or equal than rhs.
865 : */
866 6 : bool option_info_ref::operator >= (std::string const & value) const
867 : {
868 6 : return static_cast<std::string>(*this) >= value;
869 : }
870 :
871 :
872 : /** \brief Compare this option's value with the value of option \p value.
873 : *
874 : * This operator compares this option's value with the value of the
875 : * option specified in \p value.
876 : *
877 : * \param[in] value A string to compare this option's value with.
878 : *
879 : * \return true if lhs is greater or equal than rhs.
880 : */
881 6 : bool option_info_ref::operator >= (option_info_ref const & value) const
882 : {
883 6 : return static_cast<std::string>(*this) >= static_cast<std::string>(value);
884 : }
885 :
886 :
887 :
888 : /** \brief Compare \p value with the value of the right hand-side option.
889 : *
890 : * This operator compares the specified \p value with the value of the
891 : * option specified as the \p rhs (right hand-side.)
892 : *
893 : * \param[in] value A string to compare an option's value with.
894 : * \param[in] rhs The option to compare against \p value.
895 : *
896 : * \return true if value is equal to rhs.
897 : */
898 8 : bool operator == (char const * value, option_info_ref const & rhs)
899 : {
900 8 : if(value == nullptr)
901 : {
902 2 : return rhs.empty();
903 : }
904 6 : return value == static_cast<std::string>(rhs);
905 : }
906 :
907 :
908 : /** \brief Compare \p value with the value of the right hand-side option.
909 : *
910 : * This operator compares the specified \p value with the value of the
911 : * option specified as the \p rhs (right hand-side.)
912 : *
913 : * \param[in] value A string to compare an option's value with.
914 : * \param[in] rhs The option to compare against \p value.
915 : *
916 : * \return true if value is equal to rhs.
917 : */
918 11 : bool operator == (std::string const & value, option_info_ref const & rhs)
919 : {
920 11 : return value == static_cast<std::string>(rhs);
921 : }
922 :
923 :
924 : /** \brief Compare \p value with the value of the right hand-side option.
925 : *
926 : * This operator compares the specified \p value with the value of the
927 : * option specified as the \p rhs (right hand-side.)
928 : *
929 : * \param[in] value A string to compare an option's value with.
930 : * \param[in] rhs The option to compare against \p value.
931 : *
932 : * \return true if value is not equal to rhs.
933 : */
934 8 : bool operator != (char const * value, option_info_ref const & rhs)
935 : {
936 8 : if(value == nullptr)
937 : {
938 2 : return !rhs.empty();
939 : }
940 6 : return value != static_cast<std::string>(rhs);
941 : }
942 :
943 :
944 : /** \brief Compare \p value with the value of the right hand-side option.
945 : *
946 : * This operator compares the specified \p value with the value of the
947 : * option specified as the \p rhs (right hand-side.)
948 : *
949 : * \param[in] value A string to compare an option's value with.
950 : * \param[in] rhs The option to compare against \p value.
951 : *
952 : * \return true if value is not equal to rhs.
953 : */
954 6 : bool operator != (std::string const & value, option_info_ref const & rhs)
955 : {
956 6 : return value != static_cast<std::string>(rhs);
957 : }
958 :
959 :
960 : /** \brief Compare \p value with the value of the right hand-side option.
961 : *
962 : * This operator compares the specified \p value with the value of the
963 : * option specified as the \p rhs (right hand-side.)
964 : *
965 : * \param[in] value A string to compare an option's value with.
966 : * \param[in] rhs The option to compare against \p value.
967 : *
968 : * \return true if value is considered smaller than rhs.
969 : */
970 8 : bool operator < (char const * value, option_info_ref const & rhs)
971 : {
972 8 : if(value == nullptr)
973 : {
974 2 : return !rhs.empty();
975 : }
976 6 : return value < static_cast<std::string>(rhs);
977 : }
978 :
979 :
980 : /** \brief Compare \p value with the value of the right hand-side option.
981 : *
982 : * This operator compares the specified \p value with the value of the
983 : * option specified as the \p rhs (right hand-side.)
984 : *
985 : * \param[in] value A string to compare an option's value with.
986 : * \param[in] rhs The option to compare against \p value.
987 : *
988 : * \return true if value is considered smaller than rhs.
989 : */
990 6 : bool operator < (std::string const & value, option_info_ref const & rhs)
991 : {
992 6 : return value < static_cast<std::string>(rhs);
993 : }
994 :
995 :
996 : /** \brief Compare \p value with the value of the right hand-side option.
997 : *
998 : * This operator compares the specified \p value with the value of the
999 : * option specified as the \p rhs (right hand-side.)
1000 : *
1001 : * \param[in] value A string to compare an option's value with.
1002 : * \param[in] rhs The option to compare against \p value.
1003 : *
1004 : * \return true if value is considered smaller or equal to rhs.
1005 : */
1006 8 : bool operator <= (char const * value, option_info_ref const & rhs)
1007 : {
1008 8 : if(value == nullptr)
1009 : {
1010 2 : return true;
1011 : }
1012 6 : return value <= static_cast<std::string>(rhs);
1013 : }
1014 :
1015 :
1016 : /** \brief Compare \p value with the value of the right hand-side option.
1017 : *
1018 : * This operator compares the specified \p value with the value of the
1019 : * option specified as the \p rhs (right hand-side.)
1020 : *
1021 : * \param[in] value A string to compare an option's value with.
1022 : * \param[in] rhs The option to compare against \p value.
1023 : *
1024 : * \return true if value is considered smaller or equal to rhs.
1025 : */
1026 6 : bool operator <= (std::string const & value, option_info_ref const & rhs)
1027 : {
1028 6 : return value <= static_cast<std::string>(rhs);
1029 : }
1030 :
1031 :
1032 : /** \brief Compare \p value with the value of the right hand-side option.
1033 : *
1034 : * This operator compares the specified \p value with the value of the
1035 : * option specified as the \p rhs (right hand-side.)
1036 : *
1037 : * \param[in] value A string to compare an option's value with.
1038 : * \param[in] rhs The option to compare against \p value.
1039 : *
1040 : * \return true if value is considered larger than rhs.
1041 : */
1042 8 : bool operator > (char const * value, option_info_ref const & rhs)
1043 : {
1044 8 : if(value == nullptr)
1045 : {
1046 2 : return false;
1047 : }
1048 6 : return value > static_cast<std::string>(rhs);
1049 : }
1050 :
1051 :
1052 : /** \brief Compare \p value with the value of the right hand-side option.
1053 : *
1054 : * This operator compares the specified \p value with the value of the
1055 : * option specified as the \p rhs (right hand-side.)
1056 : *
1057 : * \param[in] value A string to compare an option's value with.
1058 : * \param[in] rhs The option to compare against \p value.
1059 : *
1060 : * \return true if value is considered larger than rhs.
1061 : */
1062 6 : bool operator > (std::string const & value, option_info_ref const & rhs)
1063 : {
1064 6 : return value > static_cast<std::string>(rhs);
1065 : }
1066 :
1067 :
1068 : /** \brief Compare \p value with the value of the right hand-side option.
1069 : *
1070 : * This operator compares the specified \p value with the value of the
1071 : * option specified as the \p rhs (right hand-side.)
1072 : *
1073 : * \param[in] value A string to compare an option's value with.
1074 : * \param[in] rhs The option to compare against \p value.
1075 : *
1076 : * \return true if value is considered larger or equal to rhs.
1077 : */
1078 7 : bool operator >= (char const * value, option_info_ref const & rhs)
1079 : {
1080 7 : if(value == nullptr)
1081 : {
1082 2 : return rhs.empty();
1083 : }
1084 5 : return value >= static_cast<std::string>(rhs);
1085 : }
1086 :
1087 :
1088 : /** \brief Compare \p value with the value of the right hand-side option.
1089 : *
1090 : * This operator compares the specified \p value with the value of the
1091 : * option specified as the \p rhs (right hand-side.)
1092 : *
1093 : * \param[in] value A string to compare an option's value with.
1094 : * \param[in] rhs The option to compare against \p value.
1095 : *
1096 : * \return true if value is considered larger or equal to rhs.
1097 : */
1098 5 : bool operator >= (std::string const & value, option_info_ref const & rhs)
1099 : {
1100 5 : return value >= static_cast<std::string>(rhs);
1101 : }
1102 :
1103 :
1104 :
1105 : } // namespace advgetopt
1106 :
1107 :
1108 :
1109 2 : std::string operator + (char32_t value, std::string const & rhs)
1110 : {
1111 4 : std::string v;
1112 2 : if(value != U'\0')
1113 : {
1114 1 : v = libutf8::to_u8string(value);
1115 : }
1116 4 : return v + rhs;
1117 : }
1118 :
1119 4 : std::string operator + (std::string const & lhs, char32_t value)
1120 : {
1121 8 : std::string v;
1122 4 : if(value != U'\0')
1123 : {
1124 1 : v = libutf8::to_u8string(value);
1125 : }
1126 8 : return lhs + v;
1127 6 : }
1128 :
1129 :
1130 : // vim: ts=4 sw=4 et
|