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  * $Id: confuse.h,v 1.9 2003/07/13 14:28:07 mhe Exp $
00004  *
00005  * Copyright (c) 2002-2003, Martin Hedenfalk <mhe@home.se>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  */
00021 
00041 #ifndef _cfg_h_
00042 #define _cfg_h_
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 #include <stdio.h>
00049 #include <stdarg.h>
00050 
00051 #ifdef _WIN32
00052 
00053 # ifdef HAVE__FILENO
00054 #  define fileno _fileno
00055 # endif
00056 # include <io.h>
00057 # ifdef HAVE__ISATTY
00058 #  define isatty _isatty
00059 # endif
00060 
00061 # ifdef BUILDING_DLL
00062 #  define DLLIMPORT __declspec (dllexport)
00063 # else /* Not BUILDING_DLL */
00064 #  define DLLIMPORT __declspec (dllimport)
00065 # endif /* Not BUILDING_DLL */
00066 
00067 #else /* ! _WIN32 */
00068 # define DLLIMPORT
00069 
00070 #endif /* _WIN32 */
00071 
00072 #ifndef __BORLANDC__
00073 # define __export
00074 #endif
00075 
00077 enum cfg_type_t {
00078     CFGT_NONE,
00079     CFGT_INT,     
00080     CFGT_FLOAT,   
00081     CFGT_STR,     
00082     CFGT_BOOL,    
00083     CFGT_SEC,     
00084     CFGT_FUNC     
00085 };
00086 typedef enum cfg_type_t cfg_type_t;
00087 
00089 #define CFGF_NONE 0
00090 #define CFGF_MULTI 1       
00091 #define CFGF_LIST 2        
00092 #define CFGF_NOCASE 4      
00093 #define CFGF_TITLE 8       
00094 #define CFGF_ALLOCATED 16
00095 #define CFGF_RESET 32
00096 #define CFGF_DEFINIT 64
00097 
00099 #define CFG_SUCCESS 0
00100 #define CFG_FILE_ERROR -1
00101 #define CFG_PARSE_ERROR 1
00102 
00103 typedef union cfg_value_t cfg_value_t;
00104 typedef struct cfg_opt_t cfg_opt_t;
00105 typedef struct cfg_t cfg_t;
00106 typedef struct cfg_defvalue_t cfg_defvalue_t;
00107 typedef int cfg_flag_t;
00108 
00134 typedef int (*cfg_func_t)(cfg_t *cfg, cfg_opt_t *opt,
00135                           int argc, const char **argv);
00136 
00160 typedef int (*cfg_callback_t)(cfg_t *cfg, cfg_opt_t *opt,
00161                               const char *value, void *result);
00162 
00164 typedef enum {cfg_false, cfg_true} cfg_bool_t;
00165 
00167 typedef void (*cfg_errfunc_t)(cfg_t *cfg, const char *fmt, va_list ap);
00168 
00173 struct cfg_t {
00174     cfg_flag_t flags;       
00175     char *name;             
00178     cfg_opt_t *opts;        
00179     char *title;            
00181     char *filename;         
00182     int line;               
00183     cfg_errfunc_t errfunc;  
00186 };
00187 
00190 union cfg_value_t {
00191     long int number;        
00192     double fpnumber;        
00193     cfg_bool_t boolean;     
00194     char *string;           
00195     cfg_t *section;         
00196 };
00197 
00201 struct cfg_defvalue_t {
00202     long int number;        
00203     double fpnumber;        
00204     cfg_bool_t boolean;     
00205     char *string;           
00206     char *parsed;           
00209 };
00210 
00215 struct cfg_opt_t {
00216     char *name;             
00217     cfg_type_t type;        
00218     unsigned int nvalues;   
00219     cfg_value_t **values;   
00220     cfg_flag_t flags;       
00221     cfg_opt_t *subopts;     
00222     cfg_defvalue_t def;     
00223     cfg_func_t func;        
00224     void *simple_value;     
00227     cfg_callback_t cb;      
00228 };
00229 
00230 extern const char __export confuse_copyright[];
00231 extern const char __export confuse_version[];
00232 extern const char __export confuse_author[];
00233 
00234 #define __CFG_STR(name, def, flags, svalue, cb) \
00235   {name,CFGT_STR,0,0,flags,0,{0,0,cfg_false,def,0},0,svalue,cb}
00236 #define __CFG_STR_LIST(name, def, flags, svalue, cb) \
00237   {name,CFGT_STR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00238 
00241 #define CFG_STR(name, def, flags) \
00242   __CFG_STR(name, def, flags, 0, 0)
00243 
00246 #define CFG_STR_LIST(name, def, flags) \
00247   __CFG_STR_LIST(name, def, flags, 0, 0)
00248 
00251 #define CFG_STR_CB(name, def, flags, cb) \
00252   __CFG_STR(name, def, flags, 0, cb)
00253 
00256 #define CFG_STR_LIST_CB(name, def, flags, cb) \
00257   __CFG_STR_LIST(name, def, flags, 0, cb)
00258 
00293 #define CFG_SIMPLE_STR(name, svalue) \
00294   __CFG_STR(name, 0, CFGF_NONE, svalue, 0)
00295 
00296 
00297 #define __CFG_INT(name, def, flags, svalue, cb) \
00298   {name,CFGT_INT,0,0,flags,0,{def,0,cfg_false,0,0},0,svalue,cb}
00299 #define __CFG_INT_LIST(name, def, flags, svalue, cb) \
00300   {name,CFGT_INT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00301 
00304 #define CFG_INT(name, def, flags) \
00305   __CFG_INT(name, def, flags, 0, 0)
00306 
00309 #define CFG_INT_LIST(name, def, flags) \
00310   __CFG_INT_LIST(name, def, flags, 0, 0)
00311 
00314 #define CFG_INT_CB(name, def, flags, cb) \
00315   __CFG_INT(name, def, flags, 0, cb)
00316 
00319 #define CFG_INT_LIST_CB(name, def, flags, cb) \
00320   __CFG_INT_LIST(name, def, flags, 0, cb)
00321 
00325 #define CFG_SIMPLE_INT(name, svalue) \
00326   __CFG_INT(name, 0, CFGF_NONE, svalue, 0)
00327 
00328 
00329 
00330 #define __CFG_FLOAT(name, def, flags, svalue, cb) \
00331   {name,CFGT_FLOAT,0,0,flags,0,{0,def,cfg_false,0,0},0,svalue,cb}
00332 #define __CFG_FLOAT_LIST(name, def, flags, svalue, cb) \
00333   {name,CFGT_FLOAT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00334 
00337 #define CFG_FLOAT(name, def, flags) \
00338   __CFG_FLOAT(name, def, flags, 0, 0)
00339 
00342 #define CFG_FLOAT_LIST(name, def, flags) \
00343   __CFG_FLOAT_LIST(name, def, flags, 0, 0)
00344 
00347 #define CFG_FLOAT_CB(name, def, flags, cb) \
00348   __CFG_FLOAT(name, def, flags, 0, cb)
00349 
00352 #define CFG_FLOAT_LIST_CB(name, def, flags, cb) \
00353   __CFG_FLOAT_LIST(name, def, flags, 0, cb)
00354 
00358 #define CFG_SIMPLE_FLOAT(name, svalue) \
00359   __CFG_FLOAT(name, 0, CFGF_NONE, svalue, 0)
00360 
00361 
00362 
00363 #define __CFG_BOOL(name, def, flags, svalue, cb) \
00364   {name,CFGT_BOOL,0,0,flags,0,{0,0,def,0,0},0,svalue,cb}
00365 #define __CFG_BOOL_LIST(name, def, flags, svalue, cb) \
00366   {name,CFGT_BOOL,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb}
00367 
00370 #define CFG_BOOL(name, def, flags) \
00371   __CFG_BOOL(name, def, flags, 0, 0)
00372 
00375 #define CFG_BOOL_LIST(name, def, flags) \
00376   __CFG_BOOL_LIST(name, def, flags, 0, 0)
00377 
00380 #define CFG_BOOL_CB(name, def, flags, cb) \
00381   __CFG_BOOL(name, def, flags, 0, cb)
00382 
00385 #define CFG_BOOL_LIST_CB(name, def, flags, cb) \
00386   __CFG_BOOL_LIST(name, def, flags, 0, cb)
00387 
00391 #define CFG_SIMPLE_BOOL(name, svalue) \
00392   __CFG_BOOL(name, cfg_false, CFGF_NONE, svalue, 0)
00393 
00394 
00395 
00407 #define CFG_SEC(name, opts, flags) \
00408   {name,CFGT_SEC,0,0,flags,opts,{0,0,cfg_false,0,0},0,0,0}
00409 
00410 
00411 
00418 #define CFG_FUNC(name, func) \
00419   {name,CFGT_FUNC,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},func,0,0}
00420 
00421 
00422 
00426 #define CFG_END() \
00427    {0,CFGT_NONE,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},0,0,0}
00428 
00429 
00430 
00450 DLLIMPORT cfg_t * __export cfg_init(cfg_opt_t *opts, cfg_flag_t flags);
00451 
00465 DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename);
00466 
00477 DLLIMPORT int __export cfg_parse_fp(cfg_t *cfg, FILE *fp);
00478 
00487 DLLIMPORT int __export cfg_parse_buf(cfg_t *cfg, const char *buf);
00488 
00493 DLLIMPORT void __export cfg_free_value(cfg_opt_t *opt);
00494 
00498 DLLIMPORT void __export cfg_free(cfg_t *cfg);
00499 
00503 DLLIMPORT cfg_errfunc_t __export cfg_set_error_function(cfg_t *cfg,
00504                                                         cfg_errfunc_t errfunc);
00505 
00509 DLLIMPORT void __export cfg_error(cfg_t *cfg, const char *fmt, ...);
00510 
00520 DLLIMPORT long int __export cfg_getint(cfg_t *cfg, const char *name);
00521 
00530 DLLIMPORT double __export cfg_getfloat(cfg_t *cfg, const char *name);
00531 
00540 DLLIMPORT char * __export cfg_getstr(cfg_t *cfg, const char *name);
00541 
00550 DLLIMPORT cfg_bool_t __export cfg_getbool(cfg_t *cfg, const char *name);
00551 
00562 DLLIMPORT cfg_t * __export cfg_getsec(cfg_t *cfg, const char *name);
00563 
00571 DLLIMPORT unsigned int __export cfg_size(cfg_t *cfg, const char *name);
00572 
00579 DLLIMPORT long int __export cfg_getnint(cfg_t *cfg, const char *name,
00580                                         unsigned int index);
00581 
00588 DLLIMPORT double __export cfg_getnfloat(cfg_t *cfg, const char *name,
00589                                         unsigned int index);
00590 
00597 DLLIMPORT char * __export cfg_getnstr(cfg_t *cfg, const char *name,
00598                                       unsigned int index);
00599 
00607 DLLIMPORT cfg_bool_t __export cfg_getnbool(cfg_t *cfg, const char *name,
00608                                            unsigned int index);
00609 
00618 DLLIMPORT cfg_t * __export cfg_getnsec(cfg_t *cfg, const char *name,
00619                                        unsigned int index);
00620 
00629 DLLIMPORT cfg_t * __export cfg_gettsec(cfg_t *cfg, const char *name,
00630                                        const char *title);
00631 
00638 DLLIMPORT const char * __export cfg_title(cfg_t *cfg);
00639 
00645 DLLIMPORT int __export cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc,
00646                                    const char **argv);
00647 
00654 DLLIMPORT char * __export cfg_tilde_expand(const char *filename);
00655 
00663 DLLIMPORT int __export cfg_parse_boolean(const char *s);
00664 
00674 DLLIMPORT cfg_opt_t * __export cfg_getopt(cfg_t *cfg, const char *name);
00675 
00684 DLLIMPORT void __export cfg_opt_setnint(cfg_opt_t *opt,
00685                                         long int value, unsigned int index);
00686 
00693 DLLIMPORT void __export cfg_setint(cfg_t *cfg, const char *name,
00694                                    long int value);
00695 
00705 DLLIMPORT void __export cfg_setnint(cfg_t *cfg, const char *name,
00706                                     long int value, unsigned int index);
00707 
00716 DLLIMPORT void __export cfg_opt_setnfloat(cfg_opt_t *opt,
00717                                           double value, unsigned int index);
00718 
00725 DLLIMPORT void __export cfg_setfloat(cfg_t *cfg, const char *name,
00726                                      double value);
00727 
00737 DLLIMPORT void __export cfg_setnfloat(cfg_t *cfg, const char *name,
00738                                       double value, unsigned int index);
00739 
00748 DLLIMPORT void __export cfg_opt_setnbool(cfg_opt_t *opt,
00749                                          cfg_bool_t value, unsigned int index);
00750 
00757 DLLIMPORT void __export cfg_setbool(cfg_t *cfg, const char *name,
00758                                     cfg_bool_t value);
00759 
00769 DLLIMPORT void __export cfg_setnbool(cfg_t *cfg, const char *name,
00770                                      cfg_bool_t value, unsigned int index);
00771 
00781 DLLIMPORT void __export cfg_opt_setnstr(cfg_opt_t *opt,
00782                                         const char *value, unsigned int index);
00783 
00791 DLLIMPORT void __export cfg_setstr(cfg_t *cfg, const char *name,
00792                                    const char *value);
00793 
00804 DLLIMPORT void __export cfg_setnstr(cfg_t *cfg, const char *name,
00805                                     const char *value, unsigned int index);
00806 
00817 DLLIMPORT void __export cfg_setlist(cfg_t *cfg, const char *name,
00818                                     unsigned int nvalues, ...);
00819 
00820 
00831 DLLIMPORT void __export cfg_addlist(cfg_t *cfg, const char *name,
00832                                     unsigned int nvalues, ...);
00833 
00834 #ifdef __cplusplus
00835 }
00836 #endif
00837 
00838 #endif
00839