]>
gcc.gnu.org Git - gcc.git/blob - gcc/doprint.c
1 /* Provide a version _doprnt in terms of fprintf.
2 By Kaveh Ghazi (ghazi@caip.rutgers.edu) 3/29/98
3 Copyright (C) 1998 Free Software Foundation, Inc.
11 #ifdef TEST /* Make sure to use the internal one. */
12 #define _doprnt my_doprnt
17 const int value = abs (va_arg (ap, int)); \
19 ptr++; /* Go past the asterisk. */ \
20 *sptr = '\0'; /* NULL terminate sptr. */ \
21 sprintf(buf, "%d", value); \
23 while (*sptr) sptr++; \
26 #define PRINT_CHAR(CHAR) \
34 #define PRINT_TYPE(TYPE) \
37 TYPE value = va_arg (ap, TYPE); \
38 *sptr++ = *ptr++; /* Copy the type specifier. */ \
39 *sptr = '\0'; /* NULL terminate sptr. */ \
40 result = fprintf(stream, specifier, value); \
45 total_printed += result; \
51 _doprnt (format
, ap
, stream
)
56 const char * ptr
= format
;
58 int total_printed
= 0;
62 if (*ptr
!= '%') /* While we have regular characters, print them. */
64 else /* We got a format specifier! */
66 char * sptr
= specifier
;
67 int wide_width
= 0, short_width
= 0;
69 *sptr
++ = *ptr
++; /* Copy the % and move forward. */
71 while (strchr ("-+ #0", *ptr
)) /* Move past flags. */
77 while (isdigit(*ptr
)) /* Handle explicit numeric value. */
82 *sptr
++ = *ptr
++; /* Copy and go past the period. */
86 while (isdigit(*ptr
)) /* Handle explicit numeric value. */
89 while (strchr ("hlL", *ptr
))
118 /* Short values are promoted to int, so just copy it
119 as an int and trust the C library printf to cast it
120 to the right width. */
135 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
136 PRINT_TYPE(long long);
138 PRINT_TYPE(long); /* Fake it and hope for the best. */
141 } /* End of switch (wide_width) */
142 } /* End of else statement */
143 } /* End of integer case */
155 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
156 PRINT_TYPE(long double);
158 PRINT_TYPE(double); /* Fake it and hope for the best. */
174 } /* End of switch (*ptr) */
175 } /* End of else statement */
178 return total_printed
;
185 #define M_PI (3.1415926535897932385)
188 #define RESULT(x) do \
191 printf ("printed %d characters\n", i); \
195 static int checkit
PVPROTO ((const char * format
, ...)) ATTRIBUTE_PRINTF_1
;
198 checkit
VPROTO ((const char* format
, ...))
207 VA_START (args
, format
);
210 format
= va_arg (args
, char *);
213 result
= _doprnt (format
, args
, stdout
);
222 RESULT(checkit ("<%d>\n", 0x12345678));
223 RESULT(printf ("<%d>\n", 0x12345678));
225 RESULT(checkit ("<%200d>\n", 5));
226 RESULT(printf ("<%200d>\n", 5));
228 RESULT(checkit ("<%.300d>\n", 6));
229 RESULT(printf ("<%.300d>\n", 6));
231 RESULT(checkit ("<%100.150d>\n", 7));
232 RESULT(printf ("<%100.150d>\n", 7));
234 RESULT(checkit ("<%s>\n",
235 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
236 777777777777777777333333333333366666666666622222222222777777777777733333"));
237 RESULT(printf ("<%s>\n",
238 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
239 777777777777777777333333333333366666666666622222222222777777777777733333"));
241 RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
242 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
243 RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
244 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
246 RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI
, M_PI
, M_PI
));
247 RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI
, M_PI
, M_PI
));
249 RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI
, 3, M_PI
, 3, 3, M_PI
));
250 RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI
, 3, M_PI
, 3, 3, M_PI
));
252 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
253 75, 75, 75, 75, 75, 75, 75));
254 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
255 75, 75, 75, 75, 75, 75, 75));
257 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
258 75, 75, 75, 75, 75, 75, 75));
259 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
260 75, 75, 75, 75, 75, 75, 75));
262 RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
263 RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
265 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
266 RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
267 RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
268 RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
269 RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
272 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
273 RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
274 1.23456, 1.234567890123456789L, 1.23456));
275 RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
276 1.23456, 1.234567890123456789L, 1.23456));
This page took 0.055411 seconds and 6 git commands to generate.