f2e9d3ff981259c0a79b121609d3b5793978084c
[platform/core/system/libsystem.git] / src / libsystem / config-parser.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4  * libsystem
5  *
6  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the License);
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 /**
22  * @file config-parser.h
23  *
24  * ini type config file parser
25  *
26  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
27  *
28  */
29
30 #pragma once
31
32 #include <stdio.h>
33 #ifndef __cplusplus
34 #include <stdbool.h>
35 #endif
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42  * Prototype for a parser for a specific configuration setting
43  */
44 typedef int (*ConfigParserCallback)(
45                 const char *filename,
46                 unsigned line,
47                 const char *section,
48                 const char *lvalue,
49                 int ltype,
50                 const char *rvalue,
51                 void *data);
52
53 /**
54  * @brief A callback function of #config_parse_dir.
55  *
56  * @param path a parsing config file name
57  * @param data user data to be passed by #config_parse_dir.
58  */
59 typedef int (*ConfigParseFunc)(const char *path, void *data);
60
61 /**
62  * Wraps information for parsing a specific configuration variable, to
63  * be stored in a simple array
64  */
65 typedef struct ConfigTableItem {
66         /**
67          * Section
68          */
69         const char *section;
70
71         /**
72          * Name of the variable
73          */
74         const char *lvalue;
75
76         /**
77          * Function that is called to parse the variable's value
78          */
79         ConfigParserCallback cb;
80
81         /**
82          * Distinguish different variables passed to the same callback
83          */
84         int ltype;
85
86         /**
87          * Where to store the variable's data
88          */
89         void *data;
90 } ConfigTableItem;
91
92 /**
93  * @brief config parser function
94  *
95  * @param filename full path of config file
96  * @param table a table of #ConfigTableItem to parse
97  *
98  * @return 0 on success, -errno on failure.
99  */
100 int config_parse(const char *filename, void *table);
101
102 /**
103  * @brief parse all regular config files in directory
104  *
105  * @param dir dir full path
106  * @param fp config parse function.
107  * @param data user data to be passed to config parser function
108  *
109  * @return 0 on success, -errno on failure.
110  */
111 int config_parse_dir(const char *dir, ConfigParseFunc fp, void *data);
112
113
114 /**
115  * @brief A common int type rvalue parser.
116  *
117  * @param filename a parsing config file name
118  * @param line a parsing config file line
119  * @param section a parsing config file section
120  * @param lvalue a parsing config file left value
121  * @param ltype a parsing config file left value type. (not used.)
122  * @param rvalue a parsing config file rvalue
123  * @param data user data
124  *
125  * @return 0 on success, -errno on failure.
126  */
127 int config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data);
128
129 /**
130  * @brief A common boolean type rvalue parser.
131  *
132  * @param filename a parsing config file name
133  * @param line a parsing config file line
134  * @param section a parsing config file section
135  * @param lvalue a parsing config file left value
136  * @param ltype a parsing config file left value type. (not used.)
137  * @param rvalue a parsing config file rvalue
138  * @param data user data
139  *
140  * @return 0 on success, -errno on failure.
141  */
142 int config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data);
143
144 /**
145  * @brief A common string type rvalue parser.
146  *
147  * @param filename a parsing config file name
148  * @param line a parsing config file line
149  * @param section a parsing config file section
150  * @param lvalue a parsing config file left value
151  * @param ltype a parsing config file left value type. (not used.)
152  * @param rvalue a parsing config file rvalue
153  * @param data user data
154  *
155  * @return 0 on success, -errno on failure.
156  */
157 int config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data);
158
159 /**
160  * @brief A common byte type rvalue parser.
161  *
162  * @param filename a parsing config file name
163  * @param line a parsing config file line
164  * @param section a parsing config file section
165  * @param lvalue a parsing config file left value
166  * @param ltype a parsing config file left value type. (not used.)
167  * @param rvalue a parsing config file rvalue
168  * @param data user data
169  *
170  * @return 0 on success, -errno on failure.
171  */
172 int config_parse_bytes(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data);
173
174 #ifdef __cplusplus
175 }
176 #endif