Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2010-2014 Intel Corporation 3 : : */ 4 : : 5 : : #include <ctype.h> 6 : : #include <errno.h> 7 : : #include <stdio.h> 8 : : #include <stdlib.h> 9 : : 10 : : #include <rte_string_fns.h> 11 : : #include <rte_errno.h> 12 : : 13 : : /* split string into tokens */ 14 : : int 15 : 52731 : rte_strsplit(char *string, int stringlen, 16 : : char **tokens, int maxtokens, char delim) 17 : : { 18 : : int i, tok = 0; 19 : : int tokstart = 1; /* first token is right at start of string */ 20 : : 21 [ + + ]: 52731 : if (string == NULL || tokens == NULL) 22 : 2 : goto einval_error; 23 : : 24 [ + + ]: 1897165 : for (i = 0; i < stringlen; i++) { 25 [ + + + + ]: 1897157 : if (string[i] == '\0' || tok >= maxtokens) 26 : : break; 27 [ + + ]: 1844436 : if (tokstart) { 28 : : tokstart = 0; 29 : 161349 : tokens[tok++] = &string[i]; 30 : : } 31 [ + + ]: 1844436 : if (string[i] == delim) { 32 : 108622 : string[i] = '\0'; 33 : : tokstart = 1; 34 : : } 35 : : } 36 : : return tok; 37 : : 38 : : einval_error: 39 : 2 : errno = EINVAL; 40 : 2 : return -1; 41 : : } 42 : : 43 : : /* Copy src string into dst. 44 : : * 45 : : * Return negative value and NUL-terminate if dst is too short, 46 : : * Otherwise return number of bytes copied. 47 : : */ 48 : : ssize_t 49 : 9975 : rte_strscpy(char *dst, const char *src, size_t dsize) 50 : : { 51 : : size_t nleft = dsize; 52 : : size_t res = 0; 53 : : 54 : : /* Copy as many bytes as will fit. */ 55 [ + - ]: 120003 : while (nleft != 0) { 56 : 120003 : dst[res] = src[res]; 57 [ + + ]: 120003 : if (src[res] == '\0') 58 : 9975 : return res; 59 : 110028 : res++; 60 : 110028 : nleft--; 61 : : } 62 : : 63 : : /* Not enough room in dst, set NUL and return error. */ 64 [ # # ]: 0 : if (res != 0) 65 : 0 : dst[res - 1] = '\0'; 66 : 0 : rte_errno = E2BIG; 67 : 0 : return -rte_errno; 68 : : } 69 : : 70 : : uint64_t 71 : 292 : rte_str_to_size(const char *str) 72 : : { 73 : : char *endptr; 74 : : unsigned long long size; 75 : : 76 [ + + ]: 681 : while (isspace((int)*str)) 77 : 389 : str++; 78 [ + + ]: 292 : if (*str == '-') 79 : : return 0; 80 : : 81 : 289 : errno = 0; 82 : 289 : size = strtoull(str, &endptr, 0); 83 [ + + ]: 289 : if (errno) 84 : : return 0; 85 : : 86 [ + + ]: 288 : if (*endptr == ' ') 87 : 55 : endptr++; /* allow 1 space gap */ 88 : : 89 [ + + + + ]: 288 : switch (*endptr) { 90 : 2 : case 'G': case 'g': 91 : 2 : size *= 1024; /* fall-through */ 92 : 114 : case 'M': case 'm': 93 : 114 : size *= 1024; /* fall-through */ 94 : 285 : case 'K': case 'k': 95 : 285 : size *= 1024; /* fall-through */ 96 : : default: 97 : : break; 98 : : } 99 : : return size; 100 : : }