Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/string.c

    rfc6dd18 rdd2cfa7  
    471471 * null-terminated and containing only complete characters.
    472472 *
    473  * @param dest   Destination buffer.
     473 * @param dst   Destination buffer.
    474474 * @param count Size of the destination buffer (must be > 0).
    475475 * @param src   Source string.
     
    505505 * have to be null-terminated.
    506506 *
    507  * @param dest   Destination buffer.
     507 * @param dst   Destination buffer.
    508508 * @param count Size of the destination buffer (must be > 0).
    509509 * @param src   Source string.
     
    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.
    541541 * @param src   Source string.
     
    549549}
    550550
    551 /** Convert wide string to string.
    552  *
    553  * Convert wide string @a src to string. The output is written to the buffer
    554  * specified by @a dest and @a size. @a size must be non-zero and the string
    555  * written will always be well-formed.
    556  *
    557  * @param dest  Destination buffer.
    558  * @param size  Size of the destination buffer.
    559  * @param src   Source wide string.
    560  */
    561 void wstr_to_str(char *dest, size_t size, const wchar_t *src)
    562 {
     551/** Copy NULL-terminated wide string to string
     552 *
     553 * Copy source wide string @a src to destination buffer @a dst.
     554 * No more than @a size bytes are written. NULL-terminator is always
     555 * written after the last succesfully copied character (i.e. if the
     556 * destination buffer is has at least 1 byte, it will be always
     557 * NULL-terminated).
     558 *
     559 * @param src   Source wide string.
     560 * @param dst   Destination buffer.
     561 * @param count Size of the destination buffer.
     562 *
     563 */
     564void wstr_nstr(char *dst, const wchar_t *src, size_t size)
     565{
     566        /* No space for the NULL-terminator in the buffer */
     567        if (size == 0)
     568                return;
     569       
    563570        wchar_t ch;
    564         size_t src_idx;
    565         size_t dest_off;
    566 
    567         /* There must be space for a null terminator in the buffer. */
    568         assert(size > 0);
    569        
    570         src_idx = 0;
    571         dest_off = 0;
    572 
     571        size_t src_idx = 0;
     572        size_t dst_off = 0;
     573       
    573574        while ((ch = src[src_idx++]) != 0) {
    574                 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     575                if (chr_encode(ch, dst, &dst_off, size) != EOK)
    575576                        break;
    576577        }
    577 
    578         dest[dest_off] = '\0';
    579 }
    580 
    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  */
    589 char *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 
    630 /** Convert string to wide string.
    631  *
    632  * Convert string @a src to wide string. The output is written to the
    633  * buffer specified by @a dest and @a dlen. @a dlen must be non-zero
    634  * and the wide string written will always be null-terminated.
    635  *
    636  * @param dest  Destination buffer.
    637  * @param dlen  Length of destination buffer (number of wchars).
    638  * @param src   Source string.
    639  */
    640 void str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
    641 {
    642         size_t offset;
    643         size_t di;
    644         wchar_t c;
    645 
    646         assert(dlen > 0);
    647 
    648         offset = 0;
    649         di = 0;
    650 
    651         do {
    652                 if (di >= dlen - 1)
    653                         break;
    654 
    655                 c = str_decode(src, &offset, STR_NO_LIMIT);
    656                 dest[di++] = c;
    657         } while (c != '\0');
    658 
    659         dest[dlen - 1] = '\0';
     578       
     579        if (dst_off >= size)
     580                dst[size - 1] = 0;
     581        else
     582                dst[dst_off] = 0;
    660583}
    661584
     
    896819}
    897820
    898 char *str_dup(const char *src)
    899 {
    900         size_t size = str_size(src);
    901         void *dest = malloc(size + 1);
    902        
    903         if (dest == NULL)
    904                 return (char *) NULL;
    905        
    906         return (char *) memcpy(dest, src, size + 1);
    907 }
    908 
    909 char *str_ndup(const char *src, size_t max_size)
    910 {
    911         size_t size = str_size(src);
    912         if (size > max_size)
    913                 size = max_size;
    914        
    915         char *dest = (char *) malloc(size + 1);
    916        
    917         if (dest == NULL)
    918                 return (char *) NULL;
    919        
    920         memcpy(dest, src, size);
    921         dest[size] = 0;
    922         return dest;
    923 }
    924 
    925821
    926822/** Convert initial part of string to unsigned long according to given base.
     
    947843}
    948844
     845char *str_dup(const char *src)
     846{
     847        size_t size = str_size(src);
     848        void *dest = malloc(size + 1);
     849
     850        if (dest == NULL)
     851                return (char *) NULL;
     852
     853        return (char *) memcpy(dest, src, size + 1);
     854}
     855
    949856char *strtok(char *s, const char *delim)
    950857{
Note: See TracChangeset for help on using the changeset viewer.