kvargs: add a new library to parse key/value arguments
[dpdk.git] / lib / librte_kvargs / rte_kvargs.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _RTE_KVARGS_H_
36 #define _RTE_KVARGS_H_
37
38 /**
39  * @file
40  * RTE Argument parsing
41  *
42  * This module can be used to parse arguments whose format is
43  * key1=value1;key2=value2;key3=value3;...
44  *
45  * This file provides some helpers that are especially used by virtual
46  * ethernet devices at initialization for arguments parsing.
47  */
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /** Maximum number of key/value associations */
54 #define RTE_KVARGS_MAX 32
55
56 /** separator character used between each pair */
57 #define RTE_KVARGS_PAIRS_DELIM  ';'
58
59 /** separator character used between key and value */
60 #define RTE_KVARGS_KV_DELIM     '='
61
62 /** Type of callback function used by rte_kvargs_process() */
63 typedef int (*arg_handler_t)(char *value, void *opaque);
64
65 /** A key/value association */
66 struct rte_kvargs_pair {
67         char *key;      /**< the name (key) of the association  */
68         char *value;    /**< the value associated to that key */
69 };
70
71 /** Store a list of key/value associations */
72 struct rte_kvargs {
73         unsigned count; /**< number of entries in the list */
74         size_t size;    /**< maximum number of entries */
75         struct rte_kvargs_pair pairs[RTE_KVARGS_MAX]; /**< list of key/values */
76 };
77
78 /**
79  * Initialize an empty rte_kvargs structure
80  *
81  * Set the content of the rte_kvargs structure given as a parameter
82  * to an empty list of key/value.
83  *
84  * @param kvlist
85  *   The rte_kvargs structure
86  *
87  * @return
88  *   - 0 on success
89  *   - Negative on error
90  */
91 int rte_kvargs_init(struct rte_kvargs *kvlist);
92
93 /**
94  * Append key/value associations in a rte_kvargs structure from a string
95  *
96  * The function fills a rte_kvargs structure from a string whose format
97  * is key1=value1;key2=value2;...
98  * The key/value associations are appended to those which are already
99  * present in the rte_kvargs structure.
100  *
101  * @param kvlist
102  *   The rte_kvargs structure
103  * @param name
104  *   The name of the driver
105  * @param args
106  *   The input string containing the key/value associations
107  * @param valid_keys
108  *   A list of valid keys (table of const char *, the last must be NULL).
109  *   This argument is ignored if NULL
110  *
111  * @return
112  *   - 0 on success
113  *   - Negative on error
114  */
115 int rte_kvargs_parse(struct rte_kvargs *kvlist, const char *name,
116         const char *args, const char *valid_keys[]);
117
118 /**
119  * Call a handler function for each key/value matching the key
120  *
121  * For each key/value association that matches the given key, calls the
122  * handler function with the for a given arg_name passing the value on the
123  * dictionary for that key and a given extra argument.
124  *
125  * @param kvlist
126  *   The rte_kvargs structure
127  * @param key_match
128  *   The key on which the handler should be called
129  * @param handler
130  *   The function to call for each matching key
131  * @param opaque_arg
132  *   A pointer passed unchanged to the handler
133  *
134  * @return
135  *   - 0 on success
136  *   - Negative on error
137  */
138 int rte_kvargs_process(const struct rte_kvargs *kvlist,
139         const char *key_match, arg_handler_t handler, void *opaque_arg);
140
141 /**
142  * Count the number of associations matching the given key
143  *
144  * @param kvlist
145  *   The rte_kvargs structure
146  * @param key_match
147  *   The key that should match
148
149  * @return
150  *   The number of entries
151  */
152 unsigned rte_kvargs_count(const struct rte_kvargs *kvlist,
153         const char *key_match);
154
155 #ifdef __cplusplus
156 }
157 #endif
158
159 #endif