[recipes/unit_convert_test: 1/10] Attempt at moving amount to double, unit failing



commit f0d597f84f5a4abc368090dead394d58f64ba604
Author: Paxana Amanda Xander <VeganBikePunk Gmail com>
Date:   Sun Jun 25 16:09:08 2017 -0700

    Attempt at moving amount to double, unit failing

 src/gr-ingredients-list.c   |   30 +++++++++++--------------
 src/gr-ingredients-list.h   |    8 ++++++
 src/gr-ingredients-viewer.c |   13 ++++++++--
 src/gr-unit.c               |   51 ++++++++++++++++++++++---------------------
 tests/ingredients-test.c    |    2 +-
 5 files changed, 58 insertions(+), 46 deletions(-)
---
diff --git a/src/gr-ingredients-list.c b/src/gr-ingredients-list.c
index 9fe693e..53b7549 100644
--- a/src/gr-ingredients-list.c
+++ b/src/gr-ingredients-list.c
@@ -30,16 +30,8 @@
 #include "gr-utils.h"
 
 
-typedef struct
-{
-        double amount;
-        gchar *unit;
-        gchar *name;
-        gchar *segment;
-} Ingredient;
-
 static void
-ingredient_free (Ingredient *ing)
+ingredient_free (IngredientObj *ing)
 {
         g_free (ing->name);
         g_free (ing->unit);
@@ -75,7 +67,7 @@ gr_ingredients_list_populate (GrIngredientsList  *ingredients,
                 char *segment;
                 const char *u;
                 const char *s;
-                Ingredient *ing;
+                IngredientObj *ing;
                 g_autoptr(GError) local_error = NULL;
 
                 if (lines[i][0] == '\0')
@@ -92,7 +84,7 @@ gr_ingredients_list_populate (GrIngredientsList  *ingredients,
                 ingredient = fields[2];
                 segment = fields[3];
 
-                ing = g_new0 (Ingredient, 1);
+                ing = g_new0 (IngredientObj, 1);
                 ing->amount = 1.0;
                 if (amount[0] != '\0' &&
                     !gr_number_parse (&ing->amount, &amount, &local_error)) {
@@ -172,7 +164,11 @@ gr_ingredients_list_validate (const char  *text,
 }
 
 static void
+<<<<<<< fd7e9ce6bc998a11c0b79f4b509fea298bf54776
 ingredient_scale_unit (Ingredient *ing, double scale, GString *s)
+=======
+ingredient_scale_unit (IngredientObj *ing, int num, int denom, GString *s)
+>>>>>>> Attempt at moving amount to double, unit failing
 {
         g_autofree char *scaled = NULL;
 
@@ -186,7 +182,7 @@ ingredient_scale_unit (Ingredient *ing, double scale, GString *s)
 }
 
 static void
-ingredient_scale (Ingredient *ing, int num, int denom, GString *s)
+ingredient_scale (IngredientObj *ing, int num, int denom, GString *s)
 {
         ingredient_scale_unit (ing, (double)num / (double)denom, s);
         g_string_append (s, " ");
@@ -205,7 +201,7 @@ gr_ingredients_list_scale (GrIngredientsList *ingredients,
         s = g_string_new ("");
 
         for (l = ingredients->ingredients; l; l = l->next) {
-                Ingredient *ing = (Ingredient *)l->data;
+                IngredientObj *ing = (IngredientObj *)l->data;
 
                 ingredient_scale (ing, num, denom, s);
         }
@@ -229,7 +225,7 @@ gr_ingredients_list_get_ingredients (GrIngredientsList *ingredients,
 
         ret = g_new0 (char *, g_list_length (ingredients->ingredients) + 1);
         for (i = 0, l = ingredients->ingredients; l; l = l->next) {
-                Ingredient *ing = (Ingredient *)l->data;
+                IngredientObj *ing = (IngredientObj *)l->data;
                 if (g_strcmp0 (segment, ing->segment) == 0)
                         ret[i++] = g_strdup (ing->name);
         }
@@ -246,7 +242,7 @@ gr_ingredients_list_scale_unit (GrIngredientsList *ingredients,
         GList *l;
 
         for (l = ingredients->ingredients; l; l = l->next) {
-                Ingredient *ing = (Ingredient *)l->data;
+                IngredientObj *ing = (IngredientObj *)l->data;
 
                 if (g_strcmp0 (segment, ing->segment) == 0 &&
                     g_strcmp0 (name, ing->name) == 0) {
@@ -270,7 +266,7 @@ gr_ingredients_list_get_unit (GrIngredientsList *ingredients,
         GList *l;
 
         for (l = ingredients->ingredients; l; l = l->next) {
-                Ingredient *ing = (Ingredient *)l->data;
+                IngredientObj *ing = (IngredientObj *)l->data;
 
                 if (g_strcmp0 (segment, ing->segment) == 0 &&
                     g_strcmp0 (name, ing->name) == 0) {
@@ -289,7 +285,7 @@ gr_ingredients_list_get_amount (GrIngredientsList *ingredients,
         GList *l;
 
         for (l = ingredients->ingredients; l; l = l->next) {
-                Ingredient *ing = (Ingredient *)l->data;
+                IngredientObj *ing = (IngredientObj *)l->data;
 
                 if (g_strcmp0 (segment, ing->segment) == 0 &&
                     g_strcmp0 (name, ing->name) == 0) {
diff --git a/src/gr-ingredients-list.h b/src/gr-ingredients-list.h
index ebc7341..1147491 100644
--- a/src/gr-ingredients-list.h
+++ b/src/gr-ingredients-list.h
@@ -26,6 +26,14 @@ G_BEGIN_DECLS
 
 #define GR_TYPE_INGREDIENTS_LIST (gr_ingredients_list_get_type ())
 
+typedef struct
+{
+        double amount;
+        gchar *unit;
+        gchar *name;
+        gchar *segment;
+} IngredientObj;
+
 G_DECLARE_FINAL_TYPE (GrIngredientsList, gr_ingredients_list, GR, INGREDIENTS_LIST, GObject)
 
 GrIngredientsList *gr_ingredients_list_new             (const char         *text);
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
index f2f8018..4e95ee1 100644
--- a/src/gr-ingredients-viewer.c
+++ b/src/gr-ingredients-viewer.c
@@ -27,6 +27,7 @@
 #include "gr-ingredients-list.h"
 #include "gr-ingredient.h"
 #include "gr-utils.h"
+#include "gr-number.h"
 
 #ifdef ENABLE_GSPELL
 #include <gspell/gspell.h>
@@ -318,14 +319,20 @@ gr_ingredients_viewer_set_ingredients (GrIngredientsViewer *viewer,
         for (i = 0; ings && ings[i]; i++) {
                 g_autofree char *s = NULL;
                 g_auto(GStrv) strv = NULL;
-                const char *amount;
+                double amount;
                 const char *unit;
                 GtkWidget *row;
 
                 s = gr_ingredients_list_scale_unit (ingredients, viewer->title, ings[i], viewer->scale);
-                strv = g_strsplit (s, " ", 2);
-                amount = strv[0];
+                //strv = g_strsplit (s, " ", 2);
+                //amount = strv[0];
                 unit = strv[1] ? strv[1] : "";
+                unit = gr_ingredients_list_get_unit(ingredients, viewer->title, ings[i]);
+                amount = gr_ingredients_list_get_amount(ingredients, viewer->title, ings[i]);
+
+                g_message("segment is %s", viewer->title);
+                g_message("amount is %f", amount);
+                g_message("unit is %s", unit);
 
                 row = g_object_new (GR_TYPE_INGREDIENTS_VIEWER_ROW,
                                     "amount", amount,
diff --git a/src/gr-unit.c b/src/gr-unit.c
index 2ceb41c..b9b95c1 100644
--- a/src/gr-unit.c
+++ b/src/gr-unit.c
@@ -26,7 +26,7 @@
 
 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
 };
 
@@ -35,33 +35,34 @@ typedef struct {
         const char *abbreviation;
         const char *display_name;
         const char *plural;
+        const char *measure;
 } 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") },
+        { "g",     NC_("unit abbreviation", "g"),     NC_("unit name", "gram"), NC_("unit plural", "grams"), 
NC_("unit measure", "weight") },
+        { "kg",    NC_("unit abbreviation", "kg"),    NC_("unit name", "kilogram"), NC_("unit plural", 
"kilograms"), NC_("unit measure", "weight") },
+        { "lb",    NC_("unit abbreviation", "lb"),    NC_("unit name", "pound"), NC_("unit plural", 
"pounds"), NC_("unit measure", "weight") },
+        { "oz",    NC_("unit abbreviation", "oz"),    NC_("unit name", "ounce"), NC_("unit plural", 
"ounces"), NC_("unit measure", "weight") },
+        { "l",     NC_("unit abbreviation", "l"),     NC_("unit name", "liter"), NC_("unit plural", 
"liters"), NC_("unit measure", "volume") },
+        { "dl",    NC_("unit abbreviation", "dl"),    NC_("unit name", "deciliter"), NC_("unit plural", 
"deciliters"), NC_("unit measure", "volume") },
+        { "ml",    NC_("unit abbreviation", "ml"),    NC_("unit name", "milliliter"), NC_("unit plural", 
"milliliters"), NC_("unit measure", "volume") },
+        { "fl oz", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural", 
"fluid ounces"), NC_("unit measure", "volume") },
+        { "fl. oz.", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural", 
"fluid ounces"), NC_("unit measure", "volume") },        
+        { "pt",    NC_("unit abbreviation", "pt"),    NC_("unit name", "pint"), NC_("unit plural", "pints"), 
NC_("unit measure", "volume") },
+        { "qt",    NC_("unit abbreviation", "qt"),    NC_("unit name", "quart"), NC_("unit plural", 
"quarts"), NC_("unit measure", "volume") },
+        { "gal",   NC_("unit abbreviation", "gal"),   NC_("unit name", "gallon"), NC_("unit plural", 
"gallons"), NC_("unit measure", "volume") },
+        { "cup",   NC_("unit abbreviation", "cup"),   NC_("unit name", "cup"), NC_("unit plural", "cups"), 
NC_("unit measure", "volume") },
+        { "tbsp",  NC_("unit abbreviation", "tbsp"),  NC_("unit name", "tablespoon"), NC_("unit plural", 
"tablespoons"), NC_("unit measure", "volume") },
+        { "tsp",   NC_("unit abbreviation", "tsp"),   NC_("unit name", "teaspoon"), NC_("unit plural", 
"teaspoons"), NC_("unit measure", "volume") },
+        { "box",   NC_("unit abbreviation", "box"),   NC_("unit name", "box"), NC_("unit plural", "boxes"), 
NC_("unit measure", "quantity") },
+        { "pkg",   NC_("unit abbreviation", "pkg"),   NC_("unit name", "package"), NC_("unit plural", 
"packages"), NC_("unit measure", "quantity") },
+        { "glass", NC_("unit abbreviation", "glass"), NC_("unit name", "glass"), NC_("unit plural", 
"glasses"), NC_("unit measure", "quantity") },
+        { "mm",    NC_("unit abbreviation", "mm"),    NC_("unit name", "millimeter"), NC_("unit plural", 
"millimeters"), NC_("unit measure", "length") },
+        { "cm",    NC_("unit abbreviation", "cm"),    NC_("unit name", "centimeter"), NC_("unit plural", 
"centimeters"), NC_("unit measure", "length") },
+        { "m",     NC_("unit abbreviation", "m"),     NC_("unit name", "meter"), NC_("unit plural", 
"meters"), NC_("unit measure", "length") },
+        { "st",    NC_("unit abbreviation", "st"),     NC_("unit name", "stone"), NC_("unit plural", 
"stone") , NC_("unit measure", "weight")},
+        { "pinch", NC_("unit abbreviation", "pinch"),     NC_("unit name", "pinch"), NC_("unit plural", 
"pinches"), NC_("unit measure", "volume") },
+        { "bunch", NC_("unit abbreviation", "bunch"), NC_("unit name", "bunch"), NC_("unit plural", 
"bunches"), NC_("unit measure", "quantity") },
 
 };
 
diff --git a/tests/ingredients-test.c b/tests/ingredients-test.c
index c4b55c2..ac2344c 100644
--- a/tests/ingredients-test.c
+++ b/tests/ingredients-test.c
@@ -53,7 +53,7 @@ test_file (const char *filename)
 
                 ingredients = gr_ingredients_list_new (contents);
                 for (l = ingredients->ingredients; l; l = l->next) {
-                        Ingredient *ing = (Ingredient *)l->data;
+                        IngredientObj *ing = (IngredientObj *)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, "NAME %s\n", ing->name);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]