r16292 - gnucash/branches/csv-import/src/import-export/csv - Added TransPropertyList struct and functions, fixed integer amount rounding error, added

Benjamin Sperisen lasindi at cvs.gnucash.org
Tue Jul 10 05:28:25 EDT 2007


Author: lasindi
Date: 2007-07-10 05:28:24 -0400 (Tue, 10 Jul 2007)
New Revision: 16292
Trac: http://svn.gnucash.org/trac/changeset/16292

Added:
   gnucash/branches/csv-import/src/import-export/csv/example-file.csv
Modified:
   gnucash/branches/csv-import/src/import-export/csv/Makefile.am
   gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c
Log:
Added TransPropertyList struct and functions, fixed integer amount rounding error, added 
example file


Modified: gnucash/branches/csv-import/src/import-export/csv/Makefile.am
===================================================================
--- gnucash/branches/csv-import/src/import-export/csv/Makefile.am	2007-07-10 00:28:55 UTC (rev 16291)
+++ gnucash/branches/csv-import/src/import-export/csv/Makefile.am	2007-07-10 09:28:24 UTC (rev 16292)
@@ -42,8 +42,7 @@
   ${GUILE_INCS} \
   ${QOF_CFLAGS} \
   ${GLIB_CFLAGS} \
-  $(GOFFICE_CFLAGS) \
-  -Wall
+  $(GOFFICE_CFLAGS)
 
 uidir = $(GNC_UI_DIR)
 ui_DATA = \

Added: gnucash/branches/csv-import/src/import-export/csv/example-file.csv
===================================================================
--- gnucash/branches/csv-import/src/import-export/csv/example-file.csv	2007-07-10 00:28:55 UTC (rev 16291)
+++ gnucash/branches/csv-import/src/import-export/csv/example-file.csv	2007-07-10 09:28:24 UTC (rev 16292)
@@ -0,0 +1,4 @@
+"This file has colon separators":100:01/03/95
+"and the last line":-50:02/28/96
+"uses a different form":-25.13:03/15/00
+"of date separator.":12.5:04-30-02

