New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / include / rte_string_fns.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 /**
6  * @file
7  *
8  * String-related functions as replacement for libc equivalents
9  */
10
11 #ifndef _RTE_STRING_FNS_H_
12 #define _RTE_STRING_FNS_H_
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #include <stdio.h>
19
20 /**
21  * Takes string "string" parameter and splits it at character "delim"
22  * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like
23  * strtok or strsep functions, this modifies its input string, by replacing
24  * instances of "delim" with '\\0'. All resultant tokens are returned in the
25  * "tokens" array which must have enough entries to hold "maxtokens".
26  *
27  * @param string
28  *   The input string to be split into tokens
29  *
30  * @param stringlen
31  *   The max length of the input buffer
32  *
33  * @param tokens
34  *   The array to hold the pointers to the tokens in the string
35  *
36  * @param maxtokens
37  *   The number of elements in the tokens array. At most, maxtokens-1 splits
38  *   of the string will be done.
39  *
40  * @param delim
41  *   The character on which the split of the data will be done
42  *
43  * @return
44  *   The number of tokens in the tokens array.
45  */
46 int
47 rte_strsplit(char *string, int stringlen,
48              char **tokens, int maxtokens, char delim);
49
50 /**
51  * @internal
52  * DPDK-specific version of strlcpy for systems without
53  * libc or libbsd copies of the function
54  */
55 static inline size_t
56 rte_strlcpy(char *dst, const char *src, size_t size)
57 {
58         return (size_t)snprintf(dst, size, "%s", src);
59 }
60
61 /* pull in a strlcpy function */
62 #ifdef RTE_EXEC_ENV_BSDAPP
63 #include <string.h>
64 #ifndef __BSD_VISIBLE /* non-standard functions are hidden */
65 #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
66 #endif
67
68
69 #else /* non-BSD platforms */
70 #ifdef RTE_USE_LIBBSD
71 #include <bsd/string.h>
72
73 #else /* no BSD header files, create own */
74 #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
75
76 #endif /* RTE_USE_LIBBSD */
77 #endif /* BSDAPP */
78
79 #ifdef __cplusplus
80 }
81 #endif
82
83 #endif /* RTE_STRING_FNS_H */