X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fhomebank;a=blobdiff_plain;f=src%2Fhb-split.c;fp=src%2Fhb-split.c;h=3ff677ee2cd1d465c733f2df9d87ab9630d7ed61;hp=0000000000000000000000000000000000000000;hb=59c5e08a64798d4303ae7eb3a2713bc93d98fa7b;hpb=8988b3bef0760b4cab8144715cc3d8f55688861c
diff --git a/src/hb-split.c b/src/hb-split.c
new file mode 100644
index 0000000..3ff677e
--- /dev/null
+++ b/src/hb-split.c
@@ -0,0 +1,250 @@
+/* HomeBank -- Free, easy, personal accounting for everyone.
+ * Copyright (C) 1995-2016 Maxime DOYEN
+ *
+ * This file is part of HomeBank.
+ *
+ * HomeBank is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * HomeBank is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include "homebank.h"
+
+#include "hb-transaction.h"
+#include "hb-split.h"
+
+/****************************************************************************/
+/* Debug macros */
+/****************************************************************************/
+#define MYDEBUG 0
+
+#if MYDEBUG
+#define DB(x) (x);
+#else
+#define DB(x);
+#endif
+
+/* our global datas */
+extern struct HomeBank *GLOBALS;
+extern struct Preferences *PREFS;
+
+
+/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
+
+
+static void da_split_free(Split *item)
+{
+ if(item != NULL)
+ {
+ if(item->memo != NULL)
+ g_free(item->memo);
+
+ g_free(item);
+ }
+}
+
+
+static Split *da_split_malloc(void)
+{
+ return g_malloc0(sizeof(Split));
+}
+
+
+Split *da_split_new(guint32 kcat, gdouble amount, gchar *memo)
+{
+Split *split = da_split_malloc();
+
+ split->kcat = kcat;
+ split->amount = amount;
+ split->memo = g_strdup(memo);
+ return split;
+}
+
+
+
+static Split *da_split_record_clone(Split *src_split)
+{
+Split *new_split = g_memdup(src_split, sizeof(Split));
+
+ DB( g_print("da_split_record_clone\n") );
+
+ if(new_split)
+ {
+ //duplicate the string
+ new_split->memo = g_strdup(src_split->memo);
+ DB( g_print(" clone %p -> %p\n", src_split, new_split) );
+
+ }
+ return new_split;
+}
+
+/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
+
+
+guint da_splits_count(Split *txn_splits[])
+{
+guint i, count = 0;
+
+ for(i=0;i 0)
+ dtxn->flags |= OF_SPLIT;*/
+
+ DB( g_print(" clone %p -> %p, %d splits\n", stxn_splits, dtxn_splits, count) );
+ return count;
+}
+
+
+
+/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
+
+guint da_splits_parse(Split *ope_splits[], gchar *cats, gchar *amounts, gchar *memos)
+{
+gchar **cat_a, **amt_a, **mem_a;
+guint count, i;
+guint32 kcat;
+gdouble amount;
+Split *split;
+
+ DB( g_print(" split parse %s :: %s :: %s\n", cats, amounts, memos) );
+
+ cat_a = g_strsplit (cats, "||", 0);
+ amt_a = g_strsplit (amounts, "||", 0);
+ mem_a = g_strsplit (memos, "||", 0);
+
+ count = g_strv_length(amt_a);
+ if( (count == g_strv_length(cat_a)) && (count == g_strv_length(mem_a)) )
+ {
+ for(i=0;ikcat);
+ g_string_append(amt_a, g_ascii_dtostr (buf, sizeof (buf), ope_splits[i]->amount) );
+ g_string_append(mem_a, ope_splits[i]->memo);
+
+ if((i+1) < count)
+ {
+ g_string_append(cat_a, "||");
+ g_string_append(amt_a, "||");
+ g_string_append(mem_a, "||");
+ }
+ }
+
+ *cats = g_string_free(cat_a, FALSE);
+ *amounts = g_string_free(amt_a, FALSE);
+ *memos = g_string_free(mem_a, FALSE);
+
+ return count;
+}
+
+void split_cat_consistency (Split *txn_splits[])
+{
+ guint i, nbsplit;
+
+ // check split category #1340142
+ nbsplit = da_splits_count(txn_splits);
+ for(i=0;ikcat) == NULL)
+ {
+ g_warning("split consistency: fixed invalid split cat %d", txn_splits[i]->kcat);
+ txn_splits[i]->kcat = 0;
+ }
+ }
+}
+