Ignore:
File:
1 edited

Legend:

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

    rabf09311 r19f857a  
    537537 * null-terminated and containing only complete characters.
    538538 *
    539  * @param dest  Destination buffer.
     539 * @param dest   Destination buffer.
    540540 * @param count Size of the destination buffer (must be > 0).
    541541 * @param src   Source string.
    542  *
    543542 */
    544543void str_cpy(char *dest, size_t size, const char *src)
    545544{
     545        wchar_t ch;
     546        size_t src_off;
     547        size_t dest_off;
     548
    546549        /* There must be space for a null terminator in the buffer. */
    547550        ASSERT(size > 0);
    548551       
    549         size_t src_off = 0;
    550         size_t dest_off = 0;
    551        
    552         wchar_t ch;
     552        src_off = 0;
     553        dest_off = 0;
     554
    553555        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
    554556                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    555557                        break;
    556558        }
    557        
     559
    558560        dest[dest_off] = '\0';
    559561}
     
    569571 * have to be null-terminated.
    570572 *
    571  * @param dest  Destination buffer.
     573 * @param dest   Destination buffer.
    572574 * @param count Size of the destination buffer (must be > 0).
    573575 * @param src   Source string.
    574  * @param n     Maximum number of bytes to read from @a src.
    575  *
     576 * @param n     Maximum number of bytes to read from @a src.
    576577 */
    577578void str_ncpy(char *dest, size_t size, const char *src, size_t n)
    578579{
     580        wchar_t ch;
     581        size_t src_off;
     582        size_t dest_off;
     583
    579584        /* There must be space for a null terminator in the buffer. */
    580585        ASSERT(size > 0);
    581586       
    582         size_t src_off = 0;
    583         size_t dest_off = 0;
    584        
    585         wchar_t ch;
     587        src_off = 0;
     588        dest_off = 0;
     589
    586590        while ((ch = str_decode(src, &src_off, n)) != 0) {
    587591                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    588592                        break;
    589593        }
    590        
     594
    591595        dest[dest_off] = '\0';
    592 }
    593 
    594 /** Duplicate string.
    595  *
    596  * Allocate a new string and copy characters from the source
    597  * string into it. The duplicate string is allocated via sleeping
    598  * malloc(), thus this function can sleep in no memory conditions.
    599  *
    600  * The allocation cannot fail and the return value is always
    601  * a valid pointer. The duplicate string is always a well-formed
    602  * null-terminated UTF-8 string, but it can differ from the source
    603  * string on the byte level.
    604  *
    605  * @param src Source string.
    606  *
    607  * @return Duplicate string.
    608  *
    609  */
    610 char *str_dup(const char *src)
    611 {
    612         size_t size = str_size(src) + 1;
    613         char *dest = malloc(size, 0);
    614         ASSERT(dest);
    615        
    616         str_cpy(dest, size, src);
    617         return dest;
    618 }
    619 
    620 /** Duplicate string with size limit.
    621  *
    622  * Allocate a new string and copy up to @max_size bytes from the source
    623  * string into it. The duplicate string is allocated via sleeping
    624  * malloc(), thus this function can sleep in no memory conditions.
    625  * No more than @max_size + 1 bytes is allocated, but if the size
    626  * occupied by the source string is smaller than @max_size + 1,
    627  * less is allocated.
    628  *
    629  * The allocation cannot fail and the return value is always
    630  * a valid pointer. The duplicate string is always a well-formed
    631  * null-terminated UTF-8 string, but it can differ from the source
    632  * string on the byte level.
    633  *
    634  * @param src Source string.
    635  * @param n   Maximum number of bytes to duplicate.
    636  *
    637  * @return Duplicate string.
    638  *
    639  */
    640 char *str_ndup(const char *src, size_t n)
    641 {
    642         size_t size = str_size(src);
    643         if (size > n)
    644                 size = n;
    645        
    646         char *dest = malloc(size + 1, 0);
    647         ASSERT(dest);
    648        
    649         str_ncpy(dest, size + 1, src, size);
    650         return dest;
    651596}
    652597
Note: See TracChangeset for help on using the changeset viewer.