199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2019 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson /** 699a2dd95SBruce Richardson * @file 799a2dd95SBruce Richardson * 899a2dd95SBruce Richardson * String-related functions as replacement for libc equivalents 999a2dd95SBruce Richardson */ 1099a2dd95SBruce Richardson 1199a2dd95SBruce Richardson #ifndef _RTE_STRING_FNS_H_ 1299a2dd95SBruce Richardson #define _RTE_STRING_FNS_H_ 1399a2dd95SBruce Richardson 142df20a1dSDavid Marchand #include <ctype.h> 1599a2dd95SBruce Richardson #include <stdio.h> 1699a2dd95SBruce Richardson #include <string.h> 1799a2dd95SBruce Richardson 1899a2dd95SBruce Richardson #include <rte_common.h> 192df20a1dSDavid Marchand #include <rte_compat.h> 2099a2dd95SBruce Richardson 21*719834a6SMattias Rönnblom #ifdef __cplusplus 22*719834a6SMattias Rönnblom extern "C" { 23*719834a6SMattias Rönnblom #endif 24*719834a6SMattias Rönnblom 2599a2dd95SBruce Richardson /** 2699a2dd95SBruce Richardson * Takes string "string" parameter and splits it at character "delim" 2799a2dd95SBruce Richardson * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like 2899a2dd95SBruce Richardson * strtok or strsep functions, this modifies its input string, by replacing 2999a2dd95SBruce Richardson * instances of "delim" with '\\0'. All resultant tokens are returned in the 3099a2dd95SBruce Richardson * "tokens" array which must have enough entries to hold "maxtokens". 3199a2dd95SBruce Richardson * 3299a2dd95SBruce Richardson * @param string 3399a2dd95SBruce Richardson * The input string to be split into tokens 3499a2dd95SBruce Richardson * 3599a2dd95SBruce Richardson * @param stringlen 3699a2dd95SBruce Richardson * The max length of the input buffer 3799a2dd95SBruce Richardson * 3899a2dd95SBruce Richardson * @param tokens 3999a2dd95SBruce Richardson * The array to hold the pointers to the tokens in the string 4099a2dd95SBruce Richardson * 4199a2dd95SBruce Richardson * @param maxtokens 4299a2dd95SBruce Richardson * The number of elements in the tokens array. At most, maxtokens-1 splits 4399a2dd95SBruce Richardson * of the string will be done. 4499a2dd95SBruce Richardson * 4599a2dd95SBruce Richardson * @param delim 4699a2dd95SBruce Richardson * The character on which the split of the data will be done 4799a2dd95SBruce Richardson * 4899a2dd95SBruce Richardson * @return 4999a2dd95SBruce Richardson * The number of tokens in the tokens array. 5099a2dd95SBruce Richardson */ 5199a2dd95SBruce Richardson int 5299a2dd95SBruce Richardson rte_strsplit(char *string, int stringlen, 5399a2dd95SBruce Richardson char **tokens, int maxtokens, char delim); 5499a2dd95SBruce Richardson 5599a2dd95SBruce Richardson /** 5699a2dd95SBruce Richardson * @internal 5799a2dd95SBruce Richardson * DPDK-specific version of strlcpy for systems without 5899a2dd95SBruce Richardson * libc or libbsd copies of the function 5999a2dd95SBruce Richardson */ 6099a2dd95SBruce Richardson static inline size_t 6199a2dd95SBruce Richardson rte_strlcpy(char *dst, const char *src, size_t size) 6299a2dd95SBruce Richardson { 6399a2dd95SBruce Richardson return (size_t)snprintf(dst, size, "%s", src); 6499a2dd95SBruce Richardson } 6599a2dd95SBruce Richardson 6699a2dd95SBruce Richardson /** 6799a2dd95SBruce Richardson * @internal 6899a2dd95SBruce Richardson * DPDK-specific version of strlcat for systems without 6999a2dd95SBruce Richardson * libc or libbsd copies of the function 7099a2dd95SBruce Richardson */ 7199a2dd95SBruce Richardson static inline size_t 7299a2dd95SBruce Richardson rte_strlcat(char *dst, const char *src, size_t size) 7399a2dd95SBruce Richardson { 7499a2dd95SBruce Richardson size_t l = strnlen(dst, size); 7599a2dd95SBruce Richardson if (l < size) 7699a2dd95SBruce Richardson return l + rte_strlcpy(&dst[l], src, size - l); 7799a2dd95SBruce Richardson return l + strlen(src); 7899a2dd95SBruce Richardson } 7999a2dd95SBruce Richardson 80*719834a6SMattias Rönnblom #ifdef __cplusplus 81*719834a6SMattias Rönnblom } 82*719834a6SMattias Rönnblom #endif 83*719834a6SMattias Rönnblom 8499a2dd95SBruce Richardson /* pull in a strlcpy function */ 8599a2dd95SBruce Richardson #ifdef RTE_EXEC_ENV_FREEBSD 8699a2dd95SBruce Richardson #ifndef __BSD_VISIBLE /* non-standard functions are hidden */ 8799a2dd95SBruce Richardson #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size) 8899a2dd95SBruce Richardson #define strlcat(dst, src, size) rte_strlcat(dst, src, size) 8999a2dd95SBruce Richardson #endif 9099a2dd95SBruce Richardson 9199a2dd95SBruce Richardson #else /* non-BSD platforms */ 9299a2dd95SBruce Richardson #ifdef RTE_USE_LIBBSD 9399a2dd95SBruce Richardson #include <bsd/string.h> 9499a2dd95SBruce Richardson 9599a2dd95SBruce Richardson #else /* no BSD header files, create own */ 9699a2dd95SBruce Richardson #define strlcpy(dst, src, size) rte_strlcpy(dst, src, size) 9799a2dd95SBruce Richardson #define strlcat(dst, src, size) rte_strlcat(dst, src, size) 9899a2dd95SBruce Richardson 9999a2dd95SBruce Richardson #endif /* RTE_USE_LIBBSD */ 10099a2dd95SBruce Richardson #endif /* FREEBSD */ 10199a2dd95SBruce Richardson 102*719834a6SMattias Rönnblom #ifdef __cplusplus 103*719834a6SMattias Rönnblom extern "C" { 104*719834a6SMattias Rönnblom #endif 105*719834a6SMattias Rönnblom 10699a2dd95SBruce Richardson /** 10799a2dd95SBruce Richardson * Copy string src to buffer dst of size dsize. 10899a2dd95SBruce Richardson * At most dsize-1 chars will be copied. 10999a2dd95SBruce Richardson * Always NUL-terminates, unless (dsize == 0). 11099a2dd95SBruce Richardson * 11199a2dd95SBruce Richardson * @param dst 11299a2dd95SBruce Richardson * The destination string. 11399a2dd95SBruce Richardson * 11499a2dd95SBruce Richardson * @param src 11599a2dd95SBruce Richardson * The input string to be copied. 11699a2dd95SBruce Richardson * 11799a2dd95SBruce Richardson * @param dsize 11899a2dd95SBruce Richardson * Length in bytes of the destination buffer. 11999a2dd95SBruce Richardson * 12099a2dd95SBruce Richardson * @return 121dbba7c9eSThomas Monjalon * The number of bytes copied (terminating NUL-byte excluded) on success. 12299a2dd95SBruce Richardson * -E2BIG if the destination buffer is too small. 123dbba7c9eSThomas Monjalon * rte_errno is set. 12499a2dd95SBruce Richardson */ 12599a2dd95SBruce Richardson ssize_t 12699a2dd95SBruce Richardson rte_strscpy(char *dst, const char *src, size_t dsize); 12799a2dd95SBruce Richardson 1282df20a1dSDavid Marchand /** 1292df20a1dSDavid Marchand * @warning 1302df20a1dSDavid Marchand * @b EXPERIMENTAL: this API may change without prior notice. 1312df20a1dSDavid Marchand * 1322df20a1dSDavid Marchand * Search for the first non whitespace character. 1332df20a1dSDavid Marchand * 1342df20a1dSDavid Marchand * @param src 1352df20a1dSDavid Marchand * The input string to be analysed. 1362df20a1dSDavid Marchand * 1372df20a1dSDavid Marchand * @return 1382df20a1dSDavid Marchand * The address of the first non whitespace character. 1392df20a1dSDavid Marchand */ 1402df20a1dSDavid Marchand __rte_experimental 1412df20a1dSDavid Marchand static inline const char * 1422df20a1dSDavid Marchand rte_str_skip_leading_spaces(const char *src) 1432df20a1dSDavid Marchand { 1442df20a1dSDavid Marchand const char *p = src; 1452df20a1dSDavid Marchand 1462df20a1dSDavid Marchand while (isspace(*p)) 1472df20a1dSDavid Marchand p++; 1482df20a1dSDavid Marchand 1492df20a1dSDavid Marchand return p; 1502df20a1dSDavid Marchand } 1512df20a1dSDavid Marchand 15299a2dd95SBruce Richardson #ifdef __cplusplus 15399a2dd95SBruce Richardson } 15499a2dd95SBruce Richardson #endif 15599a2dd95SBruce Richardson 15699a2dd95SBruce Richardson #endif /* RTE_STRING_FNS_H */ 157