Changeset f4b1535 in mainline for kernel/generic/src/lib/string.c


Ignore:
Timestamp:
2009-04-09T23:04:10Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6eb2e96
Parents:
095003a8
Message:

str_ncpy() vs str_cpy(). TODO: The same in userspace.

File:
1 edited

Legend:

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

    r095003a8 rf4b1535  
    529529}
    530530
    531 /** Copy NULL-terminated string.
    532  *
    533  * Copy source string @a src to destination buffer @a dst.
    534  * No more than @a size bytes are written. NULL-terminator is always
    535  * written after the last succesfully copied character (i.e. if the
    536  * destination buffer is has at least 1 byte, it will be always
    537  * NULL-terminated).
    538  *
    539  * @param src   Source string.
     531/** Copy string.
     532 *
     533 * Copy source string @a src to destination buffer @a dest.
     534 * No more than @a size bytes are written. If the size of the output buffer
     535 * is at least one byte, the output string will always be well-formed, i.e.
     536 * null-terminated and containing only complete characters.
     537 *
    540538 * @param dst   Destination buffer.
    541539 * @param count Size of the destination buffer.
    542  *
    543  */
    544 void str_ncpy(char *dst, const char *src, size_t size)
    545 {
    546         /* No space for the NULL-terminator in the buffer */
     540 * @param src   Source string.
     541 */
     542void str_cpy(char *dest, size_t size, const char *src)
     543{
     544        wchar_t ch;
     545        size_t src_off;
     546        size_t dest_off;
     547
     548        /* No space for the NULL-terminator in the buffer. */
    547549        if (size == 0)
    548550                return;
    549551       
     552        src_off = 0;
     553        dest_off = 0;
     554
     555        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
     556                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     557                        break;
     558        }
     559
     560        dest[dest_off] = '\0';
     561}
     562
     563/** Copy size-limited substring.
     564 *
     565 * Copy source string @a src to destination buffer @a dest.
     566 * No more than @a size bytes are written. If the size of the output buffer
     567 * is at least one byte, the output string will always be well-formed, i.e.
     568 * null-terminated and containing only complete characters.
     569 *
     570 * No more than @a n bytes are read from the input string, so it does not
     571 * have to be null-terminated.
     572 *
     573 * @param dst   Destination buffer.
     574 * @param count Size of the destination buffer.
     575 * @param src   Source string.
     576 */
     577void str_ncpy(char *dest, size_t size, const char *src, size_t n)
     578{
    550579        wchar_t ch;
    551         size_t str_off = 0;
    552         size_t dst_off = 0;
    553        
    554         while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) {
    555                 if (chr_encode(ch, dst, &dst_off, size) != EOK)
     580        size_t src_off;
     581        size_t dest_off;
     582
     583        /* No space for the null terminator in the buffer. */
     584        if (size == 0)
     585                return;
     586       
     587        src_off = 0;
     588        dest_off = 0;
     589
     590        while ((ch = str_decode(src, &src_off, n)) != 0) {
     591                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    556592                        break;
    557593        }
    558        
    559         if (dst_off >= size)
    560                 dst[size - 1] = 0;
    561         else
    562                 dst[dst_off] = 0;
     594
     595        dest[dest_off] = '\0';
    563596}
    564597
Note: See TracChangeset for help on using the changeset viewer.