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.13 2003/09/23 14:59:21 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 
00157 typedef void (*cfg_print_func_t)(cfg_opt_t *opt, unsigned int index, FILE *fp);
00158     
00182 typedef int (*cfg_callback_t)(cfg_t *cfg, cfg_opt_t *opt,
00183                               const char *value, void *result);
00184 
00200 typedef int (*cfg_validate_callback_t)(cfg_t *cfg, cfg_opt_t *opt);
00201 
00203 typedef enum {cfg_false, cfg_true} cfg_bool_t;
00204 
00206 typedef void (*cfg_errfunc_t)(cfg_t *cfg, const char *fmt, va_list ap);
00207 
00212 struct cfg_t {
00213     cfg_flag_t flags;       
00214     char *name;             
00217     cfg_opt_t *opts;        
00218     char *title;            
00220     char *filename;         
00221     int line;               
00222     cfg_errfunc_t errfunc;  
00225 };
00226 
00229 union cfg_value_t {
00230     long int number;        
00231     double fpnumber;        
00232     cfg_bool_t boolean;     
00233     char *string;           
00234     cfg_t *section;         
00235 };
00236 
00240 struct cfg_defvalue_t {
00241     long int number;        
00242     double fpnumber;        
00243     cfg_bool_t boolean;     
00244     char *string;           
00245     char *parsed;           
00248 };
00249 
00254 struct cfg_opt_t {
00255     char *name;             
00256     cfg_type_t type;        
00257     unsigned int nvalues;   
00258     cfg_value_t **values;   
00259     cfg_flag_t flags;       
00260     cfg_opt_t *subopts;     
00261     cfg_defvalue_t def;     
00262     cfg_func_t func;        
00263     void *simple_value;     
00266     cfg_callback_t parsecb; 
00267     cfg_validate_callback_t validcb; 
00268     cfg_print_func_t pf;    
00269 };
00270 
00271 extern const char __export confuse_copyright[];
00272 extern const char __export confuse_version[];
00273 extern const char __export confuse_author[];
00274 
00275 #define __CFG_STR(name, def, flags, svalue, cb) \
00276   {name,CFGT_STR,0,0,flags,0,{0,0,cfg_false,def,0},0,svalue,cb,0,0}
00277 #define __CFG_STR_LIST(name, def, flags, svalue, cb) \
00278   {name,CFGT_STR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0}
00279 
00282 #define CFG_STR(name, def, flags) \
00283   __CFG_STR(name, def, flags, 0, 0)
00284 
00287 #define CFG_STR_LIST(name, def, flags) \
00288   __CFG_STR_LIST(name, def, flags, 0, 0)
00289 
00292 #define CFG_STR_CB(name, def, flags, cb) \
00293   __CFG_STR(name, def, flags, 0, cb)
00294 
00297 #define CFG_STR_LIST_CB(name, def, flags, cb) \
00298   __CFG_STR_LIST(name, def, flags, 0, cb)
00299 
00352 #define CFG_SIMPLE_STR(name, svalue) \
00353   __CFG_STR(name, 0, CFGF_NONE, svalue, 0)
00354 
00355 
00356 #define __CFG_INT(name, def, flags, svalue, cb) \
00357   {name,CFGT_INT,0,0,flags,0,{def,0,cfg_false,0,0},0,svalue,cb,0,0}
00358 #define __CFG_INT_LIST(name, def, flags, svalue, cb) \
00359   {name,CFGT_INT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0}
00360 
00363 #define CFG_INT(name, def, flags) \
00364   __CFG_INT(name, def, flags, 0, 0)
00365 
00368 #define CFG_INT_LIST(name, def, flags) \
00369   __CFG_INT_LIST(name, def, flags, 0, 0)
00370 
00373 #define CFG_INT_CB(name, def, flags, cb) \
00374   __CFG_INT(name, def, flags, 0, cb)
00375 
00378 #define CFG_INT_LIST_CB(name, def, flags, cb) \
00379   __CFG_INT_LIST(name, def, flags, 0, cb)
00380 
00384 #define CFG_SIMPLE_INT(name, svalue) \
00385   __CFG_INT(name, 0, CFGF_NONE, svalue, 0)
00386 
00387 
00388 
00389 #define __CFG_FLOAT(name, def, flags, svalue, cb) \
00390   {name,CFGT_FLOAT,0,0,flags,0,{0,def,cfg_false,0,0},0,svalue,cb,0,0}
00391 #define __CFG_FLOAT_LIST(name, def, flags, svalue, cb) \
00392   {name,CFGT_FLOAT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0}
00393 
00396 #define CFG_FLOAT(name, def, flags) \
00397   __CFG_FLOAT(name, def, flags, 0, 0)
00398 
00401 #define CFG_FLOAT_LIST(name, def, flags) \
00402   __CFG_FLOAT_LIST(name, def, flags, 0, 0)
00403 
00406 #define CFG_FLOAT_CB(name, def, flags, cb) \
00407   __CFG_FLOAT(name, def, flags, 0, cb)
00408 
00411 #define CFG_FLOAT_LIST_CB(name, def, flags, cb) \
00412   __CFG_FLOAT_LIST(name, def, flags, 0, cb)
00413 
00417 #define CFG_SIMPLE_FLOAT(name, svalue) \
00418   __CFG_FLOAT(name, 0, CFGF_NONE, svalue, 0)
00419 
00420 
00421 
00422 #define __CFG_BOOL(name, def, flags, svalue, cb) \
00423   {name,CFGT_BOOL,0,0,flags,0,{0,0,def,0,0},0,svalue,cb,0,0}
00424 #define __CFG_BOOL_LIST(name, def, flags, svalue, cb) \
00425   {name,CFGT_BOOL,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0}
00426 
00429 #define CFG_BOOL(name, def, flags) \
00430   __CFG_BOOL(name, def, flags, 0, 0)
00431 
00434 #define CFG_BOOL_LIST(name, def, flags) \
00435   __CFG_BOOL_LIST(name, def, flags, 0, 0)
00436 
00439 #define CFG_BOOL_CB(name, def, flags, cb) \
00440   __CFG_BOOL(name, def, flags, 0, cb)
00441 
00444 #define CFG_BOOL_LIST_CB(name, def, flags, cb) \
00445   __CFG_BOOL_LIST(name, def, flags, 0, cb)
00446 
00450 #define CFG_SIMPLE_BOOL(name, svalue) \
00451   __CFG_BOOL(name, cfg_false, CFGF_NONE, svalue, 0)
00452 
00453 
00454 
00466 #define CFG_SEC(name, opts, flags) \
00467   {name,CFGT_SEC,0,0,flags,opts,{0,0,cfg_false,0,0},0,0,0,0,0}
00468 
00469 
00470 
00477 #define CFG_FUNC(name, func) \
00478   {name,CFGT_FUNC,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},func,0,0,0,0}
00479 
00480 
00481 
00485 #define CFG_END() \
00486    {0,CFGT_NONE,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},0,0,0,0,0}
00487 
00488 
00489 
00509 DLLIMPORT cfg_t * __export cfg_init(cfg_opt_t *opts, cfg_flag_t flags);
00510 
00524 DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename);
00525 
00536 DLLIMPORT int __export cfg_parse_fp(cfg_t *cfg, FILE *fp);
00537 
00546 DLLIMPORT int __export cfg_parse_buf(cfg_t *cfg, const char *buf);
00547 
00552 DLLIMPORT void __export cfg_free_value(cfg_opt_t *opt);
00553 
00557 DLLIMPORT void __export cfg_free(cfg_t *cfg);
00558 
00562 DLLIMPORT cfg_errfunc_t __export cfg_set_error_function(cfg_t *cfg,
00563                                                         cfg_errfunc_t errfunc);
00564 
00568 DLLIMPORT void __export cfg_error(cfg_t *cfg, const char *fmt, ...);
00569 
00575 DLLIMPORT signed long cfg_opt_getnint(cfg_opt_t *opt, unsigned int index);
00576 
00583 DLLIMPORT long int __export cfg_getnint(cfg_t *cfg, const char *name,
00584                                         unsigned int index);
00585 
00595 DLLIMPORT long int __export cfg_getint(cfg_t *cfg, const char *name);
00596 
00602 DLLIMPORT double cfg_opt_getnfloat(cfg_opt_t *opt, unsigned int index);
00603 
00610 DLLIMPORT double __export cfg_getnfloat(cfg_t *cfg, const char *name,
00611                                         unsigned int index);
00612 
00621 DLLIMPORT double __export cfg_getfloat(cfg_t *cfg, const char *name);
00622 
00628 DLLIMPORT char *cfg_opt_getnstr(cfg_opt_t *opt, unsigned int index);
00629 
00636 DLLIMPORT char * __export cfg_getnstr(cfg_t *cfg, const char *name,
00637                                       unsigned int index);
00638 
00647 DLLIMPORT char * __export cfg_getstr(cfg_t *cfg, const char *name);
00648 
00654 DLLIMPORT cfg_bool_t cfg_opt_getnbool(cfg_opt_t *opt, unsigned int index);
00655     
00663 DLLIMPORT cfg_bool_t __export cfg_getnbool(cfg_t *cfg, const char *name,
00664                                            unsigned int index);
00665 
00674 DLLIMPORT cfg_bool_t __export cfg_getbool(cfg_t *cfg, const char *name);
00675 
00681 DLLIMPORT cfg_t *cfg_opt_getnsec(cfg_opt_t *opt, unsigned int index);
00682 
00691 DLLIMPORT cfg_t * __export cfg_getnsec(cfg_t *cfg, const char *name,
00692                                        unsigned int index);
00693 
00701 DLLIMPORT cfg_t *cfg_opt_gettsec(cfg_opt_t *opt, const char *title);
00702 
00712 DLLIMPORT cfg_t * __export cfg_gettsec(cfg_t *cfg, const char *name,
00713                                        const char *title);
00714 
00725 DLLIMPORT cfg_t * __export cfg_getsec(cfg_t *cfg, const char *name);
00726 
00732 DLLIMPORT unsigned int cfg_opt_size(cfg_opt_t *opt);
00733 
00741 DLLIMPORT unsigned int __export cfg_size(cfg_t *cfg, const char *name);
00742 
00749 DLLIMPORT const char * __export cfg_title(cfg_t *cfg);
00750 
00756 DLLIMPORT int __export cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc,
00757                                    const char **argv);
00758 
00765 DLLIMPORT char * __export cfg_tilde_expand(const char *filename);
00766 
00774 DLLIMPORT int __export cfg_parse_boolean(const char *s);
00775 
00785 DLLIMPORT cfg_opt_t * __export cfg_getopt(cfg_t *cfg, const char *name);
00786 
00795 DLLIMPORT void __export cfg_opt_setnint(cfg_opt_t *opt,
00796                                         long int value, unsigned int index);
00797 
00804 DLLIMPORT void __export cfg_setint(cfg_t *cfg, const char *name,
00805                                    long int value);
00806 
00816 DLLIMPORT void __export cfg_setnint(cfg_t *cfg, const char *name,
00817                                     long int value, unsigned int index);
00818 
00827 DLLIMPORT void __export cfg_opt_setnfloat(cfg_opt_t *opt,
00828                                           double value, unsigned int index);
00829 
00836 DLLIMPORT void __export cfg_setfloat(cfg_t *cfg, const char *name,
00837                                      double value);
00838 
00848 DLLIMPORT void __export cfg_setnfloat(cfg_t *cfg, const char *name,
00849                                       double value, unsigned int index);
00850 
00859 DLLIMPORT void __export cfg_opt_setnbool(cfg_opt_t *opt,
00860                                          cfg_bool_t value, unsigned int index);
00861 
00868 DLLIMPORT void __export cfg_setbool(cfg_t *cfg, const char *name,
00869                                     cfg_bool_t value);
00870 
00880 DLLIMPORT void __export cfg_setnbool(cfg_t *cfg, const char *name,
00881                                      cfg_bool_t value, unsigned int index);
00882 
00892 DLLIMPORT void __export cfg_opt_setnstr(cfg_opt_t *opt,
00893                                         const char *value, unsigned int index);
00894 
00902 DLLIMPORT void __export cfg_setstr(cfg_t *cfg, const char *name,
00903                                    const char *value);
00904 
00915 DLLIMPORT void __export cfg_setnstr(cfg_t *cfg, const char *name,
00916                                     const char *value, unsigned int index);
00917 
00928 DLLIMPORT void __export cfg_setlist(cfg_t *cfg, const char *name,
00929                                     unsigned int nvalues, ...);
00930 
00931 
00942 DLLIMPORT void __export cfg_addlist(cfg_t *cfg, const char *name,
00943                                     unsigned int nvalues, ...);
00944 
00957 DLLIMPORT void cfg_opt_nprint_var(cfg_opt_t *opt, unsigned int index,
00958                                   FILE *fp);
00959 
00964 DLLIMPORT void cfg_opt_print_indent(cfg_opt_t *opt, FILE *fp, int indent);
00965 
00976 DLLIMPORT void cfg_opt_print(cfg_opt_t *opt, FILE *fp);
00977 
00982 DLLIMPORT void cfg_print_indent(cfg_t *cfg, FILE *fp, int indent);
00983 
00997 DLLIMPORT void cfg_print(cfg_t *cfg, FILE *fp);
00998 
01006 DLLIMPORT cfg_print_func_t cfg_opt_set_print_func(cfg_opt_t *opt,
01007                                                   cfg_print_func_t pf);
01008 
01017 DLLIMPORT cfg_print_func_t cfg_set_print_func(cfg_t *cfg, const char *name,
01018                                               cfg_print_func_t pf);
01019 
01028 DLLIMPORT cfg_validate_callback_t cfg_set_validate_func(cfg_t *cfg,
01029                                                         const char *name,
01030                                                    cfg_validate_callback_t vf);
01031 
01032 #ifdef __cplusplus
01033 }
01034 #endif
01035 
01036 #endif
01037