]> Dogcows Code - chaz/homebank/blob - src/hb-split.c
Merge branch 'upstream'
[chaz/homebank] / src / hb-split.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21
22 #include "hb-transaction.h"
23 #include "hb-split.h"
24
25 /****************************************************************************/
26 /* Debug macros */
27 /****************************************************************************/
28 #define MYDEBUG 0
29
30 #if MYDEBUG
31 #define DB(x) (x);
32 #else
33 #define DB(x);
34 #endif
35
36 /* our global datas */
37 extern struct HomeBank *GLOBALS;
38 extern struct Preferences *PREFS;
39
40
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
42
43
44 static void da_split_free(Split *item)
45 {
46 if(item != NULL)
47 {
48 if(item->memo != NULL)
49 g_free(item->memo);
50
51 g_free(item);
52 }
53 }
54
55
56 static Split *da_split_malloc(void)
57 {
58 return g_malloc0(sizeof(Split));
59 }
60
61
62 Split *da_split_new(guint32 kcat, gdouble amount, gchar *memo)
63 {
64 Split *split = da_split_malloc();
65
66 split->kcat = kcat;
67 split->amount = amount;
68 split->memo = g_strdup(memo);
69 return split;
70 }
71
72
73
74 static Split *da_split_record_clone(Split *src_split)
75 {
76 Split *new_split = g_memdup(src_split, sizeof(Split));
77
78 DB( g_print("da_split_record_clone\n") );
79
80 if(new_split)
81 {
82 //duplicate the string
83 new_split->memo = g_strdup(src_split->memo);
84 DB( g_print(" clone %p -> %p\n", src_split, new_split) );
85
86 }
87 return new_split;
88 }
89
90 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
91
92
93 guint da_splits_count(Split *txn_splits[])
94 {
95 guint i, count = 0;
96
97 for(i=0;i<TXN_MAX_SPLIT;i++)
98 {
99 if(txn_splits[i] == NULL)
100 break;
101 count++;
102 }
103 return count;
104 }
105
106
107 void da_splits_free(Split *txn_splits[])
108 {
109 guint count, i=0;
110
111 count = da_splits_count(txn_splits);
112 if(count == 0)
113 return;
114
115 DB( g_print("da_splits_free\n") );
116
117 for(;i<=count;i++)
118 {
119 DB( g_print("- freeing %d :: %p\n", i, txn_splits[i]) );
120
121 da_split_free(txn_splits[i]);
122 txn_splits[i] = NULL;
123 }
124 }
125
126
127 void da_splits_append(Split *txn_splits[], Split *new_split)
128 {
129 guint count = da_splits_count(txn_splits);
130
131 DB( g_print("da_splits_append\n") );
132
133 DB( g_print("- split[%d] at %p for ope \n", count, new_split) );
134
135 txn_splits[count] = new_split;
136 txn_splits[count + 1] = NULL;
137
138 DB( g_print("- %d splits\n", da_splits_count(txn_splits)) );
139 }
140
141
142 guint da_splits_clone(Split *stxn_splits[], Split *dtxn_splits[])
143 {
144 gint i, count;
145
146 DB( g_print("da_splits_clone\n") );
147
148 count = da_splits_count(stxn_splits);
149 for(i=0;i<count;i++)
150 {
151 dtxn_splits[i] = da_split_record_clone(stxn_splits[i]);
152 }
153
154 /* if(count > 0)
155 dtxn->flags |= OF_SPLIT;*/
156
157 DB( g_print(" clone %p -> %p, %d splits\n", stxn_splits, dtxn_splits, count) );
158 return count;
159 }
160
161
162
163 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
164
165 guint da_splits_parse(Split *ope_splits[], gchar *cats, gchar *amounts, gchar *memos)
166 {
167 gchar **cat_a, **amt_a, **mem_a;
168 guint count, i;
169 guint32 kcat;
170 gdouble amount;
171 Split *split;
172
173 DB( g_print(" split parse %s :: %s :: %s\n", cats, amounts, memos) );
174
175 cat_a = g_strsplit (cats, "||", 0);
176 amt_a = g_strsplit (amounts, "||", 0);
177 mem_a = g_strsplit (memos, "||", 0);
178
179 count = g_strv_length(amt_a);
180 if( (count == g_strv_length(cat_a)) && (count == g_strv_length(mem_a)) )
181 {
182 for(i=0;i<count;i++)
183 {
184 kcat = atoi(cat_a[i]);
185 amount = g_ascii_strtod(amt_a[i], NULL);
186 split = da_split_new(kcat, amount, mem_a[i]);
187 da_splits_append (ope_splits, split);
188 }
189
190 }
191 else
192 {
193 g_warning("invalid split parse");
194 }
195
196 g_strfreev (mem_a);
197 g_strfreev (amt_a);
198 g_strfreev (cat_a);
199
200 return count;
201 }
202
203
204
205 guint da_splits_tostring(Split *ope_splits[], gchar **cats, gchar **amounts, gchar **memos)
206 {
207 guint count, i;
208 char buf[G_ASCII_DTOSTR_BUF_SIZE];
209 GString *cat_a = g_string_new (NULL);
210 GString *amt_a = g_string_new (NULL);
211 GString *mem_a = g_string_new (NULL);
212
213 count = da_splits_count(ope_splits);
214 for(i=0;i<count;i++)
215 {
216 g_string_append_printf (cat_a, "%d", ope_splits[i]->kcat);
217 g_string_append(amt_a, g_ascii_dtostr (buf, sizeof (buf), ope_splits[i]->amount) );
218 g_string_append(mem_a, ope_splits[i]->memo);
219
220 if((i+1) < count)
221 {
222 g_string_append(cat_a, "||");
223 g_string_append(amt_a, "||");
224 g_string_append(mem_a, "||");
225 }
226 }
227
228 *cats = g_string_free(cat_a, FALSE);
229 *amounts = g_string_free(amt_a, FALSE);
230 *memos = g_string_free(mem_a, FALSE);
231
232 return count;
233 }
234
235 void split_cat_consistency (Split *txn_splits[])
236 {
237 guint i, nbsplit;
238
239 // check split category #1340142
240 nbsplit = da_splits_count(txn_splits);
241 for(i=0;i<nbsplit;i++)
242 {
243 if(da_cat_get(txn_splits[i]->kcat) == NULL)
244 {
245 g_warning("split consistency: fixed invalid split cat %d", txn_splits[i]->kcat);
246 txn_splits[i]->kcat = 0;
247 }
248 }
249 }
250
This page took 0.043036 seconds and 4 git commands to generate.