[recipes/unit-enum] Change units around a bit
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes/unit-enum] Change units around a bit
- Date: Sun, 23 Jul 2017 02:45:09 +0000 (UTC)
commit cc152ff06e54e60ee172e3fa6fb77672530d2444
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Jul 22 22:37:36 2017 -0400
Change units around a bit
Introduce a GrUnit enum, and use the enum values as the
primary identifier for units, instead of a string. Update
all users.
src/gr-ingredients-list.c | 18 +++---
src/gr-ingredients-list.h | 4 +-
src/gr-ingredients-viewer-row.c | 10 +--
src/gr-shopping-page.c | 23 ++-----
src/gr-unit.c | 120 ++++++++++++++++++++++----------------
src/gr-unit.h | 47 ++++++++++++++--
tests/ingredients-test.c | 2 +-
tests/unit.c | 6 +-
8 files changed, 137 insertions(+), 93 deletions(-)
---
diff --git a/src/gr-ingredients-list.c b/src/gr-ingredients-list.c
index 9fe693e..13a75ad 100644
--- a/src/gr-ingredients-list.c
+++ b/src/gr-ingredients-list.c
@@ -33,7 +33,7 @@
typedef struct
{
double amount;
- gchar *unit;
+ GrUnit unit;
gchar *name;
gchar *segment;
} Ingredient;
@@ -42,7 +42,6 @@ static void
ingredient_free (Ingredient *ing)
{
g_free (ing->name);
- g_free (ing->unit);
g_free (ing->segment);
g_free (ing);
}
@@ -73,7 +72,7 @@ gr_ingredients_list_populate (GrIngredientsList *ingredients,
char *unit;
char *ingredient;
char *segment;
- const char *u;
+ GrUnit u;
const char *s;
Ingredient *ing;
g_autoptr(GError) local_error = NULL;
@@ -101,14 +100,13 @@ gr_ingredients_list_populate (GrIngredientsList *ingredients,
continue;
}
- u = "";
+ u = GR_UNIT_UNKNOWN;
if (unit[0] != '\0' &&
- ((u = gr_unit_parse (&unit, &local_error)) == NULL)) {
+ ((u = gr_unit_parse (&unit, &local_error)) == GR_UNIT_UNKNOWN)) {
g_message ("%s; using %s as-is", local_error->message, unit);
- u = unit;
}
- ing->unit = g_strdup (u);
+ ing->unit = u;
ing->segment = g_strdup (segment);
s = gr_ingredient_find (ingredient);
@@ -181,7 +179,7 @@ ingredient_scale_unit (Ingredient *ing, double scale, GString *s)
g_string_append (s, scaled);
if (ing->unit) {
g_string_append (s, " ");
- g_string_append (s, ing->unit);
+ g_string_append (s, gr_unit_get_abbreviation (ing->unit));
}
}
@@ -262,7 +260,7 @@ gr_ingredients_list_scale_unit (GrIngredientsList *ingredients,
return NULL;
}
-const char *
+GrUnit
gr_ingredients_list_get_unit (GrIngredientsList *ingredients,
const char *segment,
const char *name)
@@ -278,7 +276,7 @@ gr_ingredients_list_get_unit (GrIngredientsList *ingredients,
}
}
- return NULL;
+ return GR_UNIT_UNKNOWN;
}
double
diff --git a/src/gr-ingredients-list.h b/src/gr-ingredients-list.h
index ebc7341..fb08429 100644
--- a/src/gr-ingredients-list.h
+++ b/src/gr-ingredients-list.h
@@ -22,6 +22,8 @@
#include <gtk/gtk.h>
+#include "gr-unit.h"
+
G_BEGIN_DECLS
#define GR_TYPE_INGREDIENTS_LIST (gr_ingredients_list_get_type ())
@@ -42,7 +44,7 @@ char *gr_ingredients_list_scale_unit (GrIngredientsList *ingr
char **gr_ingredients_list_get_segments (GrIngredientsList *ingredients);
char **gr_ingredients_list_get_ingredients (GrIngredientsList *ingredients,
const char *segment);
-const char *gr_ingredients_list_get_unit (GrIngredientsList *list,
+GrUnit gr_ingredients_list_get_unit (GrIngredientsList *list,
const char *segment,
const char *ingredient);
double gr_ingredients_list_get_amount (GrIngredientsList *list,
diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
index 821afa0..88aa436 100644
--- a/src/gr-ingredients-viewer-row.c
+++ b/src/gr-ingredients-viewer-row.c
@@ -689,7 +689,6 @@ get_units_model (GrIngredientsViewerRow *row)
static GtkListStore *store = NULL;
if (store == NULL) {
- const char **names;
int i;
store = gtk_list_store_new (5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING);
@@ -707,8 +706,7 @@ get_units_model (GrIngredientsViewerRow *row)
4, "",
-1);
- names = gr_unit_get_names ();
- for (i = 0; names[i]; i++) {
+ for (i = GR_UNIT_UNKNOWN; i <= GR_LAST_UNIT; i++) {
const char *abbrev;
const char *name;
const char *plural;
@@ -716,9 +714,9 @@ get_units_model (GrIngredientsViewerRow *row)
g_autofree char *tmp = NULL;
g_autofree char *tmp2 = NULL;
- abbrev = gr_unit_get_abbreviation (names[i]);
- name = gr_unit_get_display_name (names[i]);
- plural = gr_unit_get_plural (names[i]);
+ abbrev = gr_unit_get_abbreviation (i);
+ name = gr_unit_get_display_name (i);
+ plural = gr_unit_get_plural (i);
if (strcmp (abbrev, name) == 0)
diff --git a/src/gr-shopping-page.c b/src/gr-shopping-page.c
index ba3387c..b64f58a 100644
--- a/src/gr-shopping-page.c
+++ b/src/gr-shopping-page.c
@@ -138,7 +138,7 @@ recount_recipes (GrShoppingPage *page)
typedef struct {
double amount;
- char *unit;
+ GrUnit unit;
} Unit;
typedef struct {
@@ -147,14 +147,6 @@ typedef struct {
gboolean removed;
} Ingredient;
-static void
-clear_unit (gpointer data)
-{
- Unit *unit = data;
-
- g_free (unit->unit);
-}
-
static Ingredient *
ingredient_new (const char *ingredient)
{
@@ -166,7 +158,6 @@ ingredient_new (const char *ingredient)
ing = g_new0 (Ingredient, 1);
ing->ingredient = g_strdup (ingredient);
ing->units = g_array_new (FALSE, TRUE, sizeof (Unit));
- g_array_set_clear_func (ing->units, clear_unit);
ing->removed = gr_recipe_store_not_shopping_ingredient (store, ingredient);
@@ -192,7 +183,7 @@ ingredient_clear (Ingredient *ing)
static void
ingredient_add (Ingredient *ing,
double amount,
- const char *unit)
+ GrUnit unit)
{
int i;
Unit nu;
@@ -200,14 +191,14 @@ ingredient_add (Ingredient *ing,
for (i = 0; i < ing->units->len; i++) {
Unit *u = &g_array_index (ing->units, Unit, i);
- if (g_strcmp0 (u->unit, unit) == 0) {
+ if (u->unit == unit) {
u->amount += amount;
return;
}
}
nu.amount = amount;
- nu.unit = g_strdup (unit);
+ nu.unit = unit;
g_array_append_val (ing->units, nu);
}
@@ -229,7 +220,7 @@ ingredient_format_unit (Ingredient *ing)
g_string_append (s, num);
g_string_append (s, " ");
if (u->unit)
- g_string_append (s, u->unit);
+ g_string_append (s, gr_unit_get_abbreviation (u->unit));
}
return g_strdup (s->str);
@@ -435,7 +426,7 @@ removed_row_activated (GtkListBox *list,
static void
add_ingredient (GrShoppingPage *page,
double amount,
- const char *unit,
+ GrUnit unit,
const char *ingredient)
{
Ingredient *ing;
@@ -464,7 +455,7 @@ collect_ingredients_from_recipe (GrShoppingPage *page,
g_auto(GStrv) ing = NULL;
ing = gr_ingredients_list_get_ingredients (il, seg[i]);
for (j = 0; ing[j]; j++) {
- const char *unit;
+ GrUnit unit;
double amount;
amount = gr_ingredients_list_get_amount (il, seg[i], ing[j]);
diff --git a/src/gr-unit.c b/src/gr-unit.c
index 2ceb41c..ffc4e2c 100644
--- a/src/gr-unit.c
+++ b/src/gr-unit.c
@@ -26,43 +26,44 @@
static const char * const unit_names[] = {
"g", "kg", "lb", "oz", "l", "dl", "ml", "fl oz", "pt", "qt", "gal", "cup",
- "tbsp", "tsp", "box", "pkg", "glass", "st", "pinch",
+ "tbsp", "tsp", "box", "pkg", "glass", "st", "pinch", "bunch",
NULL
};
typedef struct {
- const char *unit;
+ GrUnit unit;
+ GrDimension dimension;
+ const char *name;
const char *abbreviation;
const char *display_name;
const char *plural;
-} GrUnit;
-
-static GrUnit units[] = {
- { "g", NC_("unit abbreviation", "g"), NC_("unit name", "gram"), NC_("unit plural", "grams")
},
- { "kg", NC_("unit abbreviation", "kg"), NC_("unit name", "kilogram"), NC_("unit plural",
"kilograms") },
- { "lb", NC_("unit abbreviation", "lb"), NC_("unit name", "pound"), NC_("unit plural",
"pounds") },
- { "oz", NC_("unit abbreviation", "oz"), NC_("unit name", "ounce"), NC_("unit plural",
"ounces") },
- { "l", NC_("unit abbreviation", "l"), NC_("unit name", "liter"), NC_("unit plural",
"liters") },
- { "dl", NC_("unit abbreviation", "dl"), NC_("unit name", "deciliter"), NC_("unit plural",
"deciliters") },
- { "ml", NC_("unit abbreviation", "ml"), NC_("unit name", "milliliter"), NC_("unit plural",
"milliliters") },
- { "fl oz", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural",
"fluid ounces") },
- { "fl. oz.", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural",
"fluid ounces") },
- { "pt", NC_("unit abbreviation", "pt"), NC_("unit name", "pint"), NC_("unit plural", "pints")
},
- { "qt", NC_("unit abbreviation", "qt"), NC_("unit name", "quart"), NC_("unit plural",
"quarts") },
- { "gal", NC_("unit abbreviation", "gal"), NC_("unit name", "gallon"), NC_("unit plural",
"gallons") },
- { "cup", NC_("unit abbreviation", "cup"), NC_("unit name", "cup"), NC_("unit plural", "cups") },
- { "tbsp", NC_("unit abbreviation", "tbsp"), NC_("unit name", "tablespoon"), NC_("unit plural",
"tablespoons") },
- { "tsp", NC_("unit abbreviation", "tsp"), NC_("unit name", "teaspoon"), NC_("unit plural",
"teaspoons") },
- { "box", NC_("unit abbreviation", "box"), NC_("unit name", "box"), NC_("unit plural", "boxes") },
- { "pkg", NC_("unit abbreviation", "pkg"), NC_("unit name", "package"), NC_("unit plural",
"packages") },
- { "glass", NC_("unit abbreviation", "glass"), NC_("unit name", "glass"), NC_("unit plural",
"glasses") },
- { "mm", NC_("unit abbreviation", "mm"), NC_("unit name", "millimeter"), NC_("unit plural",
"millimeters") },
- { "cm", NC_("unit abbreviation", "cm"), NC_("unit name", "centimeter"), NC_("unit plural",
"centimeters") },
- { "m", NC_("unit abbreviation", "m"), NC_("unit name", "meter"), NC_("unit plural",
"meters") },
- { "st", NC_("unit abbreviation", "st"), NC_("unit name", "stone"), NC_("unit plural",
"stone") },
- { "pinch", NC_("unit abbreviation", "pinch"), NC_("unit name", "pinch"), NC_("unit plural",
"pinches") },
- { "bunch", NC_("unit abbreviation", "bunch"), NC_("unit name", "bunch"), NC_("unit plural",
"bunches") },
-
+} GrUnitData;
+
+static GrUnitData units[] = {
+ { GR_UNIT_GRAM, GR_DIMENSION_MASS, "g", NC_("unit abbreviation", "g"),
NC_("unit name", "gram"), NC_("unit plural", "grams") },
+ { GR_UNIT_KILOGRAM, GR_DIMENSION_MASS, "kg", NC_("unit abbreviation", "kg"),
NC_("unit name", "kilogram"), NC_("unit plural", "kilograms") },
+ { GR_UNIT_POUND, GR_DIMENSION_MASS, "lb", NC_("unit abbreviation", "lb"),
NC_("unit name", "pound"), NC_("unit plural", "pounds") },
+ { GR_UNIT_OUNCE, GR_DIMENSION_VOLUME, "oz", NC_("unit abbreviation", "oz"),
NC_("unit name", "ounce"), NC_("unit plural", "ounces") },
+ { GR_UNIT_LITER, GR_DIMENSION_VOLUME, "l", NC_("unit abbreviation", "l"),
NC_("unit name", "liter"), NC_("unit plural", "liters") },
+ { GR_UNIT_DECILITER, GR_DIMENSION_VOLUME, "dl", NC_("unit abbreviation", "dl"),
NC_("unit name", "deciliter"), NC_("unit plural", "deciliters") },
+ { GR_UNIT_MILLILITER, GR_DIMENSION_VOLUME, "ml", NC_("unit abbreviation", "ml"),
NC_("unit name", "milliliter"), NC_("unit plural", "milliliters") },
+ { GR_UNIT_FLUID_OUNCE, GR_DIMENSION_VOLUME, "fl oz", NC_("unit abbreviation", "fl oz"),
NC_("unit name", "fluid ounce"), NC_("unit plural", "fluid ounces") },
+ { GR_UNIT_FLUID_OUNCE, GR_DIMENSION_VOLUME, "fl. oz.", NC_("unit abbreviation", "fl oz"),
NC_("unit name", "fluid ounce"), NC_("unit plural", "fluid ounces") },
+ { GR_UNIT_PINT, GR_DIMENSION_VOLUME, "pt", NC_("unit abbreviation", "pt"),
NC_("unit name", "pint"), NC_("unit plural", "pints") },
+ { GR_UNIT_QUART, GR_DIMENSION_VOLUME, "qt", NC_("unit abbreviation", "qt"),
NC_("unit name", "quart"), NC_("unit plural", "quarts") },
+ { GR_UNIT_GALLON, GR_DIMENSION_VOLUME, "gal", NC_("unit abbreviation", "gal"),
NC_("unit name", "gallon"), NC_("unit plural", "gallons") },
+ { GR_UNIT_CUP, GR_DIMENSION_VOLUME, "cup", NC_("unit abbreviation", "cup"),
NC_("unit name", "cup"), NC_("unit plural", "cups") },
+ { GR_UNIT_TABLESPOON, GR_DIMENSION_VOLUME, "tbsp", NC_("unit abbreviation", "tbsp"),
NC_("unit name", "tablespoon"), NC_("unit plural", "tablespoons") },
+ { GR_UNIT_TEASPOON, GR_DIMENSION_VOLUME, "tsp", NC_("unit abbreviation", "tsp"),
NC_("unit name", "teaspoon"), NC_("unit plural", "teaspoons") },
+ { GR_UNIT_BOX, GR_DIMENSION_DISCRETE, "box", NC_("unit abbreviation", "box"),
NC_("unit name", "box"), NC_("unit plural", "boxes") },
+ { GR_UNIT_BOX, GR_DIMENSION_DISCRETE, "pkg", NC_("unit abbreviation", "pkg"),
NC_("unit name", "package"), NC_("unit plural", "packages") },
+ { GR_UNIT_GLASS, GR_DIMENSION_DISCRETE, "glass", NC_("unit abbreviation", "glass"),
NC_("unit name", "glass"), NC_("unit plural", "glasses") },
+ { GR_UNIT_MILLIMETER, GR_DIMENSION_LENGTH, "mm", NC_("unit abbreviation", "mm"),
NC_("unit name", "millimeter"), NC_("unit plural", "millimeters") },
+ { GR_UNIT_CENTIMETER, GR_DIMENSION_LENGTH, "cm", NC_("unit abbreviation", "cm"),
NC_("unit name", "centimeter"), NC_("unit plural", "centimeters") },
+ { GR_UNIT_METER, GR_DIMENSION_LENGTH, "m", NC_("unit abbreviation", "m"),
NC_("unit name", "meter"), NC_("unit plural", "meters") },
+ { GR_UNIT_STONE, GR_DIMENSION_MASS, "st", NC_("unit abbreviation", "st"),
NC_("unit name", "stone"), NC_("unit plural", "stone") },
+ { GR_UNIT_PINCH, GR_DIMENSION_DISCRETE, "pinch", NC_("unit abbreviation", "pinch"),
NC_("unit name", "pinch"), NC_("unit plural", "pinches") },
+ { GR_UNIT_BUNCH, GR_DIMENSION_DISCRETE, "bunch", NC_("unit abbreviation", "bunch"),
NC_("unit name", "bunch"), NC_("unit plural", "bunches") },
};
const char **
@@ -71,46 +72,64 @@ gr_unit_get_names (void)
return (const char **)unit_names;
}
-static GrUnit *
-find_unit (const char *name)
+static GrUnitData *
+find_unit (GrUnit unit)
{
int i;
for (i = 0; i < G_N_ELEMENTS (units); i++) {
- if (strcmp (units[i].unit, name) == 0)
+ if (units[i].unit == unit)
return &(units[i]);
}
return NULL;
}
const char *
-gr_unit_get_display_name (const char *name)
+gr_unit_get_name (GrUnit unit)
{
- GrUnit *unit = find_unit (name);
- if (unit)
- return g_dpgettext2 (NULL, "unit name", unit->display_name);
+ GrUnitData *data = find_unit (unit);
+ if (data)
+ return data->name;
return NULL;
}
const char *
-gr_unit_get_plural (const char *name)
+gr_unit_get_display_name (GrUnit unit)
{
- GrUnit *unit = find_unit (name);
- if (unit)
- return g_dpgettext2 (NULL, "unit plural", unit->plural);
- return NULL;
-
+ GrUnitData *data = find_unit (unit);
+ if (data)
+ return g_dpgettext2 (NULL, "unit name", data->display_name);
+ return NULL;
}
const char *
-gr_unit_get_abbreviation (const char *name)
+gr_unit_get_plural (GrUnit unit)
{
- GrUnit *unit = find_unit (name);
- if (unit)
- return g_dpgettext2 (NULL, "unit abbreviation", unit->abbreviation);
- return NULL;
+ GrUnitData *data = find_unit (unit);
+ if (data)
+ return g_dpgettext2 (NULL, "unit plural", data->plural);
+ return gr_unit_get_display_name (unit);
+
}
const char *
+gr_unit_get_abbreviation (GrUnit unit)
+{
+ GrUnitData *data = find_unit (unit);
+ if (data)
+ return g_dpgettext2 (NULL, "unit abbreviation", data->abbreviation);
+ return gr_unit_get_display_name (unit);
+}
+
+GrDimension
+gr_unit_get_dimension (GrUnit unit)
+{
+ GrUnitData *data = find_unit (unit);
+ if (data)
+ return data->dimension;
+ return GR_DIMENSION_DISCRETE;
+}
+
+GrUnit
gr_unit_parse (char **input,
GError **error)
{
@@ -134,7 +153,7 @@ gr_unit_parse (char **input,
}
for (i = 0; i < G_N_ELEMENTS (units); i++) {
- nu = units[i].unit;
+ nu = units[i].name;
if (g_str_has_prefix (*input, nu) && space_or_nul ((*input)[strlen (nu)])) {
*input += strlen (nu);
return units[i].unit;
@@ -144,6 +163,5 @@ gr_unit_parse (char **input,
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("I don’t know this unit: %s"), *input);
- return NULL;
+ return GR_UNIT_UNKNOWN;
}
-
diff --git a/src/gr-unit.h b/src/gr-unit.h
index 46d8278..57a1395 100644
--- a/src/gr-unit.h
+++ b/src/gr-unit.h
@@ -22,12 +22,49 @@
G_BEGIN_DECLS
-const char *gr_unit_parse (char **string,
- GError **error);
+typedef enum {
+ GR_UNIT_UNKNOWN,
+ GR_UNIT_GRAM,
+ GR_UNIT_KILOGRAM,
+ GR_UNIT_POUND,
+ GR_UNIT_OUNCE,
+ GR_UNIT_LITER,
+ GR_UNIT_DECILITER,
+ GR_UNIT_MILLILITER,
+ GR_UNIT_FLUID_OUNCE,
+ GR_UNIT_PINT,
+ GR_UNIT_QUART,
+ GR_UNIT_GALLON,
+ GR_UNIT_CUP,
+ GR_UNIT_TABLESPOON,
+ GR_UNIT_TEASPOON,
+ GR_UNIT_BOX,
+ GR_UNIT_PACKAGE,
+ GR_UNIT_GLASS,
+ GR_UNIT_MILLIMETER,
+ GR_UNIT_CENTIMETER,
+ GR_UNIT_METER,
+ GR_UNIT_STONE,
+ GR_UNIT_PINCH,
+ GR_UNIT_BUNCH,
+ GR_LAST_UNIT = GR_UNIT_BUNCH
+} GrUnit;
+
+typedef enum {
+ GR_DIMENSION_DISCRETE,
+ GR_DIMENSION_VOLUME,
+ GR_DIMENSION_MASS,
+ GR_DIMENSION_LENGTH
+} GrDimension;
+
+GrUnit gr_unit_parse (char **string,
+ GError **error);
const char **gr_unit_get_names (void);
-const char *gr_unit_get_abbreviation (const char *name);
-const char *gr_unit_get_display_name (const char *name);
-const char *gr_unit_get_plural (const char *name);
+const char *gr_unit_get_name (GrUnit unit);
+const char *gr_unit_get_abbreviation (GrUnit unit);
+const char *gr_unit_get_display_name (GrUnit unit);
+const char *gr_unit_get_plural (GrUnit unit);
+GrDimension gr_unit_get_dimension (GrUnit unit);
G_END_DECLS
diff --git a/tests/ingredients-test.c b/tests/ingredients-test.c
index c4b55c2..5386a11 100644
--- a/tests/ingredients-test.c
+++ b/tests/ingredients-test.c
@@ -55,7 +55,7 @@ test_file (const char *filename)
for (l = ingredients->ingredients; l; l = l->next) {
Ingredient *ing = (Ingredient *)l->data;
g_string_append_printf (string, "AMOUNT %f\n", ing->amount);
- g_string_append_printf (string, "UNIT %s\n", ing->unit);
+ g_string_append_printf (string, "UNIT %s\n", gr_unit_get_name (ing->unit));
g_string_append_printf (string, "NAME %s\n", ing->name);
g_string_append_printf (string, "\n");
}
diff --git a/tests/unit.c b/tests/unit.c
index 0b8981a..ba56f60 100644
--- a/tests/unit.c
+++ b/tests/unit.c
@@ -30,20 +30,20 @@ static void
test_line (const char *line)
{
char *input;
- const char *unit;
+ GrUnit unit;
g_autoptr(GError) error = NULL;
g_string_append_printf (string, "INPUT '%s'\n", line);
input = (char *)line;
unit = gr_unit_parse (&input, &error);
- if (!unit) {
+ if (unit == GR_UNIT_UNKNOWN) {
g_string_append_printf (string, "REST '%s'\n", input);
g_string_append_printf (string, "ERROR %s\n", error->message);
}
else {
g_string_append_printf (string, "REST '%s'\n", input);
- g_string_append_printf (string, "UNIT %s\n", unit);
+ g_string_append_printf (string, "UNIT %s\n", gr_unit_get_name (unit));
g_string_append_printf (string, "NAME %s\n", gr_unit_get_display_name (unit));
g_string_append_printf (string, "ABBREVIATION %s\n", gr_unit_get_abbreviation (unit));
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]