Subversion Repositories HelenOS

Rev

Rev 4055 | Rev 4420 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4055 Rev 4296
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
3
 * Copyright (c) 2008 Jiri Svoboda
3
 * Copyright (c) 2008 Jiri Svoboda
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libc
30
/** @addtogroup libc
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <string.h>
36
#include <string.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
-
 
38
#include <assert.h>
38
#include <limits.h>
39
#include <limits.h>
39
#include <ctype.h>
40
#include <ctype.h>
40
#include <malloc.h>
41
#include <malloc.h>
-
 
42
#include <errno.h>
-
 
43
#include <align.h>
-
 
44
#include <mem.h>
-
 
45
#include <string.h>
41
 
46
 
-
 
47
/** Byte mask consisting of lowest @n bits (out of 8) */
-
 
48
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
-
 
49
 
-
 
50
/** Byte mask consisting of lowest @n bits (out of 32) */
-
 
51
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
-
 
52
 
-
 
53
/** Byte mask consisting of highest @n bits (out of 8) */
-
 
54
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
-
 
55
 
-
 
56
/** Number of data bits in a UTF-8 continuation byte */
-
 
57
#define CONT_BITS  6
-
 
58
 
-
 
59
/** Decode a single character from a string.
-
 
60
 *
42
/** Count the number of characters in the string, not including terminating 0.
61
 * Decode a single character from a string of size @a size. Decoding starts
-
 
62
 * at @a offset and this offset is moved to the beginning of the next
-
 
63
 * character. In case of decoding error, offset generally advances at least
-
 
64
 * by one. However, offset is never moved beyond size.
-
 
65
 *
-
 
66
 * @param str    String (not necessarily NULL-terminated).
-
 
67
 * @param offset Byte offset in string where to start decoding.
-
 
68
 * @param size   Size of the string (in bytes).
-
 
69
 *
-
 
70
 * @return Value of decoded character, U_SPECIAL on decoding error or
-
 
71
 *         NULL if attempt to decode beyond @a size.
43
 *
72
 *
44
 * @param str       String.
-
 
45
 * @return      Number of characters in string.
-
 
46
 */
73
 */
47
size_t strlen(const char *str)
74
wchar_t str_decode(const char *str, size_t *offset, size_t size)
48
{
75
{
-
 
76
    if (*offset + 1 > size)
-
 
77
        return 0;
-
 
78
   
-
 
79
    /* First byte read from string */
-
 
80
    uint8_t b0 = (uint8_t) str[(*offset)++];
-
 
81
   
-
 
82
    /* Determine code length */
-
 
83
   
-
 
84
    unsigned int b0_bits;  /* Data bits in first byte */
-
 
85
    unsigned int cbytes;   /* Number of continuation bytes */
-
 
86
   
-
 
87
    if ((b0 & 0x80) == 0) {
-
 
88
        /* 0xxxxxxx (Plain ASCII) */
-
 
89
        b0_bits = 7;
-
 
90
        cbytes = 0;
-
 
91
    } else if ((b0 & 0xe0) == 0xc0) {
-
 
92
        /* 110xxxxx 10xxxxxx */
-
 
93
        b0_bits = 5;
-
 
94
        cbytes = 1;
-
 
95
    } else if ((b0 & 0xf0) == 0xe0) {
-
 
96
        /* 1110xxxx 10xxxxxx 10xxxxxx */
-
 
97
        b0_bits = 4;
-
 
98
        cbytes = 2;
-
 
99
    } else if ((b0 & 0xf8) == 0xf0) {
-
 
100
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
-
 
101
        b0_bits = 3;
-
 
102
        cbytes = 3;
-
 
103
    } else {
-
 
104
        /* 10xxxxxx -- unexpected continuation byte */
-
 
105
        return U_SPECIAL;
-
 
106
    }
-
 
107
   
-
 
108
    if (*offset + cbytes > size)
-
 
109
        return U_SPECIAL;
-
 
110
   
-
 
111
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
-
 
112
   
-
 
113
    /* Decode continuation bytes */
-
 
114
    while (cbytes > 0) {
-
 
115
        uint8_t b = (uint8_t) str[(*offset)++];
-
 
116
       
-
 
117
        /* Must be 10xxxxxx */
-
 
118
        if ((b & 0xc0) != 0x80)
-
 
119
            return U_SPECIAL;
-
 
120
       
-
 
121
        /* Shift data bits to ch */
-
 
122
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
-
 
123
        cbytes--;
-
 
124
    }
-
 
125
   
-
 
126
    return ch;
-
 
127
}
-
 
128
 
-
 
129
/** Encode a single character to string representation.
-
 
130
 *
-
 
131
 * Encode a single character to string representation (i.e. UTF-8) and store
-
 
132
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
-
 
133
 * is moved to the position where the next character can be written to.
-
 
134
 *
-
 
135
 * @param ch     Input character.
-
 
136
 * @param str    Output buffer.
-
 
137
 * @param offset Byte offset where to start writing.
-
 
138
 * @param size   Size of the output buffer (in bytes).
-
 
139
 *
-
 
140
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
-
 
141
 *     was not enough space in the output buffer or EINVAL if the character
-
 
142
 *     code was invalid.
-
 
143
 */
-
 
144
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
-
 
145
{
-
 
146
    if (*offset >= size)
-
 
147
        return EOVERFLOW;
-
 
148
   
-
 
149
    if (!chr_check(ch))
-
 
150
        return EINVAL;
-
 
151
   
-
 
152
    /* Unsigned version of ch (bit operations should only be done
-
 
153
       on unsigned types). */
-
 
154
    uint32_t cc = (uint32_t) ch;
-
 
155
   
-
 
156
    /* Determine how many continuation bytes are needed */
-
 
157
   
-
 
158
    unsigned int b0_bits;  /* Data bits in first byte */
-
 
159
    unsigned int cbytes;   /* Number of continuation bytes */
-
 
160
   
-
 
161
    if ((cc & ~LO_MASK_32(7)) == 0) {
-
 
162
        b0_bits = 7;
49
    size_t counter = 0;
163
        cbytes = 0;
-
 
164
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
-
 
165
        b0_bits = 5;
-
 
166
        cbytes = 1;
-
 
167
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
-
 
168
        b0_bits = 4;
-
 
169
        cbytes = 2;
-
 
170
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
-
 
171
        b0_bits = 3;
-
 
172
        cbytes = 3;
-
 
173
    } else {
-
 
174
        /* Codes longer than 21 bits are not supported */
-
 
175
        return EINVAL;
-
 
176
    }
-
 
177
   
-
 
178
    /* Check for available space in buffer */
-
 
179
    if (*offset + cbytes >= size)
-
 
180
        return EOVERFLOW;
-
 
181
   
-
 
182
    /* Encode continuation bytes */
-
 
183
    unsigned int i;
-
 
184
    for (i = cbytes; i > 0; i--) {
-
 
185
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
-
 
186
        cc = cc >> CONT_BITS;
-
 
187
    }
-
 
188
   
-
 
189
    /* Encode first byte */
-
 
190
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
-
 
191
   
-
 
192
    /* Advance offset */
-
 
193
    *offset += cbytes + 1;
-
 
194
   
-
 
195
    return EOK;
-
 
196
}
50
 
197
 
-
 
198
/** Get size of string.
-
 
199
 *
-
 
200
 * Get the number of bytes which are used by the string @a str (excluding the
-
 
201
 * NULL-terminator).
-
 
202
 *
-
 
203
 * @param str String to consider.
-
 
204
 *
-
 
205
 * @return Number of bytes used by the string
-
 
206
 *
-
 
207
 */
-
 
208
size_t str_size(const char *str)
-
 
209
{
-
 
210
    size_t size = 0;
-
 
211
   
51
    while (str[counter] != 0)
212
    while (*str++ != 0)
52
        counter++;
213
        size++;
-
 
214
   
-
 
215
    return size;
-
 
216
}
53
 
217
 
-
 
218
/** Get size of wide string.
-
 
219
 *
-
 
220
 * Get the number of bytes which are used by the wide string @a str (excluding the
54
    return counter;
221
 * NULL-terminator).
-
 
222
 *
-
 
223
 * @param str Wide string to consider.
-
 
224
 *
-
 
225
 * @return Number of bytes used by the wide string
-
 
226
 *
-
 
227
 */
-
 
228
size_t wstr_size(const wchar_t *str)
-
 
229
{
-
 
230
    return (wstr_length(str) * sizeof(wchar_t));
55
}
231
}
56
 
232
 
-
 
233
/** Get size of string with length limit.
-
 
234
 *
-
 
235
 * Get the number of bytes which are used by up to @a max_len first
-
 
236
 * characters in the string @a str. If @a max_len is greater than
-
 
237
 * the length of @a str, the entire string is measured (excluding the
-
 
238
 * NULL-terminator).
-
 
239
 *
-
 
240
 * @param str     String to consider.
-
 
241
 * @param max_len Maximum number of characters to measure.
-
 
242
 *
-
 
243
 * @return Number of bytes used by the characters.
-
 
244
 *
-
 
245
 */
57
int strcmp(const char *a, const char *b)
246
size_t str_lsize(const char *str, count_t max_len)
58
{
247
{
59
    int c = 0;
248
    count_t len = 0;
-
 
249
    size_t offset = 0;
60
   
250
   
61
    while (a[c] && b[c] && (!(a[c] - b[c])))
251
    while (len < max_len) {
-
 
252
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
-
 
253
            break;
-
 
254
       
62
        c++;
255
        len++;
-
 
256
    }
63
   
257
   
64
    return (a[c] - b[c]);
258
    return offset;
65
}
259
}
66
 
260
 
-
 
261
/** Get size of wide string with length limit.
-
 
262
 *
-
 
263
 * Get the number of bytes which are used by up to @a max_len first
-
 
264
 * wide characters in the wide string @a str. If @a max_len is greater than
-
 
265
 * the length of @a str, the entire wide string is measured (excluding the
-
 
266
 * NULL-terminator).
-
 
267
 *
-
 
268
 * @param str     Wide string to consider.
-
 
269
 * @param max_len Maximum number of wide characters to measure.
-
 
270
 *
-
 
271
 * @return Number of bytes used by the wide characters.
-
 
272
 *
-
 
273
 */
67
int strncmp(const char *a, const char *b, size_t n)
274
size_t wstr_lsize(const wchar_t *str, count_t max_len)
68
{
275
{
69
    size_t c = 0;
276
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
-
 
277
}
70
 
278
 
71
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
279
/** Get number of characters in a string.
-
 
280
 *
-
 
281
 * @param str NULL-terminated string.
-
 
282
 *
-
 
283
 * @return Number of characters in string.
-
 
284
 *
72
        c++;
285
 */
-
 
286
count_t str_length(const char *str)
-
 
287
{
-
 
288
    count_t len = 0;
-
 
289
    size_t offset = 0;
73
   
290
   
74
    return ( c < n ? a[c] - b[c] : 0);
291
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
-
 
292
        len++;
75
   
293
   
-
 
294
    return len;
76
}
295
}
77
 
296
 
-
 
297
/** Get number of characters in a wide string.
-
 
298
 *
-
 
299
 * @param str NULL-terminated wide string.
-
 
300
 *
-
 
301
 * @return Number of characters in @a str.
-
 
302
 *
-
 
303
 */
78
int stricmp(const char *a, const char *b)
304
count_t wstr_length(const wchar_t *wstr)
79
{
305
{
80
    int c = 0;
306
    count_t len = 0;
81
   
307
   
82
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
308
    while (*wstr++ != 0)
83
        c++;
309
        len++;
84
   
310
   
85
    return (tolower(a[c]) - tolower(b[c]));
311
    return len;
86
}
312
}
87
 
313
 
88
/** Return pointer to the first occurence of character c in string.
314
/** Get number of characters in a string with size limit.
89
 *
315
 *
90
 * @param str       Scanned string.
316
 * @param str  NULL-terminated string.
91
 * @param c     Searched character (taken as one byte).
317
 * @param size Maximum number of bytes to consider.
-
 
318
 *
92
 * @return      Pointer to the matched character or NULL if it is not
319
 * @return Number of characters in string.
-
 
320
 *
-
 
321
 */
-
 
322
count_t str_nlength(const char *str, size_t size)
-
 
323
{
-
 
324
    count_t len = 0;
-
 
325
    size_t offset = 0;
-
 
326
   
-
 
327
    while (str_decode(str, &offset, size) != 0)
-
 
328
        len++;
-
 
329
   
-
 
330
    return len;
-
 
331
}
-
 
332
 
-
 
333
/** Get number of characters in a string with size limit.
-
 
334
 *
93
 *          found in given string.
335
 * @param str  NULL-terminated string.
-
 
336
 * @param size Maximum number of bytes to consider.
-
 
337
 *
-
 
338
 * @return Number of characters in string.
-
 
339
 *
94
 */
340
 */
95
char *strchr(const char *str, int c)
341
count_t wstr_nlength(const wchar_t *str, size_t size)
96
{
342
{
97
    while (*str != '\0') {
343
    count_t len = 0;
98
        if (*str == (char) c)
344
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
99
            return (char *) str;
345
    count_t offset = 0;
-
 
346
   
-
 
347
    while ((offset < limit) && (*str++ != 0)) {
100
        str++;
348
        len++;
-
 
349
        offset += sizeof(wchar_t);
101
    }
350
    }
-
 
351
   
-
 
352
    return len;
-
 
353
}
102
 
354
 
-
 
355
/** Check whether character is plain ASCII.
-
 
356
 *
-
 
357
 * @return True if character is plain ASCII.
-
 
358
 *
-
 
359
 */
-
 
360
bool ascii_check(wchar_t ch)
-
 
361
{
-
 
362
    if ((ch >= 0) && (ch <= 127))
-
 
363
        return true;
-
 
364
   
103
    return NULL;
365
    return false;
104
}
366
}
105
 
367
 
-
 
368
/** Check whether character is valid
-
 
369
 *
106
/** Return pointer to the last occurence of character c in string.
370
 * @return True if character is a valid Unicode code point.
107
 *
371
 *
108
 * @param str       Scanned string.
-
 
109
 * @param c     Searched character (taken as one byte).
-
 
110
 * @return      Pointer to the matched character or NULL if it is not
-
 
111
 *          found in given string.
-
 
112
 */
372
 */
113
char *strrchr(const char *str, int c)
373
bool chr_check(wchar_t ch)
114
{
374
{
-
 
375
    if ((ch >= 0) && (ch <= 1114111))
-
 
376
        return true;
-
 
377
   
115
    char *retval = NULL;
378
    return false;
-
 
379
}
116
 
380
 
-
 
381
/** Compare two NULL terminated strings.
-
 
382
 *
-
 
383
 * Do a char-by-char comparison of two NULL-terminated strings.
-
 
384
 * The strings are considered equal iff they consist of the same
-
 
385
 * characters on the minimum of their lengths.
-
 
386
 *
-
 
387
 * @param s1 First string to compare.
-
 
388
 * @param s2 Second string to compare.
-
 
389
 *
-
 
390
 * @return 0 if the strings are equal, -1 if first is smaller,
-
 
391
 *         1 if second smaller.
-
 
392
 *
-
 
393
 */
-
 
394
int str_cmp(const char *s1, const char *s2)
-
 
395
{
-
 
396
    wchar_t c1 = 0;
-
 
397
    wchar_t c2 = 0;
-
 
398
   
-
 
399
    size_t off1 = 0;
-
 
400
    size_t off2 = 0;
-
 
401
 
117
    while (*str != '\0') {
402
    while (true) {
-
 
403
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
-
 
404
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
-
 
405
 
118
        if (*str == (char) c)
406
        if (c1 < c2)
119
            retval = (char *) str;
407
            return -1;
-
 
408
       
-
 
409
        if (c1 > c2)
120
        str++;
410
            return 1;
-
 
411
 
-
 
412
        if (c1 == 0 || c2 == 0)
-
 
413
            break;     
121
    }
414
    }
122
 
415
 
-
 
416
    return 0;
-
 
417
}
-
 
418
 
-
 
419
/** Compare two NULL terminated strings with length limit.
-
 
420
 *
-
 
421
 * Do a char-by-char comparison of two NULL-terminated strings.
-
 
422
 * The strings are considered equal iff they consist of the same
-
 
423
 * characters on the minimum of their lengths and the length limit.
-
 
424
 *
-
 
425
 * @param s1      First string to compare.
-
 
426
 * @param s2      Second string to compare.
-
 
427
 * @param max_len Maximum number of characters to consider.
-
 
428
 *
-
 
429
 * @return 0 if the strings are equal, -1 if first is smaller,
-
 
430
 *         1 if second smaller.
-
 
431
 *
-
 
432
 */
-
 
433
int str_lcmp(const char *s1, const char *s2, count_t max_len)
-
 
434
{
-
 
435
    wchar_t c1 = 0;
-
 
436
    wchar_t c2 = 0;
-
 
437
   
-
 
438
    size_t off1 = 0;
-
 
439
    size_t off2 = 0;
-
 
440
   
-
 
441
    count_t len = 0;
-
 
442
 
-
 
443
    while (true) {
-
 
444
        if (len >= max_len)
-
 
445
            break;
-
 
446
 
-
 
447
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
-
 
448
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
-
 
449
 
-
 
450
        if (c1 < c2)
-
 
451
            return -1;
-
 
452
 
-
 
453
        if (c1 > c2)
-
 
454
            return 1;
-
 
455
 
-
 
456
        if (c1 == 0 || c2 == 0)
-
 
457
            break;
-
 
458
 
-
 
459
        ++len; 
-
 
460
    }
-
 
461
 
-
 
462
    return 0;
-
 
463
 
-
 
464
}
-
 
465
 
-
 
466
/** Copy string.
-
 
467
 *
-
 
468
 * Copy source string @a src to destination buffer @a dest.
-
 
469
 * No more than @a size bytes are written. If the size of the output buffer
-
 
470
 * is at least one byte, the output string will always be well-formed, i.e.
-
 
471
 * null-terminated and containing only complete characters.
-
 
472
 *
-
 
473
 * @param dst   Destination buffer.
-
 
474
 * @param count Size of the destination buffer (must be > 0).
-
 
475
 * @param src   Source string.
-
 
476
 */
-
 
477
void str_cpy(char *dest, size_t size, const char *src)
-
 
478
{
-
 
479
    wchar_t ch;
-
 
480
    size_t src_off;
-
 
481
    size_t dest_off;
-
 
482
 
-
 
483
    /* There must be space for a null terminator in the buffer. */
-
 
484
    assert(size > 0);
-
 
485
   
-
 
486
    src_off = 0;
-
 
487
    dest_off = 0;
-
 
488
 
-
 
489
    while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
-
 
490
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
-
 
491
            break;
-
 
492
    }
-
 
493
 
-
 
494
    dest[dest_off] = '\0';
-
 
495
}
-
 
496
 
-
 
497
/** Copy size-limited substring.
-
 
498
 *
-
 
499
 * Copy prefix of string @a src of max. size @a size to destination buffer
-
 
500
 * @a dest. No more than @a size bytes are written. The output string will
-
 
501
 * always be well-formed, i.e. null-terminated and containing only complete
-
 
502
 * characters.
-
 
503
 *
-
 
504
 * No more than @a n bytes are read from the input string, so it does not
-
 
505
 * have to be null-terminated.
-
 
506
 *
-
 
507
 * @param dst   Destination buffer.
-
 
508
 * @param count Size of the destination buffer (must be > 0).
-
 
509
 * @param src   Source string.
-
 
510
 * @param n Maximum number of bytes to read from @a src.
-
 
511
 */
-
 
512
void str_ncpy(char *dest, size_t size, const char *src, size_t n)
-
 
513
{
-
 
514
    wchar_t ch;
-
 
515
    size_t src_off;
-
 
516
    size_t dest_off;
-
 
517
 
-
 
518
    /* There must be space for a null terminator in the buffer. */
-
 
519
    assert(size > 0);
-
 
520
   
-
 
521
    src_off = 0;
-
 
522
    dest_off = 0;
-
 
523
 
-
 
524
    while ((ch = str_decode(src, &src_off, n)) != 0) {
-
 
525
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
-
 
526
            break;
-
 
527
    }
-
 
528
 
-
 
529
    dest[dest_off] = '\0';
-
 
530
}
-
 
531
 
-
 
532
/** Append one string to another.
-
 
533
 *
-
 
534
 * Append source string @a src to string in destination buffer @a dest.
-
 
535
 * Size of the destination buffer is @a dest. If the size of the output buffer
-
 
536
 * is at least one byte, the output string will always be well-formed, i.e.
-
 
537
 * null-terminated and containing only complete characters.
-
 
538
 *
-
 
539
 * @param dst   Destination buffer.
-
 
540
 * @param count Size of the destination buffer.
-
 
541
 * @param src   Source string.
-
 
542
 */
-
 
543
void str_append(char *dest, size_t size, const char *src)
-
 
544
{
-
 
545
    size_t dstr_size;
-
 
546
 
-
 
547
    dstr_size = str_size(dest);
-
 
548
    str_cpy(dest + dstr_size, size - dstr_size, src);
-
 
549
}
-
 
550
 
-
 
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
 */
-
 
564
void 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
   
-
 
570
    wchar_t ch;
-
 
571
    count_t src_idx = 0;
-
 
572
    size_t dst_off = 0;
-
 
573
   
-
 
574
    while ((ch = src[src_idx++]) != 0) {
-
 
575
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
-
 
576
            break;
-
 
577
    }
-
 
578
   
-
 
579
    if (dst_off >= size)
-
 
580
        dst[size - 1] = 0;
-
 
581
    else
-
 
582
        dst[dst_off] = 0;
-
 
583
}
-
 
584
 
-
 
585
/** Find first occurence of character in string.
-
 
586
 *
-
 
587
 * @param str String to search.
-
 
588
 * @param ch  Character to look for.
-
 
589
 *
-
 
590
 * @return Pointer to character in @a str or NULL if not found.
-
 
591
 */
-
 
592
const char *str_chr(const char *str, wchar_t ch)
-
 
593
{
-
 
594
    wchar_t acc;
-
 
595
    size_t off = 0;
-
 
596
   
-
 
597
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
-
 
598
        if (acc == ch)
123
    return (char *) retval;
599
            return (str + off);
-
 
600
    }
-
 
601
   
-
 
602
    return NULL;
-
 
603
}
-
 
604
 
-
 
605
/** Find last occurence of character in string.
-
 
606
 *
-
 
607
 * @param str String to search.
-
 
608
 * @param ch  Character to look for.
-
 
609
 *
-
 
610
 * @return Pointer to character in @a str or NULL if not found.
-
 
611
 */
-
 
612
const char *str_rchr(const char *str, wchar_t ch)
-
 
613
{
-
 
614
    wchar_t acc;
-
 
615
    size_t off = 0;
-
 
616
    char *res;
-
 
617
 
-
 
618
    res = NULL;
-
 
619
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
-
 
620
        if (acc == ch)
-
 
621
            res = (str + off);
-
 
622
    }
-
 
623
 
-
 
624
    return res;
-
 
625
}
-
 
626
 
-
 
627
/** Insert a wide character into a wide string.
-
 
628
 *
-
 
629
 * Insert a wide character into a wide string at position
-
 
630
 * @a pos. The characters after the position are shifted.
-
 
631
 *
-
 
632
 * @param str     String to insert to.
-
 
633
 * @param ch      Character to insert to.
-
 
634
 * @param pos     Character index where to insert.
-
 
635
 @ @param max_pos Characters in the buffer.
-
 
636
 *
-
 
637
 * @return True if the insertion was sucessful, false if the position
-
 
638
 *         is out of bounds.
-
 
639
 *
-
 
640
 */
