Subversion Repositories HelenOS

Rev

Rev 3437 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3437 Rev 4377
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
3
 * Copyright (C) 1998 by Wes Peters <wes@softweyr.com>
3
 * Copyright (c) 2008 Jiri Svoboda
4
 * Copyright (c) 1988, 1993 The Regents of the University of California.
-
 
5
 * All rights reserved.
4
 * All rights reserved.
6
 *
5
 *
7
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
9
 * are met:
8
 * are met:
Line 33... Line 32...
33
 */
32
 */
34
/** @file
33
/** @file
35
 */
34
 */
36
 
35
 
37
#include <string.h>
36
#include <string.h>
38
#include <unistd.h>
37
#include <stdlib.h>
39
#include <ctype.h>
38
#include <assert.h>
40
#include <limits.h>
39
#include <limits.h>
41
#include <align.h>
-
 
42
#include <sys/types.h>
40
#include <ctype.h>
43
#include <malloc.h>
41
#include <malloc.h>
-
 
42
#include <errno.h>
-
 
43
#include <align.h>
-
 
44
#include <mem.h>
-
 
45
#include <string.h>
-
 
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)))
44
 
55
 
45
/* Dummy implementation of mem/ functions */
56
/** Number of data bits in a UTF-8 continuation byte */
-
 
57
#define CONT_BITS  6
46
 
58
 
-
 
59
/** Decode a single character from a string.
-
 
60
 *
-
 
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.
-
 
72
 *
-
 
73
 */
47
void *memset(void *s, int c, size_t n)
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;
-
 
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) {
49
    char *os = s;
171
        b0_bits = 3;
-
 
172
        cbytes = 3;
-
 
173
    } else {
-
 
174
        /* Codes longer than 21 bits are not supported */
-
 
175
        return EINVAL;
-
 
176
    }
50
   
177
   
-
 
178
    /* Check for available space in buffer */
51
    while (n--)
179
    if (*offset + cbytes >= size)
52
        *(os++) = c;
180
        return EOVERFLOW;
53
   
181
   
-
 
182
    /* Encode continuation bytes */
54
    return s;
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;
55
}
187
    }
56
 
188
   
57
struct along {
189
    /* Encode first byte */
-
 
190
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
-
 
191
   
58
    unsigned long n;
192
    /* Advance offset */
59
} __attribute__ ((packed));
193
    *offset += cbytes + 1;
-
 
194
   
-
 
195
    return EOK;
-
 
196
}
60
 
197
 
-
 
198
/** Get size of string.
-
 
199
 *
61
static void *unaligned_memcpy(void *dst, const void *src, size_t n)
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)
62
{
209
{
63
    int i, j;
210
    size_t size = 0;
64
    struct along *adst = dst;
-
 
65
    const struct along *asrc = src;
-
 
66
 
211
   
67
    for (i = 0; i < n / sizeof(unsigned long); i++)
212
    while (*str++ != 0)
68
        adst[i].n = asrc[i].n;
213
        size++;
69
       
214
   
70
    for (j = 0; j < n % sizeof(unsigned long); j++)
215
    return size;
71
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
-
 
72
            ((unsigned char *) (((unsigned long *) src) + i))[j];
-
 
-
 
216
}
73
       
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
-
 
221
 * NULL-terminator).
-
 
222
 *
-
 
223
 * @param str Wide string to consider.
-
 
224
 *
-
 
225
 * @return Number of bytes used by the wide string
-
 
226
 *
-
 
227
 */
74
    return (char *) dst;
228
size_t wstr_size(const wchar_t *str)
-
 
229
{
-
 
230
    return (wstr_length(str) * sizeof(wchar_t));
75
}
231
}
76
 
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
 */
