aboutsummaryrefslogtreecommitdiffstats
path: root/samples/c/sample1.c
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-12-01 22:54:16 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2011-12-01 22:54:16 +0000
commit58bf1382be0cbcf3f9649286fd2719b789a1595f (patch)
treeb73665275a2d44879a8230c913b1ef21a42e57da /samples/c/sample1.c
downloadlibconfig-58bf1382be0cbcf3f9649286fd2719b789a1595f.tar.gz
Imported Upstream version 1.3.2upstream/1.3.2
Diffstat (limited to 'samples/c/sample1.c')
-rw-r--r--samples/c/sample1.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/samples/c/sample1.c b/samples/c/sample1.c
new file mode 100644
index 0000000..a119c10
--- /dev/null
+++ b/samples/c/sample1.c
@@ -0,0 +1,63 @@
+/*************************************************************************
+ ** Sample1
+ ** Load sample.cfg and increment the "X" setting
+ *************************************************************************/
+
+#include <stdio.h>
+#include <libconfig.h>
+
+struct config_t cfg;
+
+/***************************************************************************/
+
+int main(int argc, char **argv)
+{
+ char *file = "sample.cfg", *var = "x";
+
+ if(argc >= 2)
+ file = argv[1];
+
+ if(argc >= 3)
+ var = argv[2];
+
+/* Initialize the configuration */
+ config_init(&cfg);
+
+ /* Load the file */
+ printf("loading [%s]...", file);
+ if(!config_read_file(&cfg, file))
+ printf("failed\n");
+ else
+ {
+ config_setting_t *setting = NULL;
+
+ printf("ok\n");
+
+ /* Get the variable setting from the configuration.. */
+ printf("increment \"%s\"...", var);
+ setting = config_lookup(&cfg, var);
+ if(!setting)
+ printf("failed\n");
+ else
+ {
+ long x = config_setting_get_int(setting);
+ x++;
+ config_setting_set_int(setting, x);
+ printf("ok (%s=%lu)\n", var, x);
+
+ /* Save the changes */
+ printf("saving [%s]...", file);
+ config_write_file(&cfg, file);
+ printf("ok\n");
+
+ printf("Done!\n");
+ }
+ }
+
+ /* Free the configuration */
+ config_destroy(&cfg);
+
+ return 0;
+}
+
+/***************************************************************************/