first public release
[dpdk.git] / lib / librte_eal / common / include / rte_string_fns.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 /**
37  * @file
38  *
39  * Definitions of warnings for use of various insecure functions
40  */
41
42 #ifndef _RTE_STRING_FNS_H_
43 #define _RTE_STRING_FNS_H_
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include <stdio.h>
50 #include <stdarg.h>
51 #include <stddef.h>
52 #include <errno.h>
53
54 /**
55  * Safer version of snprintf that writes up to buflen characters to
56  * the output buffer and ensures that the resultant string is null-terminated,
57  * that is, it writes at most buflen-1 actual string characters to buffer. The
58  * return value is the number of characters which should be written to the
59  * buffer, so string truncation can be detected by the caller by checking if
60  * the return value is greater than or equal to the buflen.
61  *
62  * @param buffer
63  *   The buffer into which the output is to be written
64  *
65  * @param buflen
66  *   The size of the output buffer
67  *
68  * @param format
69  *   The format string to be printed to the buffer
70  *
71  * @return
72  *   The number of characters written to the buffer, or if the string has been
73  *   truncated, the number of characters which would have been written had the
74  *   buffer been sufficiently big.
75  *
76  */
77 static inline int
78 rte_snprintf(char *buffer, int buflen, const char *format, ...)
79 {
80         int len;
81         va_list ap;
82
83         if (buffer == NULL && buflen != 0)
84                 goto einval_error;
85         if (format == NULL) {
86                 if (buflen > 0)
87                         buffer[0] = '\0';
88                 goto einval_error;
89         }
90
91         va_start(ap, format);
92         len = vsnprintf(buffer, buflen, format, ap);
93         va_end(ap);
94         if (len >= buflen && buflen > 0)
95                 buffer[buflen - 1] = '\0';
96
97         return len;
98
99 einval_error:
100         errno = EINVAL;
101         return -1;
102 }
103
104
105 /**
106  * Takes string "string" parameter and splits it at character "delim"
107  * up to maxtokens-1 times - to give "maxtokens" resulting tokens. Like
108  * strtok or strsep functions, this modifies its input string, by replacing
109  * instances of "delim" with '\0'. All resultant tokens are returned in the
110  * "tokens" array which must have enough entries to hold "maxtokens".
111  *
112  * @param string
113  *   The input string to be split into tokens
114  *
115  * @param stringlen
116  *   The max length of the input buffer
117  *
118  * @param tokens
119  *   The array to hold the pointers to the tokens in the string
120  *
121  * @param maxtokens
122  *   The number of elements in the tokens array. At most, maxtokens-1 splits
123  *   of the string will be done.
124  *
125  * @param delim
126  *   The character on which the split of the data will be done
127  *
128  * @return
129  *   The number of tokens in the tokens array.
130  */
131 static inline int
132 rte_strsplit(char *string, int stringlen,
133                 char **tokens, int maxtokens, char delim)
134 {
135         int i, tok = 0;
136         int tokstart = 1; /* first token is right at start of string */
137
138         if (string == NULL || tokens == NULL)
139                 goto einval_error;
140
141         for (i = 0; i < stringlen; i++) {
142                 if (string[i] == '\0' || tok >= maxtokens)
143                         break;
144                 if (tokstart) {
145                         tokstart = 0;
146                         tokens[tok++] = &string[i];
147                 }
148                 if (string[i] == delim) {
149                         string[i] = '\0';
150                         tokstart = 1;
151                 }
152         }
153         return tok;
154
155 einval_error:
156         errno = EINVAL;
157         return -1;
158 }
159
160 #ifdef __cplusplus
161 }
162 #endif
163
164
165 #endif /* RTE_STRING_FNS_H */