Skip to content

Instantly share code, notes, and snippets.

@lanoxx
Created October 29, 2015 14:08
Show Gist options
  • Save lanoxx/4c5b13bf7097c13cbac5 to your computer and use it in GitHub Desktop.
Save lanoxx/4c5b13bf7097c13cbac5 to your computer and use it in GitHub Desktop.
CFGF_DEPRECATED and CFGF_DROP
From 07b6b7dc8082850d84516d1d4f1ebda20e192cc5 Mon Sep 17 00:00:00 2001
From: Sebastian Geiger <sbastig@gmx.net>
Date: Thu, 29 Oct 2015 15:05:35 +0100
Subject: [PATCH] Implementation draft for deprecated and drop flags
---
examples/Makefile.am | 6 +++---
examples/cfgdeprecated.c | 33 +++++++++++++++++++++++++++++++++
examples/cfgdeprecated.conf | 3 +++
src/confuse.c | 21 +++++++++++++++++++++
src/confuse.h | 2 ++
5 files changed, 62 insertions(+), 3 deletions(-)
create mode 100644 examples/cfgdeprecated.c
create mode 100644 examples/cfgdeprecated.conf
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 12732b5..543e5b2 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,6 +1,6 @@
-EXTRA_DIST = simple.conf reread.conf ftp.conf test.conf
-noinst_PROGRAMS = simple reread ftpconf cfgtest cli
-AM_CPPFLAGS = -I$(top_srcdir)/src
+EXTRA_DIST = cfgdeprecated.conf simple.conf reread.conf ftp.conf test.conf
+noinst_PROGRAMS = cfgdeprecated simple reread ftpconf cfgtest cli
+AM_CPPFLAGS = -I$(top_srcdir)/src -g -ggdb -O0
AM_LDFLAGS = -L../src/
LIBS = $(LTLIBINTL)
LDADD = ../src/libconfuse.la
diff --git a/examples/cfgdeprecated.c b/examples/cfgdeprecated.c
new file mode 100644
index 0000000..deba1d7
--- /dev/null
+++ b/examples/cfgdeprecated.c
@@ -0,0 +1,33 @@
+#include "confuse.h"
+#include <string.h>
+
+int main(void)
+{
+ cfg_opt_t opts[] =
+ {
+ CFG_STR_LIST("targets", "{World}", CFGF_DEPRECATED),
+ CFG_INT("repeat", 1, CFGF_DEPRECATED),
+ CFG_INT("foobar", 1, CFGF_DEPRECATED | CFGF_DROP),
+ CFG_END()
+ };
+ cfg_t *cfg;
+ int repeat;
+ int i;
+
+ cfg = cfg_init(opts, CFGF_NONE);
+ if(cfg_parse(cfg, "cfgdeprecated.conf") == CFG_PARSE_ERROR)
+ return 1;
+
+ repeat = cfg_getint(cfg, "repeat");
+ while(repeat--)
+ {
+ printf("Hello");
+ for(i = 0; i < cfg_size(cfg, "targets"); i++)
+ printf(", %s", cfg_getnstr(cfg, "targets", i));
+ printf("!\n");
+ }
+
+ cfg_print_indent (cfg, stdout, 4);
+ cfg_free(cfg);
+ return 0;
+}
diff --git a/examples/cfgdeprecated.conf b/examples/cfgdeprecated.conf
new file mode 100644
index 0000000..5f4dab3
--- /dev/null
+++ b/examples/cfgdeprecated.conf
@@ -0,0 +1,3 @@
+targets = {"Fish", "Cat"}
+repeat = 1
+foobar = 1
diff --git a/src/confuse.c b/src/confuse.c
index e76a08b..2f897e2 100644
--- a/src/confuse.c
+++ b/src/confuse.c
@@ -822,11 +822,32 @@ static int cfg_parse_internal(cfg_t *cfg, int level, int force_state, cfg_opt_t
cfg_error(cfg, _("premature end of file"));
return STATE_ERROR;
}
+
+ if (opt != 0 && opt->flags & CFGF_DEPRECATED) {
+ fprintf(stdout, "depr.\n");
+ if (opt->flags & CFGF_DROP) {
+ cfg_error (cfg, _("dropping deprecated config value: %s"), opt->name);
+ cfg_free_value (opt);
+ } else {
+ cfg_error (cfg, _("deprecated config value found: %s"), opt->name);
+ }
+ }
+
return STATE_EOF;
}
+ fprintf(stdout, "parsing\n");
switch (state) {
case 0: /* expecting an option name */
+ if (opt != 0 && opt->flags & CFGF_DEPRECATED) {
+ fprintf(stdout, "depr.\n");
+ if (opt->flags & CFGF_DROP) {
+ cfg_error (cfg, _("dropping deprecated config value: %s"), opt->name);
+ cfg_free_value (opt);
+ } else {
+ cfg_error (cfg, _("deprecated config value found: %s"), opt->name);
+ }
+ }
if (tok == '}') {
if (level == 0) {
cfg_error(cfg, _("unexpected closing brace"));
diff --git a/src/confuse.h b/src/confuse.h
index c831638..6cc0fec 100644
--- a/src/confuse.h
+++ b/src/confuse.h
@@ -91,6 +91,8 @@ typedef enum cfg_type_t cfg_type_t;
#define CFGF_RESET 64
#define CFGF_DEFINIT 128
#define CFGF_IGNORE_UNKNOWN 256 /**< ignore unknown options in configuration files */
+#define CFGF_DEPRECATED 512 /**< option is deprecated and should be ignored. */
+#define CFGF_DROP 1024 /**< option should be dropped after parsing */
/** Return codes from cfg_parse(). */
#define CFG_SUCCESS 0
--
1.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment