advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes | List of all members
advgetopt::validator_size Class Reference

#include <validator_size.h>

Inheritance diagram for advgetopt::validator_size:
Inheritance graph
[legend]
Collaboration diagram for advgetopt::validator_size:
Collaboration graph
[legend]

Public Types

typedef std::uint32_t flag_t
 
typedef std::shared_ptr< validatorpointer_t
 
typedef std::vector< pointer_tvector_t
 

Public Member Functions

 validator_size (string_list_t const &data)
 Initialize the size validator.
 
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 size.
 

Static Public Member Functions

static bool convert_string (std::string const &size, flag_t flags, __int128 &result)
 Convert a string to a large integer (128 bits) value representing a size.
 
static pointer_t create (std::string const &name, string_list_t const &data)
 
static pointer_t create (std::string const &name_and_params)
 Set the validator for this option.
 
static void register_validator (validator_factory const &factory)
 

Static Public Attributes

static constexpr flag_t VALIDATOR_SIZE_DEFAULT_FLAGS = 0x00
 
static constexpr flag_t VALIDATOR_SIZE_POWER_OF_TWO = 0x01
 

Private Attributes

flag_t f_flags = VALIDATOR_SIZE_DEFAULT_FLAGS
 

Detailed Description

Definition at line 48 of file validator_size.h.

Member Typedef Documentation

◆ flag_t

Definition at line 52 of file validator_size.h.

◆ pointer_t

Definition at line 64 of file validator.h.

◆ vector_t

Definition at line 65 of file validator.h.

Constructor & Destructor Documentation

◆ validator_size()

advgetopt::validator_size::validator_size ( string_list_t const flag_list)

The constructor accepts a string defining the acceptable sizes.

The string uses the following format:

flags: 'si'
| 'legacy'
constexpr flag_t option_flags_merge()
Definition flags.h:87

'si' stands for "Systeme International" (French for International System of Units); this means "1kB" will stand for "1000 bytes".

'legacy' means that one kilo bytes will be represented by 1024 bytes. So "1kB" with the legacy flag turned on represents "1024 bytes".

The 'si' and 'legacy' flags are exclusive, the last one will be effective.

Parameters
[in]flag_listThe flags used to define how to interpret the data.

Definition at line 129 of file validator_size.cpp.

References f_flags, advgetopt::option_flags_merge(), and VALIDATOR_SIZE_POWER_OF_TWO.

Member Function Documentation

◆ convert_string()

bool advgetopt::validator_size::convert_string ( std::string const value,
flag_t  flags,
__int128 result 
)
static

This function is used to convert a string to a double representing a size. The size can be specified with one of the following suffixes:

  • "B" – 1000^0 bytes
  • "kB" – 1000^1 bytes
  • "MB" – 1000^2 bytes
  • "GB" – 1000^3 bytes
  • "TB" – 1000^4 bytes
  • "PB" – 1000^5 bytes
  • "EB" – 1000^6 bytes
  • "ZB" – 1000^7 bytes
  • "YB" – 1000^8 bytes
  • "RB" – 1000^9 bytes
  • "QB" – 1000^10 bytes
  • "KiB" – 1024^1 bytes
  • "MiB" – 1024^2 bytes
  • "GiB" – 1024^3 bytes
  • "TiB" – 1024^4 bytes
  • "PiB" – 1024^5 bytes
  • "EiB" – 1024^6 bytes
  • "ZiB" – 1024^7 bytes
  • "YiB" – 1024^8 bytes
  • "RiB" – 1024^9 bytes
  • "QiB" – 1024^10 bytes

The suffix capitalization is not important since we can always distinguish both types (power of 1000 or 1024). The 'B' represents bytes either way so it does not need to be dinstinguished.

In legacy mode (VALIDATOR_SIZE_POWER_OF_TWO flag set), the 1024 power is always used.

The final result is an integer representing bytes. If you use a decimal number, it will be rounded down (floor). So "1.9B" returns 1. A decimal number is practical for larger sizes such as "1.3GiB".

Note
The result is returned in a 128 bit number because Zeta and Yeta values do not fit in 64 bits.
Todo:
We may want to support full names instead of just the minimal abbreviated suffixes (i.e. "3 bytes" fails). Also we could support bits but that is complicated because the only difference is whether you use upper or lower case characters (i.e. kB is kilo bytes, kb is kilo bits).
Parameters
[in]valueThe value to be converted to a size.
[in]flagsThe flags to determine how to interpret the suffix.
[out]resultThe resulting size in bits or bytes.
Returns
true if the conversion succeeded.

Definition at line 244 of file validator_size.cpp.

References advgetopt::validator_double::convert_string(), advgetopt::option_flags_merge(), and VALIDATOR_SIZE_POWER_OF_TWO.

Referenced by validate().

◆ create() [1/2]

validator::pointer_t advgetopt::validator::create ( std::string const name,
string_list_t const data 
)
staticinherited

◆ create() [2/2]

validator::pointer_t advgetopt::validator::create ( std::string const name_and_params)
staticinherited

This function parses the specified name and optional parameters and create a corresponding validator for this option.

The name_and_params string can be defined as:

virtual std::string name() const override
Return the name of this validator.

The list of parameters is optional. There may be an empty, just one, or any number of parameters. How the parameters are parsed is left to the validator to decide.

If the input string is empty, the current validator, if one is installed, gets removed.

Parameters
[in]name_and_paramsThe validator name and parameters.

Definition at line 626 of file validator.cpp.

References advgetopt::validator::create(), and advgetopt::option_flags_merge().

◆ name()

std::string advgetopt::validator_size::name ( ) const
overridevirtual

This function returns "size".

Returns
"size".

Implements advgetopt::validator.

Definition at line 159 of file validator_size.cpp.

◆ register_validator()

void advgetopt::validator::register_validator ( validator_factory const factory)
staticinherited

Definition at line 571 of file validator.cpp.

References advgetopt::option_flags_merge().

◆ validate()

bool advgetopt::validator_size::validate ( std::string const value) const
overridevirtual

This function verifies that the specified value is a valid size.

It makes sures that the value is a valid decimal number which optionally starts with a sign ([-+]?) and is optionally followed by a known measurement suffix.

Parameters
[in]valueThe value to validate.
Returns
true if the value validates.

Implements advgetopt::validator.

Definition at line 179 of file validator_size.cpp.

References convert_string(), f_flags, and advgetopt::option_flags_merge().

Member Data Documentation

◆ f_flags

flag_t advgetopt::validator_size::f_flags = VALIDATOR_SIZE_DEFAULT_FLAGS
private

Definition at line 72 of file validator_size.h.

Referenced by validator_size(), and validate().

◆ VALIDATOR_SIZE_DEFAULT_FLAGS

constexpr flag_t advgetopt::validator_size::VALIDATOR_SIZE_DEFAULT_FLAGS = 0x00
staticconstexpr

Definition at line 54 of file validator_size.h.

◆ VALIDATOR_SIZE_POWER_OF_TWO

constexpr flag_t advgetopt::validator_size::VALIDATOR_SIZE_POWER_OF_TWO = 0x01
staticconstexpr

Definition at line 55 of file validator_size.h.

Referenced by validator_size(), and convert_string().


The documentation for this class was generated from the following files:

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.