]>
Commit | Line | Data |
---|---|---|
ad326d84 | 1 | //----------------------------------------------------------------------------- |
2 | // Jonathan Westhues, Sept 2005 | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Common string.h functions | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include "string.h" | |
12 | #include <stdint.h> | |
13 | ||
14 | void *memcpy(void *dest, const void *src, size_t len) { | |
15 | uint8_t *d = dest; | |
16 | const uint8_t *s = src; | |
17 | while((len--) > 0) { | |
18 | *d = *s; | |
19 | d++; | |
20 | s++; | |
21 | } | |
22 | return dest; | |
23 | } | |
24 | ||
25 | ||
26 | void *memset(void *dest, int c, size_t len) { | |
27 | uint8_t *d = dest; | |
28 | while((len--) > 0) { | |
29 | *d = c; | |
30 | d++; | |
31 | } | |
32 | return dest; | |
33 | } | |
34 | ||
35 | ||
36 | void *memmove(void *dest, const void *src, size_t len) { | |
37 | uint8_t *d = dest; | |
38 | const uint8_t *s = src; | |
39 | if (dest <= src) { | |
40 | while((len--) > 0) { | |
41 | *d = *s; | |
42 | d++; | |
43 | s++; | |
44 | } | |
45 | } else { | |
46 | d = d + len - 1; | |
47 | s = s + len - 1; | |
48 | while((len--) > 0) { | |
49 | *d = *s; | |
50 | d--; | |
51 | s--; | |
52 | } | |
53 | } | |
54 | return dest; | |
55 | } | |
56 | ||
57 | ||
58 | int memcmp(const void *av, const void *bv, size_t len) { | |
59 | const uint8_t *a = av; | |
60 | const uint8_t *b = bv; | |
61 | ||
62 | while((len--) > 0) { | |
63 | if(*a != *b) { | |
64 | return *a - *b; | |
65 | } | |
66 | a++; | |
67 | b++; | |
68 | } | |
69 | return 0; | |
70 | } | |
71 | ||
72 | ||
73 | size_t strlen(const char *str) { | |
74 | int l = 0; | |
75 | while(*str) { | |
76 | l++; | |
77 | str++; | |
78 | } | |
79 | return l; | |
80 | } | |
81 | ||
82 | ||
83 | char* strncat(char *dest, const char *src, size_t n) { | |
84 | unsigned int dest_len = strlen(dest); | |
85 | unsigned int i; | |
86 | ||
87 | for (i = 0 ; i < n && src[i] != '\0' ; i++) | |
88 | dest[dest_len + i] = src[i]; | |
89 | dest[dest_len + i] = '\0'; | |
90 | ||
91 | return dest; | |
92 | } | |
93 | ||
94 | ||
95 | char* strcat(char *dest, const char *src) { | |
96 | unsigned int dest_len = strlen(dest); | |
97 | unsigned int i; | |
98 | ||
99 | for (i = 0 ; src[i] != '\0' ; i++) | |
100 | dest[dest_len + i] = src[i]; | |
101 | dest[dest_len + i] = '\0'; | |
102 | ||
103 | return dest; | |
104 | } |