OSDN Git Service

update copyrights
[pf3gnuchains/gcc-fork.git] / 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.
4  */
5
6 #include "config.h"
7 #include "system.h"
8 #undef _doprnt
9
10 #ifdef TEST /* Make sure to use the internal one. */
11 #define _doprnt my_doprnt
12 #endif
13
14 #define COPY_VA_INT \
15   do { \
16          const int value = abs (va_arg (ap, int)); \
17          char buf[32]; \
18          ptr++; /* Go past the asterisk. */ \
19          *sptr = '\0'; /* NULL terminate sptr. */ \
20          sprintf(buf, "%d", value); \
21          strcat(sptr, buf); \
22          while (*sptr) sptr++; \
23      } while (0)
24
25 #define PRINT_CHAR(CHAR) \
26   do { \
27          putc(CHAR, stream); \
28          ptr++; \
29          total_printed++; \
30          continue; \
31      } while (0)
32
33 #define PRINT_TYPE(TYPE) \
34   do { \
35         int result; \
36         TYPE value = va_arg (ap, TYPE); \
37         *sptr++ = *ptr++; /* Copy the type specifier. */ \
38         *sptr = '\0'; /* NULL terminate sptr. */ \
39         result = fprintf(stream, specifier, value); \
40         if (result == -1) \
41           return -1; \
42         else \
43           { \
44             total_printed += result; \
45             continue; \
46           } \
47       } while (0)
48
49 int
50 _doprnt (format, ap, stream)
51   const char * format;
52   va_list ap;
53   FILE * stream;
54 {
55   const char * ptr = format;
56   char specifier[128];
57   int total_printed = 0;
58   
59   while (*ptr != '\0')
60     {
61       if (*ptr != '%') /* While we have regular characters, print them. */
62         PRINT_CHAR(*ptr);
63       else /* We got a format specifier! */
64         {
65           char * sptr = specifier;
66           int wide_width = 0, short_width = 0;
67           
68           *sptr++ = *ptr++; /* Copy the % and move forward. */
69
70           while (strchr ("-+ #0", *ptr)) /* Move past flags. */
71             *sptr++ = *ptr++;
72
73           if (*ptr == '*')
74             COPY_VA_INT;
75           else
76             while (isdigit(*ptr)) /* Handle explicit numeric value. */
77               *sptr++ = *ptr++;
78           
79           if (*ptr == '.')
80             {
81               *sptr++ = *ptr++; /* Copy and go past the period. */
82               if (*ptr == '*')
83                 COPY_VA_INT;
84               else
85                 while (isdigit(*ptr)) /* Handle explicit numeric value. */
86                   *sptr++ = *ptr++;
87             }
88           while (strchr ("hlL", *ptr))
89             {
90               switch (*ptr)
91                 {
92                 case 'h':
93                   short_width = 1;
94                   break;
95                 case 'l':
96                   wide_width++;
97                   break;
98                 case 'L':
99                   wide_width = 2;
100                   break;
101                 default:
102                   abort();
103                 }
104               *sptr++ = *ptr++;
105             }
106
107           switch (*ptr)
108             {
109             case 'd':
110             case 'i':
111             case 'o':
112             case 'u':
113             case 'x':
114             case 'X':
115             case 'c':
116               {
117                 /* Short values are promoted to int, so just copy it
118                    as an int and trust the C library printf to cast it
119                    to the right width. */
120                 if (short_width)
121                   PRINT_TYPE(int);
122                 else
123                   {
124                     switch (wide_width)
125                       {
126                       case 0:
127                         PRINT_TYPE(int);
128                         break;
129                       case 1:
130                         PRINT_TYPE(long);
131                         break;
132                       case 2:
133                       default:
134 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
135                         PRINT_TYPE(long long);
136 #else
137                         PRINT_TYPE(long); /* Fake it and hope for the best. */
138 #endif
139                         break;
140                       } /* End of switch (wide_width) */
141                   } /* End of else statement */
142               } /* End of integer case */
143               break;
144             case 'f':
145             case 'e':
146             case 'E':
147             case 'g':
148             case 'G':
149               {
150                 if (wide_width == 0)
151                   PRINT_TYPE(double);
152                 else
153                   {
154 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
155                     PRINT_TYPE(long double);
156 #else
157                     PRINT_TYPE(double); /* Fake it and hope for the best. */
158 #endif
159                   }
160               }
161               break;
162             case 's':
163               PRINT_TYPE(char *);
164               break;
165             case 'p':
166               PRINT_TYPE(void *);
167               break;
168             case '%':
169               PRINT_CHAR('%');
170               break;
171             default:
172               abort();
173             } /* End of switch (*ptr) */
174         } /* End of else statement */
175     }
176
177   return total_printed;
178 }
179
180 #ifdef TEST
181
182 #include <math.h>
183 #ifndef M_PI
184 #define M_PI (3.1415926535897932385)
185 #endif
186
187 #define RESULT(x) do \
188 { \
189     int i = (x); \
190     printf ("printed %d characters\n", i); \
191     fflush(stdin); \
192 } while (0)
193
194 static int checkit PVPROTO ((const char * format, ...)) ATTRIBUTE_PRINTF_1;
195
196 static int
197 checkit VPROTO ((const char* format, ...))
198 {
199   va_list args;
200   int result;
201
202 #ifndef ANSI_PROTOTYPES
203   char *format;
204 #endif
205
206   VA_START (args, format);
207
208 #ifndef ANSI_PROTOTYPES
209   format = va_arg (args, char *);
210 #endif
211
212   result = _doprnt (format, args, stdout);
213   va_end(args);
214
215   return result;
216 }
217
218 int
219 main ()
220 {
221   RESULT(checkit ("<%d>\n", 0x12345678));
222   RESULT(printf ("<%d>\n", 0x12345678));
223
224   RESULT(checkit ("<%200d>\n", 5));
225   RESULT(printf ("<%200d>\n", 5));
226
227   RESULT(checkit ("<%.300d>\n", 6));
228   RESULT(printf ("<%.300d>\n", 6));
229
230   RESULT(checkit ("<%100.150d>\n", 7));
231   RESULT(printf ("<%100.150d>\n", 7));
232
233   RESULT(checkit ("<%s>\n",
234                   "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
235 777777777777777777333333333333366666666666622222222222777777777777733333"));
236   RESULT(printf ("<%s>\n",
237                  "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
238 777777777777777777333333333333366666666666622222222222777777777777733333"));
239
240   RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
241                   1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
242   RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
243                  1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
244
245   RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
246   RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
247
248   RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
249   RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
250
251   RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
252                   75, 75, 75, 75, 75, 75, 75));
253   RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
254                  75, 75, 75, 75, 75, 75, 75));
255
256   RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
257                   75, 75, 75, 75, 75, 75, 75));
258   RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
259                  75, 75, 75, 75, 75, 75, 75));
260
261   RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
262   RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
263
264 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
265   RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
266   RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
267   RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
268   RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
269 #endif
270
271 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
272   RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
273                   1.23456, 1.234567890123456789L, 1.23456));
274   RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
275                  1.23456, 1.234567890123456789L, 1.23456));
276 #endif
277
278   return 0;
279 }
280 #endif /* TEST */