-
 
641
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
-
 
642
{
-
 
643
    count_t len = wstr_length(str);
-
 
644
   
-
 
645
    if ((pos > len) || (pos + 1 > max_pos))
-
 
646
        return false;
-
 
647
   
-
 
648
    count_t i;
-
 
649
    for (i = len; i + 1 > pos; i--)
-
 
650
        str[i + 1] = str[i];
-
 
651
   
-
 
652
    str[pos] = ch;
-
 
653
   
-
 
654
    return true;
-
 
655
}
-
 
656
 
-
 
657
/** Remove a wide character from a wide string.
-
 
658
 *
-
 
659
 * Remove a wide character from a wide string at position
-
 
660
 * @a pos. The characters after the position are shifted.
-
 
661
 *
-
 
662
 * @param str String to remove from.
-
 
663
 * @param pos Character index to remove.
-
 
664
 *
-
 
665
 * @return True if the removal was sucessful, false if the position
-
 
666
 *         is out of bounds.
-
 
667
 *
-
 
668
 */
-
 
669
bool wstr_remove(wchar_t *str, count_t pos)
-
 
670
{
-
 
671
    count_t len = wstr_length(str);
-
 
672
   
-
 
673
    if (pos >= len)
-
 
674
        return false;
-
 
675
   
-
 
676
    count_t i;
-
 
677
    for (i = pos + 1; i <= len; i++)
-
 
678
        str[i - 1] = str[i];
-
 
679
   
-
 
680
    return true;
-
 
681
}
-
 
682
 
-
 
683
int stricmp(const char *a, const char *b)
-
 
684
{
-
 
685
    int c = 0;
-
 
686
   
-
 
687
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
-
 
688
        c++;
-
 
689
   
-
 
690
    return (tolower(a[c]) - tolower(b[c]));
124
}
691
}
125
 
692
 
126
/** Convert string to a number.
693
/** Convert string to a number.
127
 * Core of strtol and strtoul functions.
694
 * Core of strtol and strtoul functions.
128
 *
695
 *
129
 * @param nptr      Pointer to string.
696
 * @param nptr      Pointer to string.
130
 * @param endptr    If not NULL, function stores here pointer to the first
697
 * @param endptr    If not NULL, function stores here pointer to the first
131
 *          invalid character.
698
 *          invalid character.
132
 * @param base      Zero or number between 2 and 36 inclusive.
699
 * @param base      Zero or number between 2 and 36 inclusive.
133
 * @param sgn       It's set to 1 if minus found.
700
 * @param sgn       It's set to 1 if minus found.
134
 * @return      Result of conversion.
701
 * @return      Result of conversion.
135
 */
702
 */
136
static unsigned long
703
static unsigned long
137
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
704
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
138
{
705
{
139
    unsigned char c;
706
    unsigned char c;
140
    unsigned long result = 0;
707
    unsigned long result = 0;
141
    unsigned long a, b;
708
    unsigned long a, b;
142
    const char *str = nptr;
709
    const char *str = nptr;
143
    const char *tmpptr;
710
    const char *tmpptr;
144
   
711
   
145
    while (isspace(*str))
712
    while (isspace(*str))
146
        str++;
713
        str++;
147
   
714
   
148
    if (*str == '-') {
715
    if (*str == '-') {
149
        *sgn = 1;
716
        *sgn = 1;
150
        ++str;
717
        ++str;
151
    } else if (*str == '+')
718
    } else if (*str == '+')
152
        ++str;
719
        ++str;
153
   
720
   
154
    if (base) {
721
    if (base) {
155
        if ((base == 1) || (base > 36)) {
722
        if ((base == 1) || (base > 36)) {
156
            /* FIXME: set errno to EINVAL */
723
            /* FIXME: set errno to EINVAL */
157
            return 0;
724
            return 0;
158
        }
725
        }
159
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
726
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
160
            (str[1] == 'X'))) {
727
            (str[1] == 'X'))) {
161
            str += 2;
728
            str += 2;
162
        }
729
        }
163
    } else {
730
    } else {
164
        base = 10;
731
        base = 10;
165
       
732
       
166
        if (*str == '0') {
733
        if (*str == '0') {
167
            base = 8;
734
            base = 8;
168
            if ((str[1] == 'X') || (str[1] == 'x'))  {
735
            if ((str[1] == 'X') || (str[1] == 'x'))  {
169
                base = 16;
736
                base = 16;
170
                str += 2;
737
                str += 2;
171
            }
738
            }
172
        }
739
        }
173
    }
740
    }
174
   
741
   
175
    tmpptr = str;
742
    tmpptr = str;
176
 
743
 
177
    while (*str) {
744
    while (*str) {
178
        c = *str;
745
        c = *str;
179
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
746
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
180
            (c <= '9' ? c - '0' : 0xff)));
