confuse: reread.c

- Main Page
- Data Structures
- Files
- Examples

# reread.c

    00001 #include "confuse.h"
    00002 #include <string.h>
    00003 #include <signal.h>
    00004 #include <unistd.h>
    00005
    00006 cfg_t *cfg = 0;
    00007 const char *config_filename = "./reread.conf";
    00008
    00009 void read_config(void)
    00010 {
    00011     cfg_opt_t arg_opts[] = {
    00012         CFG_STR("value", "default", CFGF_NONE),
    00013         CFG_END()
    00014     };
    00015     cfg_opt_t opts[] = {
    00016         CFG_INT("delay", 3, CFGF_NONE),
    00017         CFG_STR("message", "This is a message", CFGF_NONE),
    00018         CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE),
    00019         CFG_END()
    00020     };
    00021     int ret;
    00022
    00023     char *buf = "" \
    00024         " delay = 3\n" \
    00025         "# message = \"asdfasfasfd tersf\"\n" \
    00026         " argument one { value = 1 }\n" \
    00027         " argument two { value=foo}\n";
    00028
    00029     cfg_free(cfg);
    00030
    00031     cfg = cfg_init(opts, 0);
    00032     ret = cfg_parse_buf(cfg, buf);
    00033     ret = cfg_parse(cfg, config_filename);
    00034 }
    00035
    00036 void sighandler(int sig)
    00037 {
    00038     read_config();
    00039     signal(SIGHUP, sighandler);
    00040 }
    00041
    00042 static int loop = 1;
    00043
    00044 void usr1handler(int sig)
    00045 {
    00046     loop = 0;
    00047 }
    00048
    00049 int main(void)
    00050 {
    00051     unsigned int i;
    00052
    00053     read_config();
    00054     signal(SIGHUP, sighandler);
    00055     signal(SIGUSR1, usr1handler);
    00056
    00057     while(loop)
    00058     {
    00059         printf("Message: %s", cfg_getstr(cfg, "message"));
    00060         for(i = 0; i < cfg_size(cfg, "argument"); i++)
    00061         {
    00062             cfg_t *arg = cfg_getnsec(cfg, "argument", i);
    00063             printf(", %s", cfg_getstr(arg, "value"));
    00064         }
    00065         printf("\n");
    00066
    00067         sleep(cfg_getint(cfg, "delay"));
    00068     }
    00069
    00070     cfg_free(cfg);
    00071     cfg = 0;
    00072
    00073     return 0;
    00074 }