1 |
2 |
drasko |
/* Copyright (C) 2003 Manuel Novoa III
|
2 |
|
|
*
|
3 |
|
|
* This library is free software; you can redistribute it and/or
|
4 |
|
|
* modify it under the terms of the GNU Lesser General Public
|
5 |
|
|
* License as published by the Free Software Foundation; either
|
6 |
|
|
* version 2.1 of the License, or (at your option) any later version.
|
7 |
|
|
*
|
8 |
|
|
* The GNU C Library is distributed in the hope that it will be useful,
|
9 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
11 |
|
|
* Lesser General Public License for more details.
|
12 |
|
|
*
|
13 |
|
|
* You should have received a copy of the GNU Lesser General Public
|
14 |
|
|
* License along with the GNU C Library; if not, write to the Free
|
15 |
|
|
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
16 |
|
|
* 02111-1307 USA.
|
17 |
|
|
*/
|
18 |
|
|
|
19 |
|
|
/* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
|
20 |
|
|
*
|
21 |
|
|
* This code is currently under development. Also, I plan to port
|
22 |
|
|
* it to elks which is a 16-bit environment with a fairly limited
|
23 |
|
|
* compiler. Therefore, please refrain from modifying this code
|
24 |
|
|
* and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
|
25 |
|
|
*
|
26 |
|
|
* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
|
27 |
|
|
|
28 |
|
|
#ifndef _UINTMAXTOSTR_H
|
29 |
|
|
#define _UINTMAXTOSTR_H 1
|
30 |
|
|
|
31 |
|
|
#ifdef _FEATURES_H
|
32 |
|
|
# ifndef __USE_ISOC99
|
33 |
|
|
# error features was included without defining _ISOC99_SOURCE!
|
34 |
|
|
# endif
|
35 |
|
|
#else
|
36 |
|
|
# ifndef _ISOC99_SOURCE
|
37 |
|
|
# define _ISOC99_SOURCE
|
38 |
|
|
# endif
|
39 |
|
|
#endif
|
40 |
|
|
|
41 |
|
|
#include <features.h>
|
42 |
|
|
#include <limits.h>
|
43 |
|
|
#include <stdint.h>
|
44 |
|
|
|
45 |
|
|
#if INTMAX_MAX <= 2147483647L
|
46 |
|
|
#define __UIM_BUFLEN 12 /* 10 digits + 1 nul + 1 sign */
|
47 |
|
|
#elif INTMAX_MAX <= 9223372036854775807LL
|
48 |
|
|
#define __UIM_BUFLEN 22 /* 20 digits + 1 nul + 1 sign */
|
49 |
|
|
#else
|
50 |
|
|
#error unknown number of digits for intmax_t!
|
51 |
|
|
#endif
|
52 |
|
|
|
53 |
|
|
#ifdef LLONG_MAX /* --------------- */
|
54 |
|
|
#if LLONG_MAX <= 2147483647L
|
55 |
|
|
#define __UIM_BUFLEN_LLONG 12 /* 10 digits + 1 nul + 1 sign */
|
56 |
|
|
#elif LLONG_MAX <= 9223372036854775807LL
|
57 |
|
|
#define __UIM_BUFLEN_LLONG 22 /* 20 digits + 1 nul + 1 sign */
|
58 |
|
|
#else
|
59 |
|
|
#error unknown number of digits for long long!
|
60 |
|
|
#endif
|
61 |
|
|
#endif /* ULLONG_MAX ----------------------------- */
|
62 |
|
|
|
63 |
|
|
#if LONG_MAX <= 2147483647L
|
64 |
|
|
#define __UIM_BUFLEN_LONG 12 /* 10 digits + 1 nul + 1 sign */
|
65 |
|
|
#elif LONG_MAX <= 9223372036854775807LL
|
66 |
|
|
#define __UIM_BUFLEN_LONG 22 /* 20 digits + 1 nul + 1 sign */
|
67 |
|
|
#else
|
68 |
|
|
#error unknown number of digits for long!
|
69 |
|
|
#endif
|
70 |
|
|
|
71 |
|
|
#if INT_MAX <= 32767
|
72 |
|
|
#define __UIM_BUFLEN_INT 7 /* 10 digits + 1 nul + 1 sign */
|
73 |
|
|
#elif INT_MAX <= 2147483647L
|
74 |
|
|
#define __UIM_BUFLEN_INT 12 /* 10 digits + 1 nul + 1 sign */
|
75 |
|
|
#else
|
76 |
|
|
#error unknown number of digits for int!
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
typedef enum {
|
80 |
|
|
__UIM_DECIMAL = 0,
|
81 |
|
|
__UIM_GROUP = ',', /* Base 10 locale-dependent grouping. */
|
82 |
|
|
__UIM_LOWER = 'a' - 10,
|
83 |
|
|
__UIM_UPPER = 'A' - 10,
|
84 |
|
|
} __UIM_CASE;
|
85 |
|
|
|
86 |
|
|
/* Convert the int val to a string in base abs(base). val is treated as
|
87 |
|
|
* an unsigned ??? int type if base > 0, and signed if base < 0. This
|
88 |
|
|
* is an internal function with _no_ error checking done unless assert()s
|
89 |
|
|
* are enabled.
|
90 |
|
|
*
|
91 |
|
|
* Note: bufend is a pointer to the END of the buffer passed.
|
92 |
|
|
* Call like this:
|
93 |
|
|
* char buf[SIZE], *p;
|
94 |
|
|
* p = _xltostr(buf + sizeof(buf) - 1, {unsigned int}, 10, __UIM_DECIMAL)
|
95 |
|
|
* p = _xltostr(buf + sizeof(buf) - 1, {int}, -10, __UIM_DECIMAL)
|
96 |
|
|
*
|
97 |
|
|
* WARNING: If base > 10, case _must_be_ either __UIM_LOWER or __UIM_UPPER
|
98 |
|
|
* for lower and upper case alphas respectively.
|
99 |
|
|
* WARNING: If val is really a signed type, make sure base is negative!
|
100 |
|
|
* Otherwise, you could overflow your buffer.
|
101 |
|
|
*/
|
102 |
|
|
extern char *_uintmaxtostr(char * __restrict bufend, uintmax_t uval,
|
103 |
|
|
int base, __UIM_CASE alphacase) attribute_hidden;
|
104 |
|
|
|
105 |
|
|
/* TODO -- make this either a (possibly inline) function? */
|
106 |
|
|
#ifndef __BCC__
|
107 |
|
|
#define _int10tostr(bufend, intval) \
|
108 |
|
|
_uintmaxtostr((bufend), (intval), -10, __UIM_DECIMAL)
|
109 |
|
|
#else /* bcc doesn't do prototypes, we need to explicitly cast */
|
110 |
|
|
#define _int10tostr(bufend, intval) \
|
111 |
|
|
_uintmaxtostr((bufend), (uintmax_t)(intval), -10, __UIM_DECIMAL)
|
112 |
|
|
#endif
|
113 |
|
|
|
114 |
|
|
#define __BUFLEN_INT10TOSTR __UIM_BUFLEN_INT
|
115 |
|
|
|
116 |
|
|
#endif /* _UINTMAXTOSTR_H */
|