Annotation of embedaddon/confuse/examples/simple.c, revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>