747
            (c <= '9' ? c - '0' : 0xff)));
181
        if (c > base) {
748
        if (c > base) {
182
            break;
749
            break;
183
        }
750
        }
184
       
751
       
185
        a = (result & 0xff) * base + c;
752
        a = (result & 0xff) * base + c;
186
        b = (result >> 8) * base + (a >> 8);
753
        b = (result >> 8) * base + (a >> 8);
187
       
754
       
188
        if (b > (ULONG_MAX >> 8)) {
755
        if (b > (ULONG_MAX >> 8)) {
189
            /* overflow */
756
            /* overflow */
190
            /* FIXME: errno = ERANGE*/
757
            /* FIXME: errno = ERANGE*/
191
            return ULONG_MAX;
758
            return ULONG_MAX;
192
        }
759
        }
193
   
760
   
194
        result = (b << 8) + (a & 0xff);
761
        result = (b << 8) + (a & 0xff);
195
        ++str;
762
        ++str;
196
    }
763
    }
197
   
764
   
198
    if (str == tmpptr) {
765
    if (str == tmpptr) {
199
        /*
766
        /*
200
         * No number was found => first invalid character is the first
767
         * No number was found => first invalid character is the first
201
         * character of the string.
768
         * character of the string.
202
         */
769
         */
203
        /* FIXME: set errno to EINVAL */
770
        /* FIXME: set errno to EINVAL */
204
        str = nptr;
771
        str = nptr;
205
        result = 0;
772
        result = 0;
206
    }
773
    }
207
   
774
   
208
    if (endptr)
775
    if (endptr)
209
        *endptr = (char *) str;
776
        *endptr = (char *) str;
210
 
777
 
211
    if (nptr == str) {
778
    if (nptr == str) {
212
        /*FIXME: errno = EINVAL*/
779
        /*FIXME: errno = EINVAL*/
213
        return 0;
780
        return 0;
214
    }
781
    }
215
 
782
 
216
    return result;
783
    return result;
217
}
784
}
218
 
785
 
219
/** Convert initial part of string to long int according to given base.
786
/** Convert initial part of string to long int according to given base.
220
 * The number may begin with an arbitrary number of whitespaces followed by
787
 * The number may begin with an arbitrary number of whitespaces followed by
221
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
788
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
222
 * inserted and the number will be taken as hexadecimal one. If the base is 0
789
 * inserted and the number will be taken as hexadecimal one. If the base is 0
223
 * and the number begin with a zero, number will be taken as octal one (as with
790
 * and the number begin with a zero, number will be taken as octal one (as with
224
 * base 8). Otherwise the base 0 is taken as decimal.
791
 * base 8). Otherwise the base 0 is taken as decimal.
225
 *
792
 *
226
 * @param nptr      Pointer to string.
793
 * @param nptr      Pointer to string.
227
 * @param endptr    If not NULL, function stores here pointer to the first
794
 * @param endptr    If not NULL, function stores here pointer to the first
228
 *          invalid character.
795
 *          invalid character.
229
 * @param base      Zero or number between 2 and 36 inclusive.
796
 * @param base      Zero or number between 2 and 36 inclusive.
230
 * @return      Result of conversion.
797
 * @return      Result of conversion.
231
 */
798
 */
232
long int strtol(const char *nptr, char **endptr, int base)
799
long int strtol(const char *nptr, char **endptr, int base)
233
{
800
{
234
    char sgn = 0;
801
    char sgn = 0;
235
    unsigned long number = 0;
802
    unsigned long number = 0;
236
   
803
   
237
    number = _strtoul(nptr, endptr, base, &sgn);
804
    number = _strtoul(nptr, endptr, base, &sgn);
238
 
805
 
239
    if (number > LONG_MAX) {
806
    if (number > LONG_MAX) {
240
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
807
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
241
            /* FIXME: set 0 to errno */
808
            /* FIXME: set 0 to errno */
242
            return number;     
809
            return number;     
243
        }
810
        }
244
        /* FIXME: set ERANGE to errno */
811
        /* FIXME: set ERANGE to errno */
245
        return (sgn ? LONG_MIN : LONG_MAX);
812
        return (sgn ? LONG_MIN : LONG_MAX);
246
    }
813
    }
247
   
814
   
248
    return (sgn ? -number : number);
815
    return (sgn ? -number : number);
249
}
816
}
250
 
817
 
251
 
818
 
