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 : : #ifdef __cplusplus
15 : : extern "C" {
16 : : #endif
17 : :
18 : : #include <stdio.h>
19 : : #include <string.h>
20 : :
21 : : #include <rte_common.h>
22 : :
23 : : /**
24 : : * Takes string "string" parameter and splits it at character "delim"
25 : : * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like
26 : : * strtok or strsep functions, this modifies its input string, by replacing
27 : : * instances of "delim" with '\\0'. All resultant tokens are returned in the
28 : : * "tokens" array which must have enough entries to hold "maxtokens".
29 : : *
30 : : * @param string
31 : : * The input string to be split into tokens
32 : : *
33 : : * @param stringlen
34 : : * The max length of the input buffer
35 : : *
36 : : * @param tokens
37 : : * The array to hold the pointers to the tokens in the string
38 : : *
39 : : * @param maxtokens
40 : : * The number of elements in the tokens array. At most, maxtokens-1 splits
41 : : * of the string will be done.
42 : : *
43 : : * @param delim
44 : : * The character on which the split of the data will be done
45 : : *
46 : : * @return
47 : : * The number of tokens in the tokens array.
48 : : */
49 : : int
50 : : rte_strsplit(char *string, int stringlen,
51 : : char **tokens, int maxtokens, char delim);
52 : :
53 : : /**
54 : : * @internal
55 : : * DPDK-specific version of strlcpy for systems without
56 : : * libc or libbsd copies of the function
57 : : */
58 : : static inline size_t
59 : : rte_strlcpy(char *dst, const char *src, size_t size)
60 : : {
61 [ - + - + : 71454 : return (size_t)snprintf(dst, size, "%s", src);
- + - + -
+ - + ]
62 : : }
63 : :
64 : : /**
65 : : * @internal
66 : : * DPDK-specific version of strlcat for systems without
67 : : * libc or libbsd copies of the function
68 : : */
69 : : static inline size_t
70 : 69726 : rte_strlcat(char *dst, const char *src, size_t size)
71 : : {
72 : 69726 : size_t l = strnlen(dst, size);
73 [ + - ]: 69726 : if (l < size)
74 : 69726 : return l + rte_strlcpy(&dst[l], src, size - l);
75 : 0 : return l + strlen(src);
76 : : }
77 : :
78 : : /* pull in a strlcpy function */
79 : : #ifdef RTE_EXEC_ENV_FREEBSD
80 : : #ifndef __BSD_VISIBLE /* non-standard functions are hidden */
81 : : #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
82 : : #define strlcat(dst, src, size) rte_strlcat(dst, src, size)
83 : : #endif
84 : :
85 : : #else /* non-BSD platforms */
86 : : #ifdef RTE_USE_LIBBSD
87 : : #include <bsd/string.h>
88 : :
89 : : #else /* no BSD header files, create own */
90 : : #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size)
91 : : #define strlcat(dst, src, size) rte_strlcat(dst, src, size)
92 : :
93 : : #endif /* RTE_USE_LIBBSD */
94 : : #endif /* FREEBSD */
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 : : #ifdef __cplusplus
119 : : }
120 : : #endif
121 : :
122 : : #endif /* RTE_STRING_FNS_H */
|