diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-08-19 14:53:23 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-08-19 14:53:23 +0200 |
commit | 351bf7640f040757367f5920c9626430b0664d62 (patch) | |
tree | fdfd7afc62fb06957c109ba0b7e3494cf3b3a3cd | |
parent | use CLOCK_MONOTONIC if CLOCK_MONOTONIC_RAW is not defined (diff) | |
download | mds-351bf7640f040757367f5920c9626430b0664d62.tar.gz mds-351bf7640f040757367f5920c9626430b0664d62.tar.bz2 mds-351bf7640f040757367f5920c9626430b0664d62.tar.xz |
add strict_ato* for all precise integer types, for size_t, ssize_t, and integer types that have keywords
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | doc/info/mds.texinfo | 168 | ||||
-rw-r--r-- | src/libmdsserver/util.c | 221 | ||||
-rw-r--r-- | src/libmdsserver/util.h | 188 |
3 files changed, 563 insertions, 14 deletions
diff --git a/doc/info/mds.texinfo b/doc/info/mds.texinfo index 7835753..7db4af0 100644 --- a/doc/info/mds.texinfo +++ b/doc/info/mds.texinfo @@ -6535,29 +6535,169 @@ string is not a 10-radix integer or has a value outside [@code{min}, @code{max}], @code{-1} is returned, otherwise zero is returned. -@item @code{strict_atoj} [(@code{const char* str, intmax_t* value, intmax_t min, intmax_t max}) @arrow{} @code{int}] +@item @code{strict_atou} +@fnindex @code{strict_atou} +@fnindex @code{atou} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{unsigned int} instead +of @code{int} on its parameters. + +@item @code{strict_atoh} +@fnindex @code{strict_atoh} +@fnindex @code{atoh} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{short int} instead +of @code{int} on its parameters. Note however that @code{min} and +@code{max} is in @code{int}, because smaller types should not be +passed as arguments. + +@item @code{strict_atouh} +@fnindex @code{strict_atouh} +@fnindex @code{atouh} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{unsigned short int} instead +of @code{int} on its parameters. Note however that @code{min} and +@code{max} is in @code{unsigned int}, because smaller types should +not be passed as arguments. + +@item @code{strict_atol} +@fnindex @code{strict_atol} +@fnindex @code{atol} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{long int} instead +of @code{int} on its parameters. + +@item @code{strict_atoul} +@fnindex @code{strict_atoul} +@fnindex @code{atoul} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{unsigned long int} instead +of @code{int} on its parameters. + +@item @code{strict_atoll} +@fnindex @code{strict_atoll} +@fnindex @code{atoll} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{long long int} instead +of @code{int} on its parameters. + +@item @code{strict_atoull} +@fnindex @code{strict_atoull} +@fnindex @code{atoull} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{unsigned long long int} instead +of @code{int} on its parameters. + +@item @code{strict_atoj} @fnindex @code{strict_atoj} @fnindex @code{atoj} @cpindex Integer parsing @cpindex Error management -A version of @code{atoj} that is strict about the -syntax and bounds. Parses the string @code{str} into -an @code{int} and stores it in @code{*value}. If the -string is not a 10-radix integer or has a value -outside [@code{min}, @code{max}], @code{-1} is -returned, otherwise zero is returned. +A variant of @code{strict_atoi} that uses @code{intmax_t} instead +of @code{int} on its parameters. -@item @code{strict_atouj} [(@code{const char* str, uintmax_t* value, uintmax_t min, uintmax_t max}) @arrow{} @code{int}] +@item @code{strict_atouj} @fnindex @code{strict_atouj} @fnindex @code{atouj} @cpindex Integer parsing @cpindex Error management -A version of @code{atouj} that is strict about the -syntax and bounds. Parses the string @code{str} into -an @code{int} and stores it in @code{*value}. If the -string is not a 10-radix integer or has a value -outside [@code{min}, @code{max}], @code{-1} is -returned, otherwise zero is returned. +A variant of @code{strict_atoi} that uses @code{uintmax_t} instead +of @code{int} on its parameters. + +@item @code{strict_atoz} +@fnindex @code{strict_atoz} +@fnindex @code{atoz} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{size_t} instead +of @code{int} on its parameters. + +@item @code{strict_atosz} +@fnindex @code{strict_atosz} +@fnindex @code{atosz} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{ssize_t} instead +of @code{int} on its parameters. + +@item @code{strict_ato8} +@fnindex @code{strict_ato8} +@fnindex @code{ato8} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{int8_t} instead +of @code{int} on its parameters. Note however that @code{min} and +@code{max} is in @code{int}, because smaller types should not be +passed as arguments. + +@item @code{strict_atou8} +@fnindex @code{strict_atou8} +@fnindex @code{atou8} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{uint8_t} instead +of @code{int} on its parameters. Note however that @code{min} and +@code{max} is in @code{int}, because smaller types should not be +passed as arguments. + +@item @code{strict_ato16} +@fnindex @code{strict_ato16} +@fnindex @code{ato16} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{int16_t} instead +of @code{int} on its parameters. Note however that @code{min} and +@code{max} is in @code{int}, because smaller types should not be +passed as arguments. + +@item @code{strict_atou16} +@fnindex @code{strict_atou16} +@fnindex @code{atou16} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{uint16_t} instead +of @code{int} on its parameters. Note however that @code{min} and +@code{max} is in @code{unsigned int}, because smaller types should +not be passed as arguments. + +@item @code{strict_ato32} +@fnindex @code{strict_ato32} +@fnindex @code{ato32} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{int32_t} instead +of @code{int} on its parameters. + +@item @code{strict_atou32} +@fnindex @code{strict_atou32} +@fnindex @code{atou32} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{uint32_t} instead +of @code{int} on its parameters. + +@item @code{strict_ato64} +@fnindex @code{strict_ato64} +@fnindex @code{ato64} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{int64_t} instead +of @code{int} on its parameters. + +@item @code{strict_atou64} +@fnindex @code{strict_atou64} +@fnindex @code{atou64} +@cpindex Integer parsing +@cpindex Error management +A variant of @code{strict_atoi} that uses @code{uint64_t} instead +of @code{int} on its parameters. @item @code{full_write} [(@code{int fd, const char* buffer, size_t length}) @arrow{} @code{int}] @fnindex @code{full_write} diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index f62abd2..2d6be63 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -335,6 +335,227 @@ int strict_atouj(const char* str, uintmax_t* value, uintmax_t min, uintmax_t max } +#define __strict_y(Y, TYPE, PARA_TYPE, HYPER_Y, HYPER_TYPE) \ + int strict_##Y(const char* str, TYPE* value, PARA_TYPE min, PARA_TYPE max) \ + { \ + HYPER_TYPE intermediate_value; \ + if (strict_##HYPER_Y(str, &intermediate_value, (HYPER_TYPE)min, (HYPER_TYPE)max) < 0) \ + return -1; \ + return *value = (TYPE)intermediate_value, 0; \ + } + + +#define __strict_x(X, TYPE, HYPER_X, HYPER_TYPE) \ + __strict_y(X, TYPE, TYPE, HYPER_X, HYPER_TYPE) + + +/** + * A version of `atoh` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_y(atoh, short int, int, atoi, int) + + +/** + * A version of `atouh` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_y(atouh, unsigned short int, unsigned int, atouj, uintmax_t) + + +/** + * A version of `atou` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atou, unsigned int, atouj, uintmax_t) + + +/** + * A version of `atol` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atol, long int, atoj, intmax_t) + + +/** + * A version of `atoul` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atoul, unsigned long int, atouj, uintmax_t) + + +/** + * A version of `atoll` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atoll, long long int, atoj, intmax_t) + + +/** + * A version of `atoull` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atoull, unsigned long long int, atouj, uintmax_t) + + +/** + * A version of `atoz` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atoz, size_t, atouj, uintmax_t) + + +/** + * A version of `atosz` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atosz, ssize_t, atoj, intmax_t) + + +/** + * A version of `ato8` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_y(ato8, int8_t, int, atoi, int) + + +/** + * A version of `atou8` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_y(atou8, uint8_t, int, atoi, int) + + +/** + * A version of `ato16` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_y(ato16, int16_t, int, atoi, int) + + +/** + * A version of `atou16` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_y(atou16, uint16_t, unsigned int, atouj, uintmax_t) + + +/** + * A version of `ato32` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(ato32, int32_t, atoj, intmax_t) + + +/** + * A version of `atou32` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atou32, uint32_t, atouj, uintmax_t) + + +/** + * A version of `ato64` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(ato64, int64_t, atoj, intmax_t) + + +/** + * A version of `atou64` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +__strict_x(atou64, uint64_t, atouj, uintmax_t) + + +#undef __strict_x + + /** * Send a buffer into a file and ignore interruptions * diff --git a/src/libmdsserver/util.h b/src/libmdsserver/util.h index c84156d..4761dc7 100644 --- a/src/libmdsserver/util.h +++ b/src/libmdsserver/util.h @@ -132,6 +132,194 @@ int strict_atoj(const char* str, intmax_t* value, intmax_t min, intmax_t max); int strict_atouj(const char* str, uintmax_t* value, uintmax_t min, uintmax_t max); /** + * A version of `atoh` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atoh(const char* str, short int* value, int min, int max); + +/** + * A version of `atouh` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atouh(const char* str, unsigned short int* value, unsigned int min, unsigned int max); + +/** + * A version of `atou` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atou(const char* str, unsigned int* value, unsigned int min, unsigned int max); + +/** + * A version of `atol` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atol(const char* str, long int* value, long int min, long int max); + +/** + * A version of `atoul` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atoul(const char* str, unsigned long int* value, unsigned long int min, unsigned long int max); + +/** + * A version of `atoll` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atoll(const char* str, long long int* value, long long int min, long long int max); + +/** + * A version of `atoull` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atoull(const char* str, unsigned long long int* value, + unsigned long long int min, unsigned long long int max); + +/** + * A version of `atoz` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atoz(const char* str, size_t* value, size_t min, size_t max); + +/** + * A version of `atosz` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atosz(const char* str, ssize_t* value, ssize_t min, ssize_t max); + +/** + * A version of `ato8` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_ato8(const char* str, int8_t* value, int min, int max); + +/** + * A version of `atou8` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atou8(const char* str, uint8_t* value, int min, int max); + +/** + * A version of `ato16` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_ato16(const char* str, int16_t* value, int min, int max); + +/** + * A version of `atou16` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atou16(const char* str, uint16_t* value, unsigned int min, unsigned int max); + +/** + * A version of `ato32` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_ato32(const char* str, int32_t* value, int32_t min, int32_t max); + +/** + * A version of `atou32` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atou32(const char* str, uint32_t* value, uint32_t min, uint32_t max); + +/** + * A version of `ato64` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_ato64(const char* str, int64_t* value, int64_t min, int64_t max); + +/** + * A version of `atou64` that is strict about the syntax and bounds + * + * @param str The text to parse + * @param value Slot in which to store the value + * @param min The minimum accepted value + * @param max The maximum accepted value + * @return Zero on success, -1 on syntax error + */ +int strict_atou64(const char* str, uint64_t* value, uint64_t min, uint64_t max); + +/** * Send a buffer into a file and ignore interruptions * * @param fd The file descriptor |