[gcalctool] Don't crash if currency rates have not been solved
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gcalctool] Don't crash if currency rates have not been solved
- Date: Mon, 31 Jan 2011 22:33:08 +0000 (UTC)
commit 5f536b4f1cef21bd59a93b7b147d27a3c5355f1a
Author: Robert Ancell <robert ancell canonical com>
Date: Tue Feb 1 09:10:53 2011 +1100
Don't crash if currency rates have not been solved
src/unit-category.c | 8 +++++---
src/unit.c | 24 ++++++++++++++++++------
src/unit.h | 4 ++--
3 files changed, 25 insertions(+), 11 deletions(-)
---
diff --git a/src/unit-category.c b/src/unit-category.c
index 972ef08..94f33ef 100644
--- a/src/unit-category.c
+++ b/src/unit-category.c
@@ -91,9 +91,11 @@ unit_category_convert(UnitCategory *category, const MPNumber *x, const char *x_u
unit_z = get_unit(category, z_units);
if (!unit_x || !unit_z)
return FALSE;
-
- unit_convert_from(unit_x, x, &t);
- unit_convert_to(unit_z, &t, z);
+
+ if (!unit_convert_from(unit_x, x, &t))
+ return FALSE;
+ if (!unit_convert_to(unit_z, &t, z))
+ return FALSE;
return TRUE;
}
diff --git a/src/unit.c b/src/unit.c
index ed12c5e..a529e86 100644
--- a/src/unit.c
+++ b/src/unit.c
@@ -97,7 +97,7 @@ get_variable(const char *name, MPNumber *z, void *data)
}
-static void
+static gboolean
solve_function(const gchar *function, const MPNumber *x, MPNumber *z)
{
MPEquationOptions options;
@@ -110,35 +110,47 @@ solve_function(const gchar *function, const MPNumber *x, MPNumber *z)
options.get_variable = get_variable;
options.callback_data = (void *)x;
ret = mp_equation_parse(function, &options, z, NULL);
- if (ret)
+ if (ret) {
g_warning("Failed to convert value: %s", function);
+ return FALSE;
+ }
+
+ return TRUE;
}
-void
+gboolean
unit_convert_from(Unit *unit, const MPNumber *x, MPNumber *z)
{
if (unit->priv->from_function)
- solve_function(unit->priv->from_function, x, z);
+ 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;
}
}
-void
+gboolean
unit_convert_to(Unit *unit, const MPNumber *x, MPNumber *z)
{
if (unit->priv->from_function)
- solve_function(unit->priv->to_function, x, z);
+ 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;
}
}
diff --git a/src/unit.h b/src/unit.h
index 9a1eb0f..459a2d6 100644
--- a/src/unit.h
+++ b/src/unit.h
@@ -38,9 +38,9 @@ gboolean unit_matches_symbol(Unit *unit, const gchar *symbol);
const GList *unit_get_symbols(Unit *unit);
-void unit_convert_from(Unit *unit, const MPNumber *x, MPNumber *z);
+gboolean unit_convert_from(Unit *unit, const MPNumber *x, MPNumber *z);
-void unit_convert_to(Unit *unit, const MPNumber *x, MPNumber *z);
+gboolean unit_convert_to(Unit *unit, const MPNumber *x, MPNumber *z);
gchar *unit_format(Unit *unit, MPNumber *x);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]