Annotation of embedaddon/confuse/examples/simple.c, revision 1.1.1.2
1.1.1.2 ! misho 1: #define _GNU_SOURCE
1.1 misho 2: #include <string.h>
3: #include <stdlib.h>
1.1.1.2 ! misho 4: #include <locale.h>
1.1 misho 5: #include "confuse.h"
6:
7: int main(void)
8: {
1.1.1.2 ! misho 9: static cfg_bool_t verbose = cfg_false;
! 10: static char *server = NULL;
! 11: static double delay = 1.356e-32;
! 12: static char *username = NULL;
! 13:
! 14: /* Although the macro used to specify an integer option is called
! 15: * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system
! 16: * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64
! 17: * architectures), you will get weird effects if you use an int here.
! 18: *
! 19: * If you use the regular (non-"simple") options, ie CFG_INT() and use
! 20: * cfg_getint(), this is not a problem as the data types are implicitly
! 21: * cast.
! 22: */
! 23: static long int debug = 1;
! 24:
! 25: cfg_opt_t opts[] = {
! 26: CFG_SIMPLE_BOOL("verbose", &verbose),
! 27: CFG_SIMPLE_STR("server", &server),
! 28: CFG_SIMPLE_STR("user", &username),
! 29: CFG_SIMPLE_INT("debug", &debug),
! 30: CFG_SIMPLE_FLOAT("delay", &delay),
! 31: CFG_END()
! 32: };
! 33: cfg_t *cfg;
! 34:
! 35: /* Localize messages & types according to environment, since v2.9 */
! 36: #ifdef LC_MESSAGES
! 37: setlocale(LC_MESSAGES, "");
! 38: setlocale(LC_CTYPE, "");
! 39: #endif
! 40:
! 41: /* set default value for the server option */
! 42: server = strdup("gazonk");
! 43:
! 44: cfg = cfg_init(opts, 0);
! 45: cfg_parse(cfg, "simple.conf");
! 46:
! 47: printf("verbose: %s\n", verbose ? "true" : "false");
! 48: printf("server: %s\n", server);
! 49: printf("username: %s\n", username);
! 50: printf("debug: %ld\n", debug);
! 51: printf("delay: %G\n", delay);
! 52:
! 53: printf("setting username to 'foo'\n");
! 54: /* using cfg_setstr here is not necessary at all, the equivalent
! 55: * code is:
! 56: * free(username);
! 57: * username = strdup("foo");
! 58: */
! 59: cfg_setstr(cfg, "user", "foo");
! 60: printf("username: %s\n", username);
! 61:
! 62: /* print the parsed values to another file */
! 63: {
! 64: FILE *fp = fopen("simple.conf.out", "w");
! 65:
! 66: cfg_print(cfg, fp);
! 67: fclose(fp);
! 68: }
! 69:
! 70: cfg_free(cfg);
! 71:
! 72: /* You are responsible for freeing string values. */
! 73: free(server);
! 74: free(username);
1.1 misho 75:
1.1.1.2 ! misho 76: return 0;
1.1 misho 77: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>