2 Unix snprintf implementation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 see header of snprintf.c.
23 int snprintf(holder, sizeof_holder, format, ...)
29 THANKS(for the patches and ideas):
35 Alain Magloire: alainm@rcsm.ee.mcgill.ca
44 #include <stdlib.h> /* for atoi() */
49 * For the FLOATING POINT FORMAT :
50 * the challenge was finding a way to
51 * manipulate the Real numbers without having
52 * to resort to mathematical function(it
53 * would require to link with -lm) and not
54 * going down to the bit pattern(not portable)
56 * so a number, a real is:
58 real = integral + fraction
60 integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0
61 fraction = b(1)*10^-1 + b(2)*10^-2 + ...
67 from then it was simple math
71 * size of the buffer for the integral part
72 * and the fraction part
74 #define MAX_INT 99 + 1 /* 1 for the null */
75 #define MAX_FRACT 29 + 1
78 * If the compiler supports (long long)
81 # define LONG_LONG long long
82 /*# define LONG_LONG int64_t*/
86 * If the compiler supports (long double)
89 # define LONG_DOUBLE long double
90 /*# define LONG_DOUBLE double*/
94 * numtoa() uses PRIVATE buffers to store the results,
95 * So this function is not reentrant
97 #define itoa(n) numtoa(n, 10, 0, (char **)0)
98 #define otoa(n) numtoa(n, 8, 0, (char **)0)
99 #define htoa(n) numtoa(n, 16, 0, (char **)0)
100 #define dtoa(n, p, f) numtoa(n, 10, p, f)
102 #define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;}
104 /* this struct holds everything we need */
115 int width, precision;
116 int justify; char pad;
117 int square, space, star_w, star_p, a_long, a_longlong;
120 #define PRIVATE static
122 /* signature of the functions */
124 /* the floating point stuff */
125 PRIVATE double pow_10(int);
126 PRIVATE int log_10(double);
127 PRIVATE double integral(double, double *);
128 PRIVATE char * numtoa(double, int, int, char **);
131 PRIVATE void conv_flag(char *, struct DATA *);
132 PRIVATE void floating(struct DATA *, double);
133 PRIVATE void exponent(struct DATA *, double);
134 PRIVATE void decimal(struct DATA *, double);
135 PRIVATE void octal(struct DATA *, double);
136 PRIVATE void hexa(struct DATA *, double);
137 PRIVATE void strings(struct DATA *, char *);
140 /* the floating point stuff */
141 PRIVATE double pow_10();
142 PRIVATE int log_10();
143 PRIVATE double integral();
144 PRIVATE char * numtoa();
147 PRIVATE void conv_flag();
148 PRIVATE void floating();
149 PRIVATE void exponent();
150 PRIVATE void decimal();
151 PRIVATE void octal();
153 PRIVATE void strings();
156 /* those are defines specific to snprintf to hopefully
157 * make the code clearer :-)
165 /* the conversion flags */
166 #define isflag(c) ((c) == '#' || (c) == ' ' || \
167 (c) == '*' || (c) == '+' || \
168 (c) == '-' || (c) == '.' || \
171 /* round off to the precision */
172 #define ROUND(d, p) \
174 d - pow_10(-(p)->precision) * 0.5 : \
175 d + pow_10(-(p)->precision) * 0.5
177 /* set default precision */
178 #define DEF_PREC(p) \
179 if ((p)->precision == NOT_FOUND) \
183 #define PUT_CHAR(c, p) \
184 if ((p)->counter < (p)->length) { \
185 *(p)->holder++ = (c); \
189 #define PUT_PLUS(d, p) \
190 if ((d) > 0. && (p)->justify == RIGHT) \
193 #define PUT_SPACE(d, p) \
194 if ((p)->space == FOUND && (d) > 0.) \
198 #define PAD_RIGHT(p) \
199 if ((p)->width > 0 && (p)->justify != LEFT) \
200 for (; (p)->width > 0; (p)->width--) \
201 PUT_CHAR((p)->pad, p)
204 #define PAD_LEFT(p) \
205 if ((p)->width > 0 && (p)->justify == LEFT) \
206 for (; (p)->width > 0; (p)->width--) \
207 PUT_CHAR((p)->pad, p)
209 /* if width and prec. in the args */
210 #define STAR_ARGS(p) \
211 if ((p)->star_w == FOUND) \
212 (p)->width = va_arg(args, int); \
213 if ((p)->star_p == FOUND) \
214 (p)->precision = va_arg(args, int)