77
void *memcpy(void *dst, const void *src, size_t n)
246
size_t str_lsize(const char *str, count_t max_len)
78
{
247
{
79
    int i, j;
248
    count_t len = 0;
-
 
249
    size_t offset = 0;
80
 
250
   
81
    if (((long) dst & (sizeof(long) - 1)) ||
251
    while (len < max_len) {
82
        ((long) src & (sizeof(long) - 1)))
252
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
83
        return unaligned_memcpy(dst, src, n);
253
            break;
84
 
254
       
85
    for (i = 0; i < n / sizeof(unsigned long); i++)
255
        len++;
86
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
-
 
-
 
256
    }
87
       
257
   
88
    for (j = 0; j < n % sizeof(unsigned long); j++)
258
    return offset;
89
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
-
 
90
            ((unsigned char *) (((unsigned long *) src) + i))[j];
-
 
-
 
259
}
91
       
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
92
    return (char *) dst;
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
 */
-
 
274
size_t wstr_lsize(const wchar_t *str, count_t max_len)
-
 
275
{
-
 
276
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
93
}
277
}
94
 
278
 
-
 
279
/** Get number of characters in a string.
-
 
280
 *
-
 
281
 * @param str NULL-terminated string.
-
 
282
 *
-
 
283
 * @return Number of characters in string.
-
 
284
 *
-
 
285
 */
95
void *memmove(void *dst, const void *src, size_t n)
286
count_t str_length(const char *str)
96
{
287
{
97
    int i, j;
288
    count_t len = 0;
-
 
289
    size_t offset = 0;
98
   
290
   
99
    if (src > dst)
291
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
100
        return memcpy(dst, src, n);
292
        len++;
101
 
293
   
102
    for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
294
    return len;
103
        ((unsigned char *) ((unsigned long *) dst))[j] =
-
 
104
            ((unsigned char *) ((unsigned long *) src))[j];
-
 
-
 
295
}
105
 
296
 
106
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
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
 */
107
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
304
count_t wstr_length(const wchar_t *wstr)
-
 
305
{
-
 
306
    count_t len = 0;
108
       
307
   
-
 
308
    while (*wstr++ != 0)
-
 
309
        len++;
-
 
310
   
109
    return (char *) dst;
311
    return len;
110
}
312
}
111
 
313
 
-
 
314
/** Get number of characters in a string with size limit.
-
 
315
 *
112
/** Compare two memory areas.
316
 * @param str  NULL-terminated string.
-
 
317
 * @param size Maximum number of bytes to consider.
-
 
318
 *
-
 
319
 * @return Number of characters in string.
113
 *
320
 *
114
 * @param s1        Pointer to the first area to compare.
-
 
115
 * @param s2        Pointer to the second area to compare.
-
 
116
 * @param len       Size of the first area in bytes. Both areas must have
-
 
117
 *          the same length.
-
 
118
 * @return      If len is 0, return zero. If the areas match, return
-
 
119
 *          zero. Otherwise return non-zero.
-
 
120
 */
321
 */
121
int bcmp(const char *s1, const char *s2, size_t len)
322
count_t str_nlength(const char *str, size_t size)
122
{
323
{
-
 
324
    count_t len = 0;
-
 
325
    size_t offset = 0;
-
 
326
   
123
    for (; len && *s1++ == *s2++; len--)
327
    while (str_decode(str, &offset, size) != 0)
-
 
328
        len++;
124
        ;
329
   
125
    return len;
330
    return len;
126
}
331
}
127
 
332
 
128
/** Count the number of characters in the string, not including terminating 0.
333
/** Get number of characters in a string with size limit.
-
 
334
 *
-
 
335
 * @param str  NULL-terminated string.
-
 
336
 * @param size Maximum number of bytes to consider.
129
 *
337
 *
130
 * @param str       String.
-
 
131
 * @return      Number of characters in string.
338
 * @return Number of characters in string.
-
 
339
 *
-
 
340
 */
-
 
341
count_t wstr_nlength(const wchar_t *str, size_t size)
-
 
342
{
-
 
343
    count_t len = 0;
-
 
344
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
-
 
345
    count_t offset = 0;
-
 
346
   
-
 
347
    while ((offset < limit) && (*str++ != 0)) {
-
 
348
        len++;
-
 
349
        offset += sizeof(wchar_t);
-
 
350
    }
-
 
351
   
-
 
352
    return len;
-
 
353
}
-
 
354
 
-
 
355
/** Check whether character is plain ASCII.
-
 
356
 *
-
 
357
 * @return True if character is plain ASCII.
-
 
358
 *
132
 */
