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>
#include <string.h>
#include "compiler.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)   vtracedump_Putchar_Block(c)

Functions

int fprintf (__FILE *stream, const char *format,...)
static int print (char **out, const char *format, va_list args)
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 printlimit)
int printk (const char *format,...)
static int prints (char **out, const char *string, int width, int pad, int printlimit, Bool IsNumber)
int sprintf (char *out, const char *format,...)


Define Documentation

#define PAD_RIGHT   1

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

Referenced by print(), and prints().

#define PAD_ZERO   2

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

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

#define PRINT_BUF_LEN   12

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

Referenced by printi().

#define putchar (  )     vtracedump_Putchar_Block(c)

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

Referenced by printchar().


Function Documentation

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

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

00207 {
00208 return 0;
00209 }

static int print ( char **  out,
const char *  format,
va_list  args 
) [static]

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

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

Referenced by printk(), and sprintf().

00213 {
00214         register int width, pad, printlimit;
00215         register int pc = 0;
00216         char scr[2];
00217 
00218         for (; *format != 0; ++format) {
00219             if (*format == '%') {
00220                 ++format;
00221                 width = pad = printlimit = 0;
00222                 if (*format == '\0') break;
00223                 if (*format == '%') goto out;
00224                 if (*format == '-') {
00225                     ++format;
00226                     pad = PAD_RIGHT;
00227                 }
00228                 while (*format == '0') {
00229                     ++format;
00230                     pad |= PAD_ZERO;
00231                 }
00232                 for ( ; *format >= '0' && *format <= '9'; ++format) {
00233                     width *= 10;
00234                     width += *format - '0';
00235                 }
00236                         if (*format == '.') {
00237                                 ++format;
00238                         for ( ; *format >= '0' && *format <= '9'; ++format) {
00239                                    printlimit *= 10;
00240                                    printlimit += *format - '0';
00241                                 }
00242                 }
00243                         if( 0 == printlimit )
00244                            printlimit--;
00245                 if( *format == 's' ) {
00246                     register char *s = (char *)va_arg( args, int );
00247                     pc += prints (out, s?s:"(null)", width, pad, printlimit, FALSE);
00248                     continue;
00249                 }
00250                 if( *format == 'd' ) {
00251                     pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a', printlimit);
00252                     continue;
00253                 }
00254                 if( *format == 'x' ) {
00255                     pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a', printlimit);
00256                     continue;
00257                 }
00258                 if( *format == 'X' ) {
00259                     pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A', printlimit);
00260                     continue;
00261                 }
00262                 if( *format == 'u' ) {
00263                     pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a', printlimit);
00264                     continue;
00265                 }
00266                 if( *format == 'c' ) {
00267                     /* char are converted to int then pushed on the stack */
00268                     scr[0] = (char)va_arg( args, int );
00269                     scr[1] = '\0';
00270                     pc += prints (out, scr, width, pad, printlimit, FALSE);
00271                     continue;
00272                 }
00273             }
00274             else {
00275             out:
00276                 printchar (out, *format);
00277                 ++pc;
00278             }
00279         }
00280         if (out) **out = '\0';
00281         va_end( args );
00282         return pc;
00283 }

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

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

References putchar.

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

00086 {
00087         extern int putchar(int c);
00088         
00089         if (str) {
00090             **str = c;
00091             ++(*str);
00092         }
00093         else (void)putchar(c);
00094 }

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

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

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

Referenced by print().

00163 {
00164         char print_buf[PRINT_BUF_LEN];
00165         register char *s;
00166         register int t, neg = 0, pc = 0;
00167         register unsigned int u = i;
00168 
00169         if (i == 0) {
00170             print_buf[0] = '0';
00171             print_buf[1] = '\0';
00172             return prints (out, print_buf, width, pad, printlimit, TRUE);
00173         }
00174 
00175         if (sg && b == 10 && i < 0) {
00176             neg = 1;
00177             u = -i;
00178         }
00179 
00180         s = print_buf + PRINT_BUF_LEN-1;
00181         *s = '\0';
00182 
00183         while (u) {
00184             t = u % b;
00185             if( t >= 10 )
00186                 t += letbase - '0' - 10;
00187             *--s = t + '0';
00188             u /= b;
00189         }
00190 
00191         if (neg) {
00192             if( width && (pad & PAD_ZERO) ) {
00193                 printchar (out, '-');
00194                 ++pc;
00195                 --width;
00196             }
00197             else {
00198                 *--s = '-';
00199             }
00200         }
00201 
00202         return pc + prints (out, s, width, pad, printlimit, TRUE);
00203 }

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

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

References print().

00286 {
00287         va_list args;
00288         
00289         va_start( args, format );
00290         return print( 0, format, args );
00291 }

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

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

References PAD_RIGHT, PAD_ZERO, and printchar().

Referenced by print(), and printi().

00100 {
00101         register int pc = 0, padchar = ' ';
00102         int i,len;
00103 
00104         if (width > 0) {
00105             register int len = 0;
00106             register const char *ptr;
00107             for (ptr = string; *ptr; ++ptr) ++len;
00108             if (len >= width) width = 0;
00109             else width -= len;
00110             if (pad & PAD_ZERO) padchar = '0';
00111         }
00112         if (!(pad & PAD_RIGHT)) {
00113             for ( ; width > 0; --width) {
00114                 printchar (out, padchar);
00115                 ++pc;
00116             }
00117         }
00118         if( FALSE == IsNumber )
00119         {      // The string to print is not the result of a number conversion to ascii.
00120            /* For a string, printlimit is the max number of characters to display. */
00121            for ( ; printlimit && *string ; ++string, --printlimit) {
00122               printchar (out, *string);
00123               ++pc;
00124            }
00125         }
00126         if( TRUE == IsNumber )
00127         {      // The string to print represents an integer number.
00128            /* In this case, printlimit is the min number of digits to print. */
00129 
00130            /* If the length of the number to print is less than the min nb of i
00131               digits to display, we add 0 before printing the number. */
00132            len = strlen(string);
00133            if( len < printlimit )
00134            {
00135               i = printlimit - len;
00136               for(; i; i--) {
00137                  printchar (out, '0');
00138                  ++pc;
00139               }
00140            }
00141         }
00142         /* Else: The string to print is not the result of a number conversion to ascii.
00143          * For a string, printlimit is the max number of characters to display.
00144          */
00145 
00146         for ( ; printlimit && *string ; ++string, --printlimit) {
00147            printchar (out, *string);
00148            ++pc;
00149         }
00150 
00151         for ( ; width > 0; --width) {
00152             printchar (out, padchar);
00153             ++pc;
00154         }
00155 
00156         return pc;
00157 }

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


Generated on Fri Feb 19 02:22:46 2010 for AVR32 - Control Panel demonstration. by  doxygen 1.5.5