aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-05-01 20:58:46 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-05-01 20:58:46 +0200
commitf5f3dd1a297b963d6c6617c08b4fc9404459f3e3 (patch)
treeb8a756af8084c7b6936f1bee221fcfc4a20b8e79
parent9285974bffe15033acc4413c5dd275cd8722466f (diff)
downloadxK-f5f3dd1a297b963d6c6617c08b4fc9404459f3e3.tar.gz
xK-f5f3dd1a297b963d6c6617c08b4fc9404459f3e3.tar.xz
xK-f5f3dd1a297b963d6c6617c08b4fc9404459f3e3.zip
Halfplement configuration writing
-rw-r--r--common.c90
1 files changed, 88 insertions, 2 deletions
diff --git a/common.c b/common.c
index 4b7e6bf..10d4293 100644
--- a/common.c
+++ b/common.c
@@ -32,6 +32,7 @@
#include "liberty/liberty.c"
#include <setjmp.h>
+#include <inttypes.h>
#include <arpa/inet.h>
// --- Logging -----------------------------------------------------------------
@@ -639,10 +640,95 @@ config_item_get (struct config_item_ *self, const char *path)
// TODO
}
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+struct config_writer
+{
+ struct str *output;
+ unsigned indent;
+};
+
+static void config_item_write_object_innards
+ (struct config_writer *self, struct config_item_ *object);
+
static void
-config_item_write (struct config_item_ *root, struct str *output)
+config_item_write_value (struct config_writer *self, struct config_item_ *value)
{
- // TODO
+ switch (value->type)
+ {
+ case CONFIG_ITEM_NULL:
+ str_append (self->output, "null");
+ break;
+ case CONFIG_ITEM_BOOLEAN:
+ str_append (self->output, value->value.boolean ? "on" : "off");
+ break;
+ case CONFIG_ITEM_INTEGER:
+ str_append_printf (self->output, "%" PRIi64, value->value.integer);
+ break;
+ case CONFIG_ITEM_STRING:
+ case CONFIG_ITEM_STRING_ARRAY:
+ // TODO
+ break;
+ case CONFIG_ITEM_OBJECT:
+ {
+ char indent[self->indent + 1];
+ memset (indent, '\t', self->indent);
+ indent[self->indent] = 0;
+
+ str_append (self->output, "{\n");
+
+ self->indent++;
+ config_item_write_object_innards (self, value);
+ self->indent--;
+
+ str_append_printf (self->output, "%s}", indent);
+ break;
+ }
+ default:
+ hard_assert (!"invalid item type");
+ }
+}
+
+static void
+config_item_write_kv_pair (struct config_writer *self,
+ const char *key, struct config_item_ *value)
+{
+ char indent[self->indent + 1];
+ memset (indent, '\t', self->indent);
+ indent[self->indent] = 0;
+
+ if (value->schema && value->schema->comment)
+ str_append_printf (self->output,
+ "%s# %s\n", indent, value->schema->comment);
+
+ str_append_printf (self->output, "%s%s = ", indent, key);
+ config_item_write_value (self, value);
+ str_append_c (self->output, '\n');
+}
+
+static void
+config_item_write_object_innards
+ (struct config_writer *self, struct config_item_ *object)
+{
+ hard_assert (object->type == CONFIG_ITEM_OBJECT);
+
+ struct str_map_iter iter;
+ str_map_iter_init (&iter, &object->value.object);
+
+ struct config_item_ *value;
+ while ((value = str_map_iter_next (&iter)))
+ config_item_write_kv_pair (self, iter.link->key, value);
+}
+
+static void
+config_item_write (struct config_item_ *value,
+ bool object_innards, struct str *output)
+{
+ struct config_writer writer = { .output = output, .indent = 0 };
+ if (object_innards)
+ config_item_write_object_innards (&writer, value);
+ else
+ config_item_write_value (&writer, value);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -