r18924 - gnucash/trunk/src - Add a GDate kvp_value type including conversions from and to all sorts of thingys.

Christian Stimming cstim at code.gnucash.org
Wed Mar 17 15:23:20 EDT 2010


Author: cstim
Date: 2010-03-17 15:23:20 -0400 (Wed, 17 Mar 2010)
New Revision: 18924
Trac: http://svn.gnucash.org/trac/changeset/18924

Modified:
   gnucash/trunk/src/backend/xml/sixtp-dom-generators.c
   gnucash/trunk/src/backend/xml/sixtp-dom-parsers.c
   gnucash/trunk/src/backend/xml/sixtp-dom-parsers.h
   gnucash/trunk/src/engine/engine-helpers.c
   gnucash/trunk/src/engine/kvp-scm.c
   gnucash/trunk/src/engine/test/test-date.c
   gnucash/trunk/src/libqof/backend/file/qsf-backend.c
   gnucash/trunk/src/libqof/qof/gnc-date.c
   gnucash/trunk/src/libqof/qof/gnc-date.h
   gnucash/trunk/src/libqof/qof/kvp_frame.c
   gnucash/trunk/src/libqof/qof/kvp_frame.h
   gnucash/trunk/src/libqof/qof/qofbackend.c
Log:
Add a GDate kvp_value type including conversions from and to all sorts of thingys.

Also, removed the default: switch clauses when handling a
kvp_value so that the compiler will already complain about
unhandled enum values.

Modified: gnucash/trunk/src/backend/xml/sixtp-dom-generators.c
===================================================================
--- gnucash/trunk/src/backend/xml/sixtp-dom-generators.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/backend/xml/sixtp-dom-generators.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -276,6 +276,12 @@
         val_node = xmlNewTextChild(node, NULL, BAD_CAST tag, BAD_CAST kvp_value_get_string(val));
     else if (kvp_type == KVP_TYPE_TIMESPEC)
         val_node = NULL;
+    else if (kvp_type == KVP_TYPE_GDATE)
+    {
+        GDate d = kvp_value_get_gdate(val);
+        val_node = gdate_to_dom_tree(tag, &d);
+        xmlAddChild (node, val_node);
+    }
     else
         val_node = xmlNewTextChild(node, NULL, BAD_CAST tag, NULL);
 
@@ -311,6 +317,9 @@
         xmlAddChild (node, val_node);
     }
     break;
+    case KVP_TYPE_GDATE:
+        xmlSetProp(val_node, BAD_CAST "type", BAD_CAST "gdate");
+        break;
     case KVP_TYPE_BINARY:
     {
         guint64 size;
@@ -350,8 +359,6 @@
     }
     break;
 
-    default:
-        break;
     }
 }
 

Modified: gnucash/trunk/src/backend/xml/sixtp-dom-parsers.c
===================================================================
--- gnucash/trunk/src/backend/xml/sixtp-dom-parsers.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/backend/xml/sixtp-dom-parsers.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -246,6 +246,24 @@
     return ret;
 }
 
