]> mj.ucw.cz Git - libucw.git/blob - ucw/strtonum.h
Added functions for conversions from string to uns or uintmax_t .
[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 enum str_to_num_flags {
14   STN_SIGNED = 0x20,       // The resulting range is signed
15   STN_MINUS = 0x40,        // Allow optional '-' sign
16   STN_PLUS = 0x80,         // Allow optional '+' sign
17   STN_TRUNC = 0x100,       // Allow range overflow -> truncate to the resulting range
18   STN_DEC = 0x200,         // Support decimal numbers
19   STN_HEX = 0x400,         // Support hexadecimal numbers (0x...)
20   STN_BIN = 0x800,         // Support binary numbers (0b...)
21   STN_OCT = 0x1000,        // Support octal numbers (0o...)
22   STN_UNDERSCORE = 0x2000, // Number can contain underscore characters to increase readability (eg. 1_000_000)
23   STN_ZCHAR = 0x4000,      // Number can be terminated only by \0 character
24 };
25
26 #define STN_DBASES_MASK    0x1F
27 #define STN_SIGNS          (STN_MINUS | STN_PLUS)
28 #define STN_BASES          (STN_DEC | STN_HEX | STN_BIN | STN_OCT)
29 #define STN_FLAGS      (STN_MINUS | STN_PLUS | STN_BASES)
30 #define STN_UFLAGS     (STN_FLAGS | STN_UNDERSCORE)
31 #define STN_SFLAGS     (STN_FLAGS | STN_SIGNED)
32 #define STN_USFLAGS    (STN_SFLAGS | STN_UNDERSCORE)
33
34 #define STN_DECLARE_CONVERTOR(type, suffix)                                                  \
35 const char *str_to_##suffix(type *num, const char *str, const char **next, const uns flags)
36
37 STN_DECLARE_CONVERTOR(uns, uns);
38 STN_DECLARE_CONVERTOR(long, long);
39 STN_DECLARE_CONVERTOR(uintmax_t, uintmax);
40 STN_DECLARE_CONVERTOR(unsigned long long, ull);
41
42 #endif