librnr  1.14.5
RoadNarrows Robotics Common Library 1
config.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Configuration parser declarations.
5  *
6  * The librnr config module implements a basic configuration file parser
7  * and database that provides a structure similar to what you would find on
8  * Microsoft Windows INI files.
9  *
10  * This file has been modified from the original source
11  * (\ref config_h_original_src "see below").
12  *
13  * \par Syntax:
14  * The configuration file consists of a series of sections, headed by a
15  * "[section]" header and followed by "key: value" or "key=value" entries.\n
16  * \n
17  * Comments start with the character '#'. The configuration parser wlll ignore
18  * the '#' and all trailing characters to the end of the current line.
19  *
20  * \sa
21  * \ref example_config under "Related Pages" for an example usage of
22  * configuration.
23  *
24  * \pkgsynopsis
25  * RoadNarrows Robotics Common Library 1
26  *
27  * \pkgcomponent{Library}
28  * librnr
29  *
30  * \pkgfile{rnr/config.h}
31  *
32  * \author Robin Knight (robin.knight@roadnarrows.com)
33  *
34  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
35  *
36  * \license{MIT}
37  *
38  * \EulaBegin
39  * See the README and EULA files for any copyright and licensing information.
40  * \EulaEnd
41  *
42  * <hr>
43  * \anchor config_h_original_src
44  * \par Original Source Comment Block
45  *
46  * \par Original Author
47  * Jon Travis (jtravis@p00p.org)
48  *
49  * \par Original Copyright
50  * (C) 1992-2002
51  *
52  * \par Original Header
53  * \verbatim
54  * camserv - An internet streaming picture application
55  *
56  * Copyright (C) 1999-2002 Jon Travis (jtravis@p00p.org)
57  *
58  * This program is free software; you can redistribute it and/or modify
59  * it under the terms of the GNU General Public License as published by
60  * the Free Software Foundation; either version 2 of the License, or
61  * (at your option) any later version.
62  *
63  * This program is distributed in the hope that it will be useful,
64  * but WITHOUT ANY WARRANTY; without even the implied warranty of
65  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66  * GNU General Public License for more details.
67  *
68  * You should have received a copy of the GNU General Public License
69  * along with this program; if not, write to the Free Software
70  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
71  * \endverbatim
72  *
73  * <hr>
74  */
75 ////////////////////////////////////////////////////////////////////////////////
76 
77 #ifndef _RNR_CONFIG_H
78 #define _RNR_CONFIG_H
79 
80 #include <stdio.h>
81 
82 #include "rnr/rnrconfig.h"
83 
85 
86 /*!
87  * Forward declaration of types, hiding implementation.
88  */
89 typedef struct config_t Config_T; ///< configuration database type
90 typedef struct config_section_t ConfigSection_T; ///< configuration section tpe
91 typedef struct config_iter_t ConfigIter_T; ///< configuration iterator type
92 
93 
94 // ...........................................................................
95 // Configuration Database Prototypes
96 // ...........................................................................
97 
98 extern Config_T *ConfigDbNew(const char *sMainName);
99 
100 extern void ConfigDbDelete(Config_T *pConfig);
101 
102 extern Config_T *ConfigDbRead(const char *sFileName);
103 
104 extern Config_T *ConfigDbReadFp(FILE *fp, const char *sFileName);
105 
106 extern const char *ConfigDbGetName(Config_T *pConfig);
107 
108 extern void ConfigDbSetName(Config_T *pConfig, const char *sMainName);
109 
110 extern void ConfigDbPrint(Config_T *pConfig, FILE *fp);
111 
112 
113 // ...........................................................................
114 // Configuration Section Prototypes.
115 // ...........................................................................
116 
117 extern ConfigSection_T *ConfigSectionNew(Config_T *pConfig,
118  const char *sSectionName);
119 
120 extern int ConfigSectionDelete(Config_T *pConfig,
121  ConfigSection_T *sSectionName);
122 
123 extern ConfigSection_T *ConfigSectionGet(Config_T *pConfig,
124  const char *sSectionName);
125 
126 extern void ConfigSectionPrint(ConfigSection_T *pSect, FILE *fp);
127 
128 
129 // ...........................................................................
130 // Configuration Get key=value Entry Prototypes and Inlines
131 // ...........................................................................
132 
133 extern const char *ConfigGetStr(Config_T *pConfig, const char *sSectionName,
134  const char *sKey);
135 
136 extern int ConfigGetLong(Config_T *pConfig, const char *sSectionName,
137  const char *sKey, long *pVal);
138 
139 extern int ConfigGetDouble(Config_T *pConfig, const char *sSectionName,
140  const char *sKey, double *pVal);
141 
142 /*!
143  * \brief Get and convert an int value from the configuration database.
144  *
145  * \param pConfig Configuration database.
146  * \param sSectionName Configuration section name.
147  * \param sKey Section key.
148  * \param pVal Converted value.
149  *
150  * \return Returns OK(0) on success, RC_ERROR(-1) on failure.
151  */
152 static inline int ConfigGetInt(Config_T *pConfig, const char *sSectionName,
153  const char *sKey, int *pVal)
154 {
155  return ConfigGetLong(pConfig, sSectionName, sKey, (long *)pVal);
156 }
157 
158 /*!
159  * \brief Get and convert an unsigned long value from the configuration
160  * database.
161  *
162  * \param pConfig Configuration database.
163  * \param sSectionName Configuration section name.
164  * \param sKey Section key.
165  * \param pVal Converted value.
166  *
167  * \return Returns OK(0) on success, RC_ERROR(-1) on failure.
168  */
169 static inline int ConfigGetULong(Config_T *pConfig, const char *sSectionName,
170  const char *sKey, unsigned long *pVal)
171 {
172  return ConfigGetLong(pConfig, sSectionName, sKey, (long *)pVal);
173 }
174 
175 /*!
176  * \brief Get a value from the configuration database or default.
177  *
178  * \param pConfig Configuration database.
179  * \param sSectionName Configuration section name.
180  * \param sKey Section key.
181  * \param sValDft Default value.
182  *
183  * \return
184  * Returns value found in database on success.\n
185  * Returns default value if no value found in database or on error.
186  */
187 static inline const char *ConfigGetStrDft(Config_T *pConfig,
188  const char *sSectionName,
189  const char *sKey,
190  const char *sValDft)
191 {
192  const char *sVal;
193 
194  return (sVal = ConfigGetStr(pConfig, sSectionName, sKey)) != NULL?
195  sVal: sValDft;
196 }
197 
198 // ...........................................................................
199 // Configuration Set key=value Entry Prototypes and Inlines
200 // ...........................................................................
201 
202 extern int ConfigSetStr(Config_T *pConfig, const char *sSectionName,
203  const char *sKey, const char *sVal);
204 
205 extern int ConfigSetLong(Config_T *pConfig, const char *sSectionName,
206  const char *sKey, long lVal);
207 
208 extern int ConfigSetULong(Config_T *pConfig, const char *sSectionName,
209  const char *sKey, unsigned long ulVal);
210 
211 extern int ConfigSetDouble(Config_T *pConfig, const char *sSectionName,
212  const char *sKey, double fVal);
213 
214 /*!
215  * \brief Set or update an int value in the configuration database.
216  *
217  * The long value is converted to a string prior to adding to the database.
218  *
219  * \param pConfig Configuration database.
220  * \param sSectionName Configuration section name.
221  * \param sKey Section key.
222  * \param iVal Section key int value.
223  *
224  * \return Returns OK(0) on success, RC_ERROR(-1) on failure.
225  */
226 static inline int ConfigSetInt(Config_T *pConfig, const char *sSectionName,
227  const char *sKey, int iVal)
228 {
229  return ConfigSetLong(pConfig, sSectionName, sKey, (long)iVal);
230 }
231 
232 
233 // ...........................................................................
234 // Configuration Delete Entry Functions
235 // ...........................................................................
236 
237 extern int ConfigDelete(Config_T *pConfig, const char *sSectionName,
238  const char *sKey);
239 
240 
241 // ...........................................................................
242 // Configuration Iterator Functions
243 // ...........................................................................
244 
245 extern ConfigIter_T *ConfigDbIterNew(Config_T *pConfig);
246 
248  const char *sSectionName);
249 
250 extern void ConfigIterDelete(ConfigIter_T *pIter);
251 
252 extern const char *ConfigIterNext(ConfigIter_T *pIter);
253 
255 
256 
257 #endif // _RNR_CONFIG_H
int ConfigSetDouble(Config_T *pConfig, const char *sSectionName, const char *sKey, double fVal)
Set or update a double value in the configuration database.
Definition: config.c:883
int ConfigSetLong(Config_T *pConfig, const char *sSectionName, const char *sKey, long lVal)
Set or update a long value in the configuration database.
Definition: config.c:841
int ConfigGetDouble(Config_T *pConfig, const char *sSectionName, const char *sKey, double *pVal)
Get and convert a double value from the configuration database.
Definition: config.c:759
void ConfigDbSetName(Config_T *pConfig, const char *sMainName)
Assign a new name for the configuration database.
Definition: config.c:498
int ConfigSetULong(Config_T *pConfig, const char *sSectionName, const char *sKey, unsigned long ulVal)
Set or update an unsigned long value in the configuration database.
Definition: config.c:862
int ConfigDelete(Config_T *pConfig, const char *sSectionName, const char *sKey)
Delete entry from the configuration database.
Definition: config.c:906
void ConfigDbPrint(Config_T *pConfig, FILE *fp)
Print configuration database to the output file stream.
Definition: config.c:514
ConfigSection_T * ConfigSectionGet(Config_T *pConfig, const char *sSectionName)
Get a section from the configuration database.
Definition: config.c:635
#define NULL
null pointer
Definition: rnrconfig.h:199
ConfigIter_T * ConfigDbIterNew(Config_T *pConfig)
Create a new configuration database iterator.
Definition: config.c:943
Config_T * ConfigDbNew(const char *sMainName)
Create and initialize a new empty configuration database.
Definition: config.c:243
static int ConfigGetULong(Config_T *pConfig, const char *sSectionName, const char *sKey, unsigned long *pVal)
Get and convert an unsigned long value from the configuration database.
Definition: config.h:169
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
int ConfigSectionDelete(Config_T *pConfig, ConfigSection_T *sSectionName)
Delete a section from the configuration database.
Definition: config.c:600
Config_T * ConfigDbReadFp(FILE *fp, const char *sFileName)
Create a new configuration database from a opened configuration file.
Definition: config.c:364
int ConfigGetLong(Config_T *pConfig, const char *sSectionName, const char *sKey, long *pVal)
Get and convert a long value from the configuration database.
Definition: config.c:714
RoadNarrows Robotics common configuration file.
static const char * ConfigGetStrDft(Config_T *pConfig, const char *sSectionName, const char *sKey, const char *sValDft)
Get a value from the configuration database or default.
Definition: config.h:187
void ConfigDbDelete(Config_T *pConfig)
Delete the configuration database.
Definition: config.c:295
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
const char * ConfigIterNext(ConfigIter_T *pIter)
Get the next value in iteration.
Definition: config.c:1004
void ConfigSectionPrint(ConfigSection_T *pSect, FILE *fp)
Print one section to the output file stream.
Definition: config.c:654
const char * ConfigGetStr(Config_T *pConfig, const char *sSectionName, const char *sKey)
Get a string value from the configuration database.
Definition: config.c:682
static int ConfigGetInt(Config_T *pConfig, const char *sSectionName, const char *sKey, int *pVal)
Get and convert an int value from the configuration database.
Definition: config.h:152
Config_T * ConfigDbRead(const char *sFileName)
Create a new configuration database from a configuration file.
Definition: config.c:326
void ConfigIterDelete(ConfigIter_T *pIter)
Delete a configuration iterator.
Definition: config.c:985
int ConfigSetStr(Config_T *pConfig, const char *sSectionName, const char *sKey, const char *sVal)
Set or update a string value in the configuration database.
Definition: config.c:809
static int ConfigSetInt(Config_T *pConfig, const char *sSectionName, const char *sKey, int iVal)
Set or update an int value in the configuration database.
Definition: config.h:226
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
ConfigSection_T * ConfigSectionNew(Config_T *pConfig, const char *sSectionName)
Add a new empty section to the configuration database.
Definition: config.c:547
ConfigIter_T * ConfigSectionIterNew(Config_T *pConfig, const char *sSectionName)
Create a new configuration section iterator.
Definition: config.c:963