Changeset 043eca0 in mainline


Ignore:
Timestamp:
2009-04-03T07:54:27Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d1dabe1f
Parents:
b888d5f
Message:

cleanup, string changes

Location:
kernel/generic/src/printf
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/printf/printf_core.c

    rb888d5f r043eca0  
    8383static char digits_big[] = "0123456789ABCDEF";
    8484
    85 /** Print one or more UTF-8 characters without adding newline.
    86  *
    87  * @param buf  Buffer holding UTF-8 characters with size of
     85/** Print one or more characters without adding newline.
     86 *
     87 * @param buf  Buffer holding characters with size of
    8888 *             at least size bytes. NULL is not allowed!
    8989 * @param size Size of the buffer in bytes.
    9090 * @param ps   Output method and its data.
    9191 *
    92  * @return Number of UTF-8 characters printed.
    93  *
    94  */
    95 static int printf_putnchars_utf8(const char *buf, size_t size,
     92 * @return Number of characters printed.
     93 *
     94 */
     95static int printf_putnchars(const char *buf, size_t size,
    9696    printf_spec_t *ps)
    9797{
    98         return ps->write_utf8((void *) buf, size, ps->data);
    99 }
    100 
    101 /** Print one or more UTF-32 characters without adding newline.
    102  *
    103  * @param buf  Buffer holding UTF-32 characters with size of
     98        return ps->str_write((void *) buf, size, ps->data);
     99}
     100
     101/** Print one or more wide characters without adding newline.
     102 *
     103 * @param buf  Buffer holding wide characters with size of
    104104 *             at least size bytes. NULL is not allowed!
    105105 * @param size Size of the buffer in bytes.
    106106 * @param ps   Output method and its data.
    107107 *
    108  * @return Number of UTF-32 characters printed.
    109  *
    110  */
    111 static int printf_putnchars_utf32(const wchar_t *buf, size_t size,
     108 * @return Number of wide characters printed.
     109 *
     110 */
     111static int printf_wputnchars(const wchar_t *buf, size_t size,
    112112    printf_spec_t *ps)
    113113{
    114         return ps->write_utf32((void *) buf, size, ps->data);
    115 }
    116 
    117 /** Print string without adding newline.
    118  *
    119  * @param str   String to print.
    120  * @param ps    Write function specification and support data.
     114        return ps->wstr_write((void *) buf, size, ps->data);
     115}
     116
     117/** Print string without adding a newline.
     118 *
     119 * @param str String to print.
     120 * @param ps  Write function specification and support data.
    121121 *
    122122 * @return Number of characters printed.
     
    126126{
    127127        if (str == NULL)
    128                 return printf_putnchars_utf8(nullstr, str_size(nullstr), ps);
    129        
    130         return ps->write_utf8((void *) str, str_size(str), ps->data);
     128                return printf_putnchars(nullstr, str_size(nullstr), ps);
     129       
     130        return ps->str_write((void *) str, str_size(str), ps->data);
    131131}
    132132
     
    142142{
    143143        if (!ascii_check(ch))
    144                 return ps->write_utf8((void *) &invalch, 1, ps->data);
    145        
    146         return ps->write_utf8(&ch, 1, ps->data);
    147 }
    148 
    149 /** Print one UTF-32 character.
    150  *
    151  * @param c  UTF-32 character to be printed.
     144                return ps->str_write((void *) &invalch, 1, ps->data);
     145       
     146        return ps->str_write(&ch, 1, ps->data);
     147}
     148
     149/** Print one wide character.
     150 *
     151 * @param c  Wide character to be printed.
    152152 * @param ps Output method.
    153153 *
     
    157157static int printf_putwchar(const wchar_t ch, printf_spec_t *ps)
    158158{
    159         if (!unicode_check(ch))
    160                 return ps->write_utf8((void *) &invalch, 1, ps->data);
    161        
    162         return ps->write_utf32(&ch, sizeof(wchar_t), ps->data);
     159        if (!chr_check(ch))
     160                return ps->str_write((void *) &invalch, 1, ps->data);
     161       
     162        return ps->wstr_write(&ch, sizeof(wchar_t), ps->data);
    163163}
    164164
     
    201201}
    202202
    203 /** Print one formatted UTF-32 character.
     203/** Print one formatted wide character.
    204204 *
    205205 * @param ch    Character to print.
     
    239239}
    240240
    241 /** Format and print a string.
     241/** Print string.
    242242 *
    243243 * @param str       String to be printed.
     
    271271        /* Part of @a str fitting into the alloted space. */
    272272        int retval;
    273         size_t size = str_wsize(str, precision);
    274         if ((retval = printf_putnchars_utf8(str, size, ps)) < 0)
     273        size_t size = str_lsize(str, precision);
     274        if ((retval = printf_putnchars(str, size, ps)) < 0)
    275275                return -counter;
    276276
     
    287287}
    288288
    289 /** Format and print a wide string.
    290  *
    291  * @param wstr          Wide string to be printed.
    292  * @param width         Width modifier.
    293  * @param precision     Precision modifier.
    294  * @param flags         Flags that modify the way the string is printed.
    295  *
    296  * @return              Number of characters printed, negative value
    297  *                      on failure.
    298  */
    299 static int print_wstr(wchar_t *wstr, int width, unsigned int precision,
     289/** Print wide string.
     290 *
     291 * @param str       Wide string to be printed.
     292 * @param width     Width modifier.
     293 * @param precision Precision modifier.
     294 * @param flags     Flags that modify the way the string is printed.
     295 *
     296 * @return Number of wide characters printed, negative value on failure.
     297 */
     298static int print_wstr(wchar_t *str, int width, unsigned int precision,
    300299        uint32_t flags, printf_spec_t *ps)
    301300{
    302         if (wstr == NULL)
     301        if (str == NULL)
    303302                return printf_putstr(nullstr, ps);
    304303
    305304        /* Print leading spaces. */
    306         size_t strw = wstr_length(wstr);
     305        size_t strw = wstr_length(str);
    307306        if (precision == 0)
    308307                precision = strw;
     
    320319        /* Part of @a wstr fitting into the alloted space. */
    321320        int retval;
    322         size_t size = wstr_wlength(wstr, precision) * sizeof(wchar_t);
    323         if ((retval = printf_putnchars_utf32(wstr, size, ps)) < 0)
     321        size_t size = wstr_lsize(str, precision);
     322        if ((retval = printf_wputnchars(str, size, ps)) < 0)
    324323                return -counter;
    325324
     
    424423        }
    425424       
    426         /* Print leading spaces. */
     425        /* Print leading spaces */
    427426        if (number_size > precision) {
    428                 /* Print the whole number, not only a part. */
     427                /* Print the whole number, not only a part */
    429428                precision = number_size;
    430429        }
     
    440439        }
    441440       
    442         /* Print sign. */
     441        /* Print sign */
    443442        if (sgn) {
    444443                if (printf_putchar(sgn, ps) == 1)
     
    446445        }
    447446       
    448         /* Print prefix. */
     447        /* Print prefix */
    449448        if (flags & __PRINTF_FLAG_PREFIX) {
    450449                switch(base) {
     
    479478        }
    480479       
    481         /* Print leading zeroes. */
     480        /* Print leading zeroes */
    482481        precision -= number_size;
    483482        while (precision-- > 0) {
     
    486485        }
    487486       
    488         /* Print the number itself. */
     487        /* Print the number itself */
    489488        int retval;
    490489        if ((retval = printf_putstr(++ptr, ps)) > 0)
    491490                counter += retval;
    492491       
    493         /* Print tailing spaces. */
     492        /* Print tailing spaces */
    494493       
    495494        while (width-- > 0) {
     
    546545 *  - ""   Signed or unsigned int (default value).@n
    547546 *  - "l"  Signed or unsigned long int.@n
    548  *         If conversion is "c", the character is wchar_t (UTF-32).@n
    549  *         If conversion is "s", the string is wchar_t * (UTF-32).@n
     547 *         If conversion is "c", the character is wchar_t (wide character).@n
     548 *         If conversion is "s", the string is wchar_t * (wide string).@n
    550549 *  - "ll" Signed or unsigned long long int.@n
    551550 *
     
    555554 *  - c Print single character. The character is expected to be plain
    556555 *      ASCII (e.g. only values 0 .. 127 are valid).@n
    557  *      If type is "l", then the character is expected to be UTF-32
     556 *      If type is "l", then the character is expected to be wide character
    558557 *      (e.g. values 0 .. 0x10ffff are valid).
    559558 *
    560559 *  - s Print zero terminated string. If a NULL value is passed as
    561560 *      value, "(NULL)" is printed instead.@n
    562  *      The string is expected to be correctly encoded UTF-8 (or plain
    563  *      ASCII, which is a subset of UTF-8).@n
    564  *      If type is "l", then the string is expected to be correctly
    565  *      encoded UTF-32.
     561 *      If type is "l", then the string is expected to be wide string.
    566562 *
    567563 *  - P, p Print value of a pointer. Void * value is expected and it is
     
    586582 * verbatim.
    587583 *
    588  * @param fmt Format string.
     584 * @param fmt Format NULL-terminated string.
     585 *
    589586 * @return Number of characters printed, negative value on failure.
    590587 *
     
    592589int printf_core(const char *fmt, printf_spec_t *ps, va_list ap)
    593590{
    594         size_t i = 0;  /* Index of the currently processed character from fmt */
    595         size_t nxt = 0;
    596         size_t j = 0;  /* Index to the first not printed nonformating character */
    597        
    598         wchar_t uc;           /* Current character decoded from fmt */
     591        size_t i;        /* Index of the currently processed character from fmt */
     592        size_t nxt = 0;  /* Index of the next character from fmt */
     593        size_t j = 0;    /* Index to the first not printed nonformating character */
     594       
    599595        count_t counter = 0;  /* Number of characters printed */
    600596        int retval;           /* Return values from nested functions */
     
    602598        while (true) {
    603599                i = nxt;
    604                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    605 
    606                 if (uc == '\0') break;
    607 
     600                wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
     601               
     602                if (uc == 0)
     603                        break;
     604               
    608605                /* Control character */
    609606                if (uc == '%') {
    610607                        /* Print common characters if any processed */
    611608                        if (i > j) {
    612                                 if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) {
     609                                if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) {
    613610                                        /* Error */
    614611                                        counter = -counter;
     
    626623                        do {
    627624                                i = nxt;
    628                                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     625                                uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    629626                                switch (uc) {
    630627                                case '#':
     
    654651                                        width *= 10;
    655652                                        width += uc - '0';
    656 
     653                                       
    657654                                        i = nxt;
    658                                         uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    659                                         if (uc == '\0')
     655                                        uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
     656                                        if (uc == 0)
    660657                                                break;
    661658                                        if (!isdigit(uc))
     
    665662                                /* Get width value from argument list */
    666663                                i = nxt;
    667                                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     664                                uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    668665                                width = (int) va_arg(ap, int);
    669666                                if (width < 0) {
     
    678675                        if (uc == '.') {
    679676                                i = nxt;
    680                                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     677                                uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    681678                                if (isdigit(uc)) {
    682679                                        while (true) {
    683680                                                precision *= 10;
    684681                                                precision += uc - '0';
    685 
     682                                               
    686683                                                i = nxt;
    687                                                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    688                                                 if (uc == '\0')
     684                                                uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
     685                                                if (uc == 0)
    689686                                                        break;
    690687                                                if (!isdigit(uc))
     
    694691                                        /* Get precision value from the argument list */
    695692                                        i = nxt;
    696                                         uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     693                                        uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    697694                                        precision = (int) va_arg(ap, int);
    698695                                        if (precision < 0) {
     
    713710                                qualifier = PrintfQualifierShort;
    714711                                i = nxt;
    715                                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     712                                uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    716713                                if (uc == 'h') {
    717714                                        i = nxt;
    718                                         uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     715                                        uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    719716                                        qualifier = PrintfQualifierByte;
    720717                                }
     
    724721                                qualifier = PrintfQualifierLong;
    725722                                i = nxt;
    726                                 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     723                                uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    727724                                if (uc == 'l') {
    728725                                        i = nxt;
    729                                         uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
     726                                        uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    730727                                        qualifier = PrintfQualifierLongLong;
    731728                                }
     
    879876       
    880877        if (i > j) {
    881                 if ((retval = printf_putnchars_utf8(&fmt[j], i - j, ps)) < 0) {
     878                if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) {
    882879                        /* Error */
    883880                        counter = -counter;
  • kernel/generic/src/printf/vprintf.c

    rb888d5f r043eca0  
    4444SPINLOCK_INITIALIZE(printf_lock);  /**< vprintf spinlock */
    4545
    46 static int vprintf_write_utf8(const char *str, size_t size, void *data)
     46static int vprintf_str_write(const char *str, size_t size, void *data)
    4747{
    4848        size_t offset = 0;
    4949        count_t chars = 0;
    50 
     50       
    5151        while (offset < size) {
    52                 putchar(chr_decode(str, &offset, size));
     52                putchar(str_decode(str, &offset, size));
    5353                chars++;
    5454        }
    55 
     55       
    5656        return chars;
    5757}
    5858
    59 static int vprintf_write_utf32(const wchar_t *str, size_t size, void *data)
     59static int vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
    6060{
    61         index_t index = 0;
    62 
    63         while (index < (size / sizeof(wchar_t))) {
    64                 putchar(str[index]);
    65                 index++;
     61        size_t offset = 0;
     62        count_t chars = 0;
     63       
     64        while (offset < size) {
     65                putchar(str[chars]);
     66                chars++;
     67                offset += sizeof(wchar_t);
    6668        }
    67 
    68         return index;
     69       
     70        return chars;
    6971}
    7072
     
    7476        count_t chars = 0;
    7577        wchar_t uc;
    76 
    77         while ((uc = chr_decode(str, &offset, UTF8_NO_LIMIT)) != 0) {
     78       
     79        while ((uc = str_decode(str, &offset, STR_NO_LIMIT)) != 0) {
    7880                putchar(uc);
    7981                chars++;
    8082        }
    81 
     83       
    8284        return chars;
    8385}
     
    8688{
    8789        printf_spec_t ps = {
    88                 vprintf_write_utf8,
    89                 vprintf_write_utf32,
     90                vprintf_str_write,
     91                vprintf_wstr_write,
    9092                NULL
    9193        };
    92 
     94       
    9395        ipl_t ipl = interrupts_disable();
    9496        spinlock_lock(&printf_lock);
    95 
     97       
    9698        int ret = printf_core(fmt, &ps, ap);
    97 
     99       
    98100        spinlock_unlock(&printf_lock);
    99101        interrupts_restore(ipl);
    100 
     102       
    101103        return ret;
    102104}
  • kernel/generic/src/printf/vsnprintf.c

    rb888d5f r043eca0  
    4545} vsnprintf_data_t;
    4646
    47 /** Write UTF-8 string to given buffer.
     47/** Write string to given buffer.
    4848 *
    4949 * Write at most data->size plain characters including trailing zero.
     
    5353 * but size of the input string.
    5454 *
    55  * @param str  Source UTF-8 string to print.
     55 * @param str  Source string to print.
    5656 * @param size Number of plain characters in str.
    5757 * @param data Structure describing destination string, counter
    5858 *             of used space and total string size.
    5959 *
    60  * @return Number of UTF-8 characters to print (not characters actually
     60 * @return Number of characters to print (not characters actually
    6161 *         printed).
    6262 *
    6363 */
    64 static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data)
     64static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data)
    6565{
    6666        size_t left = data->size - data->len;
     
    8686               
    8787                while (index < size) {
    88                         wchar_t uc = chr_decode(str, &index, size);
    89 
     88                        wchar_t uc = str_decode(str, &index, size);
     89                       
    9090                        if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
    9191                                break;
     
    112112}
    113113
    114 /** Write UTF-32 string to given buffer.
     114/** Write wide string to given buffer.
    115115 *
    116116 * Write at most data->size plain characters including trailing zero.
     
    120120 * but size of the input string.
    121121 *
    122  * @param str  Source UTF-32 string to print.
     122 * @param str  Source wide string to print.
    123123 * @param size Number of bytes in str.
    124124 * @param data Structure describing destination string, counter
    125125 *             of used space and total string size.
    126126 *
    127  * @return Number of UTF-8 characters to print (not characters actually
     127 * @return Number of wide characters to print (not characters actually
    128128 *         printed).
    129129 *
    130130 */
    131 static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data)
     131static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data)
    132132{
    133133        index_t index = 0;
     
    170170        };
    171171        printf_spec_t ps = {
    172                 (int(*) (const char *, size_t, void *)) vsnprintf_write_utf8,
    173                 (int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32,
     172                (int(*) (const char *, size_t, void *)) vsnprintf_str_write,
     173                (int(*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write,
    174174                &data
    175175        };
Note: See TracChangeset for help on using the changeset viewer.