+kvp_value*
+dom_tree_to_gdate_kvp_value (xmlNodePtr node)
+{
+    GDate *date;
+    kvp_value *ret = NULL;
+
+    date = dom_tree_to_gdate(node);
+
+    if (date)
+    {
+        ret = kvp_value_new_gdate(*date);
+    }
+
+    g_free(date);
+
+    return ret;
+}
+
 gboolean
 string_to_binary(const gchar *str,  void **v, guint64 *data_len)
 {
@@ -368,6 +386,7 @@
     { "string", dom_tree_to_string_kvp_value },
     { "guid", dom_tree_to_guid_kvp_value },
     { "timespec", dom_tree_to_timespec_kvp_value },
+    { "gdate", dom_tree_to_gdate_kvp_value },
     { "binary", dom_tree_to_binary_kvp_value },
     { "list", dom_tree_to_list_kvp_value },
     { "frame", dom_tree_to_frame_kvp_value },

Modified: gnucash/trunk/src/backend/xml/sixtp-dom-parsers.h
===================================================================
--- gnucash/trunk/src/backend/xml/sixtp-dom-parsers.h	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/backend/xml/sixtp-dom-parsers.h	2010-03-17 19:23:20 UTC (rev 18924)
@@ -60,6 +60,7 @@
 kvp_value* dom_tree_to_binary_kvp_value(xmlNodePtr node);
 kvp_value* dom_tree_to_list_kvp_value(xmlNodePtr node);
 kvp_value* dom_tree_to_frame_kvp_value(xmlNodePtr node);
+kvp_value* dom_tree_to_gdate_kvp_value (xmlNodePtr node);
 
 gboolean dom_tree_to_integer(xmlNodePtr node, gint64 *daint);
 gboolean dom_tree_to_guint16(xmlNodePtr node, guint16 *i);

Modified: gnucash/trunk/src/engine/engine-helpers.c
===================================================================
--- gnucash/trunk/src/engine/engine-helpers.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/engine/engine-helpers.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -640,7 +640,7 @@
 gnc_scm2KvpValue (SCM value_scm)
 {
     KvpValueType value_t;
-    KvpValue *value;
+    KvpValue *value = NULL;
     SCM type_scm;
     SCM val_scm;
 
@@ -692,6 +692,13 @@
         break;
     }
 
+    case KVP_TYPE_GDATE:
+    {
+        Timespec ts = gnc_timepair2timespec (val_scm);
+        value = kvp_value_new_gdate(timespec_to_gdate(ts));
+        break;
+    }
+
     case KVP_TYPE_BINARY:
         return NULL;
         break;
@@ -742,9 +749,6 @@
         break;
     }
 
-    default:
-        PWARN ("unexpected type: %d", value_t);
-        return NULL;
     }
 
     return value;

Modified: gnucash/trunk/src/engine/kvp-scm.c
===================================================================
--- gnucash/trunk/src/engine/kvp-scm.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/engine/kvp-scm.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -104,6 +104,8 @@
             return SWIG_NewPointerObj(frame, SWIG_TypeQuery("_p_KvpFrame"), 0);
     }
     break;
+    case KVP_TYPE_GDATE:
+        return gnc_timespec2timepair(gdate_to_timespec(kvp_value_get_gdate(val)));
 
     /* FIXME: handle types below */
     case KVP_TYPE_BINARY:

Modified: gnucash/trunk/src/engine/test/test-date.c
===================================================================
--- gnucash/trunk/src/engine/test/test-date.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/engine/test/test-date.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -72,9 +72,29 @@
 check_conversion (const char * str, Timespec expected_ts)
 {
     Timespec ts;
+    int day, month, year;
+    GDate d1, d2;
 
     ts = gnc_iso8601_to_timespec_gmt (str);
 
+    // We test the conversion to GDate against the timespec2dmy
+    // conversion, and also the conversion back to timespec and again
+    // to GDate so that it is still the original GDate
+    gnc_timespec2dmy(ts, &day, &month, &year);
+    d1 = timespec_to_gdate(ts);
+    d2 = timespec_to_gdate(gdate_to_timespec(d1));
+    if ((g_date_compare(&d1, &d2) != 0)
+        || (g_date_get_day(&d1) != day)
+        || (g_date_get_month(&d1) != month)
+        || (g_date_get_year(&d1) != year))
+    {
+        fprintf (stderr,
+                 "\nmis-converted \"%s\" to GDate\n",
+                 str);
+        failure ("misconverted timespec");
+        return FALSE;
+    }
+
     if ((expected_ts.tv_sec != ts.tv_sec) || (expected_ts.tv_nsec != ts.tv_nsec))
     {
         fprintf (stderr,

Modified: gnucash/trunk/src/libqof/backend/file/qsf-backend.c
===================================================================
--- gnucash/trunk/src/libqof/backend/file/qsf-backend.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/libqof/backend/file/qsf-backend.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -1211,7 +1211,14 @@
     case KVP_TYPE_FRAME:
 //		return kvp_value_new_frame(value->value.frame);
         break;
+    case KVP_TYPE_GDATE:
+    {
+        GDate date;
+        g_date_clear(&date, 1);
+        g_date_set_parse(&date, content);
+        return kvp_value_new_gdate(date);
     }
+    }
     return NULL;
 }
 

Modified: gnucash/trunk/src/libqof/qof/gnc-date.c
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/libqof/qof/gnc-date.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -1441,6 +1441,22 @@
     return ts.tv_sec;
 }
 
