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 /src/libmdsserver | |
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>
Diffstat (limited to 'src/libmdsserver')
-rw-r--r-- | src/libmdsserver/util.c | 221 | ||||
-rw-r--r-- | src/libmdsserver/util.h | 188 |
2 files changed, 409 insertions, 0 deletions
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 |