[recipes] Improve the error checking



commit 86448154e1e57ba576b17374d5b56b45c8a86d32
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun May 14 21:47:07 2017 -0400

    Improve the error checking
    
    In the unit field, we now allow only something we can parse
    as a number, possibly followed by the name of a unit (which
    may not be known to us).

 src/gr-ingredients-viewer-row.c |   34 +++++++++++++---------------------
 1 files changed, 13 insertions(+), 21 deletions(-)
---
diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
index 2cadb85..00be91d 100644
--- a/src/gr-ingredients-viewer-row.c
+++ b/src/gr-ingredients-viewer-row.c
@@ -27,6 +27,7 @@
 #include "gr-ingredients-viewer.h"
 #include "gr-ingredient.h"
 #include "gr-unit.h"
+#include "gr-utils.h"
 #include "gr-number.h"
 #include "gr-recipe-store.h"
 
@@ -332,32 +333,23 @@ parse_unit (const char  *text,
             char       **amount,
             char       **unit)
 {
-        g_autofree char *tmp = NULL;
-        g_autofree char **strv = NULL;
-
-        tmp = g_strstrip (g_strdup (text));
-        strv = g_strsplit (tmp, " ", 2);
+        char *tmp;
+        const char *str;
+        GrNumber number;
 
         g_clear_pointer (amount, g_free);
         g_clear_pointer (unit, g_free);
 
-        if (g_strv_length (strv) > 1) {
-                GrNumber number;
-                g_autoptr(GError) error = NULL;
-                char *tmp = strv[0];
-
-                g_print ("parsing %s as number\n", tmp);
-                if (!gr_number_parse (&number, &tmp, &error)) {
-                        g_print ("failed to parse number: %s\n", error->message);
-                        return FALSE;
-                }
+        tmp = (char *)text;
+        skip_whitespace (&tmp);
+        str = tmp;
+        if (!gr_number_parse (&number, &tmp, NULL))
+                return FALSE;
 
-                *amount = strv[0];
-                *unit = strv[1];
-        }
-        else {
-                *amount = strv[0];
-        }
+        *amount = g_strndup (str, tmp - str);
+        skip_whitespace (&tmp);
+        if (tmp)
+                *unit = g_strdup (tmp);
 
         return TRUE;
 }


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