gnucash maint: Multiple changes pushed

Christopher Lam clam at code.gnucash.org
Sun Apr 25 19:16:38 EDT 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/ae7eb9b3 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/a2f52231 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/a37060c9 (commit)
	from  https://github.com/Gnucash/gnucash/commit/973a0bb7 (commit)



commit ae7eb9b3ad6ff81bc61f83f57b97ef5d80d76d60
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Sun Apr 25 12:25:18 2021 +0800

    [gnc-pricedb] extract common code into get_nearest_price
    
    Extracts common code in:
    
    * gnc_pricedb_get_nearest_before_price
    * gnc_pricedb_get_nearest_price
    * gnc_pricedb_get_latest_price

diff --git a/libgnucash/engine/gnc-pricedb.c b/libgnucash/engine/gnc-pricedb.c
index 05019feff..fb3d59869 100644
--- a/libgnucash/engine/gnc-pricedb.c
+++ b/libgnucash/engine/gnc-pricedb.c
@@ -2571,50 +2571,46 @@ direct_price_conversion (GNCPriceDB *db, const gnc_commodity *from,
     return retval;
 }
 
-gnc_numeric
-gnc_pricedb_get_nearest_before_price (GNCPriceDB *pdb,
-                                      const gnc_commodity *orig_currency,
-                                      const gnc_commodity *new_currency,
-                                      const time64 t)
+static gnc_numeric
+get_nearest_price (GNCPriceDB *pdb,
+                   const gnc_commodity *orig_curr,
+                   const gnc_commodity *new_curr,
+                   const time64 t,
+                   gboolean before)
 {
     gnc_numeric price;
 
-    if (gnc_commodity_equiv (orig_currency, new_currency))
+    if (gnc_commodity_equiv (orig_curr, new_curr))
         return gnc_numeric_create (1, 1);
 
     /* Look for a direct price. */
-    price = direct_price_conversion (pdb, orig_currency, new_currency, t, TRUE);
+    price = direct_price_conversion (pdb, orig_curr, new_curr, t, before);
 
     /*
      * no direct price found, try find a price in another currency
      */
     if (gnc_numeric_zero_p (price))
-        price = indirect_price_conversion (pdb, orig_currency, new_currency, t, TRUE);
+        price = indirect_price_conversion (pdb, orig_curr, new_curr, t, before);
 
     return gnc_numeric_reduce (price);
 }
 
+gnc_numeric
+gnc_pricedb_get_nearest_before_price (GNCPriceDB *pdb,
+                                      const gnc_commodity *orig_currency,
+                                      const gnc_commodity *new_currency,
+                                      const time64 t)
+{
+    return get_nearest_price (pdb, orig_currency, new_currency, t, TRUE);
+}
+
 gnc_numeric
 gnc_pricedb_get_nearest_price (GNCPriceDB *pdb,
                                const gnc_commodity *orig_currency,
                                const gnc_commodity *new_currency,
                                const time64 t)
 {
-    gnc_numeric price;
-
-    if (gnc_commodity_equiv (orig_currency, new_currency))
-        return gnc_numeric_create (1, 1);
-
-    /* Look for a direct price. */
-    price = direct_price_conversion (pdb, orig_currency, new_currency, t, FALSE);
-
-    /*
-     * no direct price found, try find a price in another currency
-     */
-    if (gnc_numeric_zero_p (price))
-        price = indirect_price_conversion (pdb, orig_currency, new_currency, t, FALSE);
-
-    return gnc_numeric_reduce (price);
+    return get_nearest_price (pdb, orig_currency, new_currency, t, FALSE);
 }
 
 gnc_numeric
@@ -2622,7 +2618,7 @@ gnc_pricedb_get_latest_price (GNCPriceDB *pdb,
                               const gnc_commodity *orig_currency,
                               const gnc_commodity *new_currency)
 {
-    return gnc_pricedb_get_nearest_price (pdb, orig_currency, new_currency, INT64_MAX);
+    return get_nearest_price (pdb, orig_currency, new_currency, INT64_MAX, FALSE);
 }
 
 static gnc_numeric
@@ -2638,10 +2634,7 @@ convert_amount_at_date (GNCPriceDB *pdb,
     if (gnc_numeric_zero_p (amount))
         return amount;
 
-    if (before_date)
-        price = gnc_pricedb_get_nearest_before_price (pdb, orig_currency, new_currency, t);
-    else
-        price = gnc_pricedb_get_nearest_price (pdb, orig_currency, new_currency, t);
+    price = get_nearest_price (pdb, orig_currency, new_currency, t, before_date);
 
     /* the price retrieved may be invalid. return zero. see 798015 */
     if (gnc_numeric_check (price))

commit a2f5223148e72eae61ebe37c2a21322f4f19710e
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Sun Apr 25 11:52:41 2021 +0800

    [gnc-pricedb] use g_list_find and plug memory leak
    
    found_coms was not being freed.

diff --git a/libgnucash/engine/gnc-pricedb.c b/libgnucash/engine/gnc-pricedb.c
index f3dae1dbd..05019feff 100644
--- a/libgnucash/engine/gnc-pricedb.c
+++ b/libgnucash/engine/gnc-pricedb.c
@@ -1878,16 +1878,6 @@ price_list_scan_any_currency(GList *price_list, gpointer data)
     return TRUE;
 }
 
-static gboolean
-is_in_list (GList *list, const gnc_commodity *c)
-{
-    GList *node;
-    for (node = list; node != NULL; node = g_list_next(node))
-        if ((gnc_commodity*)node->data == c)
-            return TRUE;
-    return FALSE;
-}
-
 /* This operates on the principal that the prices are sorted by date and that we
  * want only the first one before the specified time containing both the target
  * and some other commodity. */
@@ -1901,17 +1891,17 @@ latest_before (PriceList *prices, const gnc_commodity* target, time64 t)
         gnc_commodity *com = gnc_price_get_commodity(price);
         gnc_commodity *cur = gnc_price_get_currency(price);
         time64 price_t = gnc_price_get_time64(price);
+
         if (t < price_t ||
-            (com == target && is_in_list(found_coms, cur)) ||
-            (cur == target && is_in_list(found_coms, com)))
+            (com == target && g_list_find (found_coms, cur)) ||
+            (cur == target && g_list_find (found_coms, com)))
             continue;
-        else
-        {
-            gnc_price_ref(price);
-            retval = g_list_prepend(retval, price);
-            found_coms = g_list_prepend(found_coms, com == target ? cur : com);
-        }
+
+        gnc_price_ref (price);
+        retval = g_list_prepend (retval, price);
+        found_coms = g_list_prepend (found_coms, com == target ? cur : com);
     }
+    g_list_free (found_coms);
     return g_list_reverse(retval);
 }
 

commit a37060c95d18f96ae964ec3f646d270671efb601
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Sun Apr 25 09:46:56 2021 +0800

    ignore .vscode folder

diff --git a/.gitignore b/.gitignore
index f9ca4fde5..22637973a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,6 +17,7 @@
 .cproject
 .project
 .scm-links
+.vscode/
 ChangeLog
 Makefile
 Makefile.in



Summary of changes:
 .gitignore                      |  1 +
 libgnucash/engine/gnc-pricedb.c | 75 ++++++++++++++++-------------------------
 2 files changed, 30 insertions(+), 46 deletions(-)



More information about the gnucash-changes mailing list