Modified: gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c
===================================================================
--- gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c	2007-07-10 00:28:55 UTC (rev 16291)
+++ gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c	2007-07-10 09:28:24 UTC (rev 16292)
@@ -55,7 +55,7 @@
 static time_t parse_date(const char* date_str, int format)
 {
   struct tm retvalue;
-  char mstr[3], dstr[3], ystr[3], *last_parsed_char;
+  char *last_parsed_char;
   last_parsed_char = strptime(date_str, date_format_internal[format], &retvalue);
   if(last_parsed_char != date_str + strlen(date_str))
   {
@@ -307,27 +307,34 @@
   return 0;
 }
 
-/** A struct encaspulating a property of a transaction. */
+/** A struct containing TransProperties that all describe a single transaction. */
 typedef struct
 {
+  int date_format; /**< The format for parsing dates */
+  Account* account; /**< The account the transaction belongs to */
+  GList* properties; /**< List of TransProperties */
+} TransPropertyList;
+
+/** A struct encapsulating a property of a transaction. */
+typedef struct
+{
   gboolean essential; /**< TRUE if every transaction needs this property */
   int type; /**< A value from the GncCsvColumnType enum except
              * GNC_CSV_NONE and GNC_CSV_NUM_COL_TYPES */
   void* value; /**< Pointer to the data that will be used to configure a transaction */
   /* TODO Try coming up with a more elegant way than storing this for
    * every transaction safely. */
-  int date_format; /**< The format for parsing dates */
+  TransPropertyList* set; /**< The set the property belongs to */
 } TransProperty;
 
 /** Constructor TransProperty.
  * @param type The type of the new property (see TransProperty.type for possible values)
- * @param date_format The format used for parsing dates (index in date_format_internal)
  */
-static TransProperty* trans_property_new(int type, int date_format)
+static TransProperty* trans_property_new(int type, TransPropertyList* set)
 {
   TransProperty* prop = g_malloc(sizeof(TransProperty));
   prop->type = type;
-  prop->date_format = date_format;
+  prop->set = set;
   
   switch(type)
   {
@@ -373,7 +380,7 @@
   {
   case GNC_CSV_DATE:
     prop->value = g_malloc(sizeof(time_t));
-    *((time_t*)(prop->value)) = parse_date(str, prop->date_format);
+    *((time_t*)(prop->value)) = parse_date(str, prop->set->date_format);
     return *((time_t*)(prop->value)) != -1;
 
   case GNC_CSV_DESCRIPTION:
@@ -388,31 +395,59 @@
     if(endptr != str + strlen(str))
       return FALSE;
 
-    *((gnc_numeric*)(prop->value)) = double_to_gnc_numeric(value, 1,
+    *((gnc_numeric*)(prop->value)) = double_to_gnc_numeric(value, xaccAccountGetCommoditySCU(prop->set->account),
                                                            GNC_RND_ROUND);
     return TRUE;
   }
   return FALSE; /* We should never actually get here. */
 }
 
-/** Creates a transaction from a list of "TransProperty"s.
- */
-static Transaction* trans_properties_to_trans(GList* properties, Account* account)
+/* TODO Comment */
+static TransPropertyList* trans_property_list_new(Account* account, int date_format)
 {
+  TransPropertyList* set = g_new(TransPropertyList, 1);
+  set->account = account;
+  set->date_format = date_format;
+  set->properties = NULL;
+  return set;
+}
+
+/* TODO Comment */
+static void trans_property_list_free(TransPropertyList* set)
+{
+  GList* properties_begin = set->properties;
+  while(set->properties != NULL)
+  {
+    trans_property_free((TransProperty*)(set->properties->data));
+    set->properties = g_list_next(set->properties);
+  }
+  g_list_free(properties_begin);
+  g_free(set);
+}
+
+/* TODO Comment */
+static void trans_property_list_add(TransProperty* property)
+{
+  property->set->properties = g_list_append(property->set->properties, property);
+}
+
+/* TODO Comment */
+static Transaction* trans_property_list_to_trans(TransPropertyList* set)
+{
   Transaction* trans;
   Split* split;
-  GList* properties_begin = properties;
-  GNCBook* book = gnc_account_get_book(account);
-  gnc_commodity* currency = xaccAccountGetCommodity(account);
+  GList* properties_begin = set->properties;
+  GNCBook* book = gnc_account_get_book(set->account);
+  gnc_commodity* currency = xaccAccountGetCommodity(set->account);
   gnc_numeric amount;
   
   unsigned int essential_properties_left = 2;
-  while(properties != NULL)
+  while(set->properties != NULL)
   {
-    if(((TransProperty*)(properties->data))->essential)
+    if(((TransProperty*)(set->properties->data))->essential)
       essential_properties_left--;
 
-    properties = g_list_next(properties);
+    set->properties = g_list_next(set->properties);
   }
   if(essential_properties_left)
     return NULL;
@@ -422,10 +457,10 @@
   xaccTransBeginEdit(trans);
   xaccTransSetCurrency(trans, currency);
 
-  properties = properties_begin;
-  while(properties != NULL)
+  set->properties = properties_begin;
+  while(set->properties != NULL)
   {
-    TransProperty* prop = (TransProperty*)(properties->data);
+    TransProperty* prop = (TransProperty*)(set->properties->data);
     switch(prop->type)
     {
     case GNC_CSV_DATE:
@@ -439,13 +474,13 @@
     case GNC_CSV_AMOUNT:
       amount = *((gnc_numeric*)(prop->value));
       split = xaccMallocSplit(book);
-      xaccSplitSetAccount(split, account);
+      xaccSplitSetAccount(split, set->account);
       xaccSplitSetParent(split, trans);
       xaccSplitSetAmount(split, amount);
       xaccSplitSetValue(split, amount);
       xaccSplitSetAction(split, "Deposit");
     }
-    properties = g_list_next(properties);
+    set->properties = g_list_next(set->properties);
   }
 
   return trans;
@@ -501,8 +536,7 @@
     GPtrArray* line = parse_data->orig_lines->pdata[i];
     /* This flag is TRUE if there are any errors in this row. */
     gboolean errors = FALSE;
-    GList* properties = NULL;
-    GList* properties_begin;
+    TransPropertyList* set = trans_property_list_new(account, parse_data->date_format);
     Transaction* trans = NULL;
 
     /* TODO There should eventually be a specification of what errors
@@ -514,12 +548,12 @@
       if(column_types->data[j] != GNC_CSV_NONE)
       {
         /* Affect the transaction appropriately. */
-        TransProperty* property = trans_property_new(column_types->data[j],
-                                                     parse_data->date_format);
+        TransProperty* property = trans_property_new(column_types->data[j], set);
         gboolean succeeded = trans_property_set(property, line->pdata[j]);
+        /* TODO Maybe move error handling to within TransPropertyList functions? */
         if(succeeded)
         {
-          properties = g_list_append(properties, property);
+          trans_property_list_add(property);
         }
         else
         {
@@ -533,19 +567,13 @@
     /* If we had success, add the transaction to parse_data->transaction. */
     if(!errors)
     {
-      trans = trans_properties_to_trans(properties, account);
+      trans = trans_property_list_to_trans(set);
       errors = trans == NULL;
     }
 
-    /* We need to free the properties and the properties list. */
-    properties_begin = properties;
-    while(properties != NULL)
-    {
-      trans_property_free((TransProperty*)(properties->data));
-      properties = g_list_next(properties);
-    }
-    g_list_free(properties_begin);
+    trans_property_list_free(set);
       
+
     /* If there was no success, add this line to parse_data->error_lines. */
     if(errors)
     {



More information about the gnucash-changes mailing list