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