252
/** Convert initial part of string to unsigned long according to given base.
819
/** Convert initial part of string to unsigned long according to given base.
253
 * The number may begin with an arbitrary number of whitespaces followed by
820
 * The number may begin with an arbitrary number of whitespaces followed by
254
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
821
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
255
 * inserted and the number will be taken as hexadecimal one. If the base is 0
822
 * inserted and the number will be taken as hexadecimal one. If the base is 0
256
 * and the number begin with a zero, number will be taken as octal one (as with
823
 * and the number begin with a zero, number will be taken as octal one (as with
257
 * base 8). Otherwise the base 0 is taken as decimal.
824
 * base 8). Otherwise the base 0 is taken as decimal.
258
 *
825
 *
259
 * @param nptr      Pointer to string.
826
 * @param nptr      Pointer to string.
260
 * @param endptr    If not NULL, function stores here pointer to the first
827
 * @param endptr    If not NULL, function stores here pointer to the first
261
 *          invalid character
828
 *          invalid character
262
 * @param base      Zero or number between 2 and 36 inclusive.
829
 * @param base      Zero or number between 2 and 36 inclusive.
263
 * @return      Result of conversion.
830
 * @return      Result of conversion.
264
 */
831
 */
265
unsigned long strtoul(const char *nptr, char **endptr, int base)
832
unsigned long strtoul(const char *nptr, char **endptr, int base)
266
{
833
{
267
    char sgn = 0;
834
    char sgn = 0;
268
    unsigned long number = 0;
835
    unsigned long number = 0;
269
   
836
   
270
    number = _strtoul(nptr, endptr, base, &sgn);
837
    number = _strtoul(nptr, endptr, base, &sgn);
271
 
838
 
272
    return (sgn ? -number : number);
839
    return (sgn ? -number : number);
273
}
840
}
274
 
841
 
275
char *strcpy(char *dest, const char *src)
-
 
276
{
-
 
277
    char *orig = dest;
-
 
278
   
-
 
279
    while ((*(dest++) = *(src++)))
-
 
280
        ;
-
 
281
    return orig;
-
 
282
}
-
 
283
 
-
 
284
char *strncpy(char *dest, const char *src, size_t n)
-
 
285
{
-
 
286
    char *orig = dest;
-
 
287
   
-
 
288
    while ((*(dest++) = *(src++)) && --n)
-
 
289
        ;
-
 
290
    return orig;
-
 
291
}
-
 
292
 
-
 
293
char *strcat(char *dest, const char *src)
-
 
294
{
-
 
295
    char *orig = dest;
-
 
296
    while (*dest++)
-
 
297
        ;
-
 
298
    --dest;
-
 
299
    while ((*dest++ = *src++))
-
 
300
        ;
-
 
301
    return orig;
-
 
302
}
-
 
303
 
-
 
304
char * strdup(const char *s1)
842
char *str_dup(const char *src)
305
{
843
{
306
    size_t len = strlen(s1) + 1;
844
    size_t size = str_size(src);
307
    void *ret = malloc(len);
845
    void *dest = malloc(size + 1);
308
 
846
 
309
    if (ret == NULL)
847
    if (dest == NULL)
310
        return (char *) NULL;
848
        return (char *) NULL;
311
 
849
 
312
    return (char *) memcpy(ret, s1, len);
850
    return (char *) memcpy(dest, src, size + 1);
313
}
851
}
314
 
852
 
315
char *strtok(char *s, const char *delim)
853
char *strtok(char *s, const char *delim)
316
{
854
{
317
    static char *next;
855
    static char *next;
318
 
856
 
319
    return strtok_r(s, delim, &next);
857
    return strtok_r(s, delim, &next);
320
}
858
}
321
 
859
 
322
char *strtok_r(char *s, const char *delim, char **next)
860
char *strtok_r(char *s, const char *delim, char **next)
323
{
861
{
324
    char *start, *end;
862
    char *start, *end;
325
 
863
 
326
    if (s == NULL)
864
    if (s == NULL)
327
        s = *next;
865
        s = *next;
328
 
866
 
329
    /* Skip over leading delimiters. */
867
    /* Skip over leading delimiters. */
330
    while (*s && (strchr(delim, *s) != NULL)) ++s;
868
    while (*s && (str_chr(delim, *s) != NULL)) ++s;
331
    start = s;
869
    start = s;
332
 
870
 
333
    /* Skip over token characters. */
871
    /* Skip over token characters. */
334
    while (*s && (strchr(delim, *s) == NULL)) ++s;
872
    while (*s && (str_chr(delim, *s) == NULL)) ++s;
335
    end = s;
873
    end = s;
336
    *next = (*s ? s + 1 : s);
874
    *next = (*s ? s + 1 : s);
337
 
875
 
338
    if (start == end) {
876
    if (start == end) {
339
        return NULL;    /* No more tokens. */
877
        return NULL;    /* No more tokens. */
340
    }
878
    }
341
 
879
 
342
    /* Overwrite delimiter with NULL terminator. */
880
    /* Overwrite delimiter with NULL terminator. */
343
    *end = '\0';
881
    *end = '\0';
344
    return start;
882
    return start;
345
}
883
}
346
 
884
 
347
/** @}
885
/** @}
348
 */
886
 */
349
 
887