Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2019 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 : : #include <ctype.h>
15 : : #include <stdio.h>
16 : : #include <string.h>
17 : :
18 : : #include <rte_common.h>
19 : : #include <rte_compat.h>
20 : :
21 : : #ifdef __cplusplus
22 : : extern "C" {
23 : : #endif
24 : :
25 : : /**
26 : : * Takes string "string" parameter and splits it at character "delim"
27 : : * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like
28 : : * strtok or strsep functions, this modifies its input string, by replacing
29 : : * instances of "delim" with '\\0'. All resultant tokens are returned in the
30 : : * "tokens" array which must have enough entries to hold "maxtokens".
31 : : *
32 : : * @param string
33 : : * The input string to be split into tokens
34 : : *
35 : : * @param stringlen
36 : : * The max length of the input buffer
37 : : *
38 : : * @param tokens
39 : : * The array to hold the pointers to the tokens in the string
40 : : *
41 : : * @param maxtokens
42 : : * The number of elements in the tokens array. At most, maxtokens-1 splits
43 : : * of the string will be done.
44 : : *
45 : : * @param delim
46 : : * The character on which the split of the data will be done
47 : : *
48 : : * @return
49 : : * The number of tokens in the tokens array.
50 : : */
51 : : int
52 : : rte_strsplit(char *string, int stringlen,
53 : : char **tokens, int maxtokens, char delim);
54 : :
55 : : /**
56 : : * @internal
57 : : * DPDK-specific version of strlcpy for systems without
58 : : * a native libc copy of the function
59 : : */
60 : : static inline size_t
61 : 0 : rte_strlcpy(char *dst, const char *src, size_t size)
62 : : {
63 [ - + - + : 144942 : return (size_t)snprintf(dst, size, "%s", src);
- + - + -
+ - + ]
64 : : }
65 : :
66 : : /**
67 : : * @internal
68 : : * DPDK-specific version of strlcat for systems without
69 : : * a native libc copy of the function
70 : : */
71 : : static inline size_t
72 : 115670 : rte_strlcat(char *dst, const char *src, size_t size)
73 : : {
74 : 115670 : size_t l = strnlen(dst, size);
75 [ + - ]: 115670 : if (l < size)
76 : 115670 : return l + rte_strlcpy(&dst[l], src, size - l);
77 : 0 : return l + strlen(src);
78 : : }
79 : :
80 : : #ifdef __cplusplus
81 : : }
82 : : #endif
83 : :
84 : : /* provide strlcpy/strlcat aliases where not natively available */
85 : : #if !defined(RTE_HAS_STRLCPY) || \
86 : : (defined(RTE_EXEC_ENV_FREEBSD) && !defined(__BSD_VISIBLE)) || \
87 : : (defined(__GLIBC__) && !defined(__USE_MISC))
88 : : #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
89 : : #define strlcat(dst, src, size) rte_strlcat(dst, src, size)
90 : : #endif
91 : :
92 : : #ifdef __cplusplus
93 : : extern "C" {
94 : : #endif
95 : :
96 : : /**
97 : : * Copy string src to buffer dst of size dsize.
98 : : * At most dsize-1 chars will be copied.
99 : : * Always NUL-terminates, unless (dsize == 0).
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 (terminating NUL-byte excluded) on success.
112 : : * -E2BIG if the destination buffer is too small.
113 : : * rte_errno is set.
114 : : */
115 : : ssize_t
116 : : rte_strscpy(char *dst, const char *src, size_t dsize);
117 : :
118 : : /**
119 : : * @warning
120 : : * @b EXPERIMENTAL: this API may change without prior notice.
121 : : *
122 : : * Search for the first non whitespace character.
123 : : *
124 : : * @param src
125 : : * The input string to be analysed.
126 : : *
127 : : * @return
128 : : * The address of the first non whitespace character.
129 : : */
130 : : __rte_experimental
131 : : static inline const char *
132 : 3 : rte_str_skip_leading_spaces(const char *src)
133 : : {
134 : : const char *p = src;
135 : :
136 [ + + ]: 9 : while (isspace(*p))
137 : 6 : p++;
138 : :
139 : 3 : return p;
140 : : }
141 : :
142 : : /**
143 : : * @warning
144 : : * @b EXPERIMENTAL: this API may change without prior notice.
145 : : *
146 : : * Provides the final component of a path, similar to POSIX basename function.
147 : : *
148 : : * This API provides the similar behaviour on all platforms, Linux, BSD, Windows,
149 : : * hiding the implementation differences.
150 : : * - It does not modify the input path.
151 : : * - The output buffer is passed as an argument, and the result is copied into it.
152 : : * - Expected output is the last component of the path, or the path itself if
153 : : * it does not contain a directory separator.
154 : : * - If the final component is too long to fit in the output buffer, it will be truncated.
155 : : * - For empty or NULL input paths, output buffer will contain the string ".".
156 : : * - Supports up to PATH_MAX (BSD/Linux) or _MAX_PATH (Windows) characters in the input path.
157 : : *
158 : : * @param path
159 : : * The input path string. Not modified by this function.
160 : : * @param buf
161 : : * The buffer to hold the resultant basename.
162 : : * Must be large enough to hold the result, otherwise basename will be truncated.
163 : : * @param buflen
164 : : * The size of the buffer in bytes.
165 : : * @return
166 : : * The number of bytes that were written to buf (excluding the terminating '\0').
167 : : * If the return value is >= buflen, truncation occurred.
168 : : * Return (size_t)-1 on error (Windows only)
169 : : */
170 : : __rte_experimental
171 : : size_t
172 : : rte_basename(const char *path, char *buf, size_t buflen);
173 : :
174 : : #ifdef __cplusplus
175 : : }
176 : : #endif
177 : :
178 : : #endif /* RTE_STRING_FNS_H */
|