]>
Commit | Line | Data |
---|---|---|
bd20f8f4 | 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 | //----------------------------------------------------------------------------- | |
9ab7a6c7 | 10 | #include "string.h" |
9ab7a6c7 | 11 | |
12 | void *memcpy(void *dest, const void *src, int len) | |
13 | { | |
14 | uint8_t *d = dest; | |
15 | const uint8_t *s = src; | |
16 | while((len--) > 0) { | |
17 | *d = *s; | |
18 | d++; | |
19 | s++; | |
20 | } | |
21 | return dest; | |
22 | } | |
23 | ||
24 | void *memset(void *dest, int c, int len) | |
25 | { | |
26 | uint8_t *d = dest; | |
27 | while((len--) > 0) { | |
28 | *d = c; | |
29 | d++; | |
30 | } | |
31 | return dest; | |
32 | } | |
33 | ||
34 | int memcmp(const void *av, const void *bv, int len) | |
35 | { | |
36 | const uint8_t *a = av; | |
37 | const uint8_t *b = bv; | |
38 | ||
39 | while((len--) > 0) { | |
40 | if(*a != *b) { | |
41 | return *a - *b; | |
42 | } | |
43 | a++; | |
44 | b++; | |
45 | } | |
46 | return 0; | |
47 | } | |
48 | ||
f38a1528 | 49 | void memxor(uint8_t * dest, uint8_t * src, size_t len) { |
50 | for( ; len > 0; len--,dest++,src++) | |
51 | *dest ^= *src; | |
52 | } | |
53 | ||
9ab7a6c7 | 54 | int strlen(const char *str) |
55 | { | |
56 | int l = 0; | |
57 | while(*str) { | |
58 | l++; | |
59 | str++; | |
60 | } | |
61 | return l; | |
62 | } | |
63 | ||
64 | char* strncat(char *dest, const char *src, unsigned int n) | |
65 | { | |
66 | unsigned int dest_len = strlen(dest); | |
67 | unsigned int i; | |
68 | ||
69 | for (i = 0 ; i < n && src[i] != '\0' ; i++) | |
70 | dest[dest_len + i] = src[i]; | |
71 | dest[dest_len + i] = '\0'; | |
72 | ||
73 | return dest; | |
74 | } | |
9455b51c | 75 | |
76 | char* strcat(char *dest, const char *src) | |
77 | { | |
78 | unsigned int dest_len = strlen(dest); | |
79 | unsigned int i; | |
80 | ||
81 | for (i = 0 ; src[i] != '\0' ; i++) | |
82 | dest[dest_len + i] = src[i]; | |
83 | dest[dest_len + i] = '\0'; | |
84 | ||
85 | return dest; | |
86 | } | |
87 | ////////////////////////////////////////// code to do 'itoa' | |
88 | ||
89 | /* reverse: reverse string s in place */ | |
90 | void strreverse(char s[]) | |
91 | { | |
92 | int c, i, j; | |
93 | ||
94 | for (i = 0, j = strlen(s)-1; i<j; i++, j--) { | |
95 | c = s[i]; | |
96 | s[i] = s[j]; | |
97 | s[j] = c; | |
98 | } | |
99 | } | |
100 | ||
101 | /* itoa: convert n to characters in s */ | |
102 | void itoa(int n, char s[]) | |
103 | { | |
104 | int i, sign; | |
105 | ||
106 | if ((sign = n) < 0) /* record sign */ | |
107 | n = -n; /* make n positive */ | |
108 | i = 0; | |
109 | do { /* generate digits in reverse order */ | |
110 | s[i++] = n % 10 + '0'; /* get next digit */ | |
111 | } while ((n /= 10) > 0); /* delete it */ | |
112 | if (sign < 0) | |
113 | s[i++] = '-'; | |
114 | s[i] = '\0'; | |
115 | strreverse(s); | |
116 | } | |
117 | ||
118 | //////////////////////////////////////// END 'itoa' CODE |