From adf2ce87d44d089f001ec3008970c2b1ff1140ee Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Sat, 25 Aug 2012 15:33:53 +0100 Subject: [PATCH] PDCLIB-6: Move output from %s/%c specifiers to their own functions in _PDCLIB_print. These functions now handle padding and precision appropriately --- functions/_PDCLIB/print.c | 70 +++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/functions/_PDCLIB/print.c b/functions/_PDCLIB/print.c index 439098d..c43d29b 100644 --- a/functions/_PDCLIB/print.c +++ b/functions/_PDCLIB/print.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -164,6 +165,64 @@ static void int2base( uintmax_t value, struct _PDCLIB_status_t * status ) PUT( outend[-written--] ); } +static void printstr( const char * str, struct _PDCLIB_status_t * status ) +{ + if ( status->width == 0 || status->flags & E_minus ) + { + // Simple case or left justification + while ( str[status->current] && + ( status->prec < 0 || (long)status->current < status->prec ) ) + { + PUT( str[status->current++] ); + } + + while( status->current < status->width ) + { + PUT( ' ' ); + status->current++; + } + } else { + // Right justification + size_t len = status->prec >= 0 ? strnlen( str, status->prec ) + : strlen( str ); + int padding = status->width - len; + while((long)status->current < padding) + { + PUT( ' ' ); + status->current++; + } + + for( size_t i = 0; i != len; i++ ) + { + PUT( str[i] ); + status->current++; + } + } +} + +static void printchar( char chr, struct _PDCLIB_status_t * status ) +{ + if( ! ( status->flags & E_minus ) ) + { + // Right justification + for( ; status->current + 1 < status->width; status->current++) + { + PUT( ' ' ); + } + PUT( chr ); + status->current++; + } else { + // Left justification + PUT( chr ); + status->current++; + + for( ; status->current < status->width; status->current++) + { + PUT( ' ' ); + } + } +} + const char * _PDCLIB_print( const char * spec, struct _PDCLIB_status_t * status ) { const char * orig_spec = spec; @@ -358,17 +417,14 @@ const char * _PDCLIB_print( const char * spec, struct _PDCLIB_status_t * status case 'A': break; case 'c': - /* TODO: Flags, wide chars. */ - PUT( va_arg( status->arg, int ) ); + /* TODO: wide chars. */ + printchar( va_arg( status->arg, int ), status ); return ++spec; case 's': - /* TODO: Flags, wide chars. */ + /* TODO: wide chars. */ { char * s = va_arg( status->arg, char * ); - while ( *s != '\0' ) - { - PUT( *(s++) ); - } + printstr( s, status ); return ++spec; } case 'p': -- 2.40.0