2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2011 Robert Ancell.
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
15 #include "mp-serializer.h"
16 #include "mp-equation.h"
17 #include "currency-manager.h" // FIXME: Move out of here
27 MpSerializer
*serializer
;
30 G_DEFINE_TYPE (Unit
, unit
, G_TYPE_OBJECT
);
34 unit_new(const gchar
*name
,
35 const gchar
*display_name
,
37 const gchar
*from_function
,
38 const gchar
*to_function
,
41 Unit
*unit
= g_object_new(unit_get_type(), NULL
);
45 unit
->priv
->name
= g_strdup(name
);
46 unit
->priv
->display_name
= g_strdup(display_name
);
47 unit
->priv
->format
= g_strdup(format
);
48 unit
->priv
->from_function
= g_strdup(from_function
);
49 unit
->priv
->to_function
= g_strdup(to_function
);
50 symbol_names
= g_strsplit(symbols
, ",", 0);
51 for (i
= 0; symbol_names
[i
]; i
++)
52 unit
->priv
->symbols
= g_list_append(unit
->priv
->symbols
, g_strdup(symbol_names
[i
]));
60 unit_get_name(Unit
*unit
)
62 g_return_val_if_fail (unit
!= NULL
, NULL
);
63 return unit
->priv
->name
;
68 unit_get_display_name(Unit
*unit
)
70 g_return_val_if_fail (unit
!= NULL
, NULL
);
71 return unit
->priv
->display_name
;
76 unit_matches_symbol(Unit
*unit
, const gchar
*symbol
)
80 g_return_val_if_fail (unit
!= NULL
, FALSE
);
81 g_return_val_if_fail (symbol
!= NULL
, FALSE
);
83 for (iter
= unit
->priv
->symbols
; iter
; iter
= iter
->next
) {
84 gchar
*s
= iter
->data
;
85 if (strcmp(s
, symbol
) == 0)
94 unit_get_symbols(Unit
*unit
)
96 g_return_val_if_fail (unit
!= NULL
, NULL
);
97 return unit
->priv
->symbols
;
102 variable_is_defined(const char *name
, void *data
)
109 get_variable(const char *name
, MPNumber
*z
, void *data
)
112 mp_set_from_mp(x
, z
);
118 solve_function(const gchar
*function
, const MPNumber
*x
, MPNumber
*z
)
120 MPEquationOptions options
;
123 memset(&options
, 0, sizeof(options
));
125 options
.wordlen
= 32;
126 options
.variable_is_defined
= variable_is_defined
;
127 options
.get_variable
= get_variable
;
128 options
.callback_data
= (void *)x
;
129 ret
= mp_equation_parse(function
, &options
, z
, NULL
);
131 g_warning("Failed to convert value: %s", function
);
140 unit_convert_from(Unit
*unit
, const MPNumber
*x
, MPNumber
*z
)
142 g_return_val_if_fail(unit
!= NULL
, FALSE
);
143 g_return_val_if_fail(x
!= NULL
, FALSE
);
144 g_return_val_if_fail(x
!= NULL
, FALSE
);
146 if (unit
->priv
->from_function
)
147 return solve_function(unit
->priv
->from_function
, x
, z
);
149 // FIXME: Hack to make currency work
151 r
= currency_manager_get_value(currency_manager_get_default(), unit
->priv
->name
);
162 unit_convert_to(Unit
*unit
, const MPNumber
*x
, MPNumber
*z
)
164 g_return_val_if_fail(unit
!= NULL
, FALSE
);
165 g_return_val_if_fail(x
!= NULL
, FALSE
);
166 g_return_val_if_fail(x
!= NULL
, FALSE
);
168 if (unit
->priv
->from_function
)
169 return solve_function(unit
->priv
->to_function
, x
, z
);
171 // FIXME: Hack to make currency work
173 r
= currency_manager_get_value(currency_manager_get_default(), unit
->priv
->name
);
176 mp_multiply(x
, r
, z
);
184 unit_format(Unit
*unit
, MPNumber
*x
)
186 gchar
*number_text
, *text
;
188 g_return_val_if_fail(unit
!= NULL
, FALSE
);
189 g_return_val_if_fail(x
!= NULL
, FALSE
);
191 number_text
= mp_serializer_to_string(unit
->priv
->serializer
, x
);
192 text
= g_strdup_printf(unit
->priv
->format
, number_text
);
200 unit_class_init(UnitClass
*klass
)
202 g_type_class_add_private(klass
, sizeof(UnitPrivate
));
207 unit_init(Unit
*unit
)
209 unit
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(unit
, unit_get_type(), UnitPrivate
);
210 unit
->priv
->serializer
= mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC
, 10, 2);
211 mp_serializer_set_leading_digits(unit
->priv
->serializer
, 6);