blob: cf088a49eafe0bfcc913431b9268d138c4936116 [file] [log] [blame]
Denis Vlasenkoed836cd2006-11-25 14:44:13 +00001/* 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/* Provides extern declarations of functions */
11#define DECLARE_STR_CONV(type, T, UT) \
12\
13unsigned type xstrto##UT##_range_sfx(const char *str, int b, unsigned type l, unsigned type u, const struct suffix_mult *sfx); \
14unsigned type xstrto##UT##_range(const char *str, int b, unsigned type l, unsigned type u); \
15unsigned type xstrto##UT##_sfx(const char *str, int b, const struct suffix_mult *sfx); \
16unsigned type xstrto##UT(const char *str, int b); \
17unsigned type xato##UT##_range_sfx(const char *str, unsigned type l, unsigned type u, const struct suffix_mult *sfx); \
18unsigned type xato##UT##_range(const char *str, unsigned type l, unsigned type u); \
19unsigned type xato##UT##_sfx(const char *str, const struct suffix_mult *sfx); \
20unsigned type xato##UT(const char *str); \
21type xstrto##T##_range_sfx(const char *str, int b, type l, type u, const struct suffix_mult *sfx) ;\
22type xstrto##T##_range(const char *str, int b, type l, type u); \
23type xato##T##_range_sfx(const char *str, type l, type u, const struct suffix_mult *sfx); \
24type xato##T##_range(const char *str, type l, type u); \
25type xato##T##_sfx(const char *str, const struct suffix_mult *sfx); \
26type xato##T(const char *str); \
27
28/* Unsigned long long functions always exist */
29DECLARE_STR_CONV(long long, ll, ull)
30
31
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000032/* Provides inline definitions of functions */
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000033/* (useful for mapping them to the type of the same width) */
34#define DEFINE_EQUIV_STR_CONV(narrow, N, W, UN, UW) \
35\
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000036static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000037unsigned narrow xstrto##UN##_range_sfx(const char *str, int b, unsigned narrow l, unsigned narrow u, const struct suffix_mult *sfx) \
38{ return xstrto##UW##_range_sfx(str, b, l, u, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000039static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000040unsigned narrow xstrto##UN##_range(const char *str, int b, unsigned narrow l, unsigned narrow u) \
41{ return xstrto##UW##_range(str, b, l, u); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000042static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000043unsigned narrow xstrto##UN##_sfx(const char *str, int b, const struct suffix_mult *sfx) \
44{ return xstrto##UW##_sfx(str, b, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000045static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000046unsigned narrow xstrto##UN(const char *str, int b) \
47{ return xstrto##UW(str, b); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000048static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000049unsigned narrow xato##UN##_range_sfx(const char *str, unsigned narrow l, unsigned narrow u, const struct suffix_mult *sfx) \
50{ return xato##UW##_range_sfx(str, l, u, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000051static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000052unsigned narrow xato##UN##_range(const char *str, unsigned narrow l, unsigned narrow u) \
53{ return xato##UW##_range(str, l, u); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000054static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000055unsigned narrow xato##UN##_sfx(const char *str, const struct suffix_mult *sfx) \
56{ return xato##UW##_sfx(str, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000057static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000058unsigned narrow xato##UN(const char *str) \
59{ return xato##UW(str); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000060static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000061narrow xstrto##N##_range_sfx(const char *str, int b, narrow l, narrow u, const struct suffix_mult *sfx) \
62{ return xstrto##W##_range_sfx(str, b, l, u, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000063static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000064narrow xstrto##N##_range(const char *str, int b, narrow l, narrow u) \
65{ return xstrto##W##_range(str, b, l, u); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000066static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000067narrow xato##N##_range_sfx(const char *str, narrow l, narrow u, const struct suffix_mult *sfx) \
68{ return xato##W##_range_sfx(str, l, u, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000069static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000070narrow xato##N##_range(const char *str, narrow l, narrow u) \
71{ return xato##W##_range(str, l, u); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000072static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000073narrow xato##N##_sfx(const char *str, const struct suffix_mult *sfx) \
74{ return xato##W##_sfx(str, sfx); } \
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000075static ATTRIBUTE_ALWAYS_INLINE \
Denis Vlasenkoed836cd2006-11-25 14:44:13 +000076narrow xato##N(const char *str) \
77{ return xato##W(str); } \
78
79/* If long == long long, then just map them one-to-one */
80#if ULONG_MAX == ULLONG_MAX
81DEFINE_EQUIV_STR_CONV(long, l, ll, ul, ull)
82#else
83/* Else provide extern defs */
84DECLARE_STR_CONV(long, l, ul)
85#endif
86
87/* Same for int -> [long] long */
88#if UINT_MAX == ULLONG_MAX
89DEFINE_EQUIV_STR_CONV(int, i, ll, u, ull)
90#elif UINT_MAX == ULONG_MAX
91DEFINE_EQUIV_STR_CONV(int, i, l, u, ul)
92#else
93DECLARE_STR_CONV(int, i, u)
94#endif
Denis Vlasenko43bddf32006-11-25 14:49:04 +000095
96/* Specialized */
97
98int BUG_xatou32_unimplemented(void);
Denis Vlasenkoa597aad2006-12-16 23:48:13 +000099static ATTRIBUTE_ALWAYS_INLINE uint32_t xatou32(const char *numstr)
Denis Vlasenko43bddf32006-11-25 14:49:04 +0000100{
101 if (UINT_MAX == 0xffffffff)
102 return xatou(numstr);
103 if (ULONG_MAX == 0xffffffff)
104 return xatoul(numstr);
105 return BUG_xatou32_unimplemented();
106}
Denis Vlasenkod686a042006-11-27 14:43:21 +0000107
108/* Non-aborting kind of convertors */
109
110unsigned long long bb_strtoull(const char *arg, char **endp, int base);
111long long bb_strtoll(const char *arg, char **endp, int base);
112
113#if ULONG_MAX == ULLONG_MAX
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000114static ATTRIBUTE_ALWAYS_INLINE
Denis Vlasenkod686a042006-11-27 14:43:21 +0000115unsigned long bb_strtoul(const char *arg, char **endp, int base)
116{ return bb_strtoull(arg, endp, base); }
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000117static ATTRIBUTE_ALWAYS_INLINE
118long bb_strtol(const char *arg, char **endp, int base)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000119{ return bb_strtoll(arg, endp, base); }
120#else
121unsigned long bb_strtoul(const char *arg, char **endp, int base);
122long bb_strtol(const char *arg, char **endp, int base);
123#endif
124
125#if UINT_MAX == ULLONG_MAX
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000126static ATTRIBUTE_ALWAYS_INLINE
127unsigned bb_strtou(const char *arg, char **endp, int base)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000128{ return bb_strtoull(arg, endp, base); }
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000129static ATTRIBUTE_ALWAYS_INLINE
130int bb_strtoi(const char *arg, char **endp, int base)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000131{ return bb_strtoll(arg, endp, base); }
132#elif UINT_MAX == ULONG_MAX
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000133static ATTRIBUTE_ALWAYS_INLINE
134unsigned bb_strtou(const char *arg, char **endp, int base)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000135{ return bb_strtoul(arg, endp, base); }
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000136static ATTRIBUTE_ALWAYS_INLINE
137int bb_strtoi(const char *arg, char **endp, int base)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000138{ return bb_strtol(arg, endp, base); }
139#else
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000140unsigned bb_strtou(const char *arg, char **endp, int base);
141int bb_strtoi(const char *arg, char **endp, int base);
Denis Vlasenkod686a042006-11-27 14:43:21 +0000142#endif
143
144int BUG_bb_strtou32_unimplemented(void);
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000145static ATTRIBUTE_ALWAYS_INLINE
Denis Vlasenkod686a042006-11-27 14:43:21 +0000146uint32_t bb_strtou32(const char *arg, char **endp, int base)
147{
148 if (sizeof(uint32_t) == sizeof(unsigned))
149 return bb_strtou(arg, endp, base);
150 if (sizeof(uint32_t) == sizeof(unsigned long))
151 return bb_strtoul(arg, endp, base);
152 return BUG_bb_strtou32_unimplemented();
153}
154
155/* Floating point */
156
157/* double bb_strtod(const char *arg, char **endp); */