confuse: confuse.h Source File

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

# confuse.h

Go to the documentation of this file.

    00001 /*
    00002  * Copyright (c) 2002,2003,2007 Martin Hedenfalk <martin@bzero.se>
    00003  *
    00004  * Permission to use, copy, modify, and distribute this software for any
    00005  * purpose with or without fee is hereby granted, provided that the above
    00006  * copyright notice and this permission notice appear in all copies.
    00007  *
    00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    00011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    00013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    00014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    00015  */
    00016
    00036 #ifndef _cfg_h_
    00037 #define _cfg_h_
    00038
    00039 #ifdef __cplusplus
    00040 extern "C" {
    00041 #endif
    00042
    00043 #include <stdio.h>
    00044 #include <stdarg.h>
    00045
    00046 #if defined(_WIN32) && !defined(__GNUC__)
    00047 # ifdef HAVE__FILENO
    00048 #  define fileno _fileno
    00049 # endif
    00050 # include <io.h>
    00051 # ifdef HAVE__ISATTY
    00052 #  define isatty _isatty
    00053 # endif
    00054 # ifdef BUILDING_DLL
    00055 #  define DLLIMPORT __declspec (dllexport)
    00056 # else /* ! BUILDING_DLL */
    00057 #  define DLLIMPORT __declspec (dllimport)
    00058 # endif /* BUILDING_DLL */
    00059 #else /* ! _WIN32 || __GNUC__ */
    00060 # define DLLIMPORT
    00061 #endif /* _WIN32 */
    00062
    00063 #ifndef __BORLANDC__
    00064 # define __export
    00065 #endif
    00066
    00068 enum cfg_type_t {
    00069     CFGT_NONE,
    00070     CFGT_INT,
    00071     CFGT_FLOAT,
    00072     CFGT_STR,
    00073     CFGT_BOOL,
    00074     CFGT_SEC,
    00075     CFGT_FUNC,
    00076     CFGT_PTR
    00077 };
    00078 typedef enum cfg_type_t cfg_type_t;
    00079
    00081 #define CFGF_NONE 0
    00082 #define CFGF_MULTI 1
    00083 #define CFGF_LIST 2
    00084 #define CFGF_NOCASE 4
    00085 #define CFGF_TITLE 8
    00086 #define CFGF_NODEFAULT 16
    00087 #define CFGF_NO_TITLE_DUPES 32
    00091 #define CFGF_RESET 64
    00092 #define CFGF_DEFINIT 128
    00093
    00095 #define CFG_SUCCESS 0
    00096 #define CFG_FILE_ERROR -1
    00097 #define CFG_PARSE_ERROR 1
    00098
    00099 typedef union cfg_value_t cfg_value_t;
    00100 typedef struct cfg_opt_t cfg_opt_t;
    00101 typedef struct cfg_t cfg_t;
    00102 typedef struct cfg_defvalue_t cfg_defvalue_t;
    00103 typedef int cfg_flag_t;
    00104
    00130 typedef int (*cfg_func_t)(cfg_t *cfg, cfg_opt_t *opt,
    00131                           int argc, const char **argv);
    00132
    00153 typedef void (*cfg_print_func_t)(cfg_opt_t *opt, unsigned int index, FILE *fp);
    00154
    00176 typedef int (*cfg_callback_t)(cfg_t *cfg, cfg_opt_t *opt,
    00177                               const char *value, void *result);
    00178
    00192 typedef int (*cfg_validate_callback_t)(cfg_t *cfg, cfg_opt_t *opt);
    00193
    00202 typedef void (*cfg_free_func_t)(void *value);
    00203
    00205 typedef enum {cfg_false, cfg_true} cfg_bool_t;
    00206
    00208 typedef void (*cfg_errfunc_t)(cfg_t *cfg, const char *fmt, va_list ap);
    00209
    00214 struct cfg_t {
    00215     cfg_flag_t flags;
    00216     char *name;
    00219     cfg_opt_t *opts;
    00220     char *title;
    00222     char *filename;
    00223     int line;
    00224     cfg_errfunc_t errfunc;
    00227 };
    00228
    00231 union cfg_value_t {
    00232     long int number;
    00233     double fpnumber;
    00234     cfg_bool_t boolean;
    00235     char *string;
    00236     cfg_t *section;
    00237     void *ptr;
    00238 };
    00239
    00243 struct cfg_defvalue_t {
    00244     long int number;
    00245     double fpnumber;
    00246     cfg_bool_t boolean;
    00247     char *string;
    00248     char *parsed;
    00251 };
    00252
    00257 struct cfg_opt_t {
    00258     char *name;
    00259     cfg_type_t type;
    00260     unsigned int nvalues;
    00261     cfg_value_t **values;
    00262     cfg_flag_t flags;
    00263     cfg_opt_t *subopts;
    00264     cfg_defvalue_t def;
    00265     cfg_func_t func;
    00266     void *simple_value;
    00269     cfg_callback_t parsecb;
    00270     cfg_validate_callback_t validcb;
    00271     cfg_print_func_t pf;
    00272     cfg_free_func_t freecb; /***< user-defined memory release function */
    00273 };
    00274
    00275 extern const char __export confuse_copyright[];
    00276 extern const char __export confuse_version[];
    00277 extern const char __export confuse_author[];
    00278
    00279 #define __CFG_STR(name, def, flags, svalue, cb) \
    00280   {name,CFGT_STR,0,0,flags,0,{0,0,cfg_false,def,0},0,svalue,cb,0,0,0}
    00281 #define __CFG_STR_LIST(name, def, flags, svalue, cb) \
    00282   {name,CFGT_STR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00283
    00286 #define CFG_STR(name, def, flags) \
    00287   __CFG_STR(name, def, flags, 0, 0)
    00288
    00291 #define CFG_STR_LIST(name, def, flags) \
    00292   __CFG_STR_LIST(name, def, flags, 0, 0)
    00293
    00296 #define CFG_STR_CB(name, def, flags, cb) \
    00297   __CFG_STR(name, def, flags, 0, cb)
    00298
    00301 #define CFG_STR_LIST_CB(name, def, flags, cb) \
    00302   __CFG_STR_LIST(name, def, flags, 0, cb)
    00303
    00356 #define CFG_SIMPLE_STR(name, svalue) \
    00357   __CFG_STR(name, 0, CFGF_NONE, svalue, 0)
    00358
    00359
    00360 #define __CFG_INT(name, def, flags, svalue, cb) \
    00361   {name,CFGT_INT,0,0,flags,0,{def,0,cfg_false,0,0},0,svalue,cb,0,0,0}
    00362 #define __CFG_INT_LIST(name, def, flags, svalue, cb) \
    00363   {name,CFGT_INT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00364
    00367 #define CFG_INT(name, def, flags) \
    00368   __CFG_INT(name, def, flags, 0, 0)
    00369
    00372 #define CFG_INT_LIST(name, def, flags) \
    00373   __CFG_INT_LIST(name, def, flags, 0, 0)
    00374
    00377 #define CFG_INT_CB(name, def, flags, cb) \
    00378   __CFG_INT(name, def, flags, 0, cb)
    00379
    00382 #define CFG_INT_LIST_CB(name, def, flags, cb) \
    00383   __CFG_INT_LIST(name, def, flags, 0, cb)
    00384
    00388 #define CFG_SIMPLE_INT(name, svalue) \
    00389   __CFG_INT(name, 0, CFGF_NONE, svalue, 0)
    00390
    00391
    00392
    00393 #define __CFG_FLOAT(name, def, flags, svalue, cb) \
    00394   {name,CFGT_FLOAT,0,0,flags,0,{0,def,cfg_false,0,0},0,svalue,cb,0,0,0}
    00395 #define __CFG_FLOAT_LIST(name, def, flags, svalue, cb) \
    00396   {name,CFGT_FLOAT,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00397
    00400 #define CFG_FLOAT(name, def, flags) \
    00401   __CFG_FLOAT(name, def, flags, 0, 0)
    00402
    00405 #define CFG_FLOAT_LIST(name, def, flags) \
    00406   __CFG_FLOAT_LIST(name, def, flags, 0, 0)
    00407
    00410 #define CFG_FLOAT_CB(name, def, flags, cb) \
    00411   __CFG_FLOAT(name, def, flags, 0, cb)
    00412
    00415 #define CFG_FLOAT_LIST_CB(name, def, flags, cb) \
    00416   __CFG_FLOAT_LIST(name, def, flags, 0, cb)
    00417
    00421 #define CFG_SIMPLE_FLOAT(name, svalue) \
    00422   __CFG_FLOAT(name, 0, CFGF_NONE, svalue, 0)
    00423
    00424
    00425
    00426 #define __CFG_BOOL(name, def, flags, svalue, cb) \
    00427   {name,CFGT_BOOL,0,0,flags,0,{0,0,def,0,0},0,svalue,cb,0,0,0}
    00428 #define __CFG_BOOL_LIST(name, def, flags, svalue, cb) \
    00429   {name,CFGT_BOOL,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,cb,0,0,0}
    00430
    00433 #define CFG_BOOL(name, def, flags) \
    00434   __CFG_BOOL(name, def, flags, 0, 0)
    00435
    00438 #define CFG_BOOL_LIST(name, def, flags) \
    00439   __CFG_BOOL_LIST(name, def, flags, 0, 0)
    00440
    00443 #define CFG_BOOL_CB(name, def, flags, cb) \
    00444   __CFG_BOOL(name, def, flags, 0, cb)
    00445
    00448 #define CFG_BOOL_LIST_CB(name, def, flags, cb) \
    00449   __CFG_BOOL_LIST(name, def, flags, 0, cb)
    00450
    00454 #define CFG_SIMPLE_BOOL(name, svalue) \
    00455   __CFG_BOOL(name, cfg_false, CFGF_NONE, svalue, 0)
    00456
    00457
    00458
    00470 #define CFG_SEC(name, opts, flags) \
    00471   {name,CFGT_SEC,0,0,flags,opts,{0,0,cfg_false,0,0},0,0,0,0,0,0}
    00472
    00473
    00474
    00481 #define CFG_FUNC(name, func) \
    00482   {name,CFGT_FUNC,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},func,0,0,0,0,0}
    00483
    00484
    00485 #define __CFG_PTR(name, def, flags, svalue, parsecb, freecb) \
    00486   {name,CFGT_PTR,0,0,flags,0,{0,0,cfg_false,0,def},0,svalue,parsecb,0,0,freecb}
    00487 #define __CFG_PTR_LIST(name, def, flags, svalue, parsecb, freecb) \
    00488   {name,CFGT_PTR,0,0,flags | CFGF_LIST,0,{0,0,cfg_false,0,def},0,svalue,parsecb,0,0,freecb}
    00489
    00502 #define CFG_PTR_CB(name, def, flags, parsecb, freecb) \
    00503   __CFG_PTR(name, def, flags, 0, parsecb, freecb)
    00504
    00507 #define CFG_PTR_LIST_CB(name, def, flags, parsecb, freecb) \
    00508   __CFG_PTR(name, def, flags | CFGF_LIST, 0, parsecb, freecb)
    00509
    00510 /*#define CFG_SIMPLE_PTR(name, svalue, cb) \
    00511   __CFG_PTR(name, 0, 0, svalue, cb)*/
    00512
    00513
    00517 #define CFG_END() \
    00518    {0,CFGT_NONE,0,0,CFGF_NONE,0,{0,0,cfg_false,0,0},0,0,0,0,0,0}
    00519
    00520
    00521
    00538 DLLIMPORT cfg_t * __export cfg_init(cfg_opt_t *opts, cfg_flag_t flags);
    00539
    00553 DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename);
    00554
    00565 DLLIMPORT int __export cfg_parse_fp(cfg_t *cfg, FILE *fp);
    00566
    00575 DLLIMPORT int __export cfg_parse_buf(cfg_t *cfg, const char *buf);
    00576
    00582 DLLIMPORT void __export cfg_free_value(cfg_opt_t *opt);
    00583
    00587 DLLIMPORT void __export cfg_free(cfg_t *cfg);
    00588
    00592 DLLIMPORT cfg_errfunc_t __export cfg_set_error_function(cfg_t *cfg,
    00593                                                         cfg_errfunc_t errfunc);
    00594
    00598 DLLIMPORT void __export cfg_error(cfg_t *cfg, const char *fmt, ...);
    00599
    00605 DLLIMPORT signed long __export cfg_opt_getnint(cfg_opt_t *opt, unsigned int index);
    00606
    00613 DLLIMPORT long int __export cfg_getnint(cfg_t *cfg, const char *name,
    00614                                         unsigned int index);
    00615
    00625 DLLIMPORT long int __export cfg_getint(cfg_t *cfg, const char *name);
    00626
    00632 DLLIMPORT double __export cfg_opt_getnfloat(cfg_opt_t *opt, unsigned int index);
    00633
    00640 DLLIMPORT double __export cfg_getnfloat(cfg_t *cfg, const char *name,
    00641                                         unsigned int index);
    00642
    00651 DLLIMPORT double __export cfg_getfloat(cfg_t *cfg, const char *name);
    00652
    00658 DLLIMPORT char * __export cfg_opt_getnstr(cfg_opt_t *opt, unsigned int index);
    00659
    00666 DLLIMPORT char * __export cfg_getnstr(cfg_t *cfg, const char *name,
    00667                                       unsigned int index);
    00668
    00677 DLLIMPORT char * __export cfg_getstr(cfg_t *cfg, const char *name);
    00678
    00684 DLLIMPORT cfg_bool_t __export cfg_opt_getnbool(cfg_opt_t *opt, unsigned int index);
    00685
    00693 DLLIMPORT cfg_bool_t __export cfg_getnbool(cfg_t *cfg, const char *name,
    00694                                            unsigned int index);
    00695
    00704 DLLIMPORT cfg_bool_t __export cfg_getbool(cfg_t *cfg, const char *name);
    00705
    00706
    00707 DLLIMPORT void * __export cfg_opt_getnptr(cfg_opt_t *opt, unsigned int index);
    00708 DLLIMPORT void * __export cfg_getnptr(cfg_t *cfg, const char *name, unsigned int indx);
    00709
    00718 DLLIMPORT void * __export cfg_getptr(cfg_t *cfg, const char *name);
    00719
    00720
    00726 DLLIMPORT cfg_t * __export cfg_opt_getnsec(cfg_opt_t *opt, unsigned int index);
    00727
    00736 DLLIMPORT cfg_t * __export cfg_getnsec(cfg_t *cfg, const char *name,
    00737                                        unsigned int index);
    00738
    00746 DLLIMPORT cfg_t * __export cfg_opt_gettsec(cfg_opt_t *opt, const char *title);
    00747
    00757 DLLIMPORT cfg_t * __export cfg_gettsec(cfg_t *cfg, const char *name,
    00758                                        const char *title);
    00759
    00770 DLLIMPORT cfg_t * __export cfg_getsec(cfg_t *cfg, const char *name);
    00771
    00777 DLLIMPORT unsigned int __export cfg_opt_size(cfg_opt_t *opt);
    00778
    00791 DLLIMPORT unsigned int __export cfg_size(cfg_t *cfg, const char *name);
    00792
    00799 DLLIMPORT const char * __export cfg_title(cfg_t *cfg);
    00800
    00807 DLLIMPORT const char * __export cfg_name(cfg_t *cfg);
    00808
    00815 DLLIMPORT const char * __export cfg_opt_name(cfg_opt_t *opt);
    00816
    00822 DLLIMPORT int __export cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc,
    00823                                    const char **argv);
    00824
    00831 DLLIMPORT char * __export cfg_tilde_expand(const char *filename);
    00832
    00840 DLLIMPORT int __export cfg_parse_boolean(const char *s);
    00841
    00850 DLLIMPORT cfg_opt_t * __export cfg_getopt(cfg_t *cfg, const char *name);
    00851
    00860 DLLIMPORT void __export cfg_opt_setnint(cfg_opt_t *opt,
    00861                                         long int value, unsigned int index);
    00862
    00870 DLLIMPORT void __export cfg_setint(cfg_t *cfg, const char *name,
    00871                                    long int value);
    00872
    00882 DLLIMPORT void __export cfg_setnint(cfg_t *cfg, const char *name,
    00883                                     long int value, unsigned int index);
    00884
    00893 DLLIMPORT void __export cfg_opt_setnfloat(cfg_opt_t *opt,
    00894                                           double value, unsigned int index);
    00895
    00903 DLLIMPORT void __export cfg_setfloat(cfg_t *cfg, const char *name,
    00904                                      double value);
    00905
    00915 DLLIMPORT void __export cfg_setnfloat(cfg_t *cfg, const char *name,
    00916                                       double value, unsigned int index);
    00917
    00926 DLLIMPORT void __export cfg_opt_setnbool(cfg_opt_t *opt,
    00927                                          cfg_bool_t value, unsigned int index);
    00928
    00936 DLLIMPORT void __export cfg_setbool(cfg_t *cfg, const char *name,
    00937                                     cfg_bool_t value);
    00938
    00948 DLLIMPORT void __export cfg_setnbool(cfg_t *cfg, const char *name,
    00949                                      cfg_bool_t value, unsigned int index);
    00950
    00960 DLLIMPORT void __export cfg_opt_setnstr(cfg_opt_t *opt,
    00961                                         const char *value, unsigned int index);
    00962
    00971 DLLIMPORT void __export cfg_setstr(cfg_t *cfg, const char *name,
    00972                                    const char *value);
    00973
    00984 DLLIMPORT void __export cfg_setnstr(cfg_t *cfg, const char *name,
    00985                                     const char *value, unsigned int index);
    00986
    00997 DLLIMPORT void __export cfg_setlist(cfg_t *cfg, const char *name,
    00998                                     unsigned int nvalues, ...);
    00999
    01000 DLLIMPORT int __export cfg_numopts(cfg_opt_t *opts);
    01001
    01012 DLLIMPORT void __export cfg_addlist(cfg_t *cfg, const char *name,
    01013                                     unsigned int nvalues, ...);
    01014
    01027 DLLIMPORT void __export cfg_opt_nprint_var(cfg_opt_t *opt, unsigned int index,
    01028                                   FILE *fp);
    01029
    01034 DLLIMPORT void __export cfg_opt_print_indent(cfg_opt_t *opt, FILE *fp, int indent);
    01035
    01046 DLLIMPORT void __export cfg_opt_print(cfg_opt_t *opt, FILE *fp);
    01047
    01052 DLLIMPORT void __export cfg_print_indent(cfg_t *cfg, FILE *fp, int indent);
    01053
    01067 DLLIMPORT void __export cfg_print(cfg_t *cfg, FILE *fp);
    01068
    01076 DLLIMPORT cfg_print_func_t __export cfg_opt_set_print_func(cfg_opt_t *opt,
    01077                                                   cfg_print_func_t pf);
    01078
    01087 DLLIMPORT cfg_print_func_t __export cfg_set_print_func(cfg_t *cfg, const char *name,
    01088                                               cfg_print_func_t pf);
    01089
    01098 DLLIMPORT cfg_validate_callback_t __export cfg_set_validate_func(cfg_t *cfg,
    01099                                                         const char *name,
    01100                                                         cfg_validate_callback_t vf);
    01101
    01102 #ifdef __cplusplus
    01103 }
    01104 #endif
    01105
    01106 #endif
    01107