+GDate timespec_to_gdate (Timespec ts)
+{
+    GDate result;
+    g_date_clear(&result, 1);
+    g_date_set_time_t(&result, timespecToTime_t(ts));
+    g_assert(g_date_valid(&result));
+    return result;
+}
+
+Timespec gdate_to_timespec (GDate d)
+{
+    return gnc_dmy2timespec(g_date_get_day(&d),
+                            g_date_get_month(&d),
+                            g_date_get_year(&d));
+}
+
 void
 gnc_tm_get_day_start (struct tm *tm, time_t time_val)
 {

Modified: gnucash/trunk/src/libqof/qof/gnc-date.h
===================================================================
--- gnucash/trunk/src/libqof/qof/gnc-date.h	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/libqof/qof/gnc-date.h	2010-03-17 19:23:20 UTC (rev 18924)
@@ -211,6 +211,12 @@
 /** Turns a Timespec into a time_t */
 time_t timespecToTime_t (Timespec ts);
 
+/** Turns a Timespec into a GDate */
+GDate timespec_to_gdate (Timespec ts);
+
+/** Turns a GDate into a Timespec  */
+Timespec gdate_to_timespec (GDate d);
+
 /** Convert a day, month, and year to a Timespec */
 Timespec gnc_dmy2timespec (gint day, gint month, gint year);
 

Modified: gnucash/trunk/src/libqof/qof/kvp_frame.c
===================================================================
--- gnucash/trunk/src/libqof/qof/kvp_frame.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/libqof/qof/kvp_frame.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -62,6 +62,7 @@
         KvpValueBinaryData binary;
         GList *list;
         KvpFrame *frame;
+        GDate gdate;
     } value;
 };
 
@@ -1185,6 +1186,15 @@
 }
 
 KvpValue *
+kvp_value_new_gdate(GDate value)
+{
+    KvpValue * retval = g_new0(KvpValue, 1);
+    retval->type       = KVP_TYPE_GDATE;
+    retval->value.gdate = value;
+    return retval;
+}
+
+KvpValue *
 kvp_value_new_binary(const void * value, guint64 datasize)
 {
     KvpValue * retval;
@@ -1285,7 +1295,8 @@
     case KVP_TYPE_GINT64:
     case KVP_TYPE_DOUBLE:
     case KVP_TYPE_NUMERIC:
-    default:
+    case KVP_TYPE_TIMESPEC:
+    case KVP_TYPE_GDATE:
         break;
     }
     g_free(value);
@@ -1381,6 +1392,18 @@
         return ts;
 }
 
+GDate
+kvp_value_get_gdate(const KvpValue * value)
+{
+    GDate date;
+    g_date_clear(&date, 1);
+    if (!value) return date;
+    if (value->type == KVP_TYPE_GDATE)
+        return value->value.gdate;
+    else
+        return date;
+}
+
 void *
 kvp_value_get_binary(const KvpValue * value, guint64 * size_return)
 {
@@ -1481,6 +1504,9 @@
     case KVP_TYPE_GUID:
         return kvp_value_new_guid(value->value.guid);
         break;
+    case KVP_TYPE_GDATE:
+        return kvp_value_new_gdate(value->value.gdate);
+        break;
     case KVP_TYPE_TIMESPEC:
         return kvp_value_new_timespec(value->value.timespec);
         break;
@@ -1557,6 +1583,9 @@
     case KVP_TYPE_TIMESPEC:
         return timespec_cmp(&(kva->value.timespec), &(kvb->value.timespec));
         break;
+    case KVP_TYPE_GDATE:
+        return g_date_compare(&(kva->value.gdate), &(kvb->value.gdate));
+        break;
     case KVP_TYPE_BINARY:
         /* I don't know that this is a good compare. Ab is bigger than Acef.
            But I'm not sure that actually matters here. */
@@ -1757,12 +1786,15 @@
             g_hash_table_foreach(frame->hash, kvp_frame_to_bare_string_helper, &tmp1);
         }
         return tmp1;
-        break;
     }
-    default:
-        return g_strdup_printf(" ");
-        break;
+    case KVP_TYPE_GDATE:
+        return g_strdup_printf("%04d-%02d-%02d",
+                               g_date_get_year(&val->value.gdate),
+                               g_date_get_month(&val->value.gdate),
+                               g_date_get_day(&val->value.gdate));
     }
+    g_assert(FALSE); /* must not be reached */
+    return g_strdup("");
 }
 
 gchar*
@@ -1837,10 +1869,14 @@
         return tmp2;
         break;
 
-    default:
-        return g_strdup_printf(" ");
-        break;
+    case KVP_TYPE_GDATE:
+        return g_strdup_printf("KVP_VALUE_GDATE(%04d-%02d-%02d)",
+                               g_date_get_year(&val->value.gdate),
+                               g_date_get_month(&val->value.gdate),
+                               g_date_get_day(&val->value.gdate));
     }
+    g_assert(FALSE); /* must not be reached */
+    return g_strdup("");
 }
 
 static void

Modified: gnucash/trunk/src/libqof/qof/kvp_frame.h
===================================================================
--- gnucash/trunk/src/libqof/qof/kvp_frame.h	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/libqof/qof/kvp_frame.h	2010-03-17 19:23:20 UTC (rev 18924)
@@ -96,6 +96,7 @@
     KVP_TYPE_BINARY,     /**< no QOF equivalent. */
     KVP_TYPE_GLIST,      /**< no QOF equivalent. */
     KVP_TYPE_FRAME       /**< no QOF equivalent. */
+    , KVP_TYPE_GDATE       /**< no QOF equivalent. */
 } KvpValueType;
 
 /** \deprecated Deprecated backwards compat token
@@ -564,6 +565,7 @@
 KvpValue   * kvp_value_new_timespec(Timespec timespec);
 KvpValue   * kvp_value_new_binary(const void * data, guint64 datasize);
 KvpValue   * kvp_value_new_frame(const KvpFrame * value);
+KvpValue   * kvp_value_new_gdate(GDate date);
 
 /** Creates a KvpValue from a <b>GList of kvp_value's</b>! (Not to be
  *  confused with GList's of something else!) */
@@ -647,6 +649,9 @@
 KvpFrame   * kvp_value_get_frame(const KvpValue * value);
 Timespec    kvp_value_get_timespec(const KvpValue * value);
 
+/** Value accessor for GDate */
+GDate    kvp_value_get_gdate(const KvpValue * value);
+
 /**
  * Similar returns as strcmp.
  **/

Modified: gnucash/trunk/src/libqof/qof/qofbackend.c
===================================================================
--- gnucash/trunk/src/libqof/qof/qofbackend.c	2010-03-17 10:54:35 UTC (rev 18923)
+++ gnucash/trunk/src/libqof/qof/qofbackend.c	2010-03-17 19:23:20 UTC (rev 18924)
@@ -261,7 +261,11 @@
     {
         break;  /* unsupported */
     }
+    case KVP_TYPE_GDATE    :
+    {
+        break;  /* unsupported */
     }
+    }
     if (value)
     {
         temp = g_strdup_printf("/%s", option->option_name);
@@ -379,7 +383,11 @@
     {
         break;  /* unsupported */
     }
+    case KVP_TYPE_GDATE    :
+    {
+        break;  /* unsupported */
     }
+    }
     parent = g_strdup_printf("/%s/%s", QOF_CONFIG_DESC, key);
     option.description = kvp_frame_get_string(helper->recursive, parent);
     g_free(parent);
@@ -438,7 +446,11 @@
     {
         break;  /* unsupported */
     }
+    case KVP_TYPE_GDATE    :
+    {
+        break;  /* unsupported */
     }
+    }
     LEAVE (" ");
 }
 



More information about the gnucash-changes mailing list