YAML: add support for booleans

This commit is contained in:
Ariadne Conill 2020-11-11 03:11:48 -07:00
parent 5010dce3d5
commit dbfebbff87
3 changed files with 31 additions and 5 deletions

View file

@ -38,6 +38,22 @@ lif_yaml_document_new(const char *name)
return doc;
}
struct lif_yaml_node *
lif_yaml_node_new_boolean(const char *name, bool value)
{
struct lif_yaml_node *node = calloc(1, sizeof *node);
node->malloced = true;
node->value_type = LIF_YAML_BOOLEAN;
if (name != NULL)
node->name = strdup(name);
node->value.bool_value = value;
return node;
}
struct lif_yaml_node *
lif_yaml_node_new_string(const char *name, const char *value)
{
@ -50,7 +66,7 @@ lif_yaml_node_new_string(const char *name, const char *value)
node->name = strdup(name);
if (value != NULL)
node->value = strdup(value);
node->value.str_value = strdup(value);
return node;
}
@ -96,7 +112,9 @@ lif_yaml_node_free(struct lif_yaml_node *node)
}
free(node->name);
free(node->value);
if (node->value_type == LIF_YAML_STRING)
free(node->value.str_value);
if (node->malloced)
free(node);

View file

@ -22,7 +22,8 @@
enum lif_yaml_value {
LIF_YAML_STRING,
LIF_YAML_LIST,
LIF_YAML_OBJECT
LIF_YAML_OBJECT,
LIF_YAML_BOOLEAN
};
struct lif_yaml_node {
@ -31,13 +32,17 @@ struct lif_yaml_node {
bool malloced;
char *name;
enum lif_yaml_value value_type;
char *value; /* for string nodes */
union {
char *str_value; /* for string nodes */
bool bool_value; /* for boolean nodes */
} value;
struct lif_list children; /* for list and object nodes */
};
extern void lif_yaml_document_init(struct lif_yaml_node *doc, const char *name);
extern struct lif_yaml_node *lif_yaml_document_new(const char *name);
extern struct lif_yaml_node *lif_yaml_node_new_boolean(const char *name, bool value);
extern struct lif_yaml_node *lif_yaml_node_new_string(const char *name, const char *value);
extern struct lif_yaml_node *lif_yaml_node_new_object(const char *name);
extern struct lif_yaml_node *lif_yaml_node_new_list(const char *name);

View file

@ -33,8 +33,11 @@ lif_yaml_write_node(struct lif_yaml_node *node, FILE *f, size_t indent)
switch (node->value_type)
{
case LIF_YAML_BOOLEAN:
fprintf(f, "!!bool %s\n", node->value.bool_value ? "true" : "false");
break;
case LIF_YAML_STRING:
fprintf(f, "!!str %s\n", node->value);
fprintf(f, "!!str %s\n", node->value.str_value);
break;
case LIF_YAML_OBJECT:
fprintf(f, "\n");