Changeset 3214a20 in mainline


Ignore:
Timestamp:
2006-04-24T21:05:59Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
11fa83a
Parents:
4309741
Message:

Another version of printf function, now with support for sprintf, snprintf and v*printf functions.

Location:
libc
Files:
7 added
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • libc/Makefile

    r4309741 r3214a20  
    5151        generic/futex.c \
    5252        generic/io/io.c \
    53         generic/io/print.c \
     53        generic/io/printf.c \
     54        generic/io/sprintf.c \
     55        generic/io/snprintf.c \
     56        generic/io/vprintf.c \
     57        generic/io/vsprintf.c \
     58        generic/io/vsnprintf.c \
     59        generic/io/printf_core.c \
    5460        malloc/malloc.c \
    5561        generic/psthread.c
  • libc/generic/io/printf_core.c

    r4309741 r3214a20  
    2828 */
    2929
     30#include <unistd.h>
    3031#include <stdio.h>
    31 #include <unistd.h>
    32 #include <io/io.h>
    33 #include <stdarg.h>
     32#include <io/printf_core.h>
    3433#include <ctype.h>
    3534#include <string.h>
     
    6261static char digits_big[] = "0123456789ABCDEF";  /* Big hexadecimal characters */
    6362
     63/** Print count chars from buffer without adding newline
     64 * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
     65 * @param count
     66 * @param ps output method and its data
     67 * @return 0 on success, EOF on fail
     68 */
     69static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps)
     70{
     71        if (ps->write((void *)buf, count, ps->data) == count) {
     72                return 0;
     73        }
     74       
     75        return EOF;
     76}
     77
     78/** Print string without added newline
     79 * @param str string to print
     80 * @param ps write function specification and support data
     81 * @return 0 on success or EOF on fail
     82 */
     83static int printf_putstr(const char * str, struct printf_spec *ps)
     84{
     85        size_t count;
     86       
     87        if (str == NULL) {
     88                return printf_putnchars("(NULL)", 6, ps);
     89        }
     90
     91        for (count = 0; str[count] != 0; count++);
     92
     93        if (ps->write((void *) str, count, ps->data) == count) {
     94                return 0;
     95        }
     96       
     97        return EOF;
     98}
     99
     100/** Print one character to output
     101 * @param c one character
     102 * @param ps output method
     103 * @return printed character or EOF
     104 */
     105static int printf_putchar(int c, struct printf_spec *ps)
     106{
     107        unsigned char ch = c;
     108       
     109        if (ps->write((void *) &ch, 1, ps->data) == 1) {
     110                return c;
     111        }
     112       
     113        return EOF;
     114}
    64115
    65116/** Print one formatted character
     
    69120 * @return number of printed characters or EOF
    70121 */
    71 static int print_char(char c, int width, uint64_t flags)
     122static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps)
    72123{
    73124        int counter = 0;
     
    76127                while (--width > 0) {   /* one space is consumed by character itself hence predecrement */
    77128                        /* FIXME: painful slow */
    78                         putchar(' ');   
     129                        printf_putchar(' ', ps);       
    79130                        ++counter;
    80131                }
    81132        }
    82133       
    83         if (putchar(c) == EOF) {
     134        if (printf_putchar(c, ps) == EOF) {
    84135                return EOF;
    85136        }
    86137
    87138        while (--width > 0) { /* one space is consumed by character itself hence predecrement */
    88                 putchar(' ');
     139                printf_putchar(' ', ps);
    89140                ++counter;
    90141        }
     
    101152 */
    102153                                               
    103 static int print_string(char *s, int width, int precision, uint64_t flags)
     154static int print_string(char *s, int width, int precision, uint64_t flags, struct printf_spec *ps)
    104155{
    105156        int counter = 0;
     
    107158
    108159        if (s == NULL) {
    109                 return putstr("(NULL)");
     160                return printf_putstr("(NULL)", ps);
    110161        }
    111162       
     
    121172        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    122173                while (width-- > 0) {   
    123                         putchar(' ');   
     174                        printf_putchar(' ', ps);       
    124175                        counter++;
    125176                }
     
    128179        while (precision > size) {
    129180                precision--;
    130                 putchar(' ');   
     181                printf_putchar(' ', ps);       
    131182                ++counter;
    132183        }
    133184       
    134         if (putnchars(s, precision) == EOF) {
     185        if (printf_putnchars(s, precision, ps) == EOF) {
    135186                return EOF;
    136187        }
     
    139190
    140191        while (width-- > 0) {
    141                 putchar(' ');   
     192                printf_putchar(' ', ps);       
    142193                ++counter;
    143194        }
     
    161212 *
    162213 */
    163 static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags)
     214static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags, struct printf_spec *ps)
    164215{
    165216        char *digits = digits_small;
     
    187238       
    188239        number_size = size;
    189        
     240
    190241        /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */
    191242        if (flags & __PRINTF_FLAG_PREFIX) {
     
    236287        if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
    237288                while (width-- > 0) {   
    238                         putchar(' ');   
     289                        printf_putchar(' ', ps);       
    239290                        written++;
    240291                }
     
    243294        /* print sign */
    244295        if (sgn) {
    245                 putchar(sgn);
     296                printf_putchar(sgn, ps);
    246297                written++;
    247298        }
     
    252303                switch(base) {
    253304                        case 2: /* Binary formating is not standard, but usefull */
    254                                 putchar('0');
     305                                printf_putchar('0', ps);
    255306                                if (flags & __PRINTF_FLAG_BIGCHARS) {
    256                                         putchar('B');
     307                                        printf_putchar('B', ps);
    257308                                } else {
    258                                         putchar('b');
     309                                        printf_putchar('b', ps);
    259310                                }
    260311                                written += 2;
    261312                                break;
    262313                        case 8:
    263                                 putchar('o');
     314                                printf_putchar('o', ps);
    264315                                written++;
    265316                                break;
    266317                        case 16:
    267                                 putchar('0');
     318                                printf_putchar('0', ps);
    268319                                if (flags & __PRINTF_FLAG_BIGCHARS) {
    269                                         putchar('X');
     320                                        printf_putchar('X', ps);
    270321                                } else {
    271                                         putchar('x');
     322                                        printf_putchar('x', ps);
    272323                                }
    273324                                written += 2;
     
    279330        precision -= number_size;
    280331        while (precision-- > 0) {       
    281                 putchar('0');   
     332                printf_putchar('0', ps);       
    282333                written++;
    283334        }
     
    286337        /* print number itself */
    287338
    288         written += putstr(++ptr);
     339        written += printf_putstr(++ptr, ps);
    289340       
    290341        /* print ending spaces */
    291342       
    292343        while (width-- > 0) {   
    293                 putchar(' ');   
     344                printf_putchar(' ', ps);       
    294345                written++;
    295346        }
     
    363414 * @return count of printed characters or negative value on fail.
    364415 */
    365 int printf(const char *fmt, ...)
     416int printf_core(const char *fmt, struct printf_spec *ps, va_list ap)
    366417{
    367418        int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */
     
    369420        int counter; /* counter of printed characters */
    370421        int retval; /* used to store return values from called functions */
    371         va_list ap;
    372422        char c;
    373423        qualifier_t qualifier;  /* type of argument */
     
    379429       
    380430        counter = 0;
    381         va_start(ap, fmt);
    382431       
    383432        while ((c = fmt[i])) {
     
    386435                        /* print common characters if any processed */ 
    387436                        if (i > j) {
    388                                 if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
     437                                if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
    389438                                        return -counter;
    390439                                }
     
    482531                                */
    483532                                case 's':
    484                                         if ((retval = print_string(va_arg(ap, char*), width, precision, flags)) == EOF) {
     533                                        if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) {
    485534                                                return -counter;
    486535                                        };
     
    491540                                case 'c':
    492541                                        c = va_arg(ap, unsigned int);
    493                                         if ((retval = print_char(c, width, flags )) == EOF) {
     542                                        if ((retval = print_char(c, width, flags, ps)) == EOF) {
    494543                                                return -counter;
    495544                                        };
     
    591640                        }
    592641
    593                         if ((retval = print_number(number, width, precision, base, flags)) == EOF ) {
     642                        if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) {
    594643                                return -counter;
    595644                        };
     
    604653       
    605654        if (i > j) {
    606                 if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
     655                if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
    607656                        return -counter;
    608657                }
     
    610659        }
    611660       
    612         va_end(ap);
    613661        return counter;
    614662}
  • libc/include/io/io.h

    r4309741 r3214a20  
    2828
    2929#ifndef __LIBC__IO_H__
    30 #define __LIBC__IO_IO_H__
     30#define __LIBC__IO_H__
    3131
    3232#include <libarch/types.h>
  • libc/include/stdio.h

    r4309741 r3214a20  
    3131
    3232#include <types.h>
     33#include <stdarg.h>
    3334
    3435#define EOF (-1)
     
    3738
    3839extern int printf(const char *fmt, ...);
     40extern int sprintf(char *str, const char *fmt, ...);
     41extern int snprintf(char *str, size_t size, const char *fmt, ...);
     42
     43extern int vprintf(const char *fmt, va_list ap);
     44extern int vsprintf(char *str, const char *fmt, va_list ap);
     45extern int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
     46
    3947#define fprintf(f, fmt, ...) printf(fmt, ##__VA_ARGS__)
    4048
Note: See TracChangeset for help on using the changeset viewer.