359
 */
133
size_t strlen(const char *str)
360
bool ascii_check(wchar_t ch)
134
{
361
{
-
 
362
    if ((ch >= 0) && (ch <= 127))
135
    size_t counter = 0;
363
        return true;
136
 
364
   
137
    while (str[counter] != 0)
-
 
138
        counter++;
365
    return false;
-
 
366
}
139
 
367
 
-
 
368
/** Check whether character is valid
-
 
369
 *
-
 
370
 * @return True if character is a valid Unicode code point.
-
 
371
 *
-
 
372
 */
-
 
373
bool chr_check(wchar_t ch)
-
 
374
{
-
 
375
    if ((ch >= 0) && (ch <= 1114111))
140
    return counter;
376
        return true;
-
 
377
   
-
 
378
    return false;
141
}
379
}
142
 
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
 */
143
int strcmp(const char *a, const char *b)
394
int str_cmp(const char *s1, const char *s2)
144
{
395
{
-
 
396
    wchar_t c1 = 0;
145
    int c = 0;
397
    wchar_t c2 = 0;
146
   
398
   
-
 
399
    size_t off1 = 0;
-
 
400
    size_t off2 = 0;
-
 
401
 
-
 
402
    while (true) {
-
 
403
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
-
 
404
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
-
 
405
 
-
 
406
        if (c1 < c2)
-
 
407
            return -1;
-
 
408
       
-
 
409
        if (c1 > c2)
-
 
410
            return 1;
-
 
411
 
-
 
412
        if (c1 == 0 || c2 == 0)
-
 
413
            break;     
-
 
414
    }
-
 
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
 
147
    while (a[c] && b[c] && (!(a[c] - b[c])))
456
        if (c1 == 0 || c2 == 0)
148
        c++;
457
            break;
149
   
458
 
150
    return (a[c] - b[c]);
459
        ++len; 
151
}
460
    }
152
 
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
 */
153
int strncmp(const char *a, const char *b, size_t n)
477
void str_cpy(char *dest, size_t size, const char *src)
154
{
478
{
-
 
479
    wchar_t ch;
155
    size_t c = 0;
480
    size_t src_off;
-
 
481
    size_t dest_off;
156
 
482
 
157
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
483
    /* There must be space for a null terminator in the buffer. */
158
        c++;
484
    assert(size > 0);
159
   
485
   
160
    return ( c < n ? a[c] - b[c] : 0);
486
    src_off = 0;
-
 
487
    dest_off = 0;
161
   
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;
162
}
492
    }
163
 
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
 */
164
int stricmp(const char *a, const char *b)
512
void str_ncpy(char *dest, size_t size, const char *src, size_t n)
165
{
513
{
-
 
514
    wchar_t ch;
166
    int c = 0;
515
    size_t src_off;
-
 
516
    size_t dest_off;
167
   
517
 
168
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
518
    /* There must be space for a null terminator in the buffer. */
169
        c++;
519
    assert(size > 0);
170
   
520
   
-
 
521
    src_off = 0;
-
 
522
    dest_off = 0;
-
 
523
 
171
    return (tolower(a[c]) - tolower(b[c]));
524
    while ((ch = str_decode(src, &src_off, n)) != 0) {
-
 
525
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
-
 
526
            break;
172
}
527
    }
173
 
528
 
-
 
529
    dest[dest_off] = '\0';
-
 
530
}
-
 
531
 
174
/** Return pointer to the first occurence of character c in string.
532
/** Append one string to another.
175
 *
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.
176
 * @param str       Scanned string.
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.
177
 * @param c     Searched character (taken as one byte).
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
 
178
 * @return      Pointer to the matched character or NULL if it is not
585
/** Find first occurence of character in string.
-
 
586
 *
179
 *          found in given string.
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.
180
 */
591
 */
181
char *strchr(const char *str, int c)
592
const char *str_chr(const char *str, wchar_t ch)
182
{
593
{
-
 
594
    wchar_t acc;
-
 
595
    size_t off = 0;
183
    while (*str != '\0') {
596
    size_t last = 0;
-
 
597
   
-
 
598
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
184
        if (*str == (char) c)
599
        if (acc == ch)
185
            return (char *) str;
600
            return (str + last);
186
        str++;
601
        last = off;
187
    }
602
    }
188
 
603
   
189
    return NULL;
604
    return NULL;
190
}
605
}
191
 
