diff options
| author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-25 14:49:04 +0000 |
|---|---|---|
| committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-25 14:49:04 +0000 |
| commit | 6e9494a9770cc90bda5da0025c96b29805fa6e88 (patch) | |
| tree | bcd498d7eb67a83b49f55d881fedc8ff05d89fb5 | |
| parent | 1fe1ae1163bcc29ac89da7fe240e6d0a2987ff84 (diff) | |
| download | busybox-w32-6e9494a9770cc90bda5da0025c96b29805fa6e88.tar.gz busybox-w32-6e9494a9770cc90bda5da0025c96b29805fa6e88.tar.bz2 busybox-w32-6e9494a9770cc90bda5da0025c96b29805fa6e88.zip | |
small improvements in str -> num convertors
git-svn-id: svn://busybox.net/trunk/busybox@16667 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | include/libbb.h | 6 | ||||
| -rw-r--r-- | include/xatonum.h | 12 | ||||
| -rw-r--r-- | libbb/xatonum.c | 16 |
3 files changed, 25 insertions, 9 deletions
diff --git a/include/libbb.h b/include/libbb.h index 152fb7e01..5cba27932 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -317,17 +317,11 @@ struct suffix_mult { | |||
| 317 | }; | 317 | }; |
| 318 | #include "xatonum.h" | 318 | #include "xatonum.h" |
| 319 | /* Specialized: */ | 319 | /* Specialized: */ |
| 320 | unsigned xatou_range(const char *numstr, unsigned lower, unsigned upper); | ||
| 321 | unsigned xatou_sfx(const char *numstr, const struct suffix_mult *suffixes); | ||
| 322 | unsigned xatou(const char *numstr); | ||
| 323 | int xatoi_range(const char *numstr, int lower, int upper); | ||
| 324 | int xatoi(const char *numstr); | ||
| 325 | /* Using xatoi() instead of naive atoi() is not always convenient - | 320 | /* Using xatoi() instead of naive atoi() is not always convenient - |
| 326 | * in many places people want *non-negative* values, but store them | 321 | * in many places people want *non-negative* values, but store them |
| 327 | * in signed int. Therefore we need this one: | 322 | * in signed int. Therefore we need this one: |
| 328 | * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */ | 323 | * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */ |
| 329 | int xatoi_u(const char *numstr); | 324 | int xatoi_u(const char *numstr); |
| 330 | uint32_t xatou32(const char *numstr); | ||
| 331 | /* Useful for reading port numbers */ | 325 | /* Useful for reading port numbers */ |
| 332 | uint16_t xatou16(const char *numstr); | 326 | uint16_t xatou16(const char *numstr); |
| 333 | 327 | ||
diff --git a/include/xatonum.h b/include/xatonum.h index cdb5e7393..46e49b0eb 100644 --- a/include/xatonum.h +++ b/include/xatonum.h | |||
| @@ -92,3 +92,15 @@ DEFINE_EQUIV_STR_CONV(int, i, l, u, ul) | |||
| 92 | #else | 92 | #else |
| 93 | DECLARE_STR_CONV(int, i, u) | 93 | DECLARE_STR_CONV(int, i, u) |
| 94 | #endif | 94 | #endif |
| 95 | |||
| 96 | /* Specialized */ | ||
| 97 | |||
| 98 | int BUG_xatou32_unimplemented(void); | ||
| 99 | extern inline uint32_t xatou32(const char *numstr) | ||
| 100 | { | ||
| 101 | if (UINT_MAX == 0xffffffff) | ||
| 102 | return xatou(numstr); | ||
| 103 | if (ULONG_MAX == 0xffffffff) | ||
| 104 | return xatoul(numstr); | ||
| 105 | return BUG_xatou32_unimplemented(); | ||
| 106 | } | ||
diff --git a/libbb/xatonum.c b/libbb/xatonum.c index 910667c14..0d487dd9b 100644 --- a/libbb/xatonum.c +++ b/libbb/xatonum.c | |||
| @@ -52,6 +52,15 @@ | |||
| 52 | #endif | 52 | #endif |
| 53 | 53 | ||
| 54 | #if UINT_MAX != ULONG_MAX | 54 | #if UINT_MAX != ULONG_MAX |
| 55 | extern inline unsigned bb_strtoui(const char *str, char **end, int b) | ||
| 56 | { | ||
| 57 | unsigned long v = strtoul(str, end, b); | ||
| 58 | if (v > UINT_MAX) { | ||
| 59 | errno = ERANGE; | ||
| 60 | return UINT_MAX; | ||
| 61 | } | ||
| 62 | return v; | ||
| 63 | } | ||
| 55 | #define type int | 64 | #define type int |
| 56 | #define xstrtou(rest) xstrtou##rest | 65 | #define xstrtou(rest) xstrtou##rest |
| 57 | #define xstrto(rest) xstrtoi##rest | 66 | #define xstrto(rest) xstrtoi##rest |
| @@ -60,7 +69,8 @@ | |||
| 60 | #define XSTR_UTYPE_MAX UINT_MAX | 69 | #define XSTR_UTYPE_MAX UINT_MAX |
| 61 | #define XSTR_TYPE_MAX INT_MAX | 70 | #define XSTR_TYPE_MAX INT_MAX |
| 62 | #define XSTR_TYPE_MIN INT_MIN | 71 | #define XSTR_TYPE_MIN INT_MIN |
| 63 | #define XSTR_STRTOU strtoul | 72 | /* libc has no strtoui, so we need to create/use our own */ |
| 73 | #define XSTR_STRTOU bb_strtoui | ||
| 64 | #include "xatonum_template.c" | 74 | #include "xatonum_template.c" |
| 65 | #undef type | 75 | #undef type |
| 66 | #undef xstrtou | 76 | #undef xstrtou |
| @@ -77,7 +87,7 @@ | |||
| 77 | 87 | ||
| 78 | int xatoi_u(const char *numstr) | 88 | int xatoi_u(const char *numstr) |
| 79 | { | 89 | { |
| 80 | return xatoul_range(numstr, 0, INT_MAX); | 90 | return xatou_range(numstr, 0, INT_MAX); |
| 81 | } | 91 | } |
| 82 | 92 | ||
| 83 | uint32_t xatou32(const char *numstr) | 93 | uint32_t xatou32(const char *numstr) |
| @@ -87,5 +97,5 @@ uint32_t xatou32(const char *numstr) | |||
| 87 | 97 | ||
| 88 | uint16_t xatou16(const char *numstr) | 98 | uint16_t xatou16(const char *numstr) |
| 89 | { | 99 | { |
| 90 | return xatoul_range(numstr, 0, 0xffff); | 100 | return xatou_range(numstr, 0, 0xffff); |
| 91 | } | 101 | } |
