tag: add a boolean tag

This commit is contained in:
Daniel Eklöf 2018-11-18 11:00:01 +01:00
parent 081bccf06d
commit 12e9304612
2 changed files with 26 additions and 0 deletions

23
tag.c
View file

@ -7,6 +7,7 @@ struct private {
char *name;
union {
long value_as_int;
bool value_as_bool;
double value_as_float;
char *value_as_string;
};
@ -46,6 +47,13 @@ value_int(const struct tag *tag)
return as_string;
}
static const char *
value_bool(const struct tag *tag)
{
const struct private *priv = tag->private;
return priv->value_as_bool ? "true" : "false";
}
static const char *
value_float(const struct tag *tag)
{
@ -78,6 +86,21 @@ tag_new_int(const char *name, long value)
return tag;
}
struct tag *
tag_new_bool(const char *name, bool value)
{
struct private *priv = malloc(sizeof(*priv));
priv->name = strdup(name);
priv->value_as_int = value;
struct tag *tag = malloc(sizeof(*tag));
tag->private = priv;
tag->destroy = &destroy_int_and_float;
tag->name = &tag_name;
tag->value = &value_bool;
return tag;
}
struct tag *
tag_new_float(const char *name, double value)
{

3
tag.h
View file

@ -2,6 +2,8 @@
#include <stddef.h>
#include <stdbool.h>
struct tag {
void *private;
@ -16,6 +18,7 @@ struct tag_set {
};
struct tag *tag_new_int(const char *name, long value);
struct tag *tag_new_bool(const char *name, bool value);
struct tag *tag_new_float(const char *name, double value);
struct tag *tag_new_string(const char *name, const char *value);