Changeset a33f0a6 in mainline for uspace/lib/c/generic/str.c


Ignore:
Timestamp:
2011-08-03T17:34:57Z (14 years ago)
Author:
Oleg Romanenko <romanenko.oleg@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1940326
Parents:
52a79081 (diff), 3fab770 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from mainline

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/str.c

    r52a79081 ra33f0a6  
    553553
    554554        dstr_size = str_size(dest);
     555        if (dstr_size >= size)
     556                return;
     557       
    555558        str_cpy(dest + dstr_size, size - dstr_size, src);
     559}
     560
     561/** Convert space-padded ASCII to string.
     562 *
     563 * Common legacy text encoding in hardware is 7-bit ASCII fitted into
     564 * a fixed-with byte buffer (bit 7 always zero), right-padded with spaces
     565 * (ASCII 0x20). Convert space-padded ascii to string representation.
     566 *
     567 * If the text does not fit into the destination buffer, the function converts
     568 * as many characters as possible and returns EOVERFLOW.
     569 *
     570 * If the text contains non-ASCII bytes (with bit 7 set), the whole string is
     571 * converted anyway and invalid characters are replaced with question marks
     572 * (U_SPECIAL) and the function returns EIO.
     573 *
     574 * Regardless of return value upon return @a dest will always be well-formed.
     575 *
     576 * @param dest          Destination buffer
     577 * @param size          Size of destination buffer
     578 * @param src           Space-padded ASCII.
     579 * @param n             Size of the source buffer in bytes.
     580 *
     581 * @return              EOK on success, EOVERFLOW if the text does not fit
     582 *                      destination buffer, EIO if the text contains
     583 *                      non-ASCII bytes.
     584 */
     585int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
     586{
     587        size_t sidx;
     588        size_t didx;
     589        size_t dlast;
     590        uint8_t byte;
     591        int rc;
     592        int result;
     593
     594        /* There must be space for a null terminator in the buffer. */
     595        assert(size > 0);
     596        result = EOK;
     597
     598        didx = 0;
     599        dlast = 0;
     600        for (sidx = 0; sidx < n; ++sidx) {
     601                byte = src[sidx];
     602                if (!ascii_check(byte)) {
     603                        byte = U_SPECIAL;
     604                        result = EIO;
     605                }
     606
     607                rc = chr_encode(byte, dest, &didx, size - 1);
     608                if (rc != EOK) {
     609                        assert(rc == EOVERFLOW);
     610                        dest[didx] = '\0';
     611                        return rc;
     612                }
     613
     614                /* Remember dest index after last non-empty character */
     615                if (byte != 0x20)
     616                        dlast = didx;
     617        }
     618
     619        /* Terminate string after last non-empty character */
     620        dest[dlast] = '\0';
     621        return result;
    556622}
    557623
Note: See TracChangeset for help on using the changeset viewer.