Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/string.c

    r0f06dbc rdd2cfa7  
    537537 * null-terminated and containing only complete characters.
    538538 *
    539  * @param dest   Destination buffer.
     539 * @param dst   Destination buffer.
    540540 * @param count Size of the destination buffer (must be > 0).
    541541 * @param src   Source string.
     
    571571 * have to be null-terminated.
    572572 *
    573  * @param dest   Destination buffer.
     573 * @param dst   Destination buffer.
    574574 * @param count Size of the destination buffer (must be > 0).
    575575 * @param src   Source string.
     
    596596}
    597597
    598 /** Convert wide string to string.
    599  *
    600  * Convert wide string @a src to string. The output is written to the buffer
    601  * specified by @a dest and @a size. @a size must be non-zero and the string
    602  * written will always be well-formed.
    603  *
    604  * @param dest  Destination buffer.
    605  * @param size  Size of the destination buffer.
    606  * @param src   Source wide string.
    607  */
    608 void wstr_to_str(char *dest, size_t size, const wchar_t *src)
    609 {
     598/** Copy NULL-terminated wide string to string
     599 *
     600 * Copy source wide string @a src to destination buffer @a dst.
     601 * No more than @a size bytes are written. NULL-terminator is always
     602 * written after the last succesfully copied character (i.e. if the
     603 * destination buffer is has at least 1 byte, it will be always
     604 * NULL-terminated).
     605 *
     606 * @param src   Source wide string.
     607 * @param dst   Destination buffer.
     608 * @param count Size of the destination buffer.
     609 *
     610 */
     611void wstr_nstr(char *dst, const wchar_t *src, size_t size)
     612{
     613        /* No space for the NULL-terminator in the buffer */
     614        if (size == 0)
     615                return;
     616       
    610617        wchar_t ch;
    611         size_t src_idx;
    612         size_t dest_off;
    613 
    614         /* There must be space for a null terminator in the buffer. */
    615         ASSERT(size > 0);
    616 
    617         src_idx = 0;
    618         dest_off = 0;
     618        size_t src_idx = 0;
     619        size_t dst_off = 0;
    619620       
    620621        while ((ch = src[src_idx++]) != 0) {
    621                 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     622                if (chr_encode(ch, dst, &dst_off, size) != EOK)
    622623                        break;
    623624        }
    624 
    625         dest[dest_off] = '\0';
     625       
     626        if (dst_off >= size)
     627                dst[size - 1] = 0;
     628        else
     629                dst[dst_off] = 0;
    626630}
    627631
Note: See TracChangeset for help on using the changeset viewer.