r18979 - gnucash/trunk/src/gnome - Replace IF_TYPE macro with a validation function

Geert Janssens gjanssens at code.gnucash.org
Tue Mar 30 10:29:53 EDT 2010


Author: gjanssens
Date: 2010-03-30 10:29:53 -0400 (Tue, 30 Mar 2010)
New Revision: 18979
Trac: http://svn.gnucash.org/trac/changeset/18979

Modified:
   gnucash/trunk/src/gnome/top-level.c
Log:
Replace IF_TYPE macro with a validation function
The macro is quite ugly: it's not a complete if condition (missing closing bracket)
which makes the code using it difficult to read and error-prone.
Even astyle got confused by it.
The replacement code is slightly longer (two lines per replaced IF_TYPE invocation)
but clean and readable.

Modified: gnucash/trunk/src/gnome/top-level.c
===================================================================
--- gnucash/trunk/src/gnome/top-level.c	2010-03-30 10:58:43 UTC (rev 18978)
+++ gnucash/trunk/src/gnome/top-level.c	2010-03-30 14:29:53 UTC (rev 18979)
@@ -77,27 +77,31 @@
 /* ============================================================== */
 /* HTML Handler for reports. */
 
-#define IF_TYPE(URL_TYPE_STR,ENTITY_TYPE)                                   \
-  if (strncmp (URL_TYPE_STR, location, strlen (URL_TYPE_STR)) == 0)         \
-  {                                                                         \
-    GncGUID guid;                                                              \
-    QofCollection *col;                                                     \
-    QofInstance *entity;                                                      \
-    if (!string_to_guid (location + strlen(URL_TYPE_STR), &guid))           \
-    {                                                                       \
-      result->error_message = g_strdup_printf (_("Bad URL: %s"), location); \
-      return FALSE;                                                         \
-    }                                                                       \
-    col = qof_book_get_collection (book, ENTITY_TYPE);                      \
-    entity = qof_collection_lookup_entity (col, &guid);                     \
-    if (NULL == entity)                                                     \
-    {                                                                       \
-      result->error_message = g_strdup_printf (_("Entity Not Found: %s"),   \
-                                               location);                   \
-      return FALSE;                                                         \
-    }                                                                       \
- 
+static gboolean
+validate_type(const char *url_type, const char *location,
+              const char *entity_type, GNCURLResult *result,
+              GncGUID *guid, QofInstance **entity)
+{
+    QofCollection *col;
+    QofBook     * book = gnc_get_current_book();
+    if (!string_to_guid (location + strlen(url_type), guid))
+    {
+      result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
+      return FALSE;
+    }
+    col = qof_book_get_collection (book, entity_type);
+    *entity = qof_collection_lookup_entity (col, guid);
+    if (NULL == *entity)
+    {
+      result->error_message = g_strdup_printf (_("Entity Not Found: %s"),
+                                               location);
+      return FALSE;
+    }
 
+    return TRUE;
+}
+
+
 static gboolean
 gnc_html_register_url_cb (const char *location, const char *label,
                           gboolean new_window, GNCURLResult *result)
@@ -109,6 +113,8 @@
     Transaction * trans;
     GList       * node;
     QofBook     * book = gnc_get_current_book();
+    GncGUID       guid;
+    QofInstance * entity = NULL;
 
     g_return_val_if_fail (location != NULL, FALSE);
     g_return_val_if_fail (result != NULL, FALSE);
@@ -123,80 +129,100 @@
     }
 
     /* href="gnc-register:guid=12345678901234567890123456789012" */
-    else IF_TYPE ("acct-guid=", GNC_ID_ACCOUNT)
+    else if (strncmp ("acct-guid=", location, strlen ("acct-guid=")) == 0)
+    {
+        if (!validate_type("acct-guid=", location, GNC_ID_ACCOUNT, result, &guid, &entity))
+            return FALSE;
+
         account = GNC_ACCOUNT(entity);
-}
+    }
 
