X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=string.c;h=78d54f67fd992f099f287233184343f912a05941;hp=510592abc4675970da9dc3a79356293cecf67e49;hb=9be19e7946fd413df61eca5e9a934834c5757494;hpb=f6f50d03a09d6bc423324206d274336e9905bbb4 diff --git a/string.c b/string.c index 510592ab..78d54f67 100644 --- a/string.c +++ b/string.c @@ -319,7 +319,7 @@ __must_check __malloc char *para_homedir(void) * * \return The number of substrings found in \a args. */ -__must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim) +__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim) { char *p = args; char **argv; @@ -548,3 +548,64 @@ __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...) } return ret; } + +#ifndef LLONG_MAX +#define LLONG_MAX (1 << (sizeof(long) - 1)) +#endif +#ifndef LLONG_MIN +#define LLONG_MIN (-LLONG_MAX - 1LL) +#endif + +/** + * Convert a string to a 64-bit signed integer value. + * + * \param str The string to be converted. + * \param value Result pointer. + * + * \return Positive on success, negative on errors. + * + * \sa para_atoi32(), strtol(3), atoi(3). + */ +int para_atoi64(const char *str, int64_t *value) +{ + char *endptr; + long long tmp; + + errno = 0; /* To distinguish success/failure after call */ + tmp = strtoll(str, &endptr, 10); + if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN)) + return -E_ATOI_OVERFLOW; + if (errno != 0 && tmp == 0) /* other error */ + return -E_STRTOLL; + if (endptr == str) + return -E_ATOI_NO_DIGITS; + if (*endptr != '\0') /* Further characters after number */ + return -E_ATOI_JUNK_AT_END; + *value = tmp; + return 1; +} + +/** + * Convert a string to a 32-bit signed integer value. + * + * \param str The string to be converted. + * \param value Result pointer. + * + * \return Positive on success, negative on errors. + * + * \sa para_atoi64(). +*/ +int para_atoi32(const char *str, int32_t *value) +{ + int64_t tmp; + int ret; + const int32_t max = 2147483647; + + ret = para_atoi64(str, &tmp); + if (ret < 0) + return ret; + if (tmp > max || tmp < -max - 1) + return -E_ATOI_OVERFLOW; + *value = tmp; + return 1; +}