]> mj.ucw.cz Git - libucw.git/blob - ucw/strtonum.h
Update build of manpages, contd.
[libucw.git] / ucw / strtonum.h
1 /*
2  *      UCW Library -- Conversions of Strings to Numbers: Declarations
3  *
4  *      (c) 2010 Daniel Fiala <danfiala@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _STRTONUM_H
11 #define _STRTONUM_H
12
13 #ifdef CONFIG_UCW_CLEAN_ABI
14 #define str_to_u32 ucw_str_to_u32
15 #define str_to_u64 ucw_str_to_u64
16 #define str_to_uint ucw_str_to_uint
17 #define str_to_uintmax ucw_str_to_uintmax
18 #define str_to_uns ucw_str_to_uns
19 #endif
20
21 // Set (flags & 0x1f) in the range 1 to 31 to denote the default base of the number
22 enum str_to_num_flags {
23   STN_SIGNED = 0x20,       // The resulting range is signed
24   STN_MINUS = 0x40,        // Allow optional '-' sign
25   STN_PLUS = 0x80,         // Allow optional '+' sign
26   STN_TRUNC = 0x100,       // Allow range overflow -> truncate to the allowed range
27   STN_DEC = 0x200,         // Support decimal numbers (currently no prefix)
28   STN_HEX = 0x400,         // Support hexadecimal numbers (0x...)
29   STN_BIN = 0x800,         // Support binary numbers (0b...)
30   STN_OCT = 0x1000,        // Support octal numbers (0o...)
31   STN_OCT0 = 0x2000,       // Support octal numbers (0[0-7]...)
32   STN_UNDERSCORE = 0x4000, // Number can contain underscore characters to increase readability (eg. 1_000_000)
33   STN_WHOLE = 0x8000,      // Number can be terminated only by \0 character
34 };
35
36 #define STN_DBASES_MASK    0x1F
37 #define STN_SIGNS          (STN_MINUS | STN_PLUS)
38 #define STN_BASES          (STN_DEC | STN_HEX | STN_BIN | STN_OCT)
39 #define STN_BASES0         (STN_BASES | STN_OCT0)
40 #define STN_FLAGS          (STN_MINUS | STN_PLUS | STN_BASES)
41 #define STN_UFLAGS         (STN_FLAGS | STN_UNDERSCORE)
42 #define STN_SFLAGS         (STN_FLAGS | STN_SIGNED)
43 #define STN_USFLAGS        (STN_SFLAGS | STN_UNDERSCORE)
44
45 #define STN_DECLARE_CONVERTOR(type, suffix)                                                               \
46 const char *str_to_##suffix(type *num, const char *str, const char **next, const uint flags)
47
48 #define STN_SIGNED_CONVERTOR(type, suffix, usuffix)                                                       \
49 static inline const char *str_to_##suffix(type *num, const char *str, const char **next, const uint flags) \
50 {                                                                                                         \
51   return str_to_##usuffix((void *) num, str, next, flags | STN_SIGNED | STN_PLUS | STN_MINUS);            \
52 }
53
54 STN_DECLARE_CONVERTOR(uint, uint);
55 STN_SIGNED_CONVERTOR(int, int, uint)
56
57 STN_DECLARE_CONVERTOR(u32, u32);
58 STN_SIGNED_CONVERTOR(s32, s32, u32)
59
60 STN_DECLARE_CONVERTOR(u64, u64);
61 STN_SIGNED_CONVERTOR(s64, s64, u64)
62
63 STN_DECLARE_CONVERTOR(uintmax_t, uintmax);
64 STN_SIGNED_CONVERTOR(intmax_t, intmax, uintmax)
65
66 // FIXME: For backward compatibility, will be removed soon
67 STN_DECLARE_CONVERTOR(uns, uns);
68
69 #endif