Main Page | Data Structures | File List | Data Fields | Globals | Examples

Deprecation Notice

This example file is not included with libConfuse-2.7. Please see the Examples listing for files relevent to the current release of libConfuse. This file is left in place to prevent 404s.


cfgtest.c

#include "confuse.h" #include <string.h> void print_func(cfg_opt_t *opt, unsigned int index, FILE *fp) { fprintf(fp, "%s(foo)", opt->name); } void print_ask(cfg_opt_t *opt, unsigned int index, FILE *fp) { int value = cfg_opt_getnint(opt, index); switch(value) { case 1: fprintf(fp, "yes"); break; case 2: fprintf(fp, "no"); break; case 3: default: fprintf(fp, "maybe"); break; } } /* function callback */ int cb_func(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv) { int i; /* at least one parameter is required */ if(argc == 0) { cfg_error(cfg, "Too few parameters for the '%s' function", opt->name); return -1; } printf("cb_func() called with %d parameters:\n", argc); for(i = 0; i < argc; i++) printf("parameter %d: '%s'\n", i, argv[i]); return 0; } /* value parsing callback * * VALUE must be "yes", "no" or "maybe", and the corresponding results * are the integers 1, 2 and 3. */ int cb_verify_ask(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) { if(strcmp(value, "yes") == 0) *(long int *)result = 1; else if(strcmp(value, "no") == 0) *(long int *)result = 2; else if(strcmp(value, "maybe") == 0) *(long int *)result = 3; else { cfg_error(cfg, "Invalid value for option %s: %s", opt->name, value); return -1; } return 0; } int cb_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt) { /* only validate the last bookmark */ cfg_t *sec = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1); if(!sec) { cfg_error(cfg, "section is NULL!?"); return -1; } if(cfg_getstr(sec, "machine") == 0) { cfg_error(cfg, "machine option must be set for bookmark '%s'", cfg_title(sec)); return -1; } return 0; } int main(int argc, char **argv) { unsigned int i; cfg_t *cfg; unsigned n; int ret; char *pc; static cfg_opt_t proxy_opts[] = { CFG_INT("type", 0, CFGF_NONE), CFG_STR("host", 0, CFGF_NONE), CFG_STR_LIST("exclude", "{localhost, .localnet}", CFGF_NONE), CFG_INT("port", 21, CFGF_NONE), CFG_END() }; static cfg_opt_t bookmark_opts[] = { CFG_STR("machine", 0, CFGF_NONE), CFG_INT("port", 21, CFGF_NONE), CFG_STR("login", 0, CFGF_NONE), CFG_STR("password", 0, CFGF_NONE), CFG_STR("directory", 0, CFGF_NONE), CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), CFG_SEC("proxy", proxy_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT("backlog", 42, CFGF_NONE), CFG_STR("probe-device", "eth2", CFGF_NONE), CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), CFG_FLOAT_LIST("delays", "{3.567e2, 0.2, -47.11}", CFGF_NONE), CFG_FUNC("func", &cb_func), CFG_INT_CB("ask-quit", 3, CFGF_NONE, &cb_verify_ask), CFG_INT_LIST_CB("ask-quit-array", "{maybe, yes, no}", CFGF_NONE, &cb_verify_ask), CFG_FUNC("include", &cfg_include), CFG_END() }; #ifndef _WIN32 /* for some reason, MS Visual C++ chokes on this (?) */ printf("Using %s\n\n", confuse_copyright); #endif cfg = cfg_init(opts, CFGF_NOCASE); /* set a validating callback function for bookmark sections */ cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark); ret = cfg_parse(cfg, argc > 1 ? argv[1] : "test.conf"); printf("ret == %d\n", ret); if(ret == CFG_FILE_ERROR) { perror("test.conf"); return 1; } else if(ret == CFG_PARSE_ERROR) { fprintf(stderr, "parse error\n"); return 2; } printf("backlog == %ld\n", cfg_getint(cfg, "backlog")); printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); cfg_setstr(cfg, "probe-device", "lo"); printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); n = cfg_size(cfg, "bookmark"); printf("%d configured bookmarks:\n", n); for(i = 0; i < n; i++) { cfg_t *pxy; cfg_t *bm = cfg_getnsec(cfg, "bookmark", i); printf(" bookmark #%u (%s):\n", i+1, cfg_title(bm)); printf(" machine = %s\n", cfg_getstr(bm, "machine")); printf(" port = %d\n", (int)cfg_getint(bm, "port")); printf(" login = %s\n", cfg_getstr(bm, "login")); printf(" passive-mode = %s\n", cfg_getbool(bm, "passive-mode") ? "true" : "false"); pc = cfg_getstr(bm, "directory"); printf(" directory = %s\n",pc == NULL ? "(nil)" : pc ); pc = cfg_getstr(bm, "password"); printf(" password = %s\n", pc == NULL ? "(nil)" : pc); pxy = cfg_getsec(bm, "proxy"); if(pxy) { int j, m; if(cfg_getstr(pxy, "host") == 0) { printf(" no proxy host is set, setting it to 'localhost'...\n"); /* For sections without CFGF_MULTI flag set, there is * also an extended syntax to get an option in a * subsection: */ cfg_setstr(bm, "proxy|host", "localhost"); } printf(" proxy host is %s\n", cfg_getstr(pxy, "host")); printf(" proxy type is %ld\n", cfg_getint(pxy, "type")); printf(" proxy port is %ld\n", cfg_getint(pxy, "port")); m = cfg_size(pxy, "exclude"); printf(" got %d hosts to exclude from proxying:\n", m); for(j = 0; j < m; j++) { printf(" exclude %s\n", cfg_getnstr(pxy, "exclude", j)); } } else printf(" no proxy settings configured\n"); } printf("delays are (%d):\n", cfg_size(cfg, "delays")); for(i = 0; i < cfg_size(cfg, "delays"); i++) printf(" %G\n", cfg_getnfloat(cfg, "delays", i)); printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); /* Using cfg_setint(), the integer value for the option ask-quit * is not verified by the value parsing callback. * * cfg_setint(cfg, "ask-quit", 4); printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); */ /* The following commented line will generate a failed assertion * and abort, since the option "foo" is not declared * * printf("foo == %ld\n", cfg_getint(cfg, "foo")); */ cfg_addlist(cfg, "ask-quit-array", 2, 1, 2); for(i = 0; i < cfg_size(cfg, "ask-quit-array"); i++) printf("ask-quit-array[%d] == %ld\n", i, cfg_getnint(cfg, "ask-quit-array", i)); /* print the parsed values to another file */ { FILE *fp = fopen("test.conf.out", "w"); cfg_set_print_func(cfg, "func", print_func); cfg_set_print_func(cfg, "ask-quit", print_ask); cfg_set_print_func(cfg, "ask-quit-array", print_ask); cfg_print(cfg, fp); fclose(fp); } cfg_free(cfg); return 0; }
00001 #include "confuse.h" 00002 #include <string.h> 00003 00004 void print_func(cfg_opt_t *opt, unsigned int index, FILE *fp) 00005 { 00006 fprintf(fp, "%s(foo)", opt->name); 00007 } 00008 00009 void print_ask(cfg_opt_t *opt, unsigned int index, FILE *fp) 00010 { 00011 int value = cfg_opt_getnint(opt, index); 00012 switch(value) { 00013 case 1: 00014 fprintf(fp, "yes"); 00015 break; 00016 case 2: 00017 fprintf(fp, "no"); 00018 break; 00019 case 3: 00020 default: 00021 fprintf(fp, "maybe"); 00022 break; 00023 } 00024 } 00025 00026 /* function callback 00027 */ 00028 int cb_func(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv) 00029 { 00030 int i; 00031 00032 /* at least one parameter is required */ 00033 if(argc == 0) { 00034 cfg_error(cfg, "Too few parameters for the '%s' function", 00035 opt->name); 00036 return -1; 00037 } 00038 00039 printf("cb_func() called with %d parameters:\n", argc); 00040 for(i = 0; i < argc; i++) 00041 printf("parameter %d: '%s'\n", i, argv[i]); 00042 return 0; 00043 } 00044 00045 /* value parsing callback 00046 * 00047 * VALUE must be "yes", "no" or "maybe", and the corresponding results 00048 * are the integers 1, 2 and 3. 00049 */ 00050 int cb_verify_ask(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) 00051 { 00052 if(strcmp(value, "yes") == 0) 00053 *(long int *)result = 1; 00054 else if(strcmp(value, "no") == 0) 00055 *(long int *)result = 2; 00056 else if(strcmp(value, "maybe") == 0) 00057 *(long int *)result = 3; 00058 else { 00059 cfg_error(cfg, "Invalid value for option %s: %s", opt->name, value); 00060 return -1; 00061 } 00062 return 0; 00063 } 00064 00065 int cb_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt) 00066 { 00067 /* only validate the last bookmark */ 00068 cfg_t *sec = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1); 00069 if(!sec) 00070 { 00071 cfg_error(cfg, "section is NULL!?"); 00072 return -1; 00073 } 00074 if(cfg_getstr(sec, "machine") == 0) 00075 { 00076 cfg_error(cfg, "machine option must be set for bookmark '%s'", 00077 cfg_title(sec)); 00078 return -1; 00079 } 00080 return 0; 00081 } 00082 00083 int main(int argc, char **argv) 00084 { 00085 unsigned int i; 00086 cfg_t *cfg; 00087 unsigned n; 00088 int ret; 00089 char *pc; 00090 00091 static cfg_opt_t proxy_opts[] = { 00092 CFG_INT("type", 0, CFGF_NONE), 00093 CFG_STR("host", 0, CFGF_NONE), 00094 CFG_STR_LIST("exclude", "{localhost, .localnet}", CFGF_NONE), 00095 CFG_INT("port", 21, CFGF_NONE), 00096 CFG_END() 00097 }; 00098 static cfg_opt_t bookmark_opts[] = { 00099 CFG_STR("machine", 0, CFGF_NONE), 00100 CFG_INT("port", 21, CFGF_NONE), 00101 CFG_STR("login", 0, CFGF_NONE), 00102 CFG_STR("password", 0, CFGF_NONE), 00103 CFG_STR("directory", 0, CFGF_NONE), 00104 CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), 00105 CFG_SEC("proxy", proxy_opts, CFGF_NONE), 00106 CFG_END() 00107 }; 00108 cfg_opt_t opts[] = { 00109 CFG_INT("backlog", 42, CFGF_NONE), 00110 CFG_STR("probe-device", "eth2", CFGF_NONE), 00111 CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), 00112 CFG_FLOAT_LIST("delays", "{3.567e2, 0.2, -47.11}", CFGF_NONE), 00113 CFG_FUNC("func", &cb_func), 00114 CFG_INT_CB("ask-quit", 3, CFGF_NONE, &cb_verify_ask), 00115 CFG_INT_LIST_CB("ask-quit-array", "{maybe, yes, no}", 00116 CFGF_NONE, &cb_verify_ask), 00117 CFG_FUNC("include", &cfg_include), 00118 CFG_END() 00119 }; 00120 00121 #ifndef _WIN32 00122 /* for some reason, MS Visual C++ chokes on this (?) */ 00123 printf("Using %s\n\n", confuse_copyright); 00124 #endif 00125 00126 cfg = cfg_init(opts, CFGF_NOCASE); 00127 00128 /* set a validating callback function for bookmark sections */ 00129 cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark); 00130 00131 ret = cfg_parse(cfg, argc > 1 ? argv[1] : "test.conf"); 00132 printf("ret == %d\n", ret); 00133 if(ret == CFG_FILE_ERROR) { 00134 perror("test.conf"); 00135 return 1; 00136 } else if(ret == CFG_PARSE_ERROR) { 00137 fprintf(stderr, "parse error\n"); 00138 return 2; 00139 } 00140 00141 printf("backlog == %ld\n", cfg_getint(cfg, "backlog")); 00142 00143 printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); 00144 cfg_setstr(cfg, "probe-device", "lo"); 00145 printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); 00146 00147 n = cfg_size(cfg, "bookmark"); 00148 printf("%d configured bookmarks:\n", n); 00149 for(i = 0; i < n; i++) { 00150 cfg_t *pxy; 00151 cfg_t *bm = cfg_getnsec(cfg, "bookmark", i); 00152 printf(" bookmark #%u (%s):\n", i+1, cfg_title(bm)); 00153 printf(" machine = %s\n", cfg_getstr(bm, "machine")); 00154 printf(" port = %d\n", (int)cfg_getint(bm, "port")); 00155 printf(" login = %s\n", cfg_getstr(bm, "login")); 00156 printf(" passive-mode = %s\n", 00157 cfg_getbool(bm, "passive-mode") ? "true" : "false"); 00158 pc = cfg_getstr(bm, "directory"); 00159 printf(" directory = %s\n",pc == NULL ? "(nil)" : pc ); 00160 pc = cfg_getstr(bm, "password"); 00161 printf(" password = %s\n", pc == NULL ? "(nil)" : pc); 00162 00163 pxy = cfg_getsec(bm, "proxy"); 00164 if(pxy) { 00165 int j, m; 00166 if(cfg_getstr(pxy, "host") == 0) { 00167 printf(" no proxy host is set, setting it to 'localhost'...\n"); 00168 /* For sections without CFGF_MULTI flag set, there is 00169 * also an extended syntax to get an option in a 00170 * subsection: 00171 */ 00172 cfg_setstr(bm, "proxy|host", "localhost"); 00173 } 00174 printf(" proxy host is %s\n", cfg_getstr(pxy, "host")); 00175 printf(" proxy type is %ld\n", cfg_getint(pxy, "type")); 00176 printf(" proxy port is %ld\n", cfg_getint(pxy, "port")); 00177 00178 m = cfg_size(pxy, "exclude"); 00179 printf(" got %d hosts to exclude from proxying:\n", m); 00180 for(j = 0; j < m; j++) { 00181 printf(" exclude %s\n", cfg_getnstr(pxy, "exclude", j)); 00182 } 00183 } else 00184 printf(" no proxy settings configured\n"); 00185 } 00186 00187 printf("delays are (%d):\n", cfg_size(cfg, "delays")); 00188 for(i = 0; i < cfg_size(cfg, "delays"); i++) 00189 printf(" %G\n", cfg_getnfloat(cfg, "delays", i)); 00190 00191 printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); 00192 00193 /* Using cfg_setint(), the integer value for the option ask-quit 00194 * is not verified by the value parsing callback. 00195 * 00196 * 00197 cfg_setint(cfg, "ask-quit", 4); 00198 printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); 00199 */ 00200 00201 /* The following commented line will generate a failed assertion 00202 * and abort, since the option "foo" is not declared 00203 * 00204 * 00205 printf("foo == %ld\n", cfg_getint(cfg, "foo")); 00206 */ 00207 00208 cfg_addlist(cfg, "ask-quit-array", 2, 1, 2); 00209 00210 for(i = 0; i < cfg_size(cfg, "ask-quit-array"); i++) 00211 printf("ask-quit-array[%d] == %ld\n", 00212 i, cfg_getnint(cfg, "ask-quit-array", i)); 00213 00214 /* print the parsed values to another file */ 00215 { 00216 FILE *fp = fopen("test.conf.out", "w"); 00217 cfg_set_print_func(cfg, "func", print_func); 00218 cfg_set_print_func(cfg, "ask-quit", print_ask); 00219 cfg_set_print_func(cfg, "ask-quit-array", print_ask); 00220 cfg_print(cfg, fp); 00221 fclose(fp); 00222 } 00223 00224 cfg_free(cfg); 00225 return 0; 00226 }