606
 
192
/** Return pointer to the last occurence of character c in string.
607
/** Find last occurence of character in string.
193
 *
608
 *
194
 * @param str       Scanned string.
609
 * @param str String to search.
195
 * @param c     Searched character (taken as one byte).
610
 * @param ch  Character to look for.
-
 
611
 *
196
 * @return      Pointer to the matched character or NULL if it is not
612
 * @return Pointer to character in @a str or NULL if not found.
197
 *          found in given string.
-
 
198
 */
613
 */
199
char *strrchr(const char *str, int c)
614
const char *str_rchr(const char *str, wchar_t ch)
200
{
615
{
-
 
616
    wchar_t acc;
-
 
617
    size_t off = 0;
-
 
618
    size_t last = 0;
201
    char *retval = NULL;
619
    char *res = NULL;
202
 
620
   
203
    while (*str != '\0') {
621
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
204
        if (*str == (char) c)
622
        if (acc == ch)
205
            retval = (char *) str;
623
            res = (str + last);
206
        str++;
624
        last = off;
-
 
625
    }
-
 
626
   
-
 
627
    return res;
-
 
628
}
-
 
629
 
-
 
630
/** Insert a wide character into a wide string.
-
 
631
 *
-
 
632
 * Insert a wide character into a wide string at position
-
 
633
 * @a pos. The characters after the position are shifted.
-
 
634
 *
-
 
635
 * @param str     String to insert to.
-
 
636
 * @param ch      Character to insert to.
-
 
637
 * @param pos     Character index where to insert.
-
 
638
 @ @param max_pos Characters in the buffer.
-
 
639
 *
-
 
640
 * @return True if the insertion was sucessful, false if the position
-
 
641
 *         is out of bounds.
-
 
642
 *
-
 
643
 */
-
 
644
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
-
 
645
{
-
 
646
    count_t len = wstr_length(str);
-
 
647
   
-
 
648
    if ((pos > len) || (pos + 1 > max_pos))
-
 
649
        return false;
-
 
650
   
-
 
651
    count_t i;
-
 
652
    for (i = len; i + 1 > pos; i--)
-
 
653
        str[i + 1] = str[i];
-
 
654
   
-
 
655
    str[pos] = ch;
-
 
656
   
-
 
657
    return true;
-
 
658
}
-
 
659
 
-
 
660
/** Remove a wide character from a wide string.
-
 
661
 *
-
 
662
 * Remove a wide character from a wide string at position
-
 
663
 * @a pos. The characters after the position are shifted.
-
 
664
 *
-
 
665
 * @param str String to remove from.
-
 
666
 * @param pos Character index to remove.
-
 
667
 *
-
 
668
 * @return True if the removal was sucessful, false if the position
-
 
669
 *         is out of bounds.
-
 
670
 *
-
 
671
 */
-
 
672
bool wstr_remove(wchar_t *str, count_t pos)
-
 
673
{
-
 
674
    count_t len = wstr_length(str);
-
 
675
   
-
 
676
    if (pos >= len)
-
 
677
        return false;
-
 
678
   
-
 
679
    count_t i;
-
 
680
    for (i = pos + 1; i <= len; i++)
-
 
681
        str[i - 1] = str[i];
-
 
682
   
-
 
683
    return true;
207
    }
684
}
208
 
685
 
-
 
686
int stricmp(const char *a, const char *b)
-
 
687
{
-
 
688
    int c = 0;
-
 
689
   
-
 
690
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
-
 
691
        c++;
-
 
692
   
209
    return (char *) retval;
693
    return (tolower(a[c]) - tolower(b[c]));
210
}
694
}
211
 
695
 
