NAME

nbdkit_parse_int, nbdkit_parse_unsigned, nbdkit_parse_int8_t, nbdkit_parse_int16_t, nbdkit_parse_int32_t, nbdkit_parse_int64_t, nbdkit_parse_uint8_t, nbdkit_parse_uint16_t, nbdkit_parse_uint32_t, nbdkit_parse_uint64_t - parse numbers for nbdkit

SYNOPSIS

 #include <nbdkit-plugin.h>

 int nbdkit_parse_int (const char *what, const char *str, int *r);
 int nbdkit_parse_unsigned (const char *what,
                            const char *str, unsigned *r);
 int nbdkit_parse_int8_t (const char *what,
                          const char *str, int8_t *r);
 int nbdkit_parse_uint8_t (const char *what,
                           const char *str, uint8_t *r);
 int nbdkit_parse_int16_t (const char *what,
                           const char *str, int16_t *r);
 int nbdkit_parse_uint16_t (const char *what,
                            const char *str, uint16_t *r);
 int nbdkit_parse_int32_t (const char *what,
                           const char *str, int32_t *r);
 int nbdkit_parse_uint32_t (const char *what,
                            const char *str, uint32_t *r);
 int nbdkit_parse_int64_t (const char *what,
                           const char *str, int64_t *r);
 int nbdkit_parse_uint64_t (const char *what,
                            const char *str, uint64_t *r);

DESCRIPTION

Parse string str into an integer of various types. These functions parse a decimal, hexadecimal ("0x...") or octal ("0...") number.

These functions deal correctly with overflow, out of range and parse errors, and you should use them instead of unsafe functions like sscanf(3), atoi(3) and similar.

The what parameter is printed in error messages to provide context. It should usually be a short descriptive string of what you are trying to parse, eg:

 if (nbdkit_parse_int ("random seed", value, &seed) == -1)
   return -1;

might print an error:

 random seed: could not parse number: "lalala"

RETURN VALUE

On success the functions return 0 and set *r to the parsed value (unless *r == NULL in which case the result is discarded).

On error, nbdkit_error(3) is called and the functions return -1. On error *r is always unchanged.

HISTORY

nbdkit_parse_int and the others were added in nbdkit 1.16.

SEE ALSO

nbdkit(1), nbdkit_parse_bool(3), nbdkit_parse_delay(3), nbdkit_parse_probability(3), nbdkit_parse_size(3), nbdkit-plugin(3), nbdkit-filter(3).

AUTHORS

Richard W.M. Jones

COPYRIGHT

Copyright Red Hat

LICENSE

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.