-else IF_TYPE ("trans-guid=", GNC_ID_TRANS)
-    trans = (Transaction *) entity;
+    else if (strncmp ("trans-guid=", location, strlen ("trans-guid=")) == 0)
+    {
+        if (!validate_type("trans-guid=", location, GNC_ID_TRANS, result, &guid, &entity))
+            return FALSE;
 
-for (node = xaccTransGetSplitList (trans); node; node = node->next)
-{
-    split = node->data;
-    account = xaccSplitGetAccount(split);
-    if (account) break;
-}
+        trans = (Transaction *) entity;
 
-if (!account)
-{
-    result->error_message =
-        g_strdup_printf (_("Transaction with no Accounts: %s"), location);
-    return FALSE;
-}
-}
-else IF_TYPE ("split-guid=", GNC_ID_SPLIT)
-    split = (Split *) entity;
+        for (node = xaccTransGetSplitList (trans); node; node = node->next)
+        {
+            split = node->data;
+            account = xaccSplitGetAccount(split);
+            if (account) break;
+        }
+
+        if (!account)
+        {
+            result->error_message =
+                g_strdup_printf (_("Transaction with no Accounts: %s"), location);
+            return FALSE;
+        }
+    }
+
+    else if (strncmp ("split-guid=", location, strlen ("split-guid=")) == 0)
+    {
+        if (!validate_type("split-guid=", location, GNC_ID_SPLIT, result, &guid, &entity))
+            return FALSE;
+
+        split = (Split *) entity;
         account = xaccSplitGetAccount(split);
-                  }
-                  else
-{
-    result->error_message =
-        g_strdup_printf (_("Unsupported entity type: %s"), location);
-    return FALSE;
-}
+    }
+    else
+    {
+        result->error_message =
+            g_strdup_printf (_("Unsupported entity type: %s"), location);
+        return FALSE;
+    }
 
-page = gnc_plugin_page_register_new (account, FALSE);
-       gnc_main_window_open_page (NULL, page);
-       if (split)
-{
-    gsr = gnc_plugin_page_register_get_gsr(page);
-    gnc_split_reg_jump_to_split( gsr, split );
+    page = gnc_plugin_page_register_new (account, FALSE);
+    gnc_main_window_open_page (NULL, page);
+    if (split)
+    {
+        gsr = gnc_plugin_page_register_get_gsr(page);
+        gnc_split_reg_jump_to_split( gsr, split );
+    }
+
+    return TRUE;
 }
 
-return TRUE;
-       }
+/* ============================================================== */
 
-       /* ============================================================== */
+static gboolean
+gnc_html_price_url_cb (const char *location, const char *label,
+                       gboolean new_window, GNCURLResult *result)
+{
+    QofBook     * book = gnc_get_current_book();
+    GncGUID       guid;
+    QofInstance * entity = NULL;
 
-       static gboolean
-       gnc_html_price_url_cb (const char *location, const char *label,
-                              gboolean new_window, GNCURLResult *result)
-{
-    QofBook * book = gnc_get_current_book();
     g_return_val_if_fail (location != NULL, FALSE);
     g_return_val_if_fail (result != NULL, FALSE);
 
     result->load_to_stream = FALSE;
 
     /* href="gnc-register:guid=12345678901234567890123456789012" */
-    IF_TYPE ("price-guid=", GNC_ID_PRICE)
-    if (!gnc_price_edit_by_guid (NULL, &guid))
+    if (strncmp ("price-guid=", location, strlen ("price-guid=")) == 0)
     {
-        result->error_message = g_strdup_printf (_("No such price: %s"),
-                                location);
+        if (!validate_type("price-guid=", location, GNC_ID_PRICE, result, &guid, &entity))
+            return FALSE;
+
+        if (!gnc_price_edit_by_guid (NULL, &guid))
+        {
+            result->error_message = g_strdup_printf (_("No such price: %s"),
+                                                     location);
+            return FALSE;
+        }
+    }
+    else
+    {
+        result->error_message = g_strdup_printf (_("Badly formed URL %s"),
+                                                 location);
         return FALSE;
     }
+
+    return TRUE;
 }
-else
-{
-    result->error_message = g_strdup_printf (_("Badly formed URL %s"),
-                            location);
-    return FALSE;
-}
 
-return TRUE;
-       }
-
        /** Restore all persistent program state.  This function finds the
         *  "new" state file associated with a specific book guid.  It then
         *  iterates through this state information, calling a helper function



More information about the gnucash-changes mailing list