advgetopt/validator.cpp
/** \brief Initialize the integer validator.
 *
 * The constructor accepts a string with values and ranges which are
 * used to limit the values that can be used with this parameter.
 *
 * Remember that the default does not have to be included in these
 * values. It will still be viewed as \em valid.
 *
 * The string uses the following format:
 *
 * \code
 *    start: range
 *         | start ',' range
 *
 *    range: number
 *         | number '...' number
 *
 *    number: [-+]?[0-9]+
 * \endcode
 *
 * Note that a single number is considered to be a range and is managed
 * the exact same way. A value which matches any of the ranges is considered
 * valid.
 *
 * Example:
 *
 * \code
 *     "-100...100,-1000"
 * \endcode
 *
 * This example allows all values between -100 and +100 inclusive and also
 * allows the value -1000.
 *
 * \param[in] ranges  The ranges used to limit the integer.
 */
\brief Initialize the integer validator.
The constructor accepts a string with values and ranges which are
used to limit the values that can be used with this parameter.

Remember that the default does not have to be included in these
values. It will still be viewed as \em valid.

The string uses the following format.

\code
start: range
| start ',' range





Note that a single number is considered to be a range and is managed
the exact same way. A value which matches any of the ranges is considered
valid.





This example allows all values between -100 and +100 inclusive and also
allows the value -1000.

\param[in] ranges  The ranges used to limit the integer.
\ brief Initialize the integer validator . The constructor accepts a string with values and ranges which are used to limit the values that can be used with this parameter . Remember that the default does not have to be included in these values . It will still be viewed as \ em valid . The string uses the following format . \ code start : range | start ' ' range Note that a single number is considered to be a range and is managed the exact same way . A value which matches any of the ranges is considered valid . This example allows all values between - 100 and + 100 inclusive and also allows the value - 1000 . \ param [ in ] ranges The ranges used to limit the integer .
validator_integer::validator_integer(string_list_t const & range_list)
{
    range_t range;
    for(auto r : range_list)
    {
        std::string::size_type const pos(r.find("..."));
        if(pos == std::string::npos)
        {
            if(!convert_string(r, range.f_minimum))
            {
                cppthread::log << cppthread::log_level_t::error
                               << r
                               << " is not a valid value for your ranges;"
                                  " it must only be digits, optionally preceeded by a sign (+ or -)"
                                  " and not overflow an int64_t value."
                               << cppthread::end;
                continue;
            }
            range.f_maximum = range.f_minimum;
        }
        else
        {
            std::string min_value(r.substr(0, pos));
            boost::trim(min_value);
            if(!convert_string(min_value, range.f_minimum))
            {
                cppthread::log << cppthread::log_level_t::error
                               << min_value
                               << " is not a valid value for your ranges;"
                                  " it must only be digits, optionally preceeded by a sign (+ or -)"
                                  " and not overflow an int64_t value."
                               << cppthread::end;
                continue;
            }
            std::string max_value(r.substr(pos + 3));
            boost::trim(max_value);
            if(!convert_string(max_value, range.f_maximum))
            {
                cppthread::log << cppthread::log_level_t::error
                               << max_value
                               << " is not a valid value for your ranges;"
                                  " it must only be digits, optionally preceeded by a sign (+ or -)"
                                  " and not overflow an int64_t value."
                               << cppthread::end;
                continue;
            }
            if(range.f_minimum > range.f_maximum)
            {
                cppthread::log << cppthread::log_level_t::error
                               << min_value
                               << " has to be smaller or equal to "
                               << max_value
                               << "; you have an invalid range."
                               << cppthread::end;
                continue;
            }
        }
        f_allowed_values.push_back(range);
    }
}
validator_integer :: validator_integer ( string_list_t const & range_list ) { range_t range ; for ( auto r : range_list ) { std :: string :: size_type const pos ( r . find ( " " ) ) ; if ( pos == std :: string :: npos ) { if ( ! convert_string ( r , range . f_minimum ) ) { cppthread :: log << cppthread :: log_level_t :: error << r << " " " " " " << cppthread :: end ; continue ; } range . f_maximum = range . f_minimum ; } else { std :: string min_value ( r . substr ( 0 , pos ) ) ; boost :: trim ( min_value ) ; if ( ! convert_string ( min_value , range . f_minimum ) ) { cppthread :: log << cppthread :: log_level_t :: error << min_value << " " " " " " << cppthread :: end ; continue ; } std :: string max_value ( r . substr ( pos + 3 ) ) ; boost :: trim ( max_value ) ; if ( ! convert_string ( max_value , range . f_maximum ) ) { cppthread :: log << cppthread :: log_level_t :: error << max_value << " " " " " " << cppthread :: end ; continue ; } if ( range . f_minimum > range . f_maximum ) { cppthread :: log << cppthread :: log_level_t :: error << min_value << " " << max_value << " " << cppthread :: end ; continue ; } } f_allowed_values . push_back ( range ) ; } }
\brief Initialize the integer validator.
\ brief Initialize the integer validator .