2020-11-11 09:46:28 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/yaml-writer.c
|
|
|
|
* Purpose: YAML implementation -- writer
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "libifupdown/libifupdown.h"
|
|
|
|
#include "libifupdown/yaml-base.h"
|
|
|
|
#include "libifupdown/yaml-writer.h"
|
|
|
|
|
|
|
|
static const size_t INDENT_WIDTH = 2;
|
|
|
|
|
|
|
|
static void
|
2020-11-14 04:31:48 +00:00
|
|
|
lif_yaml_write_node(struct lif_yaml_node *node, FILE *f, size_t indent, bool type_annotations)
|
2020-11-11 09:46:28 +00:00
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
if (node->name != NULL)
|
|
|
|
fprintf(f, "%*s%s: ", (int) indent, "", node->name);
|
|
|
|
|
|
|
|
size_t child_indent = indent + INDENT_WIDTH;
|
|
|
|
|
|
|
|
switch (node->value_type)
|
|
|
|
{
|
2020-11-11 10:11:48 +00:00
|
|
|
case LIF_YAML_BOOLEAN:
|
2020-11-14 04:31:48 +00:00
|
|
|
fprintf(f, "%s%s\n", type_annotations ? "!!bool " : "", node->value.bool_value ? "true" : "false");
|
2020-11-11 10:11:48 +00:00
|
|
|
break;
|
2020-11-11 09:46:28 +00:00
|
|
|
case LIF_YAML_STRING:
|
2020-11-14 04:31:48 +00:00
|
|
|
fprintf(f, "%s%s\n", type_annotations ? "!!str " : "", node->value.str_value);
|
2020-11-11 09:46:28 +00:00
|
|
|
break;
|
|
|
|
case LIF_YAML_OBJECT:
|
|
|
|
fprintf(f, "\n");
|
|
|
|
break;
|
|
|
|
case LIF_YAML_LIST:
|
|
|
|
fprintf(f, "\n");
|
|
|
|
child_indent += INDENT_WIDTH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIF_LIST_FOREACH(iter, node->children.head)
|
|
|
|
{
|
|
|
|
struct lif_yaml_node *iter_node = iter->data;
|
|
|
|
|
|
|
|
if (node->value_type == LIF_YAML_LIST)
|
|
|
|
fprintf(f, "%*s-\n", (int) (child_indent - INDENT_WIDTH), "");
|
|
|
|
|
2020-11-14 04:31:48 +00:00
|
|
|
lif_yaml_write_node(iter_node, f, child_indent, type_annotations);
|
2020-11-11 09:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-14 04:31:48 +00:00
|
|
|
lif_yaml_write(struct lif_yaml_node *doc, FILE *f, bool type_annotations)
|
2020-11-11 09:46:28 +00:00
|
|
|
{
|
2020-11-14 04:31:48 +00:00
|
|
|
lif_yaml_write_node(doc, f, 0, type_annotations);
|
2020-11-11 09:46:28 +00:00
|
|
|
}
|