212
/** Convert string to a number.
696
/** Convert string to a number.
213
 * Core of strtol and strtoul functions.
697
 * Core of strtol and strtoul functions.
214
 *
698
 *
Line 356... Line 840...
356
    number = _strtoul(nptr, endptr, base, &sgn);
840
    number = _strtoul(nptr, endptr, base, &sgn);
357
 
841
 
358
    return (sgn ? -number : number);
842
    return (sgn ? -number : number);
359
}
843
}
360
 
844
 
361
char *strcpy(char *dest, const char *src)
845
char *str_dup(const char *src)
362
{
846
{
363
    char *orig = dest;
847
    size_t size = str_size(src);
364
   
-
 
365
    while ((*(dest++) = *(src++)))
848
    void *dest = malloc(size + 1);
366
        ;
-
 
367
    return orig;
-
 
368
}
-
 
369
 
849
 
370
char *strncpy(char *dest, const char *src, size_t n)
-
 
371
{
-
 
372
    char *orig = dest;
850
    if (dest == NULL)
373
   
-
 
374
    while ((*(dest++) = *(src++)) && --n)
-
 
375
        ;
-
 
376
    return orig;
851
        return (char *) NULL;
377
}
-
 
378
 
852
 
379
char *strcat(char *dest, const char *src)
853
    return (char *) memcpy(dest, src, size + 1);
380
{
-
 
381
    char *orig = dest;
-
 
382
    while (*dest++)
-
 
383
        ;
-
 
384
    --dest;
-
 
385
    while ((*dest++ = *src++))
-
 
386
        ;
-
 
387
    return orig;
-
 
388
}
854
}
389
 
855
 
390
char * strdup(const char *s1)
856
char *strtok(char *s, const char *delim)
391
{
857
{
392
    size_t len = strlen(s1) + 1;
-
 
393
    void *ret = malloc(len);
858
    static char *next;
394
 
859
 
395
    if (ret == NULL)
-
 
396
        return (char *) NULL;
-
 
397
 
-
 
398
    return (char *) memcpy(ret, s1, len);
860
    return strtok_r(s, delim, &next);
399
}
861
}
400
 
862
 
401
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
-
 
402
char * strtok_r(char *s, const char *delim, char **last)
863
char *strtok_r(char *s, const char *delim, char **next)
403
{
864
{
404
    char *spanp, *tok;
865
    char *start, *end;
405
    int c, sc;
-
 
406
 
-
 
407
    if (s == NULL && (s = *last) == NULL)
-
 
408
        return (NULL);
-
 
409
 
866
 
410
cont:
-
 
411
    c = *s++;
-
 
412
    for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
-
 
413
        if (c == sc)
867
    if (s == NULL)
414
            goto cont;
868
        s = *next;
415
    }
-
 
416
 
869
 
417
    if (c == 0) {       /* no non-delimiter characters */
870
    /* Skip over leading delimiters. */
418
        *last = NULL;
871
    while (*s && (str_chr(delim, *s) != NULL)) ++s;
419
        return (NULL);
872
    start = s;
420
    }
-
 
421
 
873
 
-
 
874
    /* Skip over token characters. */
-
 
875
    while (*s && (str_chr(delim, *s) == NULL)) ++s;
422
    tok = s - 1;
876
    end = s;
-
 
877
    *next = (*s ? s + 1 : s);
423
 
878
 
424
    for (;;) {
-
 
425
        c = *s++;
-
 
426
        spanp = (char *)delim;
-
 
427
        do {
-
 
428
            if ((sc = *spanp++) == c) {
879
    if (start == end) {
429
                if (c == 0)
-
 
430
                    s = NULL;
-
 
431
                else
-
 
432
                    s[-1] = '\0';
-
 
433
                *last = s;
-
 
434
                return (tok);
880
        return NULL;    /* No more tokens. */
435
            }
-
 
436
        } while (sc != 0);
-
 
437
    }
-
 
438
}
881
    }
439
 
882
 
440
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
883
    /* Overwrite delimiter with NULL terminator. */
441
char * strtok(char *s, const char *delim)
-
 
442
{
-
 
443
    static char *last;
884
    *end = '\0';
444
 
-
 
445
    return (strtok_r(s, delim, &last));
885
    return start;
446
}
886
}
447
 
887
 
448
/** @}
888
/** @}
449
 */
889
 */