summaryrefslogtreecommitdiffstats
path: root/pcilib/unit.c
blob: 0295120431e25b04661c8aee206eaddf2d18b38b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "pci.h"
#include "pcilib.h"
#include "unit.h"
#include "error.h"

static pcilib_unit_transform_t pcilib_unit_transform_null = { NULL, NULL };

int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc) {
    size_t i;
    int err = 0;

    if (!n) {
        for (n = 0; desc[n].name; n++);
    }

    if ((ctx->num_units + n + 1) > ctx->alloc_units) {
	size_t size;
	pcilib_unit_description_t *units;

        for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size <<= 1);

        units = (pcilib_unit_description_t*)realloc(ctx->units, size * sizeof(pcilib_unit_description_t));
        if (!units) return PCILIB_ERROR_MEMORY;

        ctx->units = units;
        ctx->alloc_units = size;

        ctx->model_info.units = units;
    }

        // ToDo: Check if exists...
    for (i = 0; i < n; i++) {
        pcilib_unit_context_t *unit_ctx = (pcilib_unit_context_t*)malloc(sizeof(pcilib_unit_context_t));
        if (!unit_ctx) {
            err = PCILIB_ERROR_MEMORY;
            break;
        }
        memset(unit_ctx, 0, sizeof(pcilib_unit_context_t));
        unit_ctx->unit = ctx->num_units + i;
        unit_ctx->name = desc[i].name;

        HASH_ADD_KEYPTR(hh, ctx->unit_hash, unit_ctx->name, strlen(unit_ctx->name), unit_ctx);
        memcpy(ctx->units + ctx->num_units + i, &desc[i], sizeof(pcilib_unit_description_t));
    }

    memset(ctx->units + ctx->num_units + i, 0, sizeof(pcilib_unit_description_t));
    ctx->num_units += i;

    return err;
}

void pcilib_clean_units(pcilib_t *ctx) {
    pcilib_unit_context_t *s, *tmp;

    if (ctx->unit_hash) {
        HASH_ITER(hh, ctx->unit_hash, s, tmp) {
            HASH_DEL(ctx->unit_hash, s);
            free(s);
        }
    }

    memset(ctx->units, 0, sizeof(pcilib_unit_description_t));
    ctx->num_units = 0;
}

pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *name) {
    pcilib_unit_context_t *unit_ctx = NULL;

    HASH_FIND_STR(ctx->unit_hash, name, unit_ctx);
    if (unit_ctx) return unit_ctx->unit;

/*
    pcilib_unit_t i;
    for(i = 0; ctx->units[i].name; i++) {
        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_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);
}