Rev 4263 | Rev 4581 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4263 | Rev 4327 | ||
---|---|---|---|
Line 106... | Line 106... | ||
106 | #include <cpu.h> |
106 | #include <cpu.h> |
107 | #include <arch/asm.h> |
107 | #include <arch/asm.h> |
108 | #include <arch.h> |
108 | #include <arch.h> |
109 | #include <errno.h> |
109 | #include <errno.h> |
110 | #include <align.h> |
110 | #include <align.h> |
- | 111 | #include <debug.h> |
|
111 | 112 | ||
112 | /** Byte mask consisting of lowest @n bits (out of 8) */ |
113 | /** Byte mask consisting of lowest @n bits (out of 8) */ |
113 | #define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1)) |
114 | #define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1)) |
114 | 115 | ||
115 | /** Byte mask consisting of lowest @n bits (out of 32) */ |
116 | /** Byte mask consisting of lowest @n bits (out of 32) */ |
Line 526... | Line 527... | ||
526 | 527 | ||
527 | return 0; |
528 | return 0; |
528 | 529 | ||
529 | } |
530 | } |
530 | 531 | ||
531 | /** Copy NULL-terminated string. |
532 | /** Copy string. |
532 | * |
533 | * |
533 | * Copy source string @a src to destination buffer @a dst. |
534 | * Copy source string @a src to destination buffer @a dest. |
534 | * No more than @a size bytes are written. NULL-terminator is always |
535 | * No more than @a size bytes are written. If the size of the output buffer |
535 | * written after the last succesfully copied character (i.e. if the |
536 | * is at least one byte, the output string will always be well-formed, i.e. |
536 | * destination buffer is has at least 1 byte, it will be always |
537 | * null-terminated and containing only complete characters. |
537 | * NULL-terminated). |
- | |
538 | * |
538 | * |
539 | * @param src Source string. |
- | |
540 | * @param dst Destination buffer. |
539 | * @param dst Destination buffer. |
541 | * @param count Size of the destination buffer. |
540 | * @param count Size of the destination buffer (must be > 0). |
542 | * |
541 | * @param src Source string. |
543 | */ |
542 | */ |
544 | void str_ncpy(char *dst, const char *src, size_t size) |
543 | void str_cpy(char *dest, size_t size, const char *src) |
545 | { |
544 | { |
546 | /* No space for the NULL-terminator in the buffer */ |
- | |
547 | if (size == 0) |
- | |
548 | return; |
- | |
549 | - | ||
550 | wchar_t ch; |
545 | wchar_t ch; |
551 | size_t str_off = 0; |
546 | size_t src_off; |
552 | size_t dst_off = 0; |
547 | size_t dest_off; |
- | 548 | ||
- | 549 | /* There must be space for a null terminator in the buffer. */ |
|
- | 550 | ASSERT(size > 0); |
|
553 | 551 | ||
- | 552 | src_off = 0; |
|
- | 553 | dest_off = 0; |
|
- | 554 | ||
554 | while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) { |
555 | while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) { |
555 | if (chr_encode(ch, dst, &dst_off, size) != EOK) |
556 | if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) |
556 | break; |
557 | break; |
557 | } |
558 | } |
- | 559 | ||
- | 560 | dest[dest_off] = '\0'; |
|
- | 561 | } |
|
- | 562 | ||
- | 563 | /** Copy size-limited substring. |
|
- | 564 | * |
|
- | 565 | * Copy prefix of string @a src of max. size @a size to destination buffer |
|
- | 566 | * @a dest. No more than @a size bytes are written. The output string will |
|
- | 567 | * always be well-formed, i.e. null-terminated and containing only complete |
|
- | 568 | * 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 (must be > 0). |
|
- | 575 | * @param src Source string. |
|
- | 576 | * @param n Maximum number of bytes to read from @a src. |
|
- | 577 | */ |
|
- | 578 | void str_ncpy(char *dest, size_t size, const char *src, size_t n) |
|
- | 579 | { |
|
- | 580 | wchar_t ch; |
|
- | 581 | size_t src_off; |
|
- | 582 | size_t dest_off; |
|
- | 583 | ||
- | 584 | /* There must be space for a null terminator in the buffer. */ |
|
- | 585 | ASSERT(size > 0); |
|
558 | 586 | ||
559 | if (dst_off >= size) |
587 | src_off = 0; |
560 | dst[size - 1] = 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) |
|
- | 592 | break; |
|
561 | else |
593 | } |
- | 594 | ||
562 | dst[dst_off] = 0; |
595 | dest[dest_off] = '\0'; |
563 | } |
596 | } |
564 | 597 | ||
565 | /** Copy NULL-terminated wide string to string |
598 | /** Copy NULL-terminated wide string to string |
566 | * |
599 | * |
567 | * Copy source wide string @a src to destination buffer @a dst. |
600 | * Copy source wide string @a src to destination buffer @a dst. |
Line 606... | Line 639... | ||
606 | */ |
639 | */ |
607 | const char *str_chr(const char *str, wchar_t ch) |
640 | const char *str_chr(const char *str, wchar_t ch) |
608 | { |
641 | { |
609 | wchar_t acc; |
642 | wchar_t acc; |
610 | size_t off = 0; |
643 | size_t off = 0; |
- | 644 | size_t last = 0; |
|
611 | 645 | ||
612 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { |
646 | while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { |
613 | if (acc == ch) |
647 | if (acc == ch) |
614 | return (str + off); |
648 | return (str + last); |
- | 649 | last = off; |
|
615 | } |
650 | } |
616 | 651 | ||
617 | return NULL; |
652 | return NULL; |
618 | } |
653 | } |
619 | 654 |