confuse.h Source File

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.19 2004/10/17 16:57:00 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     CFGT_PTR
    00086 };
    00087 typedef enum cfg_type_t cfg_type_t;
    00088
    00090 #define CFGF_NONE 0
    00091 #define CFGF_MULTI 1
    00092 #define CFGF_LIST 2
    00093 #define CFGF_NOCASE 4
    00094 #define CFGF_TITLE 8
    00095 #define CFGF_NODEFAULT 16
    00096 #define CFGF_RESET 32
    00097 #define CFGF_DEFINIT 64
    00098
    00100 #define CFG_SUCCESS 0
    00101 #define CFG_FILE_ERROR -1
    00102 #define CFG_PARSE_ERROR 1
    00103
    00104 typedef union cfg_value_t cfg_value_t;
    00105 typedef struct cfg_opt_t cfg_opt_t;
    00106 typedef struct cfg_t cfg_t;
    00107 typedef struct cfg_defvalue_t cfg_defvalue_t;
    00108 typedef int cfg_flag_t;
    00109
    00135 typedef int (*cfg_func_t)(cfg_t *cfg, cfg_opt_t *opt,
    00136                           int argc, const char **argv);
    00137
    00158 typedef void (*cfg_print_func_t)(cfg_opt_t *opt, unsigned int index, FILE *fp);
    00159
    00181 typedef int (*cfg_callback_t)(cfg_t *cfg, cfg_opt_t *opt,
    00182                               const char *value, void *result);
    00183
    00197 typedef int (*cfg_validate_callback_t)(cfg_t *cfg, cfg_opt_t *opt);
    00198
    00207 typedef void (*cfg_free_func_t)(void *value);
    00208
    00210 typedef enum {cfg_false, cfg_true} cfg_bool_t;
    00211
    00213 typedef void (*cfg_errfunc_t)(cfg_t *cfg, const char *fmt, va_list ap);
    00214
    00219 struct cfg_t {
    00220     cfg_flag_t flags;
    00221     char *name;
    00224     cfg_opt_t *opts;
    00225     char *title;
    00227     char *filename;
    00228     int line;
    00229     cfg_errfunc_t errfunc;
    00232 };
    00233
    00236 union cfg_value_t {
    00237     long int number;
    00238     double fpnumber;
    00239     cfg_bool_t boolean;
    00240     char *string;
    00241     cfg_t *section;
    00242     void *ptr;
    00243 };
    00244
    00248 struct cfg_defvalue_t {
    00249     long int number;
    00250     double fpnumber;
    00251     cfg_bool_t boolean;
    00252     char *string;
    00253     char *parsed;
    00256 };
    00257
    00262 struct cfg_opt_t {
    00263     char *name;
    00264     cfg_type_t type;
    00265     unsigned int nvalues;
    00266     cfg_value_t **values;
    00267     cfg_flag_t flags;
    00268     cfg_opt_t *subopts;
    00269     cfg_defvalue_t def;
    00270     cfg_func_t func;
    00271     void *simple_value;
    00274     cfg_callback_t parsecb;
    00275     cfg_validate_callback_t validcb;
    00276     cfg_print_func_t pf;
    00277     cfg_free_func_t freecb; /***< user-defined memory release function */
    00278 };
    00279
    00280 extern const char __export confuse_copyright[];
    00281 extern const char __export confuse_version[];
    00282 extern const char __export confuse_author[];
    00283
    00284 #define __CFG_STR(name, def, flags, svalue, cb) \
    00285   {name,CFGT_STR,0,0,flags,0,{0,0,cfg_false,def,0},0,svalue,cb,0,0,0}
    00286 #define __CFG_STR_LIST(name, def, flags, svalue, cb) \
    00287   {name,CFGT_STR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00288
    00291 #define CFG_STR(name, def, flags) \
    00292   __CFG_STR(name, def, flags, 0, 0)
    00293
    00296 #define CFG_STR_LIST(name, def, flags) \
    00297   __CFG_STR_LIST(name, def, flags, 0, 0)
    00298
    00301 #define CFG_STR_CB(name, def, flags, cb) \
    00302   __CFG_STR(name, def, flags, 0, cb)
    00303
    00306 #define CFG_STR_LIST_CB(name, def, flags, cb) \
    00307   __CFG_STR_LIST(name, def, flags, 0, cb)
    00308
    00361 #define CFG_SIMPLE_STR(name, svalue) \
    00362   __CFG_STR(name, 0, CFGF_NONE, svalue, 0)
    00363
    00364
    00365 #define __CFG_INT(name, def, flags, svalue, cb) \
    00366   {name,CFGT_INT,0,0,flags,0,{def,0,cfg_false,0,0},0,svalue,cb,0,0,0}
    00367 #define __CFG_INT_LIST(name, def, flags, svalue, cb) \
    00368   {name,CFGT_INT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00369
    00372 #define CFG_INT(name, def, flags) \
    00373   __CFG_INT(name, def, flags, 0, 0)
    00374
    00377 #define CFG_INT_LIST(name, def, flags) \
    00378   __CFG_INT_LIST(name, def, flags, 0, 0)
    00379
    00382 #define CFG_INT_CB(name, def, flags, cb) \
    00383   __CFG_INT(name, def, flags, 0, cb)
    00384
    00387 #define CFG_INT_LIST_CB(name, def, flags, cb) \
    00388   __CFG_INT_LIST(name, def, flags, 0, cb)
    00389
    00393 #define CFG_SIMPLE_INT(name, svalue) \
    00394   __CFG_INT(name, 0, CFGF_NONE, svalue, 0)
    00395
    00396
    00397
    00398 #define __CFG_FLOAT(name, def, flags, svalue, cb) \
    00399   {name,CFGT_FLOAT,0,0,flags,0,{0,def,cfg_false,0,0},0,svalue,cb,0,0,0}
    00400 #define __CFG_FLOAT_LIST(name, def, flags, svalue, cb) \
    00401   {name,CFGT_FLOAT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00402
    00405 #define CFG_FLOAT(name, def, flags) \
    00406   __CFG_FLOAT(name, def, flags, 0, 0)
    00407
    00410 #define CFG_FLOAT_LIST(name, def, flags) \
    00411   __CFG_FLOAT_LIST(name, def, flags, 0, 0)
    00412
    00415 #define CFG_FLOAT_CB(name, def, flags, cb) \
    00416   __CFG_FLOAT(name, def, flags, 0, cb)
    00417
    00420 #define CFG_FLOAT_LIST_CB(name, def, flags, cb) \
    00421   __CFG_FLOAT_LIST(name, def, flags, 0, cb)
    00422
    00426 #define CFG_SIMPLE_FLOAT(name, svalue) \
    00427   __CFG_FLOAT(name, 0, CFGF_NONE, svalue, 0)
    00428
    00429
    00430
    00431 #define __CFG_BOOL(name, def, flags, svalue, cb) \
    00432   {name,CFGT_BOOL,0,0,flags,0,{0,0,def,0,0},0,svalue,cb,0,0,0}
    00433 #define __CFG_BOOL_LIST(name, def, flags, svalue, cb) \
    00434   {name,CFGT_BOOL,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00435
    00438 #define CFG_BOOL(name, def, flags) \
    00439   __CFG_BOOL(name, def, flags, 0, 0)
    00440
    00443 #define CFG_BOOL_LIST(name, def, flags) \
    00444   __CFG_BOOL_LIST(name, def, flags, 0, 0)
    00445
    00448 #define CFG_BOOL_CB(name, def, flags, cb) \
    00449   __CFG_BOOL(name, def, flags, 0, cb)
    00450
    00453 #define CFG_BOOL_LIST_CB(name, def, flags, cb) \
    00454   __CFG_BOOL_LIST(name, def, flags, 0, cb)
    00455
    00459 #define CFG_SIMPLE_BOOL(name, svalue) \
    00460   __CFG_BOOL(name, cfg_false, CFGF_NONE, svalue, 0)
    00461
    00462
    00463
    00475 #define CFG_SEC(name, opts, flags) \
    00476   {name,CFGT_SEC,0,0,flags,opts,{0,0,cfg_false,0,0},0,0,0,0,0,0}
    00477
    00478
    00479
    00486 #define CFG_FUNC(name, func) \
    00487   {name,CFGT_FUNC,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},func,0,0,0,0,0}
    00488
    00489
    00490 #define __CFG_PTR(name, def, flags, svalue, parsecb, freecb) \
    00491   {name,CFGT_PTR,0,0,flags,0,{0,0,cfg_false,0,def},0,svalue,parsecb,0,0,freecb}
    00492 #define __CFG_PTR_LIST(name, def, flags, svalue, parsecb, freecb) \
    00493   {name,CFGT_PTR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,parsecb,0,0,freecb}
    00494
    00507 #define CFG_PTR_CB(name, def, flags, parsecb, freecb) \
    00508   __CFG_PTR(name, def, flags, 0, parsecb, freecb)
    00509
    00512 #define CFG_PTR_LIST_CB(name, def, flags, parsecb, freecb) \
    00513   __CFG_PTR(name, def, flags | CFGF_LIST, 0, parsecb, freecb)
    00514
    00515 /*#define CFG_SIMPLE_PTR(name, svalue, cb) \
    00516   __CFG_PTR(name, 0, 0, svalue, cb)*/
    00517
    00518
    00522 #define CFG_END() \
    00523    {0,CFGT_NONE,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},0,0,0,0,0,0}
    00524
    00525
    00526
    00543 DLLIMPORT cfg_t * __export cfg_init(cfg_opt_t *opts, cfg_flag_t flags);
    00544
    00558 DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename);
    00559
    00570 DLLIMPORT int __export cfg_parse_fp(cfg_t *cfg, FILE *fp);
    00571
    00580 DLLIMPORT int __export cfg_parse_buf(cfg_t *cfg, const char *buf);
    00581
    00587 DLLIMPORT void __export cfg_free_value(cfg_opt_t *opt);
    00588
    00592 DLLIMPORT void __export cfg_free(cfg_t *cfg);
    00593
    00597 DLLIMPORT cfg_errfunc_t __export cfg_set_error_function(cfg_t *cfg,
    00598                                                         cfg_errfunc_t errfunc);
    00599
    00603 DLLIMPORT void __export cfg_error(cfg_t *cfg, const char *fmt, ...);
    00604
    00610 DLLIMPORT signed long cfg_opt_getnint(cfg_opt_t *opt, unsigned int index);
    00611
    00618 DLLIMPORT long int __export cfg_getnint(cfg_t *cfg, const char *name,
    00619                                         unsigned int index);
    00620
    00630 DLLIMPORT long int __export cfg_getint(cfg_t *cfg, const char *name);
    00631
    00637 DLLIMPORT double cfg_opt_getnfloat(cfg_opt_t *opt, unsigned int index);
    00638
    00645 DLLIMPORT double __export cfg_getnfloat(cfg_t *cfg, const char *name,
    00646                                         unsigned int index);
    00647
    00656 DLLIMPORT double __export cfg_getfloat(cfg_t *cfg, const char *name);
    00657
    00663 DLLIMPORT char *cfg_opt_getnstr(cfg_opt_t *opt, unsigned int index);
    00664
    00671 DLLIMPORT char * __export cfg_getnstr(cfg_t *cfg, const char *name,
    00672                                       unsigned int index);
    00673
    00682 DLLIMPORT char * __export cfg_getstr(cfg_t *cfg, const char *name);
    00683
    00689 DLLIMPORT cfg_bool_t cfg_opt_getnbool(cfg_opt_t *opt, unsigned int index);
    00690
    00698 DLLIMPORT cfg_bool_t __export cfg_getnbool(cfg_t *cfg, const char *name,
    00699                                            unsigned int index);
    00700
    00709 DLLIMPORT cfg_bool_t __export cfg_getbool(cfg_t *cfg, const char *name);
    00710
    00711
    00712 DLLIMPORT void *cfg_opt_getnptr(cfg_opt_t *opt, unsigned int index);
    00713 DLLIMPORT void *cfg_getnptr(cfg_t *cfg, const char *name, unsigned int indx);
    00714
    00723 DLLIMPORT void *cfg_getptr(cfg_t *cfg, const char *name);
    00724
    00725
    00731 DLLIMPORT cfg_t *cfg_opt_getnsec(cfg_opt_t *opt, unsigned int index);
    00732
    00741 DLLIMPORT cfg_t * __export cfg_getnsec(cfg_t *cfg, const char *name,
    00742                                        unsigned int index);
    00743
    00751 DLLIMPORT cfg_t *cfg_opt_gettsec(cfg_opt_t *opt, const char *title);
    00752
    00762 DLLIMPORT cfg_t * __export cfg_gettsec(cfg_t *cfg, const char *name,
    00763                                        const char *title);
    00764
    00775 DLLIMPORT cfg_t * __export cfg_getsec(cfg_t *cfg, const char *name);
    00776
    00782 DLLIMPORT unsigned int cfg_opt_size(cfg_opt_t *opt);
    00783
    00796 DLLIMPORT unsigned int __export cfg_size(cfg_t *cfg, const char *name);
    00797
    00804 DLLIMPORT const char * __export cfg_title(cfg_t *cfg);
    00805
    00812 DLLIMPORT const char *cfg_name(cfg_t *cfg);
    00813
    00820 DLLIMPORT const char *cfg_opt_name(cfg_opt_t *opt);
    00821
    00827 DLLIMPORT int __export cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc,
    00828                                    const char **argv);
    00829
    00836 DLLIMPORT char * __export cfg_tilde_expand(const char *filename);
    00837
    00845 DLLIMPORT int __export cfg_parse_boolean(const char *s);
    00846
    00855 DLLIMPORT cfg_opt_t * __export cfg_getopt(cfg_t *cfg, const char *name);
    00856
    00865 DLLIMPORT void __export cfg_opt_setnint(cfg_opt_t *opt,
    00866                                         long int value, unsigned int index);
    00867
    00875 DLLIMPORT void __export cfg_setint(cfg_t *cfg, const char *name,
    00876                                    long int value);
    00877
    00887 DLLIMPORT void __export cfg_setnint(cfg_t *cfg, const char *name,
    00888                                     long int value, unsigned int index);
    00889
    00898 DLLIMPORT void __export cfg_opt_setnfloat(cfg_opt_t *opt,
    00899                                           double value, unsigned int index);
    00900
    00908 DLLIMPORT void __export cfg_setfloat(cfg_t *cfg, const char *name,
    00909                                      double value);
    00910
    00920 DLLIMPORT void __export cfg_setnfloat(cfg_t *cfg, const char *name,
    00921                                       double value, unsigned int index);
    00922
    00931 DLLIMPORT void __export cfg_opt_setnbool(cfg_opt_t *opt,
    00932                                          cfg_bool_t value, unsigned int index);
    00933
    00941 DLLIMPORT void __export cfg_setbool(cfg_t *cfg, const char *name,
    00942                                     cfg_bool_t value);
    00943
    00953 DLLIMPORT void __export cfg_setnbool(cfg_t *cfg, const char *name,
    00954                                      cfg_bool_t value, unsigned int index);
    00955
    00965 DLLIMPORT void __export cfg_opt_setnstr(cfg_opt_t *opt,
    00966                                         const char *value, unsigned int index);
    00967
    00976 DLLIMPORT void __export cfg_setstr(cfg_t *cfg, const char *name,
    00977                                    const char *value);
    00978
    00989 DLLIMPORT void __export cfg_setnstr(cfg_t *cfg, const char *name,
    00990                                     const char *value, unsigned int index);
    00991
    01002 DLLIMPORT void __export cfg_setlist(cfg_t *cfg, const char *name,
    01003                                     unsigned int nvalues, ...);
    01004
    01005
    01016 DLLIMPORT void __export cfg_addlist(cfg_t *cfg, const char *name,
    01017                                     unsigned int nvalues, ...);
    01018
    01031 DLLIMPORT void cfg_opt_nprint_var(cfg_opt_t *opt, unsigned int index,
    01032                                   FILE *fp);
    01033
    01038 DLLIMPORT void cfg_opt_print_indent(cfg_opt_t *opt, FILE *fp, int indent);
    01039
    01050 DLLIMPORT void cfg_opt_print(cfg_opt_t *opt, FILE *fp);
    01051
    01056 DLLIMPORT void cfg_print_indent(cfg_t *cfg, FILE *fp, int indent);
    01057
    01071 DLLIMPORT void cfg_print(cfg_t *cfg, FILE *fp);
    01072
    01080 DLLIMPORT cfg_print_func_t cfg_opt_set_print_func(cfg_opt_t *opt,
    01081                                                   cfg_print_func_t pf);
    01082
    01091 DLLIMPORT cfg_print_func_t cfg_set_print_func(cfg_t *cfg, const char *name,
    01092                                               cfg_print_func_t pf);
    01093
    01102 DLLIMPORT cfg_validate_callback_t cfg_set_validate_func(cfg_t *cfg,
    01103                                                         const char *name,
    01104                                                         cfg_validate_callback_t vf);
    01105
    01106 #ifdef __cplusplus
    01107 }
    01108 #endif
    01109
    01110 #endif
    01111