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
14 #include "unit-category.h"
16 struct UnitCategoryPrivate
23 G_DEFINE_TYPE (UnitCategory
, unit_category
, G_TYPE_OBJECT
);
27 unit_category_new(const gchar
*name
, const gchar
*display_name
)
29 UnitCategory
*category
= g_object_new(unit_category_get_type(), NULL
);
30 category
->priv
->name
= g_strdup(name
);
31 category
->priv
->display_name
= g_strdup(display_name
);
37 unit_category_get_name(UnitCategory
*category
)
39 g_return_val_if_fail (category
!= NULL
, NULL
);
40 return category
->priv
->name
;
45 unit_category_get_display_name(UnitCategory
*category
)
47 g_return_val_if_fail (category
!= NULL
, NULL
);
48 return category
->priv
->display_name
;
53 unit_category_add_unit(UnitCategory
*category
, Unit
*unit
)
55 g_return_if_fail (category
!= NULL
);
56 g_return_if_fail (unit
!= NULL
);
57 category
->priv
->units
= g_list_append(category
->priv
->units
, g_object_ref(unit
));
62 unit_category_get_unit_by_name(UnitCategory
*category
, const gchar
*name
)
66 g_return_val_if_fail (category
!= NULL
, NULL
);
67 g_return_val_if_fail (name
!= NULL
, NULL
);
69 for (iter
= category
->priv
->units
; iter
; iter
= iter
->next
)
71 Unit
*unit
= iter
->data
;
72 if (strcmp(unit_get_name(unit
), name
) == 0)
81 unit_category_get_unit_by_symbol(UnitCategory
*category
, const gchar
*symbol
)
85 g_return_val_if_fail (category
!= NULL
, NULL
);
86 g_return_val_if_fail (symbol
!= NULL
, NULL
);
88 for (iter
= category
->priv
->units
; iter
; iter
= iter
->next
) {
89 Unit
*unit
= iter
->data
;
90 if (unit_matches_symbol(unit
, symbol
))
99 unit_category_get_units(UnitCategory
*category
)
101 g_return_val_if_fail (category
!= NULL
, NULL
);
102 return category
->priv
->units
;
107 unit_category_convert(UnitCategory
*category
, const MPNumber
*x
, Unit
*x_units
, Unit
*z_units
, MPNumber
*z
)
111 g_return_val_if_fail (category
!= NULL
, FALSE
);
112 g_return_val_if_fail (x_units
!= NULL
, FALSE
);
113 g_return_val_if_fail (z_units
!= NULL
, FALSE
);
114 g_return_val_if_fail (z
!= NULL
, FALSE
);
116 if (!unit_convert_from(x_units
, x
, &t
))
118 if (!unit_convert_to(z_units
, &t
, z
))
126 unit_category_class_init(UnitCategoryClass
*klass
)
128 g_type_class_add_private(klass
, sizeof(UnitCategoryPrivate
));
133 unit_category_init(UnitCategory
*category
)
135 category
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(category
, unit_category_get_type(), UnitCategoryPrivate
);