Example Documentation

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

# simple.c

    #include <string.h>
    #include "confuse.h"

    int main(void)
    {
        cfg_bool_t verbose = cfg_false;
        char *server = NULL;
        double delay = 1.356e-32;
        char *username = NULL;
        int debug = 1;

        cfg_opt_t opts[] = {
            CFG_SIMPLE_BOOL("verbose", &verbose),
            CFG_SIMPLE_STR("server", &server),
            CFG_SIMPLE_STR("user", &username),
            CFG_SIMPLE_INT("debug", &debug),
            CFG_SIMPLE_FLOAT("delay", &delay),
            CFG_END()
        };
        cfg_t *cfg;

        /* set default value for the server option */
        server = strdup("gazonk");

        cfg = cfg_init(opts, 0);
        cfg_parse(cfg, "simple.conf");

        printf("verbose: %s\n", verbose ? "true" : "false");
        printf("server: %s\n", server);
        printf("username: %s\n", username);
        printf("debug: %d\n", debug);
        printf("delay: %G\n", delay);

        printf("setting username to 'foo'\n");
        /* using cfg_setstr here is not necessary at all, the equivalent
         * code is:
         *   free(username);
         *   username = strdup("foo");
         */
        cfg_setstr(cfg, "user", "foo");
        printf("username: %s\n", username);

        /* print the parsed values to another file */
        {
            FILE *fp = fopen("simple.conf.out", "w");
            cfg_print(cfg, fp);
            fclose(fp);
        }

        cfg_free(cfg);
        return 0;
    }

    00001 #include <string.h>
    00002 #include "confuse.h"
    00003
    00004 int main(void)
    00005 {
    00006     cfg_bool_t verbose = cfg_false;
    00007     char *server = NULL;
    00008     double delay = 1.356e-32;
    00009     char *username = NULL;
    00010     int debug = 1;
    00011
    00012     cfg_opt_t opts[] = {
    00013         CFG_SIMPLE_BOOL("verbose", &verbose),
    00014         CFG_SIMPLE_STR("server", &server),
    00015         CFG_SIMPLE_STR("user", &username),
    00016         CFG_SIMPLE_INT("debug", &debug),
    00017         CFG_SIMPLE_FLOAT("delay", &delay),
    00018         CFG_END()
    00019     };
    00020     cfg_t *cfg;
    00021
    00022     /* set default value for the server option */
    00023     server = strdup("gazonk");
    00024
    00025     cfg = cfg_init(opts, 0);
    00026     cfg_parse(cfg, "simple.conf");
    00027
    00028     printf("verbose: %s\n", verbose ? "true" : "false");
    00029     printf("server: %s\n", server);
    00030     printf("username: %s\n", username);
    00031     printf("debug: %d\n", debug);
    00032     printf("delay: %G\n", delay);
    00033
    00034     printf("setting username to 'foo'\n");
    00035     /* using cfg_setstr here is not necessary at all, the equivalent
    00036      * code is:
    00037      *   free(username);
    00038      *   username = strdup("foo");
    00039      */
    00040     cfg_setstr(cfg, "user", "foo");
    00041     printf("username: %s\n", username);
    00042
    00043     /* print the parsed values to another file */
    00044     {
    00045         FILE *fp = fopen("simple.conf.out", "w");
    00046         cfg_print(cfg, fp);
    00047         fclose(fp);
    00048     }
    00049
    00050     cfg_free(cfg);
    00051     return 0;
    00052 }