summaryrefslogtreecommitdiffstats
path: root/pcilib/unit.c
diff options
context:
space:
mode:
Diffstat (limited to 'pcilib/unit.c')
-rw-r--r--pcilib/unit.c55
1 files changed, 53 insertions, 2 deletions
diff --git a/pcilib/unit.c b/pcilib/unit.c
index 2dd7113..6817afc 100644
--- a/pcilib/unit.c
+++ b/pcilib/unit.c
@@ -9,16 +9,38 @@
#include "error.h"
-pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *unit) {
+static pcilib_unit_transform_t pcilib_unit_transform_null = { NULL, NULL };
+
+pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *name) {
pcilib_unit_t i;
for(i = 0; ctx->units[i].name; i++) {
- if (!strcasecmp(ctx->units[i].name, unit))
+ if (!strcasecmp(ctx->units[i].name, name))
return i;
}
return PCILIB_UNIT_INVALID;
}
+pcilib_unit_transform_t *pcilib_find_transform_by_unit_names(pcilib_t *ctx, const char *from, const char *to) {
+ int i;
+ pcilib_unit_t unit;
+
+ if ((!from)||(!to))
+ return NULL;
+
+ if (!strcasecmp(from, to))
+ return &pcilib_unit_transform_null;
+
+ unit = pcilib_find_unit_by_name(ctx, from);
+ if (unit == PCILIB_UNIT_INVALID) return NULL;
+
+ for (i = 0; ctx->units[unit].transforms[i].unit; i++) {
+ if (!strcasecmp(ctx->units[unit].transforms[i].unit, to))
+ return &ctx->units[unit].transforms[i];
+ }
+
+ return NULL;
+}
int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc) {
if (!n) {
@@ -47,3 +69,32 @@ int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *d
return 0;
}
+
+
+int pcilib_transform_unit(pcilib_t *ctx, pcilib_unit_transform_t *trans, pcilib_value_t *value) {
+ int err;
+
+ err = pcilib_py_eval_string(ctx, trans->transform, value);
+ if (err) return err;
+
+ value->unit = trans->unit;
+ return 0;
+}
+
+
+int pcilib_transform_unit_by_name(pcilib_t *ctx, const char *to, pcilib_value_t *value) {
+ pcilib_unit_transform_t *trans;
+
+ if (!value->unit) {
+ pcilib_warning("Can't transform unit of the value with unspecified unit");
+ return PCILIB_ERROR_INVALID_ARGUMENT;
+ }
+
+ trans = pcilib_find_transform_by_unit_names(ctx, value->unit, to);
+ if (!trans) {
+ pcilib_warning("Can't transform unit (%s) to (%s)", value->unit, to);
+ return PCILIB_ERROR_NOTSUPPORTED;
+ }
+
+ return pcilib_transform_unit(ctx, trans, value);
+}