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