2 * Copyright ArcCore AB
\r
4 * A simple implementation of all formatted xxprintf functionallity.
\r
10 * What you normally would do is to print to a buffer of some kind and then
\r
11 * call write(). However little stack indicates that we can't place buffers
\r
12 * on the stack and reentrant tells us that we can't have a static buffer.
\r
13 * That leaves us with printing characters as it is ready. From a speed
\r
14 * point of view that is less than optimal, but that the way it's got to be.
\r
15 * (Could make a 16 long char buffer??)
\r
17 * This is just intended to be used as a replacement for newlibs implementation.
\r
18 * Newlib porting interface have the following API:
\r
19 * int write( int fd, char *buf, int nbytes)
\r
22 * Note that puts(), putc() are still the newlib variants....
\r
24 * printf() -> vfprintf(stdout,) -> vsnprintf(buf,)
\r
26 * vprintf() -> vfprintf(stdout,) -> vsnprintf(buf,)
\r
28 * sprintf(buf,) -> vsnprintf(buf,)
\r
29 * snprintf(buf,) -> vsnprintf(buf,)
\r
31 * IMPLEMENTATION NOTE:
\r
32 * If printing more than the limit, e.g. using vsnprintf() then
\r
33 * the emit function will only stop printing, but not interrupted
\r
34 * (The code will get more complicated that way)
\r
43 //#define HOST_TEST 1
\r
46 #define _STDOUT stdout
\r
47 #define _STDIN stdin
\r
48 #define _STDERR stderr
\r
50 #define _STDOUT (FILE *)STDOUT_FILENO
\r
51 #define _STDINT STDIN_FILENO
\r
52 #define _STDERR (FILE *)STDERR_FILENO
\r
56 int arc_putchar(int fd, int c);
\r
57 int print(FILE *file, char **buffer, size_t n, const char *format, va_list ap);
\r
59 int printf(const char *format, ...) {
\r
63 va_start(ap, format);
\r
64 rv = vfprintf(_STDOUT, format, ap);
\r
69 int fprintf(FILE *file, const char *format, ...) {
\r
73 va_start(ap, format);
\r
74 rv = vfprintf(file, format, ap);
\r
79 int sprintf(char *buffer, const char *format, ...) {
\r
83 va_start(ap, format);
\r
84 rv = vsnprintf(buffer, ~(size_t)0, format, ap);
\r
90 int snprintf(char *buffer, size_t n, const char *format, ...) {
\r
94 va_start(ap, format);
\r
95 rv = vsnprintf(buffer, n, format, ap);
\r
100 int vprintf(const char *format, va_list ap) {
\r
101 return vfprintf(_STDOUT, format, ap);
\r
104 int vsprintf(char *buffer, const char *format, va_list ap) {
\r
105 return vsnprintf(buffer, ~(size_t)0, format, ap);
\r
108 int vfprintf(FILE *file, const char *format, va_list ap) {
\r
110 /* Just print to _STDOUT */
\r
111 rv = print(file,NULL,~(size_t)0, format,ap);
\r
115 int vfiprintf(FILE *file, const char *format, va_list ap) {
\r
116 return vfprintf(file,format,ap);
\r
119 int vsnprintf(char *buffer, size_t n, const char *format, va_list ap) {
\r
122 rv = print(NULL, &buffer, n, format,ap);
\r
129 * @param file The file to print to
\r
130 * @param buf The buffer to print to
\r
131 * @param c The char to print
\r
134 static inline int emitChar( FILE *file, char **buf, char c, int *left ) {
\r
135 if( (*left) == 1 ) {
\r
139 if( buf == NULL ) {
\r
144 arc_putchar((int)file, c);
\r
154 #if defined(HOST_TEST)
\r
156 * Convert a number to a string
\r
158 * @param val The value to convert
\r
159 * @param str Pointer to a space where to put the string
\r
160 * @param base The base
\r
161 * @param negative If negative or not.
\r
163 void xtoa( unsigned long val, char* str, int base, int negative) {
\r
172 if( base < 10 && base > 16 ) {
\r
179 str[i++] = "0123456789abcdef"[ val % base ];
\r
180 } while ((val /= base) > 0);
\r
189 while(str > oStr) {
\r
196 extern void xtoa( unsigned long val, char* str, int base, int negative);
\r
200 #define FL_NONE (0<<0)
\r
201 #define FL_ZERO (1<<1)
\r
202 #define FL_HASH (1<<2)
\r
203 #define FL_SPACE (1<<3)
\r
204 #define FL_ALIGN_LEFT (1<<4)
\r
205 #define FL_TYPE_SIGNED_INT (1<<5)
\r
206 #define FL_TYPE_UNSIGNED_INT (1<<6)
\r
209 static void emitString( FILE *file, char **buffer, char *string, int width, int flags, int *left) {
\r
211 char *str = string;
\r
214 /* padding: FL_ZERO or normal ' '
\r
215 * align: FL_ALIGN_LEFT (left) or normal (right)
\r
217 pad = (flags & FL_ZERO) ? '0' : ' ';
\r
220 if( flags & FL_ALIGN_LEFT ) {
\r
221 for(i=0;str[i]; i++) {
\r
222 emitChar(file,buffer,str[i],left);
\r
226 for(;i<width;i++) {
\r
227 emitChar(file,buffer,pad,left);
\r
231 strLen = strlen(string);
\r
234 if( width > strLen ) {
\r
235 for(i=0;i<(width-strLen);i++) {
\r
236 emitChar(file,buffer,pad,left);
\r
240 for(i=0;i<strLen; i++) {
\r
241 emitChar(file,buffer,string[i],left);
\r
246 void emitInt( FILE *file, char **buffer, int val, int base, int width, int flags, int *left )
\r
248 char lBuf[12]; // longest is base 10, 2^32
\r
251 if( flags & FL_TYPE_SIGNED_INT ) {
\r
252 xtoa(val,str,base ,(val < 0));
\r
254 xtoa((unsigned)val,str,base ,0);
\r
257 emitString(file,buffer,str,width,flags,left);
\r
260 #define PRINT_CHAR(_c) *buffer++= (_c);
\r
264 * Write formatted output to an array with a maximum number of characters.
\r
266 * This is the mother of the formatted print family. The main goal here
\r
267 * is to be very small and memory efficient.
\r
271 * Flags : '-' and '0'
\r
272 * Width : Normal padding is supported, '*' is not.
\r
276 * Type : d,u,x,s,and c
\r
278 * @param file The file descriptor
\r
279 * @param buffer A pointer to the place to store the output
\r
280 * If NULL the output is instead
\r
281 * @param n The maximum number of characters to write
\r
282 * @param format The format string
\r
283 * @param ap The va list
\r
286 int print(FILE *file, char **buffer, size_t n, const char *format, va_list ap)
\r
294 while ( (ch = *format++) ) {
\r
300 emitChar(file,buffer,ch,&left);
\r
313 flags = FL_ALIGN_LEFT;
\r
316 /* Not supported or no flag */
\r
325 if( (ch >= '0') && (ch <= '9') ) {
\r
335 emitChar(file,buffer,(char )va_arg( ap, int ),&left);
\r
338 flags |= FL_TYPE_SIGNED_INT;
\r
339 emitInt(file,buffer,va_arg( ap, int ),10,width,flags,&left);
\r
342 flags |= FL_TYPE_UNSIGNED_INT;
\r
343 emitInt(file,buffer,va_arg( ap, int ),10,width,flags,&left);
\r
346 flags |= FL_TYPE_UNSIGNED_INT;
\r
347 emitInt(file,buffer,va_arg( ap, int ),16,width,flags,&left);
\r
350 str = (char *)va_arg( ap, int );
\r
352 if( str == NULL ) {
\r
356 emitString(file,buffer,str,width,flags,&left);
\r
365 emitChar(file,buffer,ch,&left);
\r
368 // va_end(ap); // Removed, TODO: Check the va_start/va_end handling (used in calling functions also).
\r
371 emitChar(file,buffer,'\0',&left);
\r
373 return 0; // Wrong.. but for now.
\r
376 #if defined(HOST_TEST)
\r
381 printf("char: %c %c = a B\n", 'a', 66);
\r
383 printf("string: %s = (null)\n", (char *)ptr);
\r
385 printf("string: %s = foobar \n", "foobar");
\r
387 printf("string: %2s = foobar \n", "foobar");
\r
388 printf("string: \"%8s\" = \" foobar\" \n", "foobar");
\r
390 printf("string: \"%-8s\" = \"foobar \" \n", "foobar");
\r
392 printf("decimal: 23 = %d \n", 23);
\r
393 printf("hex: c23 = %x \n", 0xc23);
\r
394 printf("hex: 0c23 = %04x \n", 0xc23);
\r
395 printf("decimal with blanks: 23 = %6d \n", 23);
\r
396 printf("decimal with zero: 000023 = %06d \n", 23);
\r
398 /* negative and large numbers */
\r
399 printf("decimal: -23 = %d \n", -23);
\r
400 printf("decimal: 4294967273 = %u \n", -23);
\r
401 printf("decimal: c0000000 = %x \n", 0xc0000000);
\r
403 printf("decimal: 00c000 = %06x \n", 0xc000);
\r
405 fprintf(_STDOUT, "string: %s = foobar \n", "foobar");
\r
406 sprintf(buff, "string: %s = foobar \n", "foobar");
\r
409 snprintf(buff,10, "%s\n", "12345678901234567");
\r
410 printf("\"123456789\" = \"%s\"\n",buff);
\r
412 snprintf(buff,12, "%s\n", "abcdefghijklmn");
\r
413 printf("\"abcdefghijkl\" = \"%s\"\n",buff);
\r