build/freebsd: rename macro BSDPAPP to FREEBSD
[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 #include <string.h>
20
21 /**
22  * Takes string "string" parameter and splits it at character "delim"
23  * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like
24  * strtok or strsep functions, this modifies its input string, by replacing
25  * instances of "delim" with '\\0'. All resultant tokens are returned in the
26  * "tokens" array which must have enough entries to hold "maxtokens".
27  *
28  * @param string
29  *   The input string to be split into tokens
30  *
31  * @param stringlen
32  *   The max length of the input buffer
33  *
34  * @param tokens
35  *   The array to hold the pointers to the tokens in the string
36  *
37  * @param maxtokens
38  *   The number of elements in the tokens array. At most, maxtokens-1 splits
39  *   of the string will be done.
40  *
41  * @param delim
42  *   The character on which the split of the data will be done
43  *
44  * @return
45  *   The number of tokens in the tokens array.
46  */
47 int
48 rte_strsplit(char *string, int stringlen,
49              char **tokens, int maxtokens, char delim);
50
51 /**
52  * @internal
53  * DPDK-specific version of strlcpy for systems without
54  * libc or libbsd copies of the function
55  */
56 static inline size_t
57 rte_strlcpy(char *dst, const char *src, size_t size)
58 {
59         return (size_t)snprintf(dst, size, "%s", src);
60 }
61
62 /**
63  * @internal
64  * DPDK-specific version of strlcat for systems without
65  * libc or libbsd copies of the function
66  */
67 static inline size_t
68 rte_strlcat(char *dst, const char *src, size_t size)
69 {
70         size_t l = strnlen(dst, size);
71         if (l < size)
72                 return l + rte_strlcpy(&dst[l], src, size - l);
73         return l + strlen(src);
74 }
75
76 /* pull in a strlcpy function */
77 #ifdef RTE_EXEC_ENV_FREEBSD
78 #ifndef __BSD_VISIBLE /* non-standard functions are hidden */
79 #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
80 #define strlcat(dst, src, size) rte_strlcat(dst, src, size)
81 #endif
82
83 #else /* non-BSD platforms */
84 #ifdef RTE_USE_LIBBSD
85 #include <bsd/string.h>
86
87 #else /* no BSD header files, create own */
88 #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
89 #define strlcat(dst, src, size) rte_strlcat(dst, src, size)
90
91 #endif /* RTE_USE_LIBBSD */
92 #endif /* FREEBSD */
93
94 /**
95  * Copy string src to buffer dst of size dsize.
96  * At most dsize-1 chars will be copied.
97  * Always NUL-terminates, unless (dsize == 0).
98  * Returns number of bytes copied (terminating NUL-byte excluded) on success ;
99  * negative errno on error.
100  *
101  * @param dst
102  *   The destination string.
103  *
104  * @param src
105  *   The input string to be copied.
106  *
107  * @param dsize
108  *   Length in bytes of the destination buffer.
109  *
110  * @return
111  *   The number of bytes copied on success
112  *   -E2BIG if the destination buffer is too small.
113  */
114 ssize_t
115 rte_strscpy(char *dst, const char *src, size_t dsize);
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif /* RTE_STRING_FNS_H */