convert to UNIX line endings.

This commit is contained in:
Mariano Alvira 2010-06-01 15:06:55 -04:00
parent de7ed4812d
commit 6aab3625a2

View file

@ -33,399 +33,399 @@
* $Id$ * $Id$
*/ */
/** /**
* \file printf-stdarg.c * \file printf-stdarg.c
* *
* \brief sprintf functions to replace newlib for AVR32 UC3. * \brief sprintf functions to replace newlib for AVR32 UC3.
* *
* \author $Author: umanzoli $ * \author $Author: umanzoli $
* *
* Created on : 17-mar-2009 * Created on : 17-mar-2009
* *
* $Id$ * $Id$
*/ */
/* /*
* Copyright 2001, 2002 Georges Menie (www.menie.org) * Copyright 2001, 2002 Georges Menie (www.menie.org)
* stdarg version contributed by Christian Ettinger * stdarg version contributed by Christian Ettinger
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <mc1322x.h> #include <mc1322x.h>
#include <types.h> #include <types.h>
#define __putc(x) uart1_putc(x) #define __putc(x) uart1_putc(x)
/** /**
* Structure to hold data to be passed to print function with format. * Structure to hold data to be passed to print function with format.
* Aka print context. * Aka print context.
*/ */
struct __print_ctx_t struct __print_ctx_t
{ {
//! pointer to next char to be filled. //! pointer to next char to be filled.
char* _ptr; char* _ptr;
//! maximum length of the buffer. //! maximum length of the buffer.
size_t _max_len; size_t _max_len;
}; };
typedef struct __print_ctx_t _print_ctx_t; typedef struct __print_ctx_t _print_ctx_t;
/** /**
* Pad string to right * Pad string to right
*/ */
#define _PRINTFMT_PAD_RIGHT 1 #define _PRINTFMT_PAD_RIGHT 1
/** /**
* Pad the number with zeroes * Pad the number with zeroes
*/ */
#define _PRINTFMT_PAD_ZERO 2 #define _PRINTFMT_PAD_ZERO 2
/** /**
* The following should be enough for 32 bit int * The following should be enough for 32 bit int
*/ */
#define _PRINTFMT_INT_BUF_LEN 12 #define _PRINTFMT_INT_BUF_LEN 12
/** /**
* Print a character to stdout (if string is null) * Print a character to stdout (if string is null)
* otherwise, put the character at the end of the provided string. * otherwise, put the character at the end of the provided string.
*/ */
static void __print_char( _print_ctx_t* ctx, char c ) static void __print_char( _print_ctx_t* ctx, char c )
{ {
if( ctx ) { if( ctx ) {
if( c == '\r' || c == '\n' ) { if( c == '\r' || c == '\n' ) {
if( ctx->_max_len > 1 ) { if( ctx->_max_len > 1 ) {
*(ctx->_ptr)='\r'; *(ctx->_ptr)='\r';
ctx->_max_len--; ctx->_max_len--;
ctx->_ptr++; ctx->_ptr++;
*(ctx->_ptr)='\n'; *(ctx->_ptr)='\n';
ctx->_max_len--; ctx->_max_len--;
ctx->_ptr++; ctx->_ptr++;
} else { } else {
*(ctx->_ptr)='\n'; *(ctx->_ptr)='\n';
ctx->_max_len--; ctx->_max_len--;
ctx->_ptr++; ctx->_ptr++;
} }
} else { } else {
if( ctx->_max_len ) { if( ctx->_max_len ) {
*(ctx->_ptr)=c; *(ctx->_ptr)=c;
ctx->_max_len--; ctx->_max_len--;
ctx->_ptr++; ctx->_ptr++;
} }
} }
} else { } else {
__putc( (uint8_t)c ); __putc( (uint8_t)c );
} }
} }
/** /**
* Print a string to a given string. * Print a string to a given string.
*/ */
static int __print_str( _print_ctx_t* ctx, static int __print_str( _print_ctx_t* ctx,
const char *string, const char *string,
int width, int width,
int pad, int pad,
int print_limit, int print_limit,
bool is_number ) bool is_number )
{ {
int pc = 0; int pc = 0;
int padchar = ' '; int padchar = ' ';
int i, len; int i, len;
if( width > 0 ) { if( width > 0 ) {
register int len = 0; register int len = 0;
register const char *ptr; register const char *ptr;
for( ptr = string; *ptr; ++ptr ) for( ptr = string; *ptr; ++ptr )
++len; ++len;
if( len >= width ) if( len >= width )
width = 0; width = 0;
else else
width -= len; width -= len;
if( pad & _PRINTFMT_PAD_ZERO ) if( pad & _PRINTFMT_PAD_ZERO )
padchar = '0'; padchar = '0';
} }
if( !( pad & _PRINTFMT_PAD_RIGHT ) ) { if( !( pad & _PRINTFMT_PAD_RIGHT ) ) {
for( ; width > 0; --width ) { for( ; width > 0; --width ) {
__print_char( ctx, padchar ); __print_char( ctx, padchar );
++pc; ++pc;
} }
} }
// The string to print is not the result of a number conversion to ascii. // The string to print is not the result of a number conversion to ascii.
if( false == is_number ) { if( false == is_number ) {
// For a string, printlimit is the max number of characters to display. // For a string, printlimit is the max number of characters to display.
for( ; print_limit && *string; ++string, --print_limit ) { for( ; print_limit && *string; ++string, --print_limit ) {
__print_char( ctx, *string ); __print_char( ctx, *string );
++pc; ++pc;
} }
} }
// The string to print represents an integer number. // The string to print represents an integer number.
if( true == is_number ) { if( true == is_number ) {
// In this case, printlimit is the min number of digits to print. // In this case, printlimit is the min number of digits to print.
// If the length of the number to print is less than the min nb of i // If the length of the number to print is less than the min nb of i
// digits to display, we add 0 before printing the number. // digits to display, we add 0 before printing the number.
len = strlen( string ); len = strlen( string );
if( len < print_limit ) { if( len < print_limit ) {
i = print_limit - len; i = print_limit - len;
for( ; i; i-- ) { for( ; i; i-- ) {
__print_char( ctx, '0' ); __print_char( ctx, '0' );
++pc; ++pc;
} }
} }
} }
/* /*
* Else: The string to print is not the result of a number conversion to ascii. * Else: The string to print is not the result of a number conversion to ascii.
* For a string, printlimit is the max number of characters to display. * For a string, printlimit is the max number of characters to display.
*/ */
for( ; print_limit && *string; ++string, --print_limit ) { for( ; print_limit && *string; ++string, --print_limit ) {
__print_char( ctx, *string ); __print_char( ctx, *string );
++pc; ++pc;
} }
for( ; width > 0; --width ) { for( ; width > 0; --width ) {
__print_char( ctx, padchar ); __print_char( ctx, padchar );
++pc; ++pc;
} }
return pc; return pc;
} }
/** /**
* Print a number to the given string, with the given base. * Print a number to the given string, with the given base.
*/ */
static int __print_int( _print_ctx_t* ctx, static int __print_int( _print_ctx_t* ctx,
int i, int i,
int b, int b,
int sg, int sg,
int width, int width,
int pad, int pad,
int letbase, int letbase,
int print_limit ) int print_limit )
{ {
char print_buf[_PRINTFMT_INT_BUF_LEN]; char print_buf[_PRINTFMT_INT_BUF_LEN];
register char *s; register char *s;
register int t, neg = 0, pc = 0; register int t, neg = 0, pc = 0;
register unsigned int u = i; register unsigned int u = i;
if( i == 0 ) { if( i == 0 ) {
print_buf[0] = '0'; print_buf[0] = '0';
print_buf[1] = '\0'; print_buf[1] = '\0';
return __print_str( ctx, print_buf, width, pad, print_limit, true ); return __print_str( ctx, print_buf, width, pad, print_limit, true );
} }
if( sg && b == 10 && i < 0 ) { if( sg && b == 10 && i < 0 ) {
neg = 1; neg = 1;
u = -i; u = -i;
} }
s = print_buf + _PRINTFMT_INT_BUF_LEN - 1; s = print_buf + _PRINTFMT_INT_BUF_LEN - 1;
*s = '\0'; *s = '\0';
while( u ) { while( u ) {
t = u % b; t = u % b;
if( t >= 10 ) if( t >= 10 )
t += letbase - '0' - 10; t += letbase - '0' - 10;
*--s = t + '0'; *--s = t + '0';
u /= b; u /= b;
} }
if( neg ) { if( neg ) {
if( width && ( pad & _PRINTFMT_PAD_ZERO ) ) { if( width && ( pad & _PRINTFMT_PAD_ZERO ) ) {
__print_char( ctx, '-' ); __print_char( ctx, '-' );
++pc; ++pc;
--width; --width;
} else { } else {
*--s = '-'; *--s = '-';
} }
} }
return pc + __print_str( ctx, s, width, pad, print_limit, true ); return pc + __print_str( ctx, s, width, pad, print_limit, true );
} }
/* /*
#if __GNUC__ #if __GNUC__
int fprintf( __FILE *stream, const char *format, ... ) int fprintf( __FILE *stream, const char *format, ... )
{ {
return 0; return 0;
} }
#endif #endif
*/ */
/** /**
* Print the given arguments, with given format onto string out. * Print the given arguments, with given format onto string out.
*/ */
static int __print_fmt( _print_ctx_t* ctx, const char *format, va_list args ) static int __print_fmt( _print_ctx_t* ctx, const char *format, va_list args )
{ {
int width; int width;
int pad; int pad;
int print_limit; int print_limit;
int pc = 0; int pc = 0;
char scr[2]; char scr[2];
for( ; *format != 0; ++format ) { for( ; *format != 0; ++format ) {
if( *format == '%' ) { if( *format == '%' ) {
++format; ++format;
width = pad = print_limit = 0; width = pad = print_limit = 0;
if( *format == '\0' ) { if( *format == '\0' ) {
break; break;
} }
if( *format == '%' ) { if( *format == '%' ) {
goto out; goto out;
} }
if( *format == '-' ) { if( *format == '-' ) {
++format; ++format;
pad = _PRINTFMT_PAD_RIGHT; pad = _PRINTFMT_PAD_RIGHT;
} }
while( *format == '0' ) { while( *format == '0' ) {
++format; ++format;
pad |= _PRINTFMT_PAD_ZERO; pad |= _PRINTFMT_PAD_ZERO;
} }
for( ; *format >= '0' && *format <= '9'; ++format ) { for( ; *format >= '0' && *format <= '9'; ++format ) {
width *= 10; width *= 10;
width += *format - '0'; width += *format - '0';
} }
if( *format == '.' ) { if( *format == '.' ) {
++format; ++format;
for( ; *format >= '0' && *format <= '9'; ++format ) { for( ; *format >= '0' && *format <= '9'; ++format ) {
print_limit *= 10; print_limit *= 10;
print_limit += *format - '0'; print_limit += *format - '0';
} }
} }
if( 0 == print_limit ) { if( 0 == print_limit ) {
print_limit--; print_limit--;
} }
if( *format == 'l' ) { if( *format == 'l' ) {
++format; ++format;
} }
if( *format == 's' ) { if( *format == 's' ) {
register char *s = (char *) va_arg( args, int ); register char *s = (char *) va_arg( args, int );
pc += __print_str( ctx, pc += __print_str( ctx,
s ? s : "(null)", s ? s : "(null)",
width, width,
pad, pad,
print_limit, print_limit,
false ); false );
continue; continue;
} }
if( *format == 'd' ) { if( *format == 'd' ) {
pc += __print_int( ctx, va_arg( args, int ), 10, 1, width, pad, 'a', print_limit ); pc += __print_int( ctx, va_arg( args, int ), 10, 1, width, pad, 'a', print_limit );
continue; continue;
} }
if( ( *format == 'x' ) || ( *format == 'p' ) ) { if( ( *format == 'x' ) || ( *format == 'p' ) ) {
pc += __print_int( ctx, va_arg( args, int ), 16, 0, width, pad, 'a', print_limit ); pc += __print_int( ctx, va_arg( args, int ), 16, 0, width, pad, 'a', print_limit );
continue; continue;
} }
if( *format == 'X' ) { if( *format == 'X' ) {
pc += __print_int( ctx, va_arg( args, int ), 16, 0, width, pad, 'A', print_limit ); pc += __print_int( ctx, va_arg( args, int ), 16, 0, width, pad, 'A', print_limit );
continue; continue;
} }
if( *format == 'u' ) { if( *format == 'u' ) {
pc += __print_int( ctx, va_arg( args, int ), 10, 0, width, pad, 'a', print_limit ); pc += __print_int( ctx, va_arg( args, int ), 10, 0, width, pad, 'a', print_limit );
continue; continue;
} }
if( *format == 'c' ) { if( *format == 'c' ) {
// char are converted to int then pushed on the stack // char are converted to int then pushed on the stack
scr[0] = (char) va_arg( args, int ); scr[0] = (char) va_arg( args, int );
scr[1] = '\0'; scr[1] = '\0';
pc += __print_str( ctx, scr, width, pad, print_limit, false ); pc += __print_str( ctx, scr, width, pad, print_limit, false );
continue; continue;
} }
} else { } else {
out: out:
__print_char( ctx, *format ); __print_char( ctx, *format );
++pc; ++pc;
} }
} }
if( ctx && ctx->_max_len ) { if( ctx && ctx->_max_len ) {
*(ctx->_ptr) = '\0'; *(ctx->_ptr) = '\0';
} }
return pc; return pc;
} }
/* /*
int sprintf( char *out, const char *format, ... ) int sprintf( char *out, const char *format, ... )
{ {
int retval = 0; int retval = 0;
_print_ctx_t ctx; _print_ctx_t ctx;
va_list args; va_list args;
ctx._ptr = out; ctx._ptr = out;
ctx._max_len = BLOCK_MEM_SIZE; ctx._max_len = BLOCK_MEM_SIZE;
va_start( args, format ); va_start( args, format );
retval = __print_fmt( &ctx, format, args ); retval = __print_fmt( &ctx, format, args );
va_end( args ); va_end( args );
return retval; return retval;
} }
*/ */
int printf( const char *format, ... ) int printf( const char *format, ... )
{ {
int retval = 0; int retval = 0;
// memory_t* buf; // memory_t* buf;
va_list args; va_list args;
/* /*
buf = memory_alloc( 10 ); buf = memory_alloc( 10 );
if( buf ) { if( buf ) {
_print_ctx_t ctx; _print_ctx_t ctx;
ctx._ptr = (char*)buf->_data; ctx._ptr = (char*)buf->_data;
ctx._max_len = BLOCK_MEM_SIZE; ctx._max_len = BLOCK_MEM_SIZE;
va_start( args, format ); va_start( args, format );
retval = __print_fmt( &ctx, format, args ); retval = __print_fmt( &ctx, format, args );
va_end( args ); va_end( args );
buf->_len = strlen( (const char*)buf->_data ); buf->_len = strlen( (const char*)buf->_data );
// LCD_WriteString( ll, buf ); // LCD_WriteString( ll, buf );
ll++; ll++;
ll &= 0x03; ll &= 0x03;
if( uart_task_send( buf ) == false ) { if( uart_task_send( buf ) == false ) {
memory_free( buf ); memory_free( buf );
} }
} }
*/ */
va_start( args, format ); va_start( args, format );
retval = __print_fmt( NULL, format, args ); retval = __print_fmt( NULL, format, args );
va_end( args ); va_end( args );
return retval; return retval;
} }