diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-25 14:44:13 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-25 14:44:13 +0000 |
commit | ed836cdc30642ddbecc286b279d461ca44135cbb (patch) | |
tree | 70735d4bd1e34de43aa3f8092446caf460bd2540 /libbb/xatonum.c | |
parent | 809a6e310443d0b5010c8b293cab0160608c1b02 (diff) | |
download | busybox-w32-ed836cdc30642ddbecc286b279d461ca44135cbb.tar.gz busybox-w32-ed836cdc30642ddbecc286b279d461ca44135cbb.tar.bz2 busybox-w32-ed836cdc30642ddbecc286b279d461ca44135cbb.zip |
regularize str -> num convertors
Diffstat (limited to 'libbb/xatonum.c')
-rw-r--r-- | libbb/xatonum.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/libbb/xatonum.c b/libbb/xatonum.c new file mode 100644 index 000000000..910667c14 --- /dev/null +++ b/libbb/xatonum.c | |||
@@ -0,0 +1,91 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * ascii-to-numbers implementations for busybox | ||
4 | * | ||
5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include "libbb.h" | ||
11 | |||
12 | #define type long long | ||
13 | #define xstrtou(rest) xstrtoull##rest | ||
14 | #define xstrto(rest) xstrtoll##rest | ||
15 | #define xatou(rest) xatoull##rest | ||
16 | #define xato(rest) xatoll##rest | ||
17 | #define XSTR_UTYPE_MAX ULLONG_MAX | ||
18 | #define XSTR_TYPE_MAX LLONG_MAX | ||
19 | #define XSTR_TYPE_MIN LLONG_MIN | ||
20 | #define XSTR_STRTOU strtoull | ||
21 | #include "xatonum_template.c" | ||
22 | #undef type | ||
23 | #undef xstrtou | ||
24 | #undef xstrto | ||
25 | #undef xatou | ||
26 | #undef xato | ||
27 | #undef XSTR_UTYPE_MAX | ||
28 | #undef XSTR_TYPE_MAX | ||
29 | #undef XSTR_TYPE_MIN | ||
30 | #undef XSTR_STRTOU | ||
31 | |||
32 | #if ULONG_MAX != ULLONG_MAX | ||
33 | #define type long | ||
34 | #define xstrtou(rest) xstrtoul##rest | ||
35 | #define xstrto(rest) xstrtol##rest | ||
36 | #define xatou(rest) xatoul##rest | ||
37 | #define xato(rest) xatol##rest | ||
38 | #define XSTR_UTYPE_MAX ULONG_MAX | ||
39 | #define XSTR_TYPE_MAX LONG_MAX | ||
40 | #define XSTR_TYPE_MIN LONG_MIN | ||
41 | #define XSTR_STRTOU strtoul | ||
42 | #include "xatonum_template.c" | ||
43 | #undef type | ||
44 | #undef xstrtou | ||
45 | #undef xstrto | ||
46 | #undef xatou | ||
47 | #undef xato | ||
48 | #undef XSTR_UTYPE_MAX | ||
49 | #undef XSTR_TYPE_MAX | ||
50 | #undef XSTR_TYPE_MIN | ||
51 | #undef XSTR_STRTOU | ||
52 | #endif | ||
53 | |||
54 | #if UINT_MAX != ULONG_MAX | ||
55 | #define type int | ||
56 | #define xstrtou(rest) xstrtou##rest | ||
57 | #define xstrto(rest) xstrtoi##rest | ||
58 | #define xatou(rest) xatou##rest | ||
59 | #define xato(rest) xatoi##rest | ||
60 | #define XSTR_UTYPE_MAX UINT_MAX | ||
61 | #define XSTR_TYPE_MAX INT_MAX | ||
62 | #define XSTR_TYPE_MIN INT_MIN | ||
63 | #define XSTR_STRTOU strtoul | ||
64 | #include "xatonum_template.c" | ||
65 | #undef type | ||
66 | #undef xstrtou | ||
67 | #undef xstrto | ||
68 | #undef xatou | ||
69 | #undef xato | ||
70 | #undef XSTR_UTYPE_MAX | ||
71 | #undef XSTR_TYPE_MAX | ||
72 | #undef XSTR_TYPE_MIN | ||
73 | #undef XSTR_STRTOU | ||
74 | #endif | ||
75 | |||
76 | /* A few special cases */ | ||
77 | |||
78 | int xatoi_u(const char *numstr) | ||
79 | { | ||
80 | return xatoul_range(numstr, 0, INT_MAX); | ||
81 | } | ||
82 | |||
83 | uint32_t xatou32(const char *numstr) | ||
84 | { | ||
85 | return xatoul_range(numstr, 0, 0xffffffff); | ||
86 | } | ||
87 | |||
88 | uint16_t xatou16(const char *numstr) | ||
89 | { | ||
90 | return xatoul_range(numstr, 0, 0xffff); | ||
91 | } | ||