printf-stdarg.c File Reference


Detailed Description

sprintf functions to replace newlib for AVR32 UC3.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file printf-stdarg.c.

#include <sys/reent.h>
#include <stdarg.h>

Go to the source code of this file.

Defines

#define PAD_RIGHT   1
#define PAD_ZERO   2
#define PRINT_BUF_LEN   12
#define putchar(c)   print_dbg_char(c)

Functions

int fprintf (__FILE *stream, const char *format,...)
static void printchar (char **str, int c)
static int printi (char **out, int i, int b, int sg, int width, int pad, int letbase)
int printk (const char *format,...)
int printk_va (char **out, const char *format, va_list args)
static int prints (char **out, const char *string, int width, int pad)
int sprintf (char *out, const char *format,...)


Define Documentation

#define PAD_RIGHT   1

Definition at line 86 of file printf-stdarg.c.

Referenced by printk_va(), and prints().

#define PAD_ZERO   2

Definition at line 87 of file printf-stdarg.c.

Referenced by printi(), printk_va(), and prints().

#define PRINT_BUF_LEN   12

Definition at line 120 of file printf-stdarg.c.

Referenced by printi().

#define putchar (  )     print_dbg_char(c)

Definition at line 73 of file printf-stdarg.c.

Referenced by printchar().


Function Documentation

int fprintf ( __FILE *  stream,
const char *  format,
  ... 
)

Definition at line 165 of file printf-stdarg.c.

00166 {
00167 return 0;
00168 }

static void printchar ( char **  str,
int  c 
) [static]

Definition at line 75 of file printf-stdarg.c.

References putchar.

Referenced by printi(), printk_va(), and prints().

00076 {
00077     extern int putchar(int c);
00078     
00079     if (str) {
00080         **str = c;
00081         ++(*str);
00082     }
00083     else (void)putchar(c);
00084 }

static int printi ( char **  out,
int  i,
int  b,
int  sg,
int  width,
int  pad,
int  letbase 
) [static]

Definition at line 122 of file printf-stdarg.c.

References PAD_ZERO, PRINT_BUF_LEN, printchar(), and prints().

Referenced by printk_va().

00123 {
00124     char print_buf[PRINT_BUF_LEN];
00125     register char *s;
00126     register int t, neg = 0, pc = 0;
00127     register unsigned int u = i;
00128 
00129     if (i == 0) {
00130         print_buf[0] = '0';
00131         print_buf[1] = '\0';
00132         return prints (out, print_buf, width, pad);
00133     }
00134 
00135     if (sg && b == 10 && i < 0) {
00136         neg = 1;
00137         u = -i;
00138     }
00139 
00140     s = print_buf + PRINT_BUF_LEN-1;
00141     *s = '\0';
00142 
00143     while (u) {
00144         t = u % b;
00145         if( t >= 10 )
00146             t += letbase - '0' - 10;
00147         *--s = t + '0';
00148         u /= b;
00149     }
00150 
00151     if (neg) {
00152         if( width && (pad & PAD_ZERO) ) {
00153             printchar (out, '-');
00154             ++pc;
00155             --width;
00156         }
00157         else {
00158             *--s = '-';
00159         }
00160     }
00161 
00162     return pc + prints (out, s, width, pad);
00163 }

int printk ( const char *  format,
  ... 
)

int printk_va ( char **  out,
const char *  format,
va_list  args 
)

Definition at line 169 of file printf-stdarg.c.

References PAD_RIGHT, PAD_ZERO, printchar(), printi(), prints(), and width.

Referenced by printk(), and sprintf().

00170 {
00171     register int width, pad;
00172     register int pc = 0;
00173     char scr[2];
00174 
00175     for (; *format != 0; ++format) {
00176         if (*format == '%') {
00177             ++format;
00178             width = pad = 0;
00179             if (*format == '\0') break;
00180             if (*format == '%') goto out;
00181             if (*format == '-') {
00182                 ++format;
00183                 pad = PAD_RIGHT;
00184             }
00185             while (*format == '0') {
00186                 ++format;
00187                 pad |= PAD_ZERO;
00188             }
00189             for ( ; *format >= '0' && *format <= '9'; ++format) {
00190                 width *= 10;
00191                 width += *format - '0';
00192             }
00193             if( *format == 's' ) {
00194                 register char *s = (char *)va_arg( args, int );
00195                 pc += prints (out, s?s:"(null)", width, pad);
00196                 continue;
00197             }
00198             if( *format == 'd' ) {
00199                 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
00200                 continue;
00201             }
00202                         if( *format == 'p' ) {
00203                                 pad = 8;
00204                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
00205                 continue;
00206             }
00207             if( *format == 'x' ) {
00208                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
00209                 continue;
00210             }
00211             if( *format == 'X' ) {
00212                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
00213                 continue;
00214             }
00215             if( *format == 'u' ) {
00216                 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
00217                 continue;
00218             }
00219             if( *format == 'c' ) {
00220                 /* char are converted to int then pushed on the stack */
00221                 scr[0] = (char)va_arg( args, int );
00222                 scr[1] = '\0';
00223                 pc += prints (out, scr, width, pad);
00224                 continue;
00225             }
00226         }
00227         else {
00228         out:
00229             printchar (out, *format);
00230             ++pc;
00231         }
00232     }
00233     if (out) **out = '\0';
00234     va_end( args );
00235     return pc;
00236 }

static int prints ( char **  out,
const char *  string,
int  width,
int  pad 
) [static]

Definition at line 89 of file printf-stdarg.c.

References PAD_RIGHT, PAD_ZERO, and printchar().

Referenced by printi(), and printk_va().

00090 {
00091     register int pc = 0, padchar = ' ';
00092 
00093     if (width > 0) {
00094         register int len = 0;
00095         register const char *ptr;
00096         for (ptr = string; *ptr; ++ptr) ++len;
00097         if (len >= width) width = 0;
00098         else width -= len;
00099         if (pad & PAD_ZERO) padchar = '0';
00100     }
00101     if (!(pad & PAD_RIGHT)) {
00102         for ( ; width > 0; --width) {
00103             printchar (out, padchar);
00104             ++pc;
00105         }
00106     }
00107     for ( ; *string ; ++string) {
00108         printchar (out, *string);
00109         ++pc;
00110     }
00111     for ( ; width > 0; --width) {
00112         printchar (out, padchar);
00113         ++pc;
00114     }
00115 
00116     return pc;
00117 }

int sprintf ( char *  out,
const char *  format,
  ... 
)

Definition at line 246 of file printf-stdarg.c.

References printk_va().

00247 {
00248         va_list args;
00249         
00250         va_start( args, format );
00251         return printk_va( &out, format, args );
00252 }


Generated on Thu Dec 17 19:57:55 2009 for AVR32 - H&D by  doxygen 1.5.5