source: mainline/uspace/lib/c/generic/str.c@ 8227d63

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8227d63 was 8227d63, checked in by Jiri Svoboda <jiri@…>, 10 years ago

UI for creating and deleting partitions.

  • Property mode set to 100644
File size: 44.0 KB
Line 
1/*
2 * Copyright (c) 2005 Martin Decky
3 * Copyright (c) 2008 Jiri Svoboda
4 * Copyright (c) 2011 Martin Sucha
5 * Copyright (c) 2011 Oleg Romanenko
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * - The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/** @addtogroup libc
33 * @{
34 */
35/** @file
36 */
37
38#include <str.h>
39#include <stdlib.h>
40#include <assert.h>
41#include <stdint.h>
42#include <ctype.h>
43#include <malloc.h>
44#include <errno.h>
45#include <align.h>
46#include <mem.h>
47#include <str.h>
48
49/** Check the condition if wchar_t is signed */
50#ifdef WCHAR_IS_UNSIGNED
51 #define WCHAR_SIGNED_CHECK(cond) (true)
52#else
53 #define WCHAR_SIGNED_CHECK(cond) (cond)
54#endif
55
56/** Byte mask consisting of lowest @n bits (out of 8) */
57#define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
58
59/** Byte mask consisting of lowest @n bits (out of 32) */
60#define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
61
62/** Byte mask consisting of highest @n bits (out of 8) */
63#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
64
65/** Number of data bits in a UTF-8 continuation byte */
66#define CONT_BITS 6
67
68/** Decode a single character from a string.
69 *
70 * Decode a single character from a string of size @a size. Decoding starts
71 * at @a offset and this offset is moved to the beginning of the next
72 * character. In case of decoding error, offset generally advances at least
73 * by one. However, offset is never moved beyond size.
74 *
75 * @param str String (not necessarily NULL-terminated).
76 * @param offset Byte offset in string where to start decoding.
77 * @param size Size of the string (in bytes).
78 *
79 * @return Value of decoded character, U_SPECIAL on decoding error or
80 * NULL if attempt to decode beyond @a size.
81 *
82 */
83wchar_t str_decode(const char *str, size_t *offset, size_t size)
84{
85 if (*offset + 1 > size)
86 return 0;
87
88 /* First byte read from string */
89 uint8_t b0 = (uint8_t) str[(*offset)++];
90
91 /* Determine code length */
92
93 unsigned int b0_bits; /* Data bits in first byte */
94 unsigned int cbytes; /* Number of continuation bytes */
95
96 if ((b0 & 0x80) == 0) {
97 /* 0xxxxxxx (Plain ASCII) */
98 b0_bits = 7;
99 cbytes = 0;
100 } else if ((b0 & 0xe0) == 0xc0) {
101 /* 110xxxxx 10xxxxxx */
102 b0_bits = 5;
103 cbytes = 1;
104 } else if ((b0 & 0xf0) == 0xe0) {
105 /* 1110xxxx 10xxxxxx 10xxxxxx */
106 b0_bits = 4;
107 cbytes = 2;
108 } else if ((b0 & 0xf8) == 0xf0) {
109 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
110 b0_bits = 3;
111 cbytes = 3;
112 } else {
113 /* 10xxxxxx -- unexpected continuation byte */
114 return U_SPECIAL;
115 }
116
117 if (*offset + cbytes > size)
118 return U_SPECIAL;
119
120 wchar_t ch = b0 & LO_MASK_8(b0_bits);
121
122 /* Decode continuation bytes */
123 while (cbytes > 0) {
124 uint8_t b = (uint8_t) str[(*offset)++];
125
126 /* Must be 10xxxxxx */
127 if ((b & 0xc0) != 0x80)
128 return U_SPECIAL;
129
130 /* Shift data bits to ch */
131 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
132 cbytes--;
133 }
134
135 return ch;
136}
137
138/** Decode a single character from a string to the left.
139 *
140 * Decode a single character from a string of size @a size. Decoding starts
141 * at @a offset and this offset is moved to the beginning of the previous
142 * character. In case of decoding error, offset generally decreases at least
143 * by one. However, offset is never moved before 0.
144 *
145 * @param str String (not necessarily NULL-terminated).
146 * @param offset Byte offset in string where to start decoding.
147 * @param size Size of the string (in bytes).
148 *
149 * @return Value of decoded character, U_SPECIAL on decoding error or
150 * NULL if attempt to decode beyond @a start of str.
151 *
152 */
153wchar_t str_decode_reverse(const char *str, size_t *offset, size_t size)
154{
155 if (*offset == 0)
156 return 0;
157
158 size_t processed = 0;
159 /* Continue while continuation bytes found */
160 while (*offset > 0 && processed < 4) {
161 uint8_t b = (uint8_t) str[--(*offset)];
162
163 if (processed == 0 && (b & 0x80) == 0) {
164 /* 0xxxxxxx (Plain ASCII) */
165 return b & 0x7f;
166 }
167 else if ((b & 0xe0) == 0xc0 || (b & 0xf0) == 0xe0 ||
168 (b & 0xf8) == 0xf0) {
169 /* Start byte */
170 size_t start_offset = *offset;
171 return str_decode(str, &start_offset, size);
172 }
173 else if ((b & 0xc0) != 0x80) {
174 /* Not a continuation byte */
175 return U_SPECIAL;
176 }
177 processed++;
178 }
179 /* Too many continuation bytes */
180 return U_SPECIAL;
181}
182
183/** Encode a single character to string representation.
184 *
185 * Encode a single character to string representation (i.e. UTF-8) and store
186 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
187 * is moved to the position where the next character can be written to.
188 *
189 * @param ch Input character.
190 * @param str Output buffer.
191 * @param offset Byte offset where to start writing.
192 * @param size Size of the output buffer (in bytes).
193 *
194 * @return EOK if the character was encoded successfully, EOVERFLOW if there
195 * was not enough space in the output buffer or EINVAL if the character
196 * code was invalid.
197 */
198int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
199{
200 if (*offset >= size)
201 return EOVERFLOW;
202
203 if (!chr_check(ch))
204 return EINVAL;
205
206 /* Unsigned version of ch (bit operations should only be done
207 on unsigned types). */
208 uint32_t cc = (uint32_t) ch;
209
210 /* Determine how many continuation bytes are needed */
211
212 unsigned int b0_bits; /* Data bits in first byte */
213 unsigned int cbytes; /* Number of continuation bytes */
214
215 if ((cc & ~LO_MASK_32(7)) == 0) {
216 b0_bits = 7;
217 cbytes = 0;
218 } else if ((cc & ~LO_MASK_32(11)) == 0) {
219 b0_bits = 5;
220 cbytes = 1;
221 } else if ((cc & ~LO_MASK_32(16)) == 0) {
222 b0_bits = 4;
223 cbytes = 2;
224 } else if ((cc & ~LO_MASK_32(21)) == 0) {
225 b0_bits = 3;
226 cbytes = 3;
227 } else {
228 /* Codes longer than 21 bits are not supported */
229 return EINVAL;
230 }
231
232 /* Check for available space in buffer */
233 if (*offset + cbytes >= size)
234 return EOVERFLOW;
235
236 /* Encode continuation bytes */
237 unsigned int i;
238 for (i = cbytes; i > 0; i--) {
239 str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
240 cc = cc >> CONT_BITS;
241 }
242
243 /* Encode first byte */
244 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
245
246 /* Advance offset */
247 *offset += cbytes + 1;
248
249 return EOK;
250}
251
252/** Get size of string.
253 *
254 * Get the number of bytes which are used by the string @a str (excluding the
255 * NULL-terminator).
256 *
257 * @param str String to consider.
258 *
259 * @return Number of bytes used by the string
260 *
261 */
262size_t str_size(const char *str)
263{
264 size_t size = 0;
265
266 while (*str++ != 0)
267 size++;
268
269 return size;
270}
271
272/** Get size of wide string.
273 *
274 * Get the number of bytes which are used by the wide string @a str (excluding the
275 * NULL-terminator).
276 *
277 * @param str Wide string to consider.
278 *
279 * @return Number of bytes used by the wide string
280 *
281 */
282size_t wstr_size(const wchar_t *str)
283{
284 return (wstr_length(str) * sizeof(wchar_t));
285}
286
287/** Get size of string with length limit.
288 *
289 * Get the number of bytes which are used by up to @a max_len first
290 * characters in the string @a str. If @a max_len is greater than
291 * the length of @a str, the entire string is measured (excluding the
292 * NULL-terminator).
293 *
294 * @param str String to consider.
295 * @param max_len Maximum number of characters to measure.
296 *
297 * @return Number of bytes used by the characters.
298 *
299 */
300size_t str_lsize(const char *str, size_t max_len)
301{
302 size_t len = 0;
303 size_t offset = 0;
304
305 while (len < max_len) {
306 if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
307 break;
308
309 len++;
310 }
311
312 return offset;
313}
314
315/** Get size of string with size limit.
316 *
317 * Get the number of bytes which are used by the string @a str
318 * (excluding the NULL-terminator), but no more than @max_size bytes.
319 *
320 * @param str String to consider.
321 * @param max_size Maximum number of bytes to measure.
322 *
323 * @return Number of bytes used by the string
324 *
325 */
326size_t str_nsize(const char *str, size_t max_size)
327{
328 size_t size = 0;
329
330 while ((*str++ != 0) && (size < max_size))
331 size++;
332
333 return size;
334}
335
336/** Get size of wide string with size limit.
337 *
338 * Get the number of bytes which are used by the wide string @a str
339 * (excluding the NULL-terminator), but no more than @max_size bytes.
340 *
341 * @param str Wide string to consider.
342 * @param max_size Maximum number of bytes to measure.
343 *
344 * @return Number of bytes used by the wide string
345 *
346 */
347size_t wstr_nsize(const wchar_t *str, size_t max_size)
348{
349 return (wstr_nlength(str, max_size) * sizeof(wchar_t));
350}
351
352/** Get size of wide string with length limit.
353 *
354 * Get the number of bytes which are used by up to @a max_len first
355 * wide characters in the wide string @a str. If @a max_len is greater than
356 * the length of @a str, the entire wide string is measured (excluding the
357 * NULL-terminator).
358 *
359 * @param str Wide string to consider.
360 * @param max_len Maximum number of wide characters to measure.
361 *
362 * @return Number of bytes used by the wide characters.
363 *
364 */
365size_t wstr_lsize(const wchar_t *str, size_t max_len)
366{
367 return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
368}
369
370/** Get number of characters in a string.
371 *
372 * @param str NULL-terminated string.
373 *
374 * @return Number of characters in string.
375 *
376 */
377size_t str_length(const char *str)
378{
379 size_t len = 0;
380 size_t offset = 0;
381
382 while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
383 len++;
384
385 return len;
386}
387
388/** Get number of characters in a wide string.
389 *
390 * @param str NULL-terminated wide string.
391 *
392 * @return Number of characters in @a str.
393 *
394 */
395size_t wstr_length(const wchar_t *wstr)
396{
397 size_t len = 0;
398
399 while (*wstr++ != 0)
400 len++;
401
402 return len;
403}
404
405/** Get number of characters in a string with size limit.
406 *
407 * @param str NULL-terminated string.
408 * @param size Maximum number of bytes to consider.
409 *
410 * @return Number of characters in string.
411 *
412 */
413size_t str_nlength(const char *str, size_t size)
414{
415 size_t len = 0;
416 size_t offset = 0;
417
418 while (str_decode(str, &offset, size) != 0)
419 len++;
420
421 return len;
422}
423
424/** Get number of characters in a string with size limit.
425 *
426 * @param str NULL-terminated string.
427 * @param size Maximum number of bytes to consider.
428 *
429 * @return Number of characters in string.
430 *
431 */
432size_t wstr_nlength(const wchar_t *str, size_t size)
433{
434 size_t len = 0;
435 size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
436 size_t offset = 0;
437
438 while ((offset < limit) && (*str++ != 0)) {
439 len++;
440 offset += sizeof(wchar_t);
441 }
442
443 return len;
444}
445
446/** Get character display width on a character cell display.
447 *
448 * @param ch Character
449 * @return Width of character in cells.
450 */
451size_t chr_width(wchar_t ch)
452{
453 return 1;
454}
455
456/** Get string display width on a character cell display.
457 *
458 * @param str String
459 * @return Width of string in cells.
460 */
461size_t str_width(const char *str)
462{
463 size_t width = 0;
464 size_t offset = 0;
465 wchar_t ch;
466
467 while ((ch = str_decode(str, &offset, STR_NO_LIMIT)) != 0)
468 width += chr_width(ch);
469
470 return width;
471}
472
473/** Check whether character is plain ASCII.
474 *
475 * @return True if character is plain ASCII.
476 *
477 */
478bool ascii_check(wchar_t ch)
479{
480 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
481 return true;
482
483 return false;
484}
485
486/** Check whether character is valid
487 *
488 * @return True if character is a valid Unicode code point.
489 *
490 */
491bool chr_check(wchar_t ch)
492{
493 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
494 return true;
495
496 return false;
497}
498
499/** Compare two NULL terminated strings.
500 *
501 * Do a char-by-char comparison of two NULL-terminated strings.
502 * The strings are considered equal iff their length is equal
503 * and both strings consist of the same sequence of characters.
504 *
505 * A string S1 is less than another string S2 if it has a character with
506 * lower value at the first character position where the strings differ.
507 * If the strings differ in length, the shorter one is treated as if
508 * padded by characters with a value of zero.
509 *
510 * @param s1 First string to compare.
511 * @param s2 Second string to compare.
512 *
513 * @return 0 if the strings are equal, -1 if the first is less than the second,
514 * 1 if the second is less than the first.
515 *
516 */
517int str_cmp(const char *s1, const char *s2)
518{
519 wchar_t c1 = 0;
520 wchar_t c2 = 0;
521
522 size_t off1 = 0;
523 size_t off2 = 0;
524
525 while (true) {
526 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
527 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
528
529 if (c1 < c2)
530 return -1;
531
532 if (c1 > c2)
533 return 1;
534
535 if (c1 == 0 || c2 == 0)
536 break;
537 }
538
539 return 0;
540}
541
542/** Compare two NULL terminated strings with length limit.
543 *
544 * Do a char-by-char comparison of two NULL-terminated strings.
545 * The strings are considered equal iff
546 * min(str_length(s1), max_len) == min(str_length(s2), max_len)
547 * and both strings consist of the same sequence of characters,
548 * up to max_len characters.
549 *
550 * A string S1 is less than another string S2 if it has a character with
551 * lower value at the first character position where the strings differ.
552 * If the strings differ in length, the shorter one is treated as if
553 * padded by characters with a value of zero. Only the first max_len
554 * characters are considered.
555 *
556 * @param s1 First string to compare.
557 * @param s2 Second string to compare.
558 * @param max_len Maximum number of characters to consider.
559 *
560 * @return 0 if the strings are equal, -1 if the first is less than the second,
561 * 1 if the second is less than the first.
562 *
563 */
564int str_lcmp(const char *s1, const char *s2, size_t max_len)
565{
566 wchar_t c1 = 0;
567 wchar_t c2 = 0;
568
569 size_t off1 = 0;
570 size_t off2 = 0;
571
572 size_t len = 0;
573
574 while (true) {
575 if (len >= max_len)
576 break;
577
578 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
579 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
580
581 if (c1 < c2)
582 return -1;
583
584 if (c1 > c2)
585 return 1;
586
587 if (c1 == 0 || c2 == 0)
588 break;
589
590 ++len;
591 }
592
593 return 0;
594
595}
596
597/** Compare two NULL terminated strings in case-insensitive manner.
598 *
599 * Do a char-by-char comparison of two NULL-terminated strings.
600 * The strings are considered equal iff their length is equal
601 * and both strings consist of the same sequence of characters
602 * when converted to lower case.
603 *
604 * A string S1 is less than another string S2 if it has a character with
605 * lower value at the first character position where the strings differ.
606 * If the strings differ in length, the shorter one is treated as if
607 * padded by characters with a value of zero.
608 *
609 * @param s1 First string to compare.
610 * @param s2 Second string to compare.
611 *
612 * @return 0 if the strings are equal, -1 if the first is less than the second,
613 * 1 if the second is less than the first.
614 *
615 */
616int str_casecmp(const char *s1, const char *s2)
617{
618 wchar_t c1 = 0;
619 wchar_t c2 = 0;
620
621 size_t off1 = 0;
622 size_t off2 = 0;
623
624 while (true) {
625 c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
626 c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
627
628 if (c1 < c2)
629 return -1;
630
631 if (c1 > c2)
632 return 1;
633
634 if (c1 == 0 || c2 == 0)
635 break;
636 }
637
638 return 0;
639}
640
641/** Compare two NULL terminated strings with length limit in case-insensitive
642 * manner.
643 *
644 * Do a char-by-char comparison of two NULL-terminated strings.
645 * The strings are considered equal iff
646 * min(str_length(s1), max_len) == min(str_length(s2), max_len)
647 * and both strings consist of the same sequence of characters,
648 * up to max_len characters.
649 *
650 * A string S1 is less than another string S2 if it has a character with
651 * lower value at the first character position where the strings differ.
652 * If the strings differ in length, the shorter one is treated as if
653 * padded by characters with a value of zero. Only the first max_len
654 * characters are considered.
655 *
656 * @param s1 First string to compare.
657 * @param s2 Second string to compare.
658 * @param max_len Maximum number of characters to consider.
659 *
660 * @return 0 if the strings are equal, -1 if the first is less than the second,
661 * 1 if the second is less than the first.
662 *
663 */
664int str_lcasecmp(const char *s1, const char *s2, size_t max_len)
665{
666 wchar_t c1 = 0;
667 wchar_t c2 = 0;
668
669 size_t off1 = 0;
670 size_t off2 = 0;
671
672 size_t len = 0;
673
674 while (true) {
675 if (len >= max_len)
676 break;
677
678 c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
679 c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
680
681 if (c1 < c2)
682 return -1;
683
684 if (c1 > c2)
685 return 1;
686
687 if (c1 == 0 || c2 == 0)
688 break;
689
690 ++len;
691 }
692
693 return 0;
694
695}
696
697/** Test whether p is a prefix of s.
698 *
699 * Do a char-by-char comparison of two NULL-terminated strings
700 * and determine if p is a prefix of s.
701 *
702 * @param s The string in which to look
703 * @param p The string to check if it is a prefix of s
704 *
705 * @return true iff p is prefix of s else false
706 *
707 */
708bool str_test_prefix(const char *s, const char *p)
709{
710 wchar_t c1 = 0;
711 wchar_t c2 = 0;
712
713 size_t off1 = 0;
714 size_t off2 = 0;
715
716 while (true) {
717 c1 = str_decode(s, &off1, STR_NO_LIMIT);
718 c2 = str_decode(p, &off2, STR_NO_LIMIT);
719
720 if (c2 == 0)
721 return true;
722
723 if (c1 != c2)
724 return false;
725
726 if (c1 == 0)
727 break;
728 }
729
730 return false;
731}
732
733/** Copy string.
734 *
735 * Copy source string @a src to destination buffer @a dest.
736 * No more than @a size bytes are written. If the size of the output buffer
737 * is at least one byte, the output string will always be well-formed, i.e.
738 * null-terminated and containing only complete characters.
739 *
740 * @param dest Destination buffer.
741 * @param count Size of the destination buffer (must be > 0).
742 * @param src Source string.
743 *
744 */
745void str_cpy(char *dest, size_t size, const char *src)
746{
747 /* There must be space for a null terminator in the buffer. */
748 assert(size > 0);
749
750 size_t src_off = 0;
751 size_t dest_off = 0;
752
753 wchar_t ch;
754 while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
755 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
756 break;
757 }
758
759 dest[dest_off] = '\0';
760}
761
762/** Copy size-limited substring.
763 *
764 * Copy prefix of string @a src of max. size @a size to destination buffer
765 * @a dest. No more than @a size bytes are written. The output string will
766 * always be well-formed, i.e. null-terminated and containing only complete
767 * characters.
768 *
769 * No more than @a n bytes are read from the input string, so it does not
770 * have to be null-terminated.
771 *
772 * @param dest Destination buffer.
773 * @param count Size of the destination buffer (must be > 0).
774 * @param src Source string.
775 * @param n Maximum number of bytes to read from @a src.
776 *
777 */
778void str_ncpy(char *dest, size_t size, const char *src, size_t n)
779{
780 /* There must be space for a null terminator in the buffer. */
781 assert(size > 0);
782
783 size_t src_off = 0;
784 size_t dest_off = 0;
785
786 wchar_t ch;
787 while ((ch = str_decode(src, &src_off, n)) != 0) {
788 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
789 break;
790 }
791
792 dest[dest_off] = '\0';
793}
794
795/** Append one string to another.
796 *
797 * Append source string @a src to string in destination buffer @a dest.
798 * Size of the destination buffer is @a dest. If the size of the output buffer
799 * is at least one byte, the output string will always be well-formed, i.e.
800 * null-terminated and containing only complete characters.
801 *
802 * @param dest Destination buffer.
803 * @param count Size of the destination buffer.
804 * @param src Source string.
805 */
806void str_append(char *dest, size_t size, const char *src)
807{
808 size_t dstr_size;
809
810 dstr_size = str_size(dest);
811 if (dstr_size >= size)
812 return;
813
814 str_cpy(dest + dstr_size, size - dstr_size, src);
815}
816
817/** Convert space-padded ASCII to string.
818 *
819 * Common legacy text encoding in hardware is 7-bit ASCII fitted into
820 * a fixed-width byte buffer (bit 7 always zero), right-padded with spaces
821 * (ASCII 0x20). Convert space-padded ascii to string representation.
822 *
823 * If the text does not fit into the destination buffer, the function converts
824 * as many characters as possible and returns EOVERFLOW.
825 *
826 * If the text contains non-ASCII bytes (with bit 7 set), the whole string is
827 * converted anyway and invalid characters are replaced with question marks
828 * (U_SPECIAL) and the function returns EIO.
829 *
830 * Regardless of return value upon return @a dest will always be well-formed.
831 *
832 * @param dest Destination buffer
833 * @param size Size of destination buffer
834 * @param src Space-padded ASCII.
835 * @param n Size of the source buffer in bytes.
836 *
837 * @return EOK on success, EOVERFLOW if the text does not fit
838 * destination buffer, EIO if the text contains
839 * non-ASCII bytes.
840 */
841int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
842{
843 size_t sidx;
844 size_t didx;
845 size_t dlast;
846 uint8_t byte;
847 int rc;
848 int result;
849
850 /* There must be space for a null terminator in the buffer. */
851 assert(size > 0);
852 result = EOK;
853
854 didx = 0;
855 dlast = 0;
856 for (sidx = 0; sidx < n; ++sidx) {
857 byte = src[sidx];
858 if (!ascii_check(byte)) {
859 byte = U_SPECIAL;
860 result = EIO;
861 }
862
863 rc = chr_encode(byte, dest, &didx, size - 1);
864 if (rc != EOK) {
865 assert(rc == EOVERFLOW);
866 dest[didx] = '\0';
867 return rc;
868 }
869
870 /* Remember dest index after last non-empty character */
871 if (byte != 0x20)
872 dlast = didx;
873 }
874
875 /* Terminate string after last non-empty character */
876 dest[dlast] = '\0';
877 return result;
878}
879
880/** Convert wide string to string.
881 *
882 * Convert wide string @a src to string. The output is written to the buffer
883 * specified by @a dest and @a size. @a size must be non-zero and the string
884 * written will always be well-formed.
885 *
886 * @param dest Destination buffer.
887 * @param size Size of the destination buffer.
888 * @param src Source wide string.
889 */
890void wstr_to_str(char *dest, size_t size, const wchar_t *src)
891{
892 wchar_t ch;
893 size_t src_idx;
894 size_t dest_off;
895
896 /* There must be space for a null terminator in the buffer. */
897 assert(size > 0);
898
899 src_idx = 0;
900 dest_off = 0;
901
902 while ((ch = src[src_idx++]) != 0) {
903 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
904 break;
905 }
906
907 dest[dest_off] = '\0';
908}
909
910/** Convert UTF16 string to string.
911 *
912 * Convert utf16 string @a src to string. The output is written to the buffer
913 * specified by @a dest and @a size. @a size must be non-zero and the string
914 * written will always be well-formed. Surrogate pairs also supported.
915 *
916 * @param dest Destination buffer.
917 * @param size Size of the destination buffer.
918 * @param src Source utf16 string.
919 *
920 * @return EOK, if success, negative otherwise.
921 */
922int utf16_to_str(char *dest, size_t size, const uint16_t *src)
923{
924 size_t idx = 0, dest_off = 0;
925 wchar_t ch;
926 int rc = EOK;
927
928 /* There must be space for a null terminator in the buffer. */
929 assert(size > 0);
930
931 while (src[idx]) {
932 if ((src[idx] & 0xfc00) == 0xd800) {
933 if (src[idx + 1] && (src[idx + 1] & 0xfc00) == 0xdc00) {
934 ch = 0x10000;
935 ch += (src[idx] & 0x03FF) << 10;
936 ch += (src[idx + 1] & 0x03FF);
937 idx += 2;
938 }
939 else
940 break;
941 } else {
942 ch = src[idx];
943 idx++;
944 }
945 rc = chr_encode(ch, dest, &dest_off, size - 1);
946 if (rc != EOK)
947 break;
948 }
949 dest[dest_off] = '\0';
950 return rc;
951}
952
953int str_to_utf16(uint16_t *dest, size_t size, const char *src)
954{
955 int rc = EOK;
956 size_t offset = 0;
957 size_t idx = 0;
958 wchar_t c;
959
960 assert(size > 0);
961
962 while ((c = str_decode(src, &offset, STR_NO_LIMIT)) != 0) {
963 if (c > 0x10000) {
964 if (idx + 2 >= size - 1) {
965 rc = EOVERFLOW;
966 break;
967 }
968 c = (c - 0x10000);
969 dest[idx] = 0xD800 | (c >> 10);
970 dest[idx + 1] = 0xDC00 | (c & 0x3FF);
971 idx++;
972 } else {
973 dest[idx] = c;
974 }
975
976 idx++;
977 if (idx >= size - 1) {
978 rc = EOVERFLOW;
979 break;
980 }
981 }
982
983 dest[idx] = '\0';
984 return rc;
985}
986
987
988/** Convert wide string to new string.
989 *
990 * Convert wide string @a src to string. Space for the new string is allocated
991 * on the heap.
992 *
993 * @param src Source wide string.
994 * @return New string.
995 */
996char *wstr_to_astr(const wchar_t *src)
997{
998 char dbuf[STR_BOUNDS(1)];
999 char *str;
1000 wchar_t ch;
1001
1002 size_t src_idx;
1003 size_t dest_off;
1004 size_t dest_size;
1005
1006 /* Compute size of encoded string. */
1007
1008 src_idx = 0;
1009 dest_size = 0;
1010
1011 while ((ch = src[src_idx++]) != 0) {
1012 dest_off = 0;
1013 if (chr_encode(ch, dbuf, &dest_off, STR_BOUNDS(1)) != EOK)
1014 break;
1015 dest_size += dest_off;
1016 }
1017
1018 str = malloc(dest_size + 1);
1019 if (str == NULL)
1020 return NULL;
1021
1022 /* Encode string. */
1023
1024 src_idx = 0;
1025 dest_off = 0;
1026
1027 while ((ch = src[src_idx++]) != 0) {
1028 if (chr_encode(ch, str, &dest_off, dest_size) != EOK)
1029 break;
1030 }
1031
1032 str[dest_size] = '\0';
1033 return str;
1034}
1035
1036
1037/** Convert string to wide string.
1038 *
1039 * Convert string @a src to wide string. The output is written to the
1040 * buffer specified by @a dest and @a dlen. @a dlen must be non-zero
1041 * and the wide string written will always be null-terminated.
1042 *
1043 * @param dest Destination buffer.
1044 * @param dlen Length of destination buffer (number of wchars).
1045 * @param src Source string.
1046 */
1047void str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
1048{
1049 size_t offset;
1050 size_t di;
1051 wchar_t c;
1052
1053 assert(dlen > 0);
1054
1055 offset = 0;
1056 di = 0;
1057
1058 do {
1059 if (di >= dlen - 1)
1060 break;
1061
1062 c = str_decode(src, &offset, STR_NO_LIMIT);
1063 dest[di++] = c;
1064 } while (c != '\0');
1065
1066 dest[dlen - 1] = '\0';
1067}
1068
1069/** Convert string to wide string.
1070 *
1071 * Convert string @a src to wide string. A new wide NULL-terminated
1072 * string will be allocated on the heap.
1073 *
1074 * @param src Source string.
1075 */
1076wchar_t *str_to_awstr(const char *str)
1077{
1078 size_t len = str_length(str);
1079
1080 wchar_t *wstr = calloc(len+1, sizeof(wchar_t));
1081 if (wstr == NULL)
1082 return NULL;
1083
1084 str_to_wstr(wstr, len + 1, str);
1085 return wstr;
1086}
1087
1088/** Find first occurence of character in string.
1089 *
1090 * @param str String to search.
1091 * @param ch Character to look for.
1092 *
1093 * @return Pointer to character in @a str or NULL if not found.
1094 */
1095char *str_chr(const char *str, wchar_t ch)
1096{
1097 wchar_t acc;
1098 size_t off = 0;
1099 size_t last = 0;
1100
1101 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
1102 if (acc == ch)
1103 return (char *) (str + last);
1104 last = off;
1105 }
1106
1107 return NULL;
1108}
1109
1110/** Removes specified trailing characters from a string.
1111 *
1112 * @param str String to remove from.
1113 * @param ch Character to remove.
1114 */
1115void str_rtrim(char *str, wchar_t ch)
1116{
1117 size_t off = 0;
1118 size_t pos = 0;
1119 wchar_t c;
1120 bool update_last_chunk = true;
1121 char *last_chunk = NULL;
1122
1123 while ((c = str_decode(str, &off, STR_NO_LIMIT))) {
1124 if (c != ch) {
1125 update_last_chunk = true;
1126 last_chunk = NULL;
1127 } else if (update_last_chunk) {
1128 update_last_chunk = false;
1129 last_chunk = (str + pos);
1130 }
1131 pos = off;
1132 }
1133
1134 if (last_chunk)
1135 *last_chunk = '\0';
1136}
1137
1138/** Removes specified leading characters from a string.
1139 *
1140 * @param str String to remove from.
1141 * @param ch Character to remove.
1142 */
1143void str_ltrim(char *str, wchar_t ch)
1144{
1145 wchar_t acc;
1146 size_t off = 0;
1147 size_t pos = 0;
1148 size_t str_sz = str_size(str);
1149
1150 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
1151 if (acc != ch)
1152 break;
1153 else
1154 pos = off;
1155 }
1156
1157 if (pos > 0) {
1158 memmove(str, &str[pos], str_sz - pos);
1159 pos = str_sz - pos;
1160 str[str_sz - pos] = '\0';
1161 }
1162}
1163
1164/** Find last occurence of character in string.
1165 *
1166 * @param str String to search.
1167 * @param ch Character to look for.
1168 *
1169 * @return Pointer to character in @a str or NULL if not found.
1170 */
1171char *str_rchr(const char *str, wchar_t ch)
1172{
1173 wchar_t acc;
1174 size_t off = 0;
1175 size_t last = 0;
1176 const char *res = NULL;
1177
1178 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
1179 if (acc == ch)
1180 res = (str + last);
1181 last = off;
1182 }
1183
1184 return (char *) res;
1185}
1186
1187/** Insert a wide character into a wide string.
1188 *
1189 * Insert a wide character into a wide string at position
1190 * @a pos. The characters after the position are shifted.
1191 *
1192 * @param str String to insert to.
1193 * @param ch Character to insert to.
1194 * @param pos Character index where to insert.
1195 @ @param max_pos Characters in the buffer.
1196 *
1197 * @return True if the insertion was sucessful, false if the position
1198 * is out of bounds.
1199 *
1200 */
1201bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
1202{
1203 size_t len = wstr_length(str);
1204
1205 if ((pos > len) || (pos + 1 > max_pos))
1206 return false;
1207
1208 size_t i;
1209 for (i = len; i + 1 > pos; i--)
1210 str[i + 1] = str[i];
1211
1212 str[pos] = ch;
1213
1214 return true;
1215}
1216
1217/** Remove a wide character from a wide string.
1218 *
1219 * Remove a wide character from a wide string at position
1220 * @a pos. The characters after the position are shifted.
1221 *
1222 * @param str String to remove from.
1223 * @param pos Character index to remove.
1224 *
1225 * @return True if the removal was sucessful, false if the position
1226 * is out of bounds.
1227 *
1228 */
1229bool wstr_remove(wchar_t *str, size_t pos)
1230{
1231 size_t len = wstr_length(str);
1232
1233 if (pos >= len)
1234 return false;
1235
1236 size_t i;
1237 for (i = pos + 1; i <= len; i++)
1238 str[i - 1] = str[i];
1239
1240 return true;
1241}
1242
1243int stricmp(const char *a, const char *b)
1244{
1245 int c = 0;
1246
1247 while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
1248 c++;
1249
1250 return (tolower(a[c]) - tolower(b[c]));
1251}
1252
1253/** Convert string to a number.
1254 * Core of strtol and strtoul functions.
1255 *
1256 * @param nptr Pointer to string.
1257 * @param endptr If not NULL, function stores here pointer to the first
1258 * invalid character.
1259 * @param base Zero or number between 2 and 36 inclusive.
1260 * @param sgn It's set to 1 if minus found.
1261 * @return Result of conversion.
1262 */
1263static unsigned long
1264_strtoul(const char *nptr, char **endptr, int base, char *sgn)
1265{
1266 unsigned char c;
1267 unsigned long result = 0;
1268 unsigned long a, b;
1269 const char *str = nptr;
1270 const char *tmpptr;
1271
1272 while (isspace(*str))
1273 str++;
1274
1275 if (*str == '-') {
1276 *sgn = 1;
1277 ++str;
1278 } else if (*str == '+')
1279 ++str;
1280
1281 if (base) {
1282 if ((base == 1) || (base > 36)) {
1283 /* FIXME: set errno to EINVAL */
1284 return 0;
1285 }
1286 if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
1287 (str[1] == 'X'))) {
1288 str += 2;
1289 }
1290 } else {
1291 base = 10;
1292
1293 if (*str == '0') {
1294 base = 8;
1295 if ((str[1] == 'X') || (str[1] == 'x')) {
1296 base = 16;
1297 str += 2;
1298 }
1299 }
1300 }
1301
1302 tmpptr = str;
1303
1304 while (*str) {
1305 c = *str;
1306 c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
1307 (c <= '9' ? c - '0' : 0xff)));
1308 if (c >= base) {
1309 break;
1310 }
1311
1312 a = (result & 0xff) * base + c;
1313 b = (result >> 8) * base + (a >> 8);
1314
1315 if (b > (ULONG_MAX >> 8)) {
1316 /* overflow */
1317 /* FIXME: errno = ERANGE*/
1318 return ULONG_MAX;
1319 }
1320
1321 result = (b << 8) + (a & 0xff);
1322 ++str;
1323 }
1324
1325 if (str == tmpptr) {
1326 /*
1327 * No number was found => first invalid character is the first
1328 * character of the string.
1329 */
1330 /* FIXME: set errno to EINVAL */
1331 str = nptr;
1332 result = 0;
1333 }
1334
1335 if (endptr)
1336 *endptr = (char *) str;
1337
1338 if (nptr == str) {
1339 /*FIXME: errno = EINVAL*/
1340 return 0;
1341 }
1342
1343 return result;
1344}
1345
1346/** Convert initial part of string to long int according to given base.
1347 * The number may begin with an arbitrary number of whitespaces followed by
1348 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
1349 * inserted and the number will be taken as hexadecimal one. If the base is 0
1350 * and the number begin with a zero, number will be taken as octal one (as with
1351 * base 8). Otherwise the base 0 is taken as decimal.
1352 *
1353 * @param nptr Pointer to string.
1354 * @param endptr If not NULL, function stores here pointer to the first
1355 * invalid character.
1356 * @param base Zero or number between 2 and 36 inclusive.
1357 * @return Result of conversion.
1358 */
1359long int strtol(const char *nptr, char **endptr, int base)
1360{
1361 char sgn = 0;
1362 unsigned long number = 0;
1363
1364 number = _strtoul(nptr, endptr, base, &sgn);
1365
1366 if (number > LONG_MAX) {
1367 if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
1368 /* FIXME: set 0 to errno */
1369 return number;
1370 }
1371 /* FIXME: set ERANGE to errno */
1372 return (sgn ? LONG_MIN : LONG_MAX);
1373 }
1374
1375 return (sgn ? -number : number);
1376}
1377
1378/** Duplicate string.
1379 *
1380 * Allocate a new string and copy characters from the source
1381 * string into it. The duplicate string is allocated via sleeping
1382 * malloc(), thus this function can sleep in no memory conditions.
1383 *
1384 * The allocation cannot fail and the return value is always
1385 * a valid pointer. The duplicate string is always a well-formed
1386 * null-terminated UTF-8 string, but it can differ from the source
1387 * string on the byte level.
1388 *
1389 * @param src Source string.
1390 *
1391 * @return Duplicate string.
1392 *
1393 */
1394char *str_dup(const char *src)
1395{
1396 size_t size = str_size(src) + 1;
1397 char *dest = (char *) malloc(size);
1398 if (dest == NULL)
1399 return (char *) NULL;
1400
1401 str_cpy(dest, size, src);
1402 return dest;
1403}
1404
1405/** Duplicate string with size limit.
1406 *
1407 * Allocate a new string and copy up to @max_size bytes from the source
1408 * string into it. The duplicate string is allocated via sleeping
1409 * malloc(), thus this function can sleep in no memory conditions.
1410 * No more than @max_size + 1 bytes is allocated, but if the size
1411 * occupied by the source string is smaller than @max_size + 1,
1412 * less is allocated.
1413 *
1414 * The allocation cannot fail and the return value is always
1415 * a valid pointer. The duplicate string is always a well-formed
1416 * null-terminated UTF-8 string, but it can differ from the source
1417 * string on the byte level.
1418 *
1419 * @param src Source string.
1420 * @param n Maximum number of bytes to duplicate.
1421 *
1422 * @return Duplicate string.
1423 *
1424 */
1425char *str_ndup(const char *src, size_t n)
1426{
1427 size_t size = str_size(src);
1428 if (size > n)
1429 size = n;
1430
1431 char *dest = (char *) malloc(size + 1);
1432 if (dest == NULL)
1433 return (char *) NULL;
1434
1435 str_ncpy(dest, size + 1, src, size);
1436 return dest;
1437}
1438
1439/** Convert initial part of string to unsigned long according to given base.
1440 * The number may begin with an arbitrary number of whitespaces followed by
1441 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
1442 * inserted and the number will be taken as hexadecimal one. If the base is 0
1443 * and the number begin with a zero, number will be taken as octal one (as with
1444 * base 8). Otherwise the base 0 is taken as decimal.
1445 *
1446 * @param nptr Pointer to string.
1447 * @param endptr If not NULL, function stores here pointer to the first
1448 * invalid character
1449 * @param base Zero or number between 2 and 36 inclusive.
1450 * @return Result of conversion.
1451 */
1452unsigned long strtoul(const char *nptr, char **endptr, int base)
1453{
1454 char sgn = 0;
1455 unsigned long number = 0;
1456
1457 number = _strtoul(nptr, endptr, base, &sgn);
1458
1459 return (sgn ? -number : number);
1460}
1461
1462/** Split string by delimiters.
1463 *
1464 * @param s String to be tokenized. May not be NULL.
1465 * @param delim String with the delimiters.
1466 * @param next Variable which will receive the pointer to the
1467 * continuation of the string following the first
1468 * occurrence of any of the delimiter characters.
1469 * May be NULL.
1470 * @return Pointer to the prefix of @a s before the first
1471 * delimiter character. NULL if no such prefix
1472 * exists.
1473 */
1474char *str_tok(char *s, const char *delim, char **next)
1475{
1476 char *start, *end;
1477
1478 if (!s)
1479 return NULL;
1480
1481 size_t len = str_size(s);
1482 size_t cur;
1483 size_t tmp;
1484 wchar_t ch;
1485
1486 /* Skip over leading delimiters. */
1487 for (tmp = cur = 0;
1488 (ch = str_decode(s, &tmp, len)) && str_chr(delim, ch); /**/)
1489 cur = tmp;
1490 start = &s[cur];
1491
1492 /* Skip over token characters. */
1493 for (tmp = cur;
1494 (ch = str_decode(s, &tmp, len)) && !str_chr(delim, ch); /**/)
1495 cur = tmp;
1496 end = &s[cur];
1497 if (next)
1498 *next = (ch ? &s[tmp] : &s[cur]);
1499
1500 if (start == end)
1501 return NULL; /* No more tokens. */
1502
1503 /* Overwrite delimiter with NULL terminator. */
1504 *end = '\0';
1505 return start;
1506}
1507
1508/** Convert string to uint64_t (internal variant).
1509 *
1510 * @param nptr Pointer to string.
1511 * @param endptr Pointer to the first invalid character is stored here.
1512 * @param base Zero or number between 2 and 36 inclusive.
1513 * @param neg Indication of unary minus is stored here.
1514 * @apram result Result of the conversion.
1515 *
1516 * @return EOK if conversion was successful.
1517 *
1518 */
1519static int str_uint(const char *nptr, char **endptr, unsigned int base,
1520 bool *neg, uint64_t *result)
1521{
1522 assert(endptr != NULL);
1523 assert(neg != NULL);
1524 assert(result != NULL);
1525
1526 *neg = false;
1527 const char *str = nptr;
1528
1529 /* Ignore leading whitespace */
1530 while (isspace(*str))
1531 str++;
1532
1533 if (*str == '-') {
1534 *neg = true;
1535 str++;
1536 } else if (*str == '+')
1537 str++;
1538
1539 if (base == 0) {
1540 /* Decode base if not specified */
1541 base = 10;
1542
1543 if (*str == '0') {
1544 base = 8;
1545 str++;
1546
1547 switch (*str) {
1548 case 'b':
1549 case 'B':
1550 base = 2;
1551 str++;
1552 break;
1553 case 'o':
1554 case 'O':
1555 base = 8;
1556 str++;
1557 break;
1558 case 'd':
1559 case 'D':
1560 case 't':
1561 case 'T':
1562 base = 10;
1563 str++;
1564 break;
1565 case 'x':
1566 case 'X':
1567 base = 16;
1568 str++;
1569 break;
1570 default:
1571 str--;
1572 }
1573 }
1574 } else {
1575 /* Check base range */
1576 if ((base < 2) || (base > 36)) {
1577 *endptr = (char *) str;
1578 return EINVAL;
1579 }
1580 }
1581
1582 *result = 0;
1583 const char *startstr = str;
1584
1585 while (*str != 0) {
1586 unsigned int digit;
1587
1588 if ((*str >= 'a') && (*str <= 'z'))
1589 digit = *str - 'a' + 10;
1590 else if ((*str >= 'A') && (*str <= 'Z'))
1591 digit = *str - 'A' + 10;
1592 else if ((*str >= '0') && (*str <= '9'))
1593 digit = *str - '0';
1594 else
1595 break;
1596
1597 if (digit >= base)
1598 break;
1599
1600 uint64_t prev = *result;
1601 *result = (*result) * base + digit;
1602
1603 if (*result < prev) {
1604 /* Overflow */
1605 *endptr = (char *) str;
1606 return EOVERFLOW;
1607 }
1608
1609 str++;
1610 }
1611
1612 if (str == startstr) {
1613 /*
1614 * No digits were decoded => first invalid character is
1615 * the first character of the string.
1616 */
1617 str = nptr;
1618 }
1619
1620 *endptr = (char *) str;
1621
1622 if (str == nptr)
1623 return EINVAL;
1624
1625 return EOK;
1626}
1627
1628/** Convert string to uint8_t.
1629 *
1630 * @param nptr Pointer to string.
1631 * @param endptr If not NULL, pointer to the first invalid character
1632 * is stored here.
1633 * @param base Zero or number between 2 and 36 inclusive.
1634 * @param strict Do not allow any trailing characters.
1635 * @param result Result of the conversion.
1636 *
1637 * @return EOK if conversion was successful.
1638 *
1639 */
1640int str_uint8_t(const char *nptr, const char **endptr, unsigned int base,
1641 bool strict, uint8_t *result)
1642{
1643 assert(result != NULL);
1644
1645 bool neg;
1646 char *lendptr;
1647 uint64_t res;
1648 int ret = str_uint(nptr, &lendptr, base, &neg, &res);
1649
1650 if (endptr != NULL)
1651 *endptr = (char *) lendptr;
1652
1653 if (ret != EOK)
1654 return ret;
1655
1656 /* Do not allow negative values */
1657 if (neg)
1658 return EINVAL;
1659
1660 /* Check whether we are at the end of
1661 the string in strict mode */
1662 if ((strict) && (*lendptr != 0))
1663 return EINVAL;
1664
1665 /* Check for overflow */
1666 uint8_t _res = (uint8_t) res;
1667 if (_res != res)
1668 return EOVERFLOW;
1669
1670 *result = _res;
1671
1672 return EOK;
1673}
1674
1675/** Convert string to uint16_t.
1676 *
1677 * @param nptr Pointer to string.
1678 * @param endptr If not NULL, pointer to the first invalid character
1679 * is stored here.
1680 * @param base Zero or number between 2 and 36 inclusive.
1681 * @param strict Do not allow any trailing characters.
1682 * @param result Result of the conversion.
1683 *
1684 * @return EOK if conversion was successful.
1685 *
1686 */
1687int str_uint16_t(const char *nptr, const char **endptr, unsigned int base,
1688 bool strict, uint16_t *result)
1689{
1690 assert(result != NULL);
1691
1692 bool neg;
1693 char *lendptr;
1694 uint64_t res;
1695 int ret = str_uint(nptr, &lendptr, base, &neg, &res);
1696
1697 if (endptr != NULL)
1698 *endptr = (char *) lendptr;
1699
1700 if (ret != EOK)
1701 return ret;
1702
1703 /* Do not allow negative values */
1704 if (neg)
1705 return EINVAL;
1706
1707 /* Check whether we are at the end of
1708 the string in strict mode */
1709 if ((strict) && (*lendptr != 0))
1710 return EINVAL;
1711
1712 /* Check for overflow */
1713 uint16_t _res = (uint16_t) res;
1714 if (_res != res)
1715 return EOVERFLOW;
1716
1717 *result = _res;
1718
1719 return EOK;
1720}
1721
1722/** Convert string to uint32_t.
1723 *
1724 * @param nptr Pointer to string.
1725 * @param endptr If not NULL, pointer to the first invalid character
1726 * is stored here.
1727 * @param base Zero or number between 2 and 36 inclusive.
1728 * @param strict Do not allow any trailing characters.
1729 * @param result Result of the conversion.
1730 *
1731 * @return EOK if conversion was successful.
1732 *
1733 */
1734int str_uint32_t(const char *nptr, const char **endptr, unsigned int base,
1735 bool strict, uint32_t *result)
1736{
1737 assert(result != NULL);
1738
1739 bool neg;
1740 char *lendptr;
1741 uint64_t res;
1742 int ret = str_uint(nptr, &lendptr, base, &neg, &res);
1743
1744 if (endptr != NULL)
1745 *endptr = (char *) lendptr;
1746
1747 if (ret != EOK)
1748 return ret;
1749
1750 /* Do not allow negative values */
1751 if (neg)
1752 return EINVAL;
1753
1754 /* Check whether we are at the end of
1755 the string in strict mode */
1756 if ((strict) && (*lendptr != 0))
1757 return EINVAL;
1758
1759 /* Check for overflow */
1760 uint32_t _res = (uint32_t) res;
1761 if (_res != res)
1762 return EOVERFLOW;
1763
1764 *result = _res;
1765
1766 return EOK;
1767}
1768
1769/** Convert string to uint64_t.
1770 *
1771 * @param nptr Pointer to string.
1772 * @param endptr If not NULL, pointer to the first invalid character
1773 * is stored here.
1774 * @param base Zero or number between 2 and 36 inclusive.
1775 * @param strict Do not allow any trailing characters.
1776 * @param result Result of the conversion.
1777 *
1778 * @return EOK if conversion was successful.
1779 *
1780 */
1781int str_uint64_t(const char *nptr, const char **endptr, unsigned int base,
1782 bool strict, uint64_t *result)
1783{
1784 assert(result != NULL);
1785
1786 bool neg;
1787 char *lendptr;
1788 int ret = str_uint(nptr, &lendptr, base, &neg, result);
1789
1790 if (endptr != NULL)
1791 *endptr = (char *) lendptr;
1792
1793 if (ret != EOK)
1794 return ret;
1795
1796 /* Do not allow negative values */
1797 if (neg)
1798 return EINVAL;
1799
1800 /* Check whether we are at the end of
1801 the string in strict mode */
1802 if ((strict) && (*lendptr != 0))
1803 return EINVAL;
1804
1805 return EOK;
1806}
1807
1808/** Convert string to size_t.
1809 *
1810 * @param nptr Pointer to string.
1811 * @param endptr If not NULL, pointer to the first invalid character
1812 * is stored here.
1813 * @param base Zero or number between 2 and 36 inclusive.
1814 * @param strict Do not allow any trailing characters.
1815 * @param result Result of the conversion.
1816 *
1817 * @return EOK if conversion was successful.
1818 *
1819 */
1820int str_size_t(const char *nptr, const char **endptr, unsigned int base,
1821 bool strict, size_t *result)
1822{
1823 assert(result != NULL);
1824
1825 bool neg;
1826 char *lendptr;
1827 uint64_t res;
1828 int ret = str_uint(nptr, &lendptr, base, &neg, &res);
1829
1830 if (endptr != NULL)
1831 *endptr = (char *) lendptr;
1832
1833 if (ret != EOK)
1834 return ret;
1835
1836 /* Do not allow negative values */
1837 if (neg)
1838 return EINVAL;
1839
1840 /* Check whether we are at the end of
1841 the string in strict mode */
1842 if ((strict) && (*lendptr != 0))
1843 return EINVAL;
1844
1845 /* Check for overflow */
1846 size_t _res = (size_t) res;
1847 if (_res != res)
1848 return EOVERFLOW;
1849
1850 *result = _res;
1851
1852 return EOK;
1853}
1854
1855void order_suffix(const uint64_t val, uint64_t *rv, char *suffix)
1856{
1857 if (val > UINT64_C(10000000000000000000)) {
1858 *rv = val / UINT64_C(1000000000000000000);
1859 *suffix = 'Z';
1860 } else if (val > UINT64_C(1000000000000000000)) {
1861 *rv = val / UINT64_C(1000000000000000);
1862 *suffix = 'E';
1863 } else if (val > UINT64_C(1000000000000000)) {
1864 *rv = val / UINT64_C(1000000000000);
1865 *suffix = 'T';
1866 } else if (val > UINT64_C(1000000000000)) {
1867 *rv = val / UINT64_C(1000000000);
1868 *suffix = 'G';
1869 } else if (val > UINT64_C(1000000000)) {
1870 *rv = val / UINT64_C(1000000);
1871 *suffix = 'M';
1872 } else if (val > UINT64_C(1000000)) {
1873 *rv = val / UINT64_C(1000);
1874 *suffix = 'k';
1875 } else {
1876 *rv = val;
1877 *suffix = ' ';
1878 }
1879}
1880
1881void bin_order_suffix(const uint64_t val, uint64_t *rv, const char **suffix,
1882 bool fixed)
1883{
1884 if (val > UINT64_C(1152921504606846976)) {
1885 *rv = val / UINT64_C(1125899906842624);
1886 *suffix = "EiB";
1887 } else if (val > UINT64_C(1125899906842624)) {
1888 *rv = val / UINT64_C(1099511627776);
1889 *suffix = "TiB";
1890 } else if (val > UINT64_C(1099511627776)) {
1891 *rv = val / UINT64_C(1073741824);
1892 *suffix = "GiB";
1893 } else if (val > UINT64_C(1073741824)) {
1894 *rv = val / UINT64_C(1048576);
1895 *suffix = "MiB";
1896 } else if (val > UINT64_C(1048576)) {
1897 *rv = val / UINT64_C(1024);
1898 *suffix = "KiB";
1899 } else {
1900 *rv = val;
1901 if (fixed)
1902 *suffix = "B ";
1903 else
1904 *suffix = "B";
1905 }
1906}
1907
1908/** @}
1909 */
Note: See TracBrowser for help on using the repository browser.