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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* 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.h"
#include "mp-serializer.h"
#include "mp-equation.h"
#include "currency-manager.h" // FIXME: Move out of here
struct UnitPrivate
{
gchar *name;
gchar *display_name;
gchar *format;
GList *symbols;
gchar *from_function;
gchar *to_function;
MpSerializer *serializer;
};
G_DEFINE_TYPE_WITH_PRIVATE (Unit, unit, G_TYPE_OBJECT);
Unit *
unit_new(const gchar *name,
const gchar *display_name,
const gchar *format,
const gchar *from_function,
const gchar *to_function,
const gchar *symbols)
{
Unit *unit = g_object_new(unit_get_type(), NULL);
gchar **symbol_names;
int i;
unit->priv->name = g_strdup(name);
unit->priv->display_name = g_strdup(display_name);
unit->priv->format = g_strdup(format);
unit->priv->from_function = g_strdup(from_function);
unit->priv->to_function = g_strdup(to_function);
symbol_names = g_strsplit(symbols, ",", 0);
for (i = 0; symbol_names[i]; i++)
unit->priv->symbols = g_list_append(unit->priv->symbols, symbol_names[i]);
g_free(symbol_names);
return unit;
}
const gchar *
unit_get_name(Unit *unit)
{
g_return_val_if_fail (unit != NULL, NULL);
return unit->priv->name;
}
const gchar *
unit_get_display_name(Unit *unit)
{
g_return_val_if_fail (unit != NULL, NULL);
return unit->priv->display_name;
}
gboolean
unit_matches_symbol(Unit *unit, const gchar *symbol)
{
GList *iter;
g_return_val_if_fail (unit != NULL, FALSE);
g_return_val_if_fail (symbol != NULL, FALSE);
for (iter = unit->priv->symbols; iter; iter = iter->next) {
gchar *s = iter->data;
if (strcmp(s, symbol) == 0)
return TRUE;
}
return FALSE;
}
const GList *
unit_get_symbols(Unit *unit)
{
g_return_val_if_fail (unit != NULL, NULL);
return unit->priv->symbols;
}
static int
variable_is_defined(const char *name, void *data)
{
return TRUE;
}
static int
get_variable(const char *name, MPNumber *z, void *data)
{
MPNumber *x = data;
mp_set_from_mp(x, z);
return TRUE;
}
static gboolean
solve_function(const gchar *function, const MPNumber *x, MPNumber *z)
{
MPEquationOptions options;
int ret;
memset(&options, 0, sizeof(options));
options.base = 10;
options.wordlen = 32;
options.variable_is_defined = variable_is_defined;
options.get_variable = get_variable;
options.callback_data = (void *)x;
ret = mp_equation_parse(function, &options, z, NULL);
if (ret) {
g_warning("Failed to convert value: %s", function);
return FALSE;
}
return TRUE;
}
gboolean
unit_convert_from(Unit *unit, const MPNumber *x, MPNumber *z)
{
g_return_val_if_fail(unit != NULL, FALSE);
g_return_val_if_fail(x != NULL, FALSE);
g_return_val_if_fail(z != NULL, FALSE);
if (unit->priv->from_function)
return solve_function(unit->priv->from_function, x, z);
else {
// FIXME: Hack to make currency work
const MPNumber *r;
r = currency_manager_get_value(currency_manager_get_default(), unit->priv->name);
if (!r)
return FALSE;
mp_divide(x, r, z);
return TRUE;
}
}
gboolean
unit_convert_to(Unit *unit, const MPNumber *x, MPNumber *z)
{
g_return_val_if_fail(unit != NULL, FALSE);
g_return_val_if_fail(x != NULL, FALSE);
g_return_val_if_fail(x != NULL, FALSE);
if (unit->priv->from_function)
return solve_function(unit->priv->to_function, x, z);
else {
// FIXME: Hack to make currency work
const MPNumber *r;
r = currency_manager_get_value(currency_manager_get_default(), unit->priv->name);
if (!r)
return FALSE;
mp_multiply(x, r, z);
return TRUE;
}
}
gchar *
unit_format(Unit *unit, MPNumber *x)
{
gchar *number_text, *text;
g_return_val_if_fail(unit != NULL, FALSE);
g_return_val_if_fail(x != NULL, FALSE);
number_text = mp_serializer_to_string(unit->priv->serializer, x);
text = g_strdup_printf(unit->priv->format, number_text);
g_free(number_text);
return text;
}
static void
unit_class_init(UnitClass *klass)
{
}
static void
unit_init(Unit *unit)
{
unit->priv = unit_get_instance_private (unit);
unit->priv->serializer = mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 2);
mp_serializer_set_leading_digits(unit->priv->serializer, 6);
}
|