diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:51:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:51:24 +0000 |
commit | f7548d6d28c313cf80e6f3ef89aed16a19815df1 (patch) | |
tree | a3f6f2a3f247293bee59ecd28e8cd8ceb6ca064a /src/lib/strnum.h | |
parent | Initial commit. (diff) | |
download | dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.tar.xz dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.zip |
Adding upstream version 1:2.3.19.1+dfsg1.upstream/1%2.3.19.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/strnum.h')
-rw-r--r-- | src/lib/strnum.h | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/src/lib/strnum.h b/src/lib/strnum.h new file mode 100644 index 0000000..471d4aa --- /dev/null +++ b/src/lib/strnum.h @@ -0,0 +1,192 @@ +#ifndef STRNUM_H +#define STRNUM_H + +/* str_to_*() functions return 0 if string is nothing more than a valid number + in valid range. Otherwise -1 is returned and num_r is left untouched + + str_parse_*() helpers do not require the number to be the entire string + and pass back the pointer just past a valid parsed integer in endp_r if + it is non-NULL. What is written to endp_r in error cases is undefined. +*/ + +/* + * Unsigned decimal + */ + +int str_to_uint(const char *str, unsigned int *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint(const char *str, unsigned int *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_ulong(const char *str, unsigned long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_ulong(const char *str, unsigned long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_ullong(const char *str, unsigned long long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_ullong(const char *str, unsigned long long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uint32(const char *str, uint32_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint32(const char *str, uint32_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uint64(const char *str, uint64_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint64(const char *str, uint64_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uintmax(const char *str, uintmax_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uintmax(const char *str, uintmax_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +/* Returns TRUE if str is a valid unsigned number that equals to num. */ +bool str_uint_equals(const char *str, uintmax_t num); + +/* + * Unsigned hexadecimal + */ + +int str_to_uint_hex(const char *str, unsigned int *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint_hex(const char *str, unsigned int *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_ulong_hex(const char *str, unsigned long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_ulong_hex(const char *str, unsigned long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_ullong_hex(const char *str, unsigned long long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_ullong_hex(const char *str, unsigned long long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uint32_hex(const char *str, uint32_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint32_hex(const char *str, uint32_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uint64_hex(const char *str, uint64_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint64_hex(const char *str, uint64_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uintmax_hex(const char *str, uintmax_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uintmax_hex(const char *str, uintmax_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +/* + * Unsigned octal + */ + +int str_to_uint_oct(const char *str, unsigned int *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint_oct(const char *str, unsigned int *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_ulong_oct(const char *str, unsigned long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_ulong_oct(const char *str, unsigned long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_ullong_oct(const char *str, unsigned long long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_ullong_oct(const char *str, unsigned long long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uint32_oct(const char *str, uint32_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint32_oct(const char *str, uint32_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uint64_oct(const char *str, uint64_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uint64_oct(const char *str, uint64_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_uintmax_oct(const char *str, uintmax_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uintmax_oct(const char *str, uintmax_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +/* + * Signed + */ + +int str_to_int(const char *str, int *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_int(const char *str, int *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_long(const char *str, long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_long(const char *str, long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_llong(const char *str, long long *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_llong(const char *str, long long *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_int32(const char *str, int32_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_int32(const char *str, int32_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_int64(const char *str, int64_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_int64(const char *str, int64_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_intmax(const char *str, intmax_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_intmax(const char *str, intmax_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +/* + * Special numeric types + */ + +int str_to_uid(const char *str, uid_t *num_r) + ATTR_WARN_UNUSED_RESULT; + +int str_to_gid(const char *str, gid_t *num_r) + ATTR_WARN_UNUSED_RESULT; + +int str_to_pid(const char *str, pid_t *num_r) + ATTR_WARN_UNUSED_RESULT; + +int str_to_ino(const char *str, ino_t *num_r) + ATTR_WARN_UNUSED_RESULT; + +int str_to_uoff(const char *str, uoff_t *num_r) + ATTR_WARN_UNUSED_RESULT; +int str_parse_uoff(const char *str, uoff_t *num_r, + const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3); + +int str_to_time(const char *str, time_t *num_r) + ATTR_WARN_UNUSED_RESULT; + +/* + * Utility + */ + +/* Return TRUE if all characters in string are numbers. + Stop when `end_char' is found from string. */ +bool str_is_numeric(const char *str, char end_char) ATTR_PURE; + +/* Return TRUE when string has one or more numbers, followed + with zero or one dot, followed with at least one number. */ +bool str_is_float(const char *str, char end_char) ATTR_PURE; + +/* Returns human readable string about what is wrong with the string. + This function assumes that str_to_*() had already returned -1 for the + string. */ +const char *str_num_error(const char *str); + +#endif |