blob: 3a973a5ec61be8368cc0dbc9150b2c45ee1f364d [file] [log] [blame]
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001#ifndef __WILC_STRUTILS_H__
2#define __WILC_STRUTILS_H__
3
4/*!
5 * @file wilc_strutils.h
6 * @brief Basic string utilities
7 * @author syounan
8 * @sa wilc_oswrapper.h top level OS wrapper file
9 * @date 16 Aug 2010
10 * @version 1.0
11 */
12
13#ifndef CONFIG_WILC_STRING_UTILS
14#error the feature CONFIG_WILC_STRING_UTILS must be supported to include this file
15#endif
16
17/*!
18 * @brief Compares two memory buffers
19 * @param[in] pvArg1 pointer to the first memory location
20 * @param[in] pvArg2 pointer to the second memory location
21 * @param[in] u32Count the size of the memory buffers
22 * @return 0 if the 2 buffers are equal, 1 if pvArg1 is bigger than pvArg2,
23 * -1 if pvArg1 smaller than pvArg2
24 * @note this function repeats the functionality of standard memcmp
25 * @author syounan
26 * @date 18 Aug 2010
27 * @version 1.0
28 */
29WILC_Sint32 WILC_memcmp(const void *pvArg1, const void *pvArg2, WILC_Uint32 u32Count);
30
31/*!
32 * @brief Internal implementation for memory copy
33 * @param[in] pvTarget the target buffer to which the data is copied into
34 * @param[in] pvSource pointer to the second memory location
35 * @param[in] u32Count the size of the data to copy
36 * @note this function should not be used directly, use WILC_memcpy instead
37 * @author syounan
38 * @date 18 Aug 2010
39 * @version 1.0
40 */
41void WILC_memcpy_INTERNAL(void *pvTarget, const void *pvSource, WILC_Uint32 u32Count);
42
43/*!
44 * @brief Copies the contents of a memory buffer into another
45 * @param[in] pvTarget the target buffer to which the data is copied into
46 * @param[in] pvSource pointer to the second memory location
47 * @param[in] u32Count the size of the data to copy
48 * @return WILC_SUCCESS if copy is successfully handeled
49 * WILC_FAIL if copy failed
50 * @note this function repeats the functionality of standard memcpy,
51 * however memcpy is undefined if the two buffers overlap but this
52 * implementation will check for overlap and report error
53 * @author syounan
54 * @date 18 Aug 2010
55 * @version 1.0
56 */
57static WILC_ErrNo WILC_memcpy(void *pvTarget, const void *pvSource, WILC_Uint32 u32Count)
58{
59 if (
60 (((WILC_Uint8 *)pvTarget <= (WILC_Uint8 *)pvSource)
61 && (((WILC_Uint8 *)pvTarget + u32Count) > (WILC_Uint8 *)pvSource))
62
63 || (((WILC_Uint8 *)pvSource <= (WILC_Uint8 *)pvTarget)
64 && (((WILC_Uint8 *)pvSource + u32Count) > (WILC_Uint8 *)pvTarget))
65 ) {
66 /* ovelapped memory, return Error */
67 return WILC_FAIL;
68 } else {
69 WILC_memcpy_INTERNAL(pvTarget, pvSource, u32Count);
70 return WILC_SUCCESS;
71 }
72}
73
74/*!
75 * @brief Sets the contents of a memory buffer with the given value
76 * @param[in] pvTarget the target buffer which contsnts will be set
77 * @param[in] u8SetValue the value to be used
78 * @param[in] u32Count the size of the memory buffer
79 * @return value of pvTarget
80 * @note this function repeats the functionality of standard memset
81 * @author syounan
82 * @date 18 Aug 2010
83 * @version 1.0
84 */
85void *WILC_memset(void *pvTarget, WILC_Uint8 u8SetValue, WILC_Uint32 u32Count);
86
87/*!
88 * @brief Concatenates the contents of 2 strings up to a given count
89 * @param[in] pcTarget the target string, its null character will be overwritten
90 * and contents of pcSource will be concatentaed to it
91 * @param[in] pcSource the source string the will be concatentaed
92 * @param[in] u32Count copying will proceed until a null character in pcSource
93 * is encountered or u32Count of bytes copied
94 * @return value of pcTarget
95 * @note this function repeats the functionality of standard strncat
96 * @author syounan
97 * @date 18 Aug 2010
98 * @version 1.0
99 */
100WILC_Char *WILC_strncat(WILC_Char *pcTarget, const WILC_Char *pcSource,
101 WILC_Uint32 u32Count);
102
103/*!
104 * @brief copies the contents of source string into the target string
105 * @param[in] pcTarget the target string buffer
106 * @param[in] pcSource the source string the will be copied
107 * @param[in] u32Count copying will proceed until a null character in pcSource
108 * is encountered or u32Count of bytes copied
109 * @return value of pcTarget
110 * @note this function repeats the functionality of standard strncpy
111 * @author syounan
112 * @date 18 Aug 2010
113 * @version 1.0
114 */
115WILC_Char *WILC_strncpy(WILC_Char *pcTarget, const WILC_Char *pcSource,
116 WILC_Uint32 u32Count);
117
118/*!
119 * @brief Compares two strings
120 * @details Compares 2 strings reporting which is bigger, WILC_NULL is considered
121 * the smallest string, then a zero length string then all other
122 * strings depending on thier ascii characters order
123 * @param[in] pcStr1 the first string, WILC_NULL is valid and considered smaller
124 * than any other non-NULL string (incliding zero lenght strings)
125 * @param[in] pcStr2 the second string, WILC_NULL is valid and considered smaller
126 * than any other non-NULL string (incliding zero lenght strings)
127 * @return 0 if the 2 strings are equal, 1 if pcStr1 is bigger than pcStr2,
128 * -1 if pcStr1 smaller than pcStr2
129 * @note this function repeats the functionality of standard strcmp
130 * @author syounan
131 * @date 18 Aug 2010
132 * @version 1.0
133 */
134WILC_Sint32 WILC_strcmp(const WILC_Char *pcStr1, const WILC_Char *pcStr2);
135
136/*!
137 * @brief Compares two strings up to u32Count characters
138 * @details Compares 2 strings reporting which is bigger, WILC_NULL is considered
139 * the smallest string, then a zero length string then all other
140 * strings depending on thier ascii characters order with small case
141 * converted to uppder case
142 * @param[in] pcStr1 the first string, WILC_NULL is valid and considered smaller
143 * than any other non-NULL string (incliding zero lenght strings)
144 * @param[in] pcStr2 the second string, WILC_NULL is valid and considered smaller
145 * than any other non-NULL string (incliding zero lenght strings)
146 * @param[in] u32Count copying will proceed until a null character in pcStr1 or
147 * pcStr2 is encountered or u32Count of bytes copied
148 * @return 0 if the 2 strings are equal, 1 if pcStr1 is bigger than pcStr2,
149 * -1 if pcStr1 smaller than pcStr2
150 * @author aabozaeid
151 * @date 7 Dec 2010
152 * @version 1.0
153 */
154WILC_Sint32 WILC_strncmp(const WILC_Char *pcStr1, const WILC_Char *pcStr2,
155 WILC_Uint32 u32Count);
156
157/*!
158 * @brief Compares two strings ignoring the case of its latin letters
159 * @details Compares 2 strings reporting which is bigger, WILC_NULL is considered
160 * the smallest string, then a zero length string then all other
161 * strings depending on thier ascii characters order with small case
162 * converted to uppder case
163 * @param[in] pcStr1 the first string, WILC_NULL is valid and considered smaller
164 * than any other non-NULL string (incliding zero lenght strings)
165 * @param[in] pcStr2 the second string, WILC_NULL is valid and considered smaller
166 * than any other non-NULL string (incliding zero lenght strings)
167 * @return 0 if the 2 strings are equal, 1 if pcStr1 is bigger than pcStr2,
168 * -1 if pcStr1 smaller than pcStr2
169 * @author syounan
170 * @date 1 Nov 2010
171 * @version 2.0
172 */
173WILC_Sint32 WILC_strcmp_IgnoreCase(const WILC_Char *pcStr1, const WILC_Char *pcStr2);
174
175/*!
176 * @brief Compares two strings ignoring the case of its latin letters up to
177 * u32Count characters
178 * @details Compares 2 strings reporting which is bigger, WILC_NULL is considered
179 * the smallest string, then a zero length string then all other
180 * strings depending on thier ascii characters order with small case
181 * converted to uppder case
182 * @param[in] pcStr1 the first string, WILC_NULL is valid and considered smaller
183 * than any other non-NULL string (incliding zero lenght strings)
184 * @param[in] pcStr2 the second string, WILC_NULL is valid and considered smaller
185 * than any other non-NULL string (incliding zero lenght strings)
186 * @param[in] u32Count copying will proceed until a null character in pcStr1 or
187 * pcStr2 is encountered or u32Count of bytes copied
188 * @return 0 if the 2 strings are equal, 1 if pcStr1 is bigger than pcStr2,
189 * -1 if pcStr1 smaller than pcStr2
190 * @author aabozaeid
191 * @date 7 Dec 2010
192 * @version 1.0
193 */
194WILC_Sint32 WILC_strncmp_IgnoreCase(const WILC_Char *pcStr1, const WILC_Char *pcStr2,
195 WILC_Uint32 u32Count);
196
197/*!
198 * @brief gets the length of a string
199 * @param[in] pcStr the string
200 * @return the length
201 * @note this function repeats the functionality of standard strlen
202 * @author syounan
203 * @date 18 Aug 2010
204 * @version 1.0
205 */
206WILC_Uint32 WILC_strlen(const WILC_Char *pcStr);
207
208/*!
209 * @brief convert string to integer
210 * @param[in] pcStr the string
211 * @return the value of string
212 * @note this function repeats the functionality of the libc atoi
213 * @author bfahmy
214 * @date 28 Aug 2010
215 * @version 1.0
216 */
217WILC_Sint32 WILC_strtoint(const WILC_Char *pcStr);
218
219/*!
220 * @brief print a formatted string into a buffer
221 * @param[in] pcTarget the buffer where the resulting string is written
222 * @param[in] u32Size size of the output beffer including the \0 terminating
223 * character
224 * @param[in] pcFormat format of the string
225 * @return number of character written or would have been written if the
226 * string were not truncated
227 * @note this function repeats the functionality of standard snprintf
228 * @author syounan
229 * @date 1 Nov 2010
230 * @version 2.0
231 */
232WILC_Sint32 WILC_snprintf(WILC_Char *pcTarget, WILC_Uint32 u32Size,
233 const WILC_Char *pcFormat, ...);
234
235
236#ifdef CONFIG_WILC_EXTENDED_STRING_OPERATIONS
237
238
239/**
240 * @brief
241 * @details Searches for the first occurrence of the character c in the first n bytes
242 * of the string pointed to by the argument str.
243 * Returns a pointer pointing to the first matching character,
244 * or null if no match was found.
245 * @param[in]
246 * @return
247 * @note
248 * @author remil
249 * @date 3 Nov 2010
250 * @version 1.0
251 */
252WILC_Char *WILC_memchr(const void *str, WILC_Char c, WILC_Sint32 n);
253
254/**
255 * @brief
256 * @details Searches for the first occurrence of the character c (an unsigned char)
257 * in the string pointed to by the argument str.
258 * The terminating null character is considered to be part of the string.
259 * Returns a pointer pointing to the first matching character,
260 * or null if no match was found.
261 * @param[in]
262 * @return
263 * @note
264 * @author remil
265 * @date 3 Nov 2010
266 * @version 1.0
267 */
268WILC_Char *WILC_strchr(const WILC_Char *str, WILC_Char c);
269
270/**
271 * @brief
272 * @details Appends the string pointed to by str2 to the end of the string pointed to by str1.
273 * The terminating null character of str1 is overwritten.
274 * Copying stops once the terminating null character of str2 is copied. If overlapping occurs, the result is undefined.
275 * The argument str1 is returned.
276 * @param[in] WILC_Char* str1,
277 * @param[in] WILC_Char* str2,
278 * @return WILC_Char*
279 * @note
280 * @author remil
281 * @date 3 Nov 2010
282 * @version 1.0
283 */
284WILC_Char *WILC_strcat(WILC_Char *str1, const WILC_Char *str2);
285
286
287/**
288 * @brief
289 * @details Copy pcSource to pcTarget
290 * @param[in] WILC_Char* pcTarget
291 * @param[in] const WILC_Char* pcSource
292 * @return WILC_Char*
293 * @note
294 * @author remil
295 * @date 3 Nov 2010
296 * @version 1.0
297 */
298WILC_Char *WILC_strcpy(WILC_Char *pcTarget, const WILC_Char *pcSource);
299
300
301
302/**
303 * @brief
304 * @details Finds the first sequence of characters in the string str1 that
305 * does not contain any character specified in str2.
306 * Returns the length of this first sequence of characters found that
307 * do not match with str2.
308 * @param[in] const WILC_Char *str1
309 * @param[in] const WILC_Char *str2
310 * @return WILC_Uint32
311 * @note
312 * @author remil
313 * @date 3 Nov 2010
314 * @version 1.0
315 */
316WILC_Uint32 WILC_strcspn(const WILC_Char *str1, const WILC_Char *str2);
317
318
319/**
320 * @brief
321 * @details Searches an internal array for the error number errnum and returns a pointer
322 * to an error message string.
323 * Returns a pointer to an error message string.
324 * @param[in] WILC_Sint32 errnum
325 * @return WILC_Char*
326 * @note
327 * @author remil
328 * @date 3 Nov 2010
329 * @version 1.0
330 */
331WILC_Char *WILC_strerror(WILC_Sint32 errnum);
332
333/**
334 * @brief
335 * @details Finds the first occurrence of the entire string str2
336 * (not including the terminating null character) which appears in the string str1.
337 * Returns a pointer to the first occurrence of str2 in str1.
338 * If no match was found, then a null pointer is returned.
339 * If str2 points to a string of zero length, then the argument str1 is returned.
340 * @param[in] const WILC_Char *str1
341 * @param[in] const WILC_Char *str2
342 * @return WILC_Char*
343 * @note
344 * @author remil
345 * @date 3 Nov 2010
346 * @version 1.0
347 */
348WILC_Char *WILC_strstr(const WILC_Char *str1, const WILC_Char *str2);
349
350/**
351 * @brief
352 * @details Searches for the first occurrence of the character c (an unsigned char)
353 * in the string pointed to by the argument str.
354 * The terminating null character is considered to be part of the string.
355 * Returns a pointer pointing to the first matching character,
356 * or null if no match was found.
357 * @param[in]
358 * @return
359 * @note
360 * @author remil
361 * @date 3 Nov 2010
362 * @version 1.0
363 */
364WILC_Char *WILC_strchr(const WILC_Char *str, WILC_Char c);
365
366
367/**
368 * @brief
369 * @details Parses the C string str interpreting its content as a floating point
370 * number and returns its value as a double.
371 * If endptr is not a null pointer, the function also sets the value pointed
372 * by endptr to point to the first character after the number.
373 * @param[in] const WILC_Char* str
374 * @param[in] WILC_Char** endptr
375 * @return WILC_Double
376 * @note
377 * @author remil
378 * @date 11 Nov 2010
379 * @version 1.0
380 */
381WILC_Double WILC_StringToDouble(const WILC_Char *str,
382 WILC_Char **endptr);
383
384
385/**
386 * @brief Parses the C string str interpreting its content as an unsigned integral
387 * number of the specified base, which is returned as an unsigned long int value.
388 * @details The function first discards as many whitespace characters as necessary
389 * until the first non-whitespace character is found.
390 * Then, starting from this character, takes as many characters as possible
391 * that are valid following a syntax that depends on the base parameter,
392 * and interprets them as a numerical value.
393 * Finally, a pointer to the first character following the integer
394 * representation in str is stored in the object pointed by endptr.
395 * @param[in] const WILC_Char *str
396 * @param[in] WILC_Char **endptr
397 * @param[in] WILC_Sint32 base
398 * @return WILC_Uint32
399 * @note
400 * @author remil
401 * @date 11 Nov 2010
402 * @version 1.0
403 */
404WILC_Uint32 WILC_StringToUint32(const WILC_Char *str,
405 WILC_Char **endptr,
406 WILC_Sint32 base);
407
408
409
410#endif
411
412#endif