fb0bbe8628a7141625e64c8a13a27f45ad85a07c
[dpdk.git] / lib / librte_eal / common / eal_common_string_fns.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <errno.h>
38
39 #include <rte_string_fns.h>
40
41 /* safe version os snprintf */
42 int
43 rte_snprintf(char *buffer, int buflen, const char *format, ...)
44 {
45         int len;
46         va_list ap;
47
48         if (buffer == NULL && buflen != 0)
49                 goto einval_error;
50         if (format == NULL) {
51                 if (buflen > 0)
52                         buffer[0] = '\0';
53                 goto einval_error;
54         }
55
56         va_start(ap, format);
57         len = vsnprintf(buffer, buflen, format, ap);
58         va_end(ap);
59         if (len >= buflen && buflen > 0)
60                 buffer[buflen - 1] = '\0';
61
62         return len;
63
64 einval_error:
65         errno = EINVAL;
66         return -1;
67 }
68
69 /* split string into tokens */
70 int
71 rte_strsplit(char *string, int stringlen,
72              char **tokens, int maxtokens, char delim)
73 {
74         int i, tok = 0;
75         int tokstart = 1; /* first token is right at start of string */
76
77         if (string == NULL || tokens == NULL)
78                 goto einval_error;
79
80         for (i = 0; i < stringlen; i++) {
81                 if (string[i] == '\0' || tok >= maxtokens)
82                         break;
83                 if (tokstart) {
84                         tokstart = 0;
85                         tokens[tok++] = &string[i];
86                 }
87                 if (string[i] == delim) {
88                         string[i] = '\0';
89                         tokstart = 1;
90                 }
91         }
92         return tok;
93
94 einval_error:
95         errno = EINVAL;
96         return -1;
97 }