]>
Dogcows Code - chaz/homebank/blob - src/hb-split.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2019 Maxime DOYEN
4 * This file is part of HomeBank.
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.
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.
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/>.
22 #include "hb-transaction.h"
25 /****************************************************************************/
27 /****************************************************************************/
36 /* our global datas */
37 extern struct HomeBank
*GLOBALS
;
38 extern struct Preferences
*PREFS
;
41 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
44 void da_split_free(Split
*item
)
48 if(item
->memo
!= NULL
)
56 Split
*da_split_malloc(void)
58 return g_malloc0(sizeof(Split
));
62 void da_split_destroy(GPtrArray
*splits
)
64 DB( g_print("da_split_destroy\n") );
66 g_ptr_array_free(splits
, TRUE
);
70 GPtrArray
*da_split_new(void)
72 DB( g_print("da_split_new\n") );
73 return g_ptr_array_new_with_free_func((GDestroyNotify
)da_split_free
);
77 static GPtrArray
*da_split_new_full(guint size
)
79 DB( g_print("da_split_new\n") );
80 return g_ptr_array_new_full(size
, (GDestroyNotify
)da_split_free
);
84 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
86 static gint
da_splits_compare_func(gpointer a
, gpointer b
)
88 Split
*sa
= *(Split
**)a
;
89 Split
*sb
= *(Split
**)b
;
91 DB( g_print(" sort %d %s - %d %s\n", sa
->pos
, sa
->memo
, sb
->pos
, sb
->memo
) );
93 return sa
->pos
- sb
->pos
;
96 void da_splits_sort(GPtrArray
*splits
)
98 DB( g_print("da_splits_sort\n") );
102 //g_warning("NULL splits");
106 g_ptr_array_sort(splits
, (GCompareFunc
) da_splits_compare_func
);
110 guint
da_splits_length(GPtrArray
*splits
)
112 DB( g_print("da_splits_length\n") );
116 //g_warning("NULL splits");
124 gboolean
da_splits_remove(GPtrArray
*splits
, Split
*item
)
126 DB( g_print("da_splits_remove\n") );
130 g_warning("NULL splits");
134 return g_ptr_array_remove(splits
, item
);
138 void da_splits_append(GPtrArray
*splits
, Split
*item
)
140 DB( g_print("da_splits_append\n") );
144 g_warning("NULL splits");
148 if(splits
->len
<= TXN_MAX_SPLIT
)
149 g_ptr_array_add (splits
, item
);
153 Split
*da_splits_get(GPtrArray
*splits
, guint index
)
155 return g_ptr_array_index(splits
, index
);
159 GPtrArray
*da_splits_clone(GPtrArray
*src_splits
)
161 GPtrArray
*new_splits
;
164 DB( g_print("da_splits_clone\n") );
166 if(src_splits
== NULL
)
168 //g_warning("NULL splits");
172 new_splits
= da_split_new_full (src_splits
->len
);
173 for(i
=0;i
<src_splits
->len
;i
++)
177 src
= g_ptr_array_index(src_splits
, i
);
178 new = da_split_malloc ();
180 new->kcat
= src
->kcat
;
181 new->memo
= g_strdup(src
->memo
);
182 new->amount
= src
->amount
;
183 da_splits_append (new_splits
, new);
189 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
192 guint
da_splits_parse(GPtrArray
*splits
, gchar
*cats
, gchar
*amounts
, gchar
*memos
)
194 gchar
**cat_a
, **amt_a
, **mem_a
;
201 DB( g_print(" split parse %s :: %s :: %s\n", cats
, amounts
, memos
) );
203 cat_a
= g_strsplit (cats
, "||", 0);
204 amt_a
= g_strsplit (amounts
, "||", 0);
205 mem_a
= g_strsplit (memos
, "||", 0);
207 count
= g_strv_length(amt_a
);
208 if( (count
== g_strv_length(cat_a
)) && (count
== g_strv_length(mem_a
)) )
212 split
= da_split_malloc();
213 split
->kcat
= atoi(cat_a
[i
]);
214 split
->memo
= g_strdup(mem_a
[i
]);
215 split
->amount
= g_ascii_strtod(amt_a
[i
], NULL
);
216 da_splits_append (splits
, split
);
221 g_warning("invalid split parse");
232 guint
da_splits_tostring(GPtrArray
*splits
, gchar
**cats
, gchar
**amounts
, gchar
**memos
)
236 char buf
[G_ASCII_DTOSTR_BUF_SIZE
];
237 GString
*cat_a
, *amt_a
, *mem_a
;
242 DB( g_print(" splits tostring\n") );
244 cat_a
= g_string_new (NULL
);
245 amt_a
= g_string_new (NULL
);
246 mem_a
= g_string_new (NULL
);
248 for(i
=0;i
<splits
->len
;i
++)
250 split
= g_ptr_array_index(splits
, i
);
251 g_string_append_printf (cat_a
, "%d", split
->kcat
);
252 g_string_append(amt_a
, g_ascii_dtostr (buf
, sizeof (buf
), split
->amount
) );
253 g_string_append(mem_a
, split
->memo
);
255 if((i
+1) < splits
->len
)
257 g_string_append(cat_a
, "||");
258 g_string_append(amt_a
, "||");
259 g_string_append(mem_a
, "||");
263 *cats
= g_string_free(cat_a
, FALSE
);
264 *amounts
= g_string_free(amt_a
, FALSE
);
265 *memos
= g_string_free(mem_a
, FALSE
);
271 guint
da_splits_consistency (GPtrArray
*splits
)
279 // check split category #1340142
280 for(i
=0;i
<splits
->len
;i
++)
282 split
= g_ptr_array_index(splits
, i
);
283 if(da_cat_get(split
->kcat
) == NULL
)
285 g_warning("split consistency: fixed invalid split cat %d", split
->kcat
);
This page took 0.04841 seconds and 4 git commands to generate.