summaryrefslogtreecommitdiff
path: root/src/unit-category.c
blob: af4fb181553bf01bff8ef0dac68d3bdc0533782f (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
135
136
137
138
139
140
/*
 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
 * Copyright (C) 2008-2011 Robert Ancell.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <string.h>

#include "unit-category.h"

struct UnitCategoryPrivate
{
    gchar *name;
    gchar *display_name;
    GList *units;
};

G_DEFINE_TYPE_WITH_PRIVATE (UnitCategory, unit_category, G_TYPE_OBJECT);


UnitCategory *
unit_category_new(const gchar *name, const gchar *display_name)
{
    UnitCategory *category = g_object_new(unit_category_get_type(), NULL);
    category->priv->name = g_strdup(name);
    category->priv->display_name = g_strdup(display_name);
    return category;
}


const gchar *
unit_category_get_name(UnitCategory *category)
{
    g_return_val_if_fail (category != NULL, NULL);
    return category->priv->name;
}


const gchar *
unit_category_get_display_name(UnitCategory *category)
{
    g_return_val_if_fail (category != NULL, NULL);
    return category->priv->display_name;
}


void
unit_category_add_unit(UnitCategory *category, Unit *unit)
{
    g_return_if_fail (category != NULL);
    g_return_if_fail (unit != NULL);
    category->priv->units = g_list_append(category->priv->units, g_object_ref(unit));
}


Unit *
unit_category_get_unit_by_name(UnitCategory *category, const gchar *name)
{
    GList *iter;

    g_return_val_if_fail (category != NULL, NULL);
    g_return_val_if_fail (name != NULL, NULL);

    for (iter = category->priv->units; iter; iter = iter->next)
    {
        Unit *unit = iter->data;
        if (strcmp(unit_get_name(unit), name) == 0)
            return unit;
    }

    return NULL;
}


Unit *
unit_category_get_unit_by_symbol(UnitCategory *category, const gchar *symbol)
{
    GList *iter;

    g_return_val_if_fail (category != NULL, NULL);
    g_return_val_if_fail (symbol != NULL, NULL);

    for (iter = category->priv->units; iter; iter = iter->next) {
        Unit *unit = iter->data;
        if (unit_matches_symbol(unit, symbol))
            return unit;
    }

    return NULL;
}


const GList *
unit_category_get_units(UnitCategory *category)
{
    g_return_val_if_fail (category != NULL, NULL);
    return category->priv->units;
}


gboolean
unit_category_convert(const UnitCategory *category, const MPNumber *x, Unit *x_units, Unit *z_units, MPNumber *z)
{
    g_return_val_if_fail (category != NULL, FALSE);
    g_return_val_if_fail (x_units != NULL, FALSE);
    g_return_val_if_fail (z_units != NULL, FALSE);
    g_return_val_if_fail (z != NULL, FALSE);

    MPNumber t = mp_new();
    if (!unit_convert_from(x_units, x, &t))
    {
        mp_clear(&t);
        return FALSE;
    }
    if (!unit_convert_to(z_units, &t, z))
    {
        mp_clear(&t);
        return FALSE;
    }
    mp_clear(&t);
    return TRUE;
}


static void
unit_category_class_init(UnitCategoryClass *klass)
{
}


static void
unit_category_init(UnitCategory *category)
{
    category->priv = unit_category_get_instance_private (category);
}