Annotation of embedaddon/confuse/examples/cfgtest.c, revision 1.1
1.1 ! misho 1: #include "confuse.h"
! 2: #include <string.h>
! 3:
! 4: void print_func(cfg_opt_t *opt, unsigned int index, FILE *fp)
! 5: {
! 6: fprintf(fp, "%s(foo)", opt->name);
! 7: }
! 8:
! 9: void print_ask(cfg_opt_t *opt, unsigned int index, FILE *fp)
! 10: {
! 11: int value = cfg_opt_getnint(opt, index);
! 12:
! 13: switch (value) {
! 14: case 1:
! 15: fprintf(fp, "yes");
! 16: break;
! 17: case 2:
! 18: fprintf(fp, "no");
! 19: break;
! 20: case 3:
! 21: default:
! 22: fprintf(fp, "maybe");
! 23: break;
! 24: }
! 25: }
! 26:
! 27: /* function callback
! 28: */
! 29: int cb_func(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv)
! 30: {
! 31: int i;
! 32:
! 33: /* at least one parameter is required */
! 34: if (argc == 0) {
! 35: cfg_error(cfg, "Too few parameters for the '%s' function", opt->name);
! 36: return -1;
! 37: }
! 38:
! 39: printf("cb_func() called with %d parameters:\n", argc);
! 40: for (i = 0; i < argc; i++)
! 41: printf("parameter %d: '%s'\n", i, argv[i]);
! 42: return 0;
! 43: }
! 44:
! 45: /* value parsing callback
! 46: *
! 47: * VALUE must be "yes", "no" or "maybe", and the corresponding results
! 48: * are the integers 1, 2 and 3.
! 49: */
! 50: int cb_verify_ask(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
! 51: {
! 52: if (strcmp(value, "yes") == 0)
! 53: *(long int *)result = 1;
! 54: else if (strcmp(value, "no") == 0)
! 55: *(long int *)result = 2;
! 56: else if (strcmp(value, "maybe") == 0)
! 57: *(long int *)result = 3;
! 58: else {
! 59: cfg_error(cfg, "Invalid value for option %s: %s", opt->name, value);
! 60: return -1;
! 61: }
! 62: return 0;
! 63: }
! 64:
! 65: int cb_validate_bookmark(cfg_t *cfg, cfg_opt_t *opt)
! 66: {
! 67: /* only validate the last bookmark */
! 68: cfg_t *sec = cfg_opt_getnsec(opt, cfg_opt_size(opt) - 1);
! 69:
! 70: if (!sec) {
! 71: cfg_error(cfg, "section is NULL!?");
! 72: return -1;
! 73: }
! 74: if (cfg_getstr(sec, "machine") == 0) {
! 75: cfg_error(cfg, "machine option must be set for bookmark '%s'", cfg_title(sec));
! 76: return -1;
! 77: }
! 78: return 0;
! 79: }
! 80:
! 81: int main(int argc, char **argv)
! 82: {
! 83: unsigned int i;
! 84: cfg_t *cfg;
! 85: unsigned n;
! 86: int ret;
! 87:
! 88: cfg_opt_t proxy_opts[] = {
! 89: CFG_INT("type", 0, CFGF_NONE),
! 90: CFG_STR("host", 0, CFGF_NONE),
! 91: CFG_STR_LIST("exclude", "{localhost, .localnet}", CFGF_NONE),
! 92: CFG_INT("port", 21, CFGF_NONE),
! 93: CFG_END()
! 94: };
! 95: cfg_opt_t bookmark_opts[] = {
! 96: CFG_STR("machine", 0, CFGF_NONE),
! 97: CFG_INT("port", 21, CFGF_NONE),
! 98: CFG_STR("login", 0, CFGF_NONE),
! 99: CFG_STR("password", 0, CFGF_NONE),
! 100: CFG_STR("directory", 0, CFGF_NONE),
! 101: CFG_BOOL("passive-mode", cfg_false, CFGF_NONE),
! 102: CFG_SEC("proxy", proxy_opts, CFGF_NONE),
! 103: CFG_END()
! 104: };
! 105: cfg_opt_t opts[] = {
! 106: CFG_INT("backlog", 42, CFGF_NONE),
! 107: CFG_STR("probe-device", "eth2", CFGF_NONE),
! 108: CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE),
! 109: CFG_FLOAT_LIST("delays", "{3.567e2, 0.2, -47.11}", CFGF_NONE),
! 110: CFG_FUNC("func", &cb_func),
! 111: CFG_INT_CB("ask-quit", 3, CFGF_NONE, &cb_verify_ask),
! 112: CFG_INT_LIST_CB("ask-quit-array", "{maybe, yes, no}",
! 113: CFGF_NONE, &cb_verify_ask),
! 114: CFG_FUNC("include", &cfg_include),
! 115: CFG_END()
! 116: };
! 117:
! 118: #ifndef _WIN32
! 119: /* for some reason, MS Visual C++ chokes on this (?) */
! 120: printf("Using %s\n\n", confuse_copyright);
! 121: #endif
! 122:
! 123: cfg = cfg_init(opts, CFGF_NOCASE);
! 124:
! 125: /* set a validating callback function for bookmark sections */
! 126: cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark);
! 127:
! 128: ret = cfg_parse(cfg, argc > 1 ? argv[1] : "test.conf");
! 129: printf("ret == %d\n", ret);
! 130: if (ret == CFG_FILE_ERROR) {
! 131: perror("test.conf");
! 132: return 1;
! 133: } else if (ret == CFG_PARSE_ERROR) {
! 134: fprintf(stderr, "parse error\n");
! 135: return 2;
! 136: }
! 137:
! 138: printf("backlog == %ld\n", cfg_getint(cfg, "backlog"));
! 139:
! 140: printf("probe device is %s\n", cfg_getstr(cfg, "probe-device"));
! 141: cfg_setstr(cfg, "probe-device", "lo");
! 142: printf("probe device is %s\n", cfg_getstr(cfg, "probe-device"));
! 143:
! 144: n = cfg_size(cfg, "bookmark");
! 145: printf("%d configured bookmarks:\n", n);
! 146: for (i = 0; i < n; i++) {
! 147: cfg_t *pxy;
! 148: cfg_t *bm = cfg_getnsec(cfg, "bookmark", i);
! 149:
! 150: printf(" bookmark #%u (%s):\n", i + 1, cfg_title(bm));
! 151: printf(" machine = %s\n", cfg_getstr(bm, "machine"));
! 152: printf(" port = %d\n", (int)cfg_getint(bm, "port"));
! 153: printf(" login = %s\n", cfg_getstr(bm, "login"));
! 154: printf(" passive-mode = %s\n", cfg_getbool(bm, "passive-mode") ? "true" : "false");
! 155: printf(" directory = %s\n", cfg_getstr(bm, "directory"));
! 156: printf(" password = %s\n", cfg_getstr(bm, "password"));
! 157:
! 158: pxy = cfg_getsec(bm, "proxy");
! 159: if (pxy) {
! 160: int j, m;
! 161:
! 162: if (cfg_getstr(pxy, "host") == 0) {
! 163: printf(" no proxy host is set, setting it to 'localhost'...\n");
! 164: /* For sections without CFGF_MULTI flag set, there is
! 165: * also an extended syntax to get an option in a
! 166: * subsection:
! 167: */
! 168: cfg_setstr(bm, "proxy|host", "localhost");
! 169: }
! 170: printf(" proxy host is %s\n", cfg_getstr(pxy, "host"));
! 171: printf(" proxy type is %ld\n", cfg_getint(pxy, "type"));
! 172: printf(" proxy port is %ld\n", cfg_getint(pxy, "port"));
! 173:
! 174: m = cfg_size(pxy, "exclude");
! 175: printf(" got %d hosts to exclude from proxying:\n", m);
! 176: for (j = 0; j < m; j++) {
! 177: printf(" exclude %s\n", cfg_getnstr(pxy, "exclude", j));
! 178: }
! 179: } else
! 180: printf(" no proxy settings configured\n");
! 181: }
! 182:
! 183: printf("delays are (%d):\n", cfg_size(cfg, "delays"));
! 184: for (i = 0; i < cfg_size(cfg, "delays"); i++)
! 185: printf(" %G\n", cfg_getnfloat(cfg, "delays", i));
! 186:
! 187: printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit"));
! 188:
! 189: /* Using cfg_setint(), the integer value for the option ask-quit
! 190: * is not verified by the value parsing callback.
! 191: *
! 192: *
! 193: cfg_setint(cfg, "ask-quit", 4);
! 194: printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit"));
! 195: */
! 196:
! 197: /* The following commented line will generate a failed assertion
! 198: * and abort, since the option "foo" is not declared
! 199: *
! 200: *
! 201: printf("foo == %ld\n", cfg_getint(cfg, "foo"));
! 202: */
! 203:
! 204: cfg_addlist(cfg, "ask-quit-array", 2, 1, 2);
! 205:
! 206: for (i = 0; i < cfg_size(cfg, "ask-quit-array"); i++)
! 207: printf("ask-quit-array[%d] == %ld\n", i, cfg_getnint(cfg, "ask-quit-array", i));
! 208:
! 209: /* print the parsed values to another file */
! 210: {
! 211: FILE *fp = fopen("test.conf.out", "w");
! 212:
! 213: cfg_set_print_func(cfg, "func", print_func);
! 214: cfg_set_print_func(cfg, "ask-quit", print_ask);
! 215: cfg_set_print_func(cfg, "ask-quit-array", print_ask);
! 216: cfg_print(cfg, fp);
! 217: fclose(fp);
! 218: }
! 219:
! 220: cfg_free(cfg);
! 221: return 0;
! 222: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>