Changeset b67c7d64 in mainline


Ignore:
Timestamp:
2009-11-30T19:51:29Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e866806
Parents:
0f06dbc
Message:

Add wstr_to_astr() for easy conversion from wide string to string.

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/input.c

    r0f06dbc rb67c7d64  
    134134static char *tinput_get_str(tinput_t *ti)
    135135{
    136         char *str;
    137 
    138         str = malloc(STR_BOUNDS(ti->nc) + 1);
    139         if (str == NULL)
    140                 return NULL;
    141 
    142         wstr_to_str(str, STR_BOUNDS(ti->nc) + 1, ti->buffer);
    143 
    144         return str;
     136        return wstr_to_astr(ti->buffer);
    145137}
    146138
  • uspace/app/edit/edit.c

    r0f06dbc rb67c7d64  
    391391
    392392        buffer[nc] = '\0';
    393 
    394         str = malloc(STR_BOUNDS(wstr_length(buffer)) + 1);
    395         if (str == NULL)
    396                 return NULL;
    397 
    398         wstr_to_str(str, STR_BOUNDS(wstr_length(buffer)) + 1, buffer);
     393        str = wstr_to_astr(buffer);
    399394
    400395        console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0);
  • uspace/lib/libc/generic/string.c

    r0f06dbc rb67c7d64  
    579579}
    580580
     581/** Convert wide string to new string.
     582 *
     583 * Convert wide string @a src to string. Space for the new string is allocated
     584 * on the heap.
     585 *
     586 * @param src   Source wide string.
     587 * @return      New string.
     588 */
     589char *wstr_to_astr(const wchar_t *src)
     590{
     591        char dbuf[STR_BOUNDS(1)];
     592        char *str;
     593        wchar_t ch;
     594
     595        size_t src_idx;
     596        size_t dest_off;
     597        size_t dest_size;
     598
     599        /* Compute size of encoded string. */
     600
     601        src_idx = 0;
     602        dest_size = 0;
     603
     604        while ((ch = src[src_idx++]) != 0) {
     605                dest_off = 0;
     606                if (chr_encode(ch, dbuf, &dest_off, STR_BOUNDS(1)) != EOK)
     607                        break;
     608                dest_size += dest_off;
     609        }
     610
     611        str = malloc(dest_size + 1);
     612        if (str == NULL)
     613                return NULL;
     614
     615        /* Encode string. */
     616
     617        src_idx = 0;
     618        dest_off = 0;
     619
     620        while ((ch = src[src_idx++]) != 0) {
     621                if (chr_encode(ch, str, &dest_off, dest_size) != EOK)
     622                        break;
     623        }
     624
     625        str[dest_size] = '\0';
     626        return str;
     627}
     628
     629
    581630/** Convert string to wide string.
    582631 *
  • uspace/lib/libc/include/string.h

    r0f06dbc rb67c7d64  
    7474
    7575extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
     76extern char *wstr_to_astr(const wchar_t *src);
    7677extern void str_to_wstr(wchar_t *dest, size_t dlen, const char *src);
    7778
Note: See TracChangeset for help on using the changeset viewer.