New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / common / eal_common_string_fns.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <errno.h>
9
10 #include <rte_string_fns.h>
11
12 /* split string into tokens */
13 int
14 rte_strsplit(char *string, int stringlen,
15              char **tokens, int maxtokens, char delim)
16 {
17         int i, tok = 0;
18         int tokstart = 1; /* first token is right at start of string */
19
20         if (string == NULL || tokens == NULL)
21                 goto einval_error;
22
23         for (i = 0; i < stringlen; i++) {
24                 if (string[i] == '\0' || tok >= maxtokens)
25                         break;
26                 if (tokstart) {
27                         tokstart = 0;
28                         tokens[tok++] = &string[i];
29                 }
30                 if (string[i] == delim) {
31                         string[i] = '\0';
32                         tokstart = 1;
33                 }
34         }
35         return tok;
36
37 einval_error:
38         errno = EINVAL;
39         return -1;
40 }