]> pd.if.org Git - pdclib.old/commitdiff
PDCLIB-6: Move output from %s/%c specifiers to their own functions in _PDCLIB_print...
authorOwen Shepherd <owen.shepherd@e43.eu>
Sat, 25 Aug 2012 14:33:53 +0000 (15:33 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Sat, 25 Aug 2012 14:33:53 +0000 (15:33 +0100)
functions/_PDCLIB/print.c

index 439098d69bc604331de9b5e832178fb431c4c32a..c43d29b12be52f3140e25c27db2006b3a471ae3e 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdarg.h>
+#include <string.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdbool.h>
@@ -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':