Main Page   Data Structures   File List   Data Fields   Globals   Examples  

confuse.h

Go to the documentation of this file.
00001 /* Configuration file parser -*- tab-width: 4; -*-
00002  *
00003  * Copyright (c) 2002-2003, Martin Hedenfalk <mhe@home.se>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00018  */
00019 
00039 #ifndef _cfg_h_
00040 #define _cfg_h_
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 #include <stdio.h>
00047 #include <stdarg.h>
00048 
00049 #ifdef _WIN32
00050 
00051 # ifdef HAVE__FILENO
00052 #  define fileno _fileno
00053 # endif
00054 # ifdef HAVE__ISATTY
00055 #  define isatty _isatty
00056 # endif
00057 
00058 # ifdef BUILDING_DLL
00059 #  define DLLIMPORT __declspec (dllexport)
00060 # else /* Not BUILDING_DLL */
00061 #  define DLLIMPORT __declspec (dllimport)
00062 # endif /* Not BUILDING_DLL */
00063 
00064 #else /* ! _WIN32 */
00065 # define DLLIMPORT
00066 
00067 #endif /* _WIN32 */
00068 
00069 #ifndef __BORLANDC__
00070 # define __export
00071 #endif
00072 
00074 enum cfg_type_t {
00075     CFGT_NONE,
00076     CFGT_INT,     
00077     CFGT_FLOAT,   
00078     CFGT_STR,     
00079     CFGT_BOOL,    
00080     CFGT_SEC,     
00081     CFGT_FUNC     
00082 };
00083 typedef enum cfg_type_t cfg_type_t;
00084 
00086 #define CFGF_NONE 0
00087 #define CFGF_MULTI 1       
00088 #define CFGF_LIST 2        
00089 #define CFGF_NOCASE 4      
00090 #define CFGF_TITLE 8       
00091 #define CFGF_ALLOCATED 16
00092 #define CFGF_RESET 32
00093 
00095 #define CFG_SUCCESS 0
00096 #define CFG_FILE_ERROR -1
00097 #define CFG_PARSE_ERROR 1
00098 
00099 #define is_set(f, x) ((f & x) == f)
00100 
00101 typedef union cfg_value_t cfg_value_t;
00102 typedef struct cfg_opt_t cfg_opt_t;
00103 typedef struct cfg_t cfg_t;
00104 typedef struct cfg_defvalue_t cfg_defvalue_t;
00105 typedef int cfg_flag_t;
00106 
00132 typedef int (*cfg_func_t)(cfg_t *cfg, cfg_opt_t *opt,
00133                           int argc, const char **argv);
00134 
00158 typedef int (*cfg_callback_t)(cfg_t *cfg, cfg_opt_t *opt,
00159                               const char *value, void *result);
00160 
00162 typedef enum {cfg_false, cfg_true} cfg_bool_t;
00163 
00165 typedef void (*cfg_errfunc_t)(cfg_t *cfg, const char *fmt, va_list ap);
00166 
00171 struct cfg_t {
00172     cfg_flag_t flags;       
00173     char *name;             
00176     cfg_opt_t *opts;        
00177     char *title;            
00179     char *filename;         
00180     int line;               
00181     cfg_errfunc_t errfunc;  
00184 };
00185 
00188 union cfg_value_t {
00189     long int number;        
00190     double fpnumber;        
00191     cfg_bool_t boolean;     
00192     char *string;           
00193     cfg_t *section;         
00194 };
00195 
00196 struct cfg_defvalue_t {
00197     long int number;
00198     double fpnumber;
00199     cfg_bool_t boolean;
00200     char *string;
00201     char *parsed;
00202 };
00203 
00207 struct cfg_opt_t {
00208     char *name;             
00209     cfg_type_t type;        
00210     unsigned int nvalues;   
00211     cfg_value_t **values;   
00212     cfg_flag_t flags;       
00213     cfg_opt_t *subopts;     
00214     cfg_defvalue_t def;     
00215     cfg_func_t func;        
00216     void *simple_value;     
00219     cfg_callback_t cb;      
00220 };
00221 
00222 extern const char __export confuse_copyright[];
00223 extern const char __export confuse_version[];
00224 extern const char __export confuse_author[];
00225 
00226 #define __CFG_STR(name, def, flags, svalue, cb) \
00227   {name,CFGT_STR,0,0,flags,0,{0,0,cfg_false,def,0},0,svalue,cb}
00228 #define __CFG_STR_LIST(name, def, flags, svalue, cb) \
00229   {name,CFGT_STR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00230 
00233 #define CFG_STR(name, def, flags) \
00234   __CFG_STR(name, def, flags, 0, 0)
00235 
00238 #define CFG_STR_LIST(name, def, flags) \
00239   __CFG_STR_LIST(name, def, flags, 0, 0)
00240 
00243 #define CFG_STR_CB(name, def, flags, cb) \
00244   __CFG_STR(name, def, flags, 0, cb)
00245 
00248 #define CFG_STR_LIST_CB(name, def, flags, cb) \
00249   __CFG_STR_LIST(name, def, flags, 0, cb)
00250 
00285 #define CFG_SIMPLE_STR(name, svalue) \
00286   __CFG_STR(name, 0, CFGF_NONE, svalue, 0)
00287 
00288 
00289 #define __CFG_INT(name, def, flags, svalue, cb) \
00290   {name,CFGT_INT,0,0,flags,0,{def,0,cfg_false,0,0},0,svalue,cb}
00291 #define __CFG_INT_LIST(name, def, flags, svalue, cb) \
00292   {name,CFGT_INT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00293 
00296 #define CFG_INT(name, def, flags) \
00297   __CFG_INT(name, def, flags, 0, 0)
00298 
00301 #define CFG_INT_LIST(name, def, flags) \
00302   __CFG_INT_LIST(name, def, flags, 0, 0)
00303 
00306 #define CFG_INT_CB(name, def, flags, cb) \
00307   __CFG_INT(name, def, flags, 0, cb)
00308 
00311 #define CFG_INT_LIST_CB(name, def, flags, cb) \
00312   __CFG_INT_LIST(name, def, flags, 0, cb)
00313 
00317 #define CFG_SIMPLE_INT(name, svalue) \
00318   __CFG_INT(name, 0, CFGF_NONE, svalue, 0)
00319 
00320 
00321 
00322 #define __CFG_FLOAT(name, def, flags, svalue, cb) \
00323   {name,CFGT_FLOAT,0,0,flags,0,{0,def,cfg_false,0,0},0,svalue,cb}
00324 #define __CFG_FLOAT_LIST(name, def, flags, svalue, cb) \
00325   {name,CFGT_FLOAT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00326 
00329 #define CFG_FLOAT(name, def, flags) \
00330   __CFG_FLOAT(name, def, flags, 0, 0)
00331 
00334 #define CFG_FLOAT_LIST(name, def, flags) \
00335   __CFG_FLOAT_LIST(name, def, flags, 0, 0)
00336 
00339 #define CFG_FLOAT_CB(name, def, flags, cb) \
00340   __CFG_FLOAT(name, def, flags, 0, cb)
00341 
00344 #define CFG_FLOAT_LIST_CB(name, def, flags, cb) \
00345   __CFG_FLOAT_LIST(name, def, flags, 0, cb)
00346 
00350 #define CFG_SIMPLE_FLOAT(name, svalue) \
00351   __CFG_FLOAT(name, 0, CFGF_NONE, svalue, 0)
00352 
00353 
00354 
00355 #define __CFG_BOOL(name, def, flags, svalue, cb) \
00356   {name,CFGT_BOOL,0,0,flags,0,{0,0,def,0,0},0,svalue,cb}
00357 #define __CFG_BOOL_LIST(name, def, flags, svalue, cb) \
00358   {name,CFGT_BOOL,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00359 
00362 #define CFG_BOOL(name, def, flags) \
00363   __CFG_BOOL(name, def, flags, 0, 0)
00364 
00367 #define CFG_BOOL_LIST(name, def, flags) \
00368   __CFG_BOOL_LIST(name, def, flags, 0, 0)
00369 
00372 #define CFG_BOOL_CB(name, def, flags, cb) \
00373   __CFG_BOOL(name, def, flags, 0, cb)
00374 
00377 #define CFG_BOOL_LIST_CB(name, def, flags, cb) \
00378   __CFG_BOOL_LIST(name, def, flags, 0, cb)
00379 
00383 #define CFG_SIMPLE_BOOL(name, svalue) \
00384   __CFG_BOOL(name, cfg_false, CFGF_NONE, svalue, 0)
00385 
00386 
00387 
00399 #define CFG_SEC(name, opts, flags) \
00400   {name,CFGT_SEC,0,0,flags,opts,{0,0,cfg_false,0,0},0,0,0}
00401 
00402 
00403 
00410 #define CFG_FUNC(name, func) \
00411   {name,CFGT_FUNC,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},func,0,0}
00412 
00413 
00414 
00418 #define CFG_END() \
00419    {0,CFGT_NONE,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},0,0,0}
00420 
00421 
00422 
00442 DLLIMPORT cfg_t * __export cfg_init(cfg_opt_t *opts, cfg_flag_t flags);
00443 
00457 DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename);
00458 
00469 DLLIMPORT int __export cfg_parse_fp(cfg_t *cfg, FILE *fp);
00470 
00479 DLLIMPORT int __export cfg_parse_buf(cfg_t *cfg, const char *buf);
00480 
00485 DLLIMPORT void __export cfg_free_value(cfg_opt_t *opt);
00486 
00490 DLLIMPORT void __export cfg_free(cfg_t *cfg);
00491 
00495 DLLIMPORT cfg_errfunc_t __export cfg_set_error_function(cfg_t *cfg,
00496                                                         cfg_errfunc_t errfunc);
00497 
00501 DLLIMPORT void __export cfg_error(cfg_t *cfg, const char *fmt, ...);
00502 
00512 DLLIMPORT long int __export cfg_getint(cfg_t *cfg, const char *name);
00513 
00522 DLLIMPORT double __export cfg_getfloat(cfg_t *cfg, const char *name);
00523 
00532 DLLIMPORT char * __export cfg_getstr(cfg_t *cfg, const char *name);
00533 
00542 DLLIMPORT cfg_bool_t __export cfg_getbool(cfg_t *cfg, const char *name);
00543 
00553 DLLIMPORT cfg_t * __export cfg_getsec(cfg_t *cfg, const char *name);
00554 
00563 DLLIMPORT unsigned int __export cfg_size(cfg_t *cfg, const char *name);
00564 
00571 DLLIMPORT long int __export cfg_getnint(cfg_t *cfg, const char *name,
00572                                         unsigned int index);
00573 
00580 DLLIMPORT double __export cfg_getnfloat(cfg_t *cfg, const char *name,
00581                                         unsigned int index);
00582 
00589 DLLIMPORT char * __export cfg_getnstr(cfg_t *cfg, const char *name,
00590                                       unsigned int index);
00591 
00599 DLLIMPORT cfg_bool_t __export cfg_getnbool(cfg_t *cfg, const char *name,
00600                                            unsigned int index);
00601 
00609 DLLIMPORT cfg_t * __export cfg_getnsec(cfg_t *cfg, const char *name,
00610                                        unsigned int index);
00611 
00619 DLLIMPORT cfg_t * __export cfg_gettsec(cfg_t *cfg, const char *name,
00620                                        const char *title);
00621 
00628 DLLIMPORT const char * __export cfg_title(cfg_t *cfg);
00629 
00635 DLLIMPORT int __export cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc,
00636                                    const char **argv);
00637 
00644 DLLIMPORT char * __export cfg_tilde_expand(const char *filename);
00645 
00653 DLLIMPORT int __export cfg_parse_boolean(const char *s);
00654 
00663 DLLIMPORT cfg_opt_t * __export cfg_getopt(cfg_t *cfg, const char *name);
00664 
00674 DLLIMPORT void __export cfg_opt_setnint(cfg_t *cfg, cfg_opt_t *opt,
00675                                         long int value, unsigned int index);
00676 
00683 DLLIMPORT void __export cfg_setint(cfg_t *cfg, const char *name,
00684                                    long int value);
00685 
00695 DLLIMPORT void __export cfg_setnint(cfg_t *cfg, const char *name,
00696                                     long int value, unsigned int index);
00697 
00707 DLLIMPORT void __export cfg_opt_setnfloat(cfg_t *cfg, cfg_opt_t *opt,
00708                                           double value, unsigned int index);
00709 
00716 DLLIMPORT void __export cfg_setfloat(cfg_t *cfg, const char *name,
00717                                      double value);
00718 
00728 DLLIMPORT void __export cfg_setnfloat(cfg_t *cfg, const char *name,
00729                                       double value, unsigned int index);
00730 
00740 DLLIMPORT void __export cfg_opt_setnbool(cfg_t *cfg, cfg_opt_t *opt,
00741                                          cfg_bool_t value, unsigned int index);
00742 
00749 DLLIMPORT void __export cfg_setbool(cfg_t *cfg, const char *name,
00750                                     cfg_bool_t value);
00751 
00761 DLLIMPORT void __export cfg_setnbool(cfg_t *cfg, const char *name,
00762                                      cfg_bool_t value, unsigned int index);
00763 
00774 DLLIMPORT void __export cfg_opt_setnstr(cfg_t *cfg, cfg_opt_t *opt,
00775                                         const char *value, unsigned int index);
00776 
00784 DLLIMPORT void __export cfg_setstr(cfg_t *cfg, const char *name,
00785                                    const char *value);
00786 
00797 DLLIMPORT void __export cfg_setnstr(cfg_t *cfg, const char *name,
00798                                     const char *value, unsigned int index);
00799 
00800 DLLIMPORT void __export cfg_setlist(cfg_t *cfg, const char *name,
00801                                     unsigned int nvalues, ...);
00802 
00803 
00804 DLLIMPORT void __export cfg_addlist(cfg_t *cfg, const char *name,
00805                                     unsigned int nvalues, ...);
00806 
00807 #ifdef __cplusplus
00808 }
00809 #endif
00810 
00811 #endif
00812