6763cdb735cc60eeed39cbd8abfc0e6f37844835
[collectd.git] / src / esl_config.h
1 /*
2  * Copyright (c) 2007, Anthony Minessale II
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 
16  * * Neither the name of the original author; nor the names of any contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  * 
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 OWNER
25  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /**
35  * @defgroup config Config File Parser
36  * @ingroup config
37  * This module implements a basic interface and file format parser
38  * 
39  * <pre>
40  *
41  * EXAMPLE 
42  * 
43  * [category1]
44  * var1 => val1
45  * var2 => val2
46  * \# lines that begin with \# are comments
47  * \#var3 => val3
48  * </pre>
49  * @{
50  */
51
52 #ifndef ESL_CONFIG_H
53 #define ESL_CONFIG_H
54
55 #include "esl.h"
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif /* defined(__cplusplus) */
60
61
62 #define ESL_URL_SEPARATOR "://"
63
64
65 #ifdef WIN32
66 #define ESL_PATH_SEPARATOR "\\"
67 #ifndef ESL_CONFIG_DIR
68 #define ESL_CONFIG_DIR "c:\\openesl"
69 #endif
70 #define esl_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR))
71 #else
72 #define ESL_PATH_SEPARATOR "/"
73 #ifndef ESL_CONFIG_DIR
74 #define ESL_CONFIG_DIR "/etc/openesl"
75 #endif
76 #define esl_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR))
77 #endif
78
79 /*!
80   \brief Evaluate the truthfullness of a string expression
81   \param expr a string expression
82   \return true or false 
83 */
84 #define esl_true(expr)\
85 (expr && ( !strcasecmp(expr, "yes") ||\
86 !strcasecmp(expr, "on") ||\
87 !strcasecmp(expr, "true") ||\
88 !strcasecmp(expr, "enabled") ||\
89 !strcasecmp(expr, "active") ||\
90 !strcasecmp(expr, "allow") ||\
91 atoi(expr))) ? 1 : 0
92
93 /*!
94   \brief Evaluate the falsefullness of a string expression
95   \param expr a string expression
96   \return true or false 
97 */
98 #define esl_false(expr)\
99 (expr && ( !strcasecmp(expr, "no") ||\
100 !strcasecmp(expr, "off") ||\
101 !strcasecmp(expr, "false") ||\
102 !strcasecmp(expr, "disabled") ||\
103 !strcasecmp(expr, "inactive") ||\
104 !strcasecmp(expr, "disallow") ||\
105 !atoi(expr))) ? 1 : 0
106
107 typedef struct esl_config esl_config_t;
108
109 /*! \brief A simple file handle representing an open configuration file **/
110 struct esl_config {
111         /*! FILE stream buffer to the opened file */
112         FILE *file;
113         /*! path to the file */
114         char path[512];
115         /*! current category */
116         char category[256];
117         /*! current section */
118         char section[256];
119         /*! buffer of current line being read */
120         char buf[1024];
121         /*! current line number in file */
122         int lineno;
123         /*! current category number in file */
124         int catno;
125         /*! current section number in file */
126         int sectno;
127
128         int lockto;
129 };
130
131 /*!
132   \brief Open a configuration file
133   \param cfg (esl_config_t *) config handle to use
134   \param file_path path to the file
135   \return 1 (true) on success 0 (false) on failure
136 */
137 ESL_DECLARE(int) esl_config_open_file(esl_config_t * cfg, const char *file_path);
138
139 /*!
140   \brief Close a previously opened configuration file
141   \param cfg (esl_config_t *) config handle to use
142 */
143 ESL_DECLARE(void) esl_config_close_file(esl_config_t * cfg);
144
145 /*!
146   \brief Retrieve next name/value pair from configuration file
147   \param cfg (esl_config_t *) config handle to use
148   \param var pointer to aim at the new variable name
149   \param val pointer to aim at the new value
150 */
151 ESL_DECLARE(int) esl_config_next_pair(esl_config_t * cfg, char **var, char **val);
152
153 /*!
154   \brief Retrieve the CAS bits from a configuration string value
155   \param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx)
156   \param outbits pointer to aim at the CAS bits
157 */
158 ESL_DECLARE(int) esl_config_get_cas_bits(char *strvalue, unsigned char *outbits);
159
160
161 /** @} */
162
163 #ifdef __cplusplus
164 }
165 #endif /* defined(__cplusplus) */
166
167 #endif /* defined(ESL_CONFIG_H) */
168
169 /* For Emacs:
170  * Local Variables:
171  * mode:c
172  * indent-tabs-mode:t
173  * tab-width:4
174  * c-basic-offset:4
175  * End:
176  * For VIM:
177  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
178  */