Rev 2071 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2071 | Rev 3022 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup generic |
29 | /** @addtogroup generic |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | #include <string.h> |
33 | #include <string.h> |
34 | 34 | ||
35 | /** |
35 | /** |
36 | * @file |
36 | * @file |
37 | * @brief String manipulation functions. |
37 | * @brief String manipulation functions. |
38 | */ |
38 | */ |
39 | 39 | ||
40 | /** Return number of characters in a string. |
40 | /** Return number of characters in a string. |
41 | * |
41 | * |
42 | * @param str NULL terminated string. |
42 | * @param str NULL terminated string. |
43 | * |
43 | * |
44 | * @return Number of characters in str. |
44 | * @return Number of characters in str. |
45 | */ |
45 | */ |
46 | size_t strlen(const char *str) |
46 | size_t strlen(const char *str) |
47 | { |
47 | { |
48 | int i; |
48 | int i; |
49 | 49 | ||
50 | for (i = 0; str[i]; i++) |
50 | for (i = 0; str[i]; i++) |
51 | ; |
51 | ; |
52 | 52 | ||
53 | return i; |
53 | return i; |
54 | } |
54 | } |
55 | 55 | ||
56 | /** Compare two NULL terminated strings |
56 | /** Compare two NULL terminated strings |
57 | * |
57 | * |
58 | * Do a char-by-char comparison of two NULL terminated strings. |
58 | * Do a char-by-char comparison of two NULL terminated strings. |
59 | * The strings are considered equal iff they consist of the same |
59 | * The strings are considered equal iff they consist of the same |
60 | * characters on the minimum of their lengths. |
60 | * characters on the minimum of their lengths. |
61 | * |
61 | * |
62 | * @param src First string to compare. |
62 | * @param src First string to compare. |
63 | * @param dst Second string to compare. |
63 | * @param dst Second string to compare. |
64 | * |
64 | * |
65 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
65 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
66 | * |
66 | * |
67 | */ |
67 | */ |
68 | int strcmp(const char *src, const char *dst) |
68 | int strcmp(const char *src, const char *dst) |
69 | { |
69 | { |
70 | for (; *src && *dst; src++, dst++) { |
70 | for (; *src && *dst; src++, dst++) { |
71 | if (*src < *dst) |
71 | if (*src < *dst) |
72 | return -1; |
72 | return -1; |
73 | if (*src > *dst) |
73 | if (*src > *dst) |
74 | return 1; |
74 | return 1; |
75 | } |
75 | } |
76 | if (*src == *dst) |
76 | if (*src == *dst) |
77 | return 0; |
77 | return 0; |
78 | if (!*src) |
78 | if (!*src) |
79 | return -1; |
79 | return -1; |
80 | return 1; |
80 | return 1; |
81 | } |
81 | } |
82 | 82 | ||
83 | 83 | ||
84 | /** Compare two NULL terminated strings |
84 | /** Compare two NULL terminated strings |
85 | * |
85 | * |
86 | * Do a char-by-char comparison of two NULL terminated strings. |
86 | * Do a char-by-char comparison of two NULL terminated strings. |
87 | * The strings are considered equal iff they consist of the same |
87 | * The strings are considered equal iff they consist of the same |
88 | * characters on the minimum of their lengths and specified maximal |
88 | * characters on the minimum of their lengths and specified maximal |
89 | * length. |
89 | * length. |
90 | * |
90 | * |
91 | * @param src First string to compare. |
91 | * @param src First string to compare. |
92 | * @param dst Second string to compare. |
92 | * @param dst Second string to compare. |
93 | * @param len Maximal length for comparison. |
93 | * @param len Maximal length for comparison. |
94 | * |
94 | * |
95 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
95 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
96 | * |
96 | * |
97 | */ |
97 | */ |
98 | int strncmp(const char *src, const char *dst, size_t len) |
98 | int strncmp(const char *src, const char *dst, size_t len) |
99 | { |
99 | { |
100 | int i; |
100 | int i; |
101 | 101 | ||
102 | for (i = 0; *src && *dst && i < len; src++, dst++, i++) { |
102 | for (i = 0; *src && *dst && i < len; src++, dst++, i++) { |
103 | if (*src < *dst) |
103 | if (*src < *dst) |
104 | return -1; |
104 | return -1; |
105 | if (*src > *dst) |
105 | if (*src > *dst) |
106 | return 1; |
106 | return 1; |
107 | } |
107 | } |
108 | if (i == len || *src == *dst) |
108 | if (i == len || *src == *dst) |
109 | return 0; |
109 | return 0; |
110 | if (!*src) |
110 | if (!*src) |
111 | return -1; |
111 | return -1; |
112 | return 1; |
112 | return 1; |
113 | } |
113 | } |
114 | 114 | ||
115 | /** Copy NULL terminated string. |
115 | /** Copy NULL terminated string. |
116 | * |
116 | * |
117 | * Copy at most 'len' characters from string 'src' to 'dest'. |
117 | * Copy at most 'len' characters from string 'src' to 'dest'. |
118 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
118 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
119 | * last copied character. |
119 | * last copied character. |
120 | * |
120 | * |
121 | * @param src Source string. |
121 | * @param src Source string. |
122 | * @param dest Destination buffer. |
122 | * @param dest Destination buffer. |
123 | * @param len Size of destination buffer. |
123 | * @param len Size of destination buffer. |
124 | */ |
124 | */ |
125 | void strncpy(char *dest, const char *src, size_t len) |
125 | void strncpy(char *dest, const char *src, size_t len) |
126 | { |
126 | { |
127 | int i; |
127 | int i; |
128 | for (i = 0; i < len; i++) { |
128 | for (i = 0; i < len; i++) { |
129 | if (!(dest[i] = src[i])) |
129 | if (!(dest[i] = src[i])) |
130 | return; |
130 | return; |
131 | } |
131 | } |
132 | dest[i-1] = '\0'; |
132 | dest[i-1] = '\0'; |
133 | } |
133 | } |
134 | 134 | ||
135 | /** Convert ascii representation to unative_t |
135 | /** Convert ascii representation to unative_t |
136 | * |
136 | * |
137 | * Supports 0x for hexa & 0 for octal notation. |
137 | * Supports 0x for hexa & 0 for octal notation. |
138 | * Does not check for overflows, does not support negative numbers |
138 | * Does not check for overflows, does not support negative numbers |
139 | * |
139 | * |
140 | * @param text Textual representation of number |
140 | * @param text Textual representation of number |
141 | * @return Converted number or 0 if no valid number ofund |
141 | * @return Converted number or 0 if no valid number ofund |
142 | */ |
142 | */ |
143 | unative_t atoi(const char *text) |
143 | unative_t atoi(const char *text) |
144 | { |
144 | { |
145 | int base = 10; |
145 | int base = 10; |
146 | unative_t result = 0; |
146 | unative_t result = 0; |
147 | 147 | ||
148 | if (text[0] == '0' && text[1] == 'x') { |
148 | if (text[0] == '0' && text[1] == 'x') { |
149 | base = 16; |
149 | base = 16; |
150 | text += 2; |
150 | text += 2; |
151 | } else if (text[0] == '0') |
151 | } else if (text[0] == '0') |
152 | base = 8; |
152 | base = 8; |
153 | 153 | ||
154 | while (*text) { |
154 | while (*text) { |
155 | if (base != 16 && \ |
155 | if (base != 16 && \ |
156 | ((*text >= 'A' && *text <= 'F' ) |
156 | ((*text >= 'A' && *text <= 'F' ) |
157 | || (*text >='a' && *text <='f'))) |
157 | || (*text >='a' && *text <='f'))) |
158 | break; |
158 | break; |
159 | if (base == 8 && *text >='8') |
159 | if (base == 8 && *text >='8') |
160 | break; |
160 | break; |
161 | 161 | ||
162 | if (*text >= '0' && *text <= '9') { |
162 | if (*text >= '0' && *text <= '9') { |
163 | result *= base; |
163 | result *= base; |
164 | result += *text - '0'; |
164 | result += *text - '0'; |
165 | } else if (*text >= 'A' && *text <= 'F') { |
165 | } else if (*text >= 'A' && *text <= 'F') { |
166 | result *= base; |
166 | result *= base; |
167 | result += *text - 'A' + 10; |
167 | result += *text - 'A' + 10; |
168 | } else if (*text >= 'a' && *text <= 'f') { |
168 | } else if (*text >= 'a' && *text <= 'f') { |
169 | result *= base; |
169 | result *= base; |
170 | result += *text - 'a' + 10; |
170 | result += *text - 'a' + 10; |
171 | } else |
171 | } else |
172 | break; |
172 | break; |
173 | text++; |
173 | text++; |
174 | } |
174 | } |
175 | 175 | ||
176 | return result; |
176 | return result; |
177 | } |
177 | } |
178 | 178 | ||
179 | /** @} |
179 | /** @} |
180 | */ |
180 | */ |
181 | 181 |