mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-22 20:25:39 +02:00
tag: add as_int(), as_bool() and as_float()
This commit is contained in:
parent
44a2dbb201
commit
1712362da9
2 changed files with 109 additions and 0 deletions
106
tag.c
106
tag.c
|
@ -1,5 +1,6 @@
|
||||||
#include "tag.h"
|
#include "tag.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
@ -47,6 +48,27 @@ int_as_string(const struct tag *tag)
|
||||||
return as_string;
|
return as_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static long
|
||||||
|
int_as_int(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
int_as_bool(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double
|
||||||
|
int_as_float(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_int;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
bool_as_string(const struct tag *tag)
|
bool_as_string(const struct tag *tag)
|
||||||
{
|
{
|
||||||
|
@ -54,6 +76,27 @@ bool_as_string(const struct tag *tag)
|
||||||
return priv->value_as_bool ? "true" : "false";
|
return priv->value_as_bool ? "true" : "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static long
|
||||||
|
bool_as_int(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
bool_as_bool(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double
|
||||||
|
bool_as_float(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_bool;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
float_as_string(const struct tag *tag)
|
float_as_string(const struct tag *tag)
|
||||||
{
|
{
|
||||||
|
@ -64,6 +107,27 @@ float_as_string(const struct tag *tag)
|
||||||
return as_string;
|
return as_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static long
|
||||||
|
float_as_int(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_float;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
float_as_bool(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_float;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double
|
||||||
|
float_as_float(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
return priv->value_as_float;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
string_as_string(const struct tag *tag)
|
string_as_string(const struct tag *tag)
|
||||||
{
|
{
|
||||||
|
@ -71,6 +135,36 @@ string_as_string(const struct tag *tag)
|
||||||
return priv->value_as_string;
|
return priv->value_as_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static long
|
||||||
|
string_as_int(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
|
||||||
|
long value;
|
||||||
|
int matches = sscanf(priv->value_as_string, "%ld", &value);
|
||||||
|
return matches == 1 ? value : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
string_as_bool(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
|
||||||
|
uint8_t value;
|
||||||
|
int matches = sscanf(priv->value_as_string, "%hhu", &value);
|
||||||
|
return matches == 1 ? value : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double
|
||||||
|
string_as_float(const struct tag *tag)
|
||||||
|
{
|
||||||
|
const struct private *priv = tag->private;
|
||||||
|
|
||||||
|
double value;
|
||||||
|
int matches = sscanf(priv->value_as_string, "%lf", &value);
|
||||||
|
return matches == 1 ? value : 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct tag *
|
struct tag *
|
||||||
tag_new_int(const char *name, long value)
|
tag_new_int(const char *name, long value)
|
||||||
{
|
{
|
||||||
|
@ -83,6 +177,9 @@ tag_new_int(const char *name, long value)
|
||||||
tag->destroy = &destroy_int_and_float;
|
tag->destroy = &destroy_int_and_float;
|
||||||
tag->name = &tag_name;
|
tag->name = &tag_name;
|
||||||
tag->as_string = &int_as_string;
|
tag->as_string = &int_as_string;
|
||||||
|
tag->as_int = &int_as_int;
|
||||||
|
tag->as_bool = &int_as_bool;
|
||||||
|
tag->as_float = &int_as_float;
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,6 +195,9 @@ tag_new_bool(const char *name, bool value)
|
||||||
tag->destroy = &destroy_int_and_float;
|
tag->destroy = &destroy_int_and_float;
|
||||||
tag->name = &tag_name;
|
tag->name = &tag_name;
|
||||||
tag->as_string = &bool_as_string;
|
tag->as_string = &bool_as_string;
|
||||||
|
tag->as_int = &bool_as_int;
|
||||||
|
tag->as_bool = &bool_as_bool;
|
||||||
|
tag->as_float = &bool_as_float;
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,6 +213,9 @@ tag_new_float(const char *name, double value)
|
||||||
tag->destroy = &destroy_int_and_float;
|
tag->destroy = &destroy_int_and_float;
|
||||||
tag->name = &tag_name;
|
tag->name = &tag_name;
|
||||||
tag->as_string = &float_as_string;
|
tag->as_string = &float_as_string;
|
||||||
|
tag->as_int = &float_as_int;
|
||||||
|
tag->as_bool = &float_as_bool;
|
||||||
|
tag->as_float = &float_as_float;
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,6 +231,9 @@ tag_new_string(const char *name, const char *value)
|
||||||
tag->destroy = &destroy_string;
|
tag->destroy = &destroy_string;
|
||||||
tag->name = &tag_name;
|
tag->name = &tag_name;
|
||||||
tag->as_string = &string_as_string;
|
tag->as_string = &string_as_string;
|
||||||
|
tag->as_int = &string_as_int;
|
||||||
|
tag->as_bool = &string_as_bool;
|
||||||
|
tag->as_float = &string_as_float;
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
tag.h
3
tag.h
|
@ -10,6 +10,9 @@ struct tag {
|
||||||
void (*destroy)(struct tag *tag);
|
void (*destroy)(struct tag *tag);
|
||||||
const char *(*name)(const struct tag *tag);
|
const char *(*name)(const struct tag *tag);
|
||||||
const char *(*as_string)(const struct tag *tag);
|
const char *(*as_string)(const struct tag *tag);
|
||||||
|
long (*as_int)(const struct tag *tag);
|
||||||
|
bool (*as_bool)(const struct tag *tag);
|
||||||
|
double (*as_float)(const struct tag *tag);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct tag_set {
|
struct tag_set {
|
||||||
|
|
Loading…
Add table
Reference in a new issue