]>
Dogcows Code - chaz/homebank/blob - src/hb-split.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 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 static void da_split_free(Split
*item
)
48 if(item
->memo
!= NULL
)
56 static Split
*da_split_malloc(void)
58 return g_malloc0(sizeof(Split
));
62 Split
*da_split_new(guint32 kcat
, gdouble amount
, gchar
*memo
)
64 Split
*split
= da_split_malloc();
67 split
->amount
= amount
;
68 split
->memo
= g_strdup(memo
);
74 static Split
*da_split_record_clone(Split
*src_split
)
76 Split
*new_split
= g_memdup(src_split
, sizeof(Split
));
78 DB( g_print("da_split_record_clone\n") );
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
) );
90 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
93 guint
da_splits_count(Split
*txn_splits
[])
97 for(i
=0;i
<TXN_MAX_SPLIT
;i
++)
99 if(txn_splits
[i
] == NULL
)
107 void da_splits_free(Split
*txn_splits
[])
111 count
= da_splits_count(txn_splits
);
115 DB( g_print("da_splits_free\n") );
119 DB( g_print("- freeing %d :: %p\n", i
, txn_splits
[i
]) );
121 da_split_free(txn_splits
[i
]);
122 txn_splits
[i
] = NULL
;
127 void da_splits_append(Split
*txn_splits
[], Split
*new_split
)
129 guint count
= da_splits_count(txn_splits
);
131 DB( g_print("da_splits_append\n") );
133 DB( g_print("- split[%d] at %p for ope \n", count
, new_split
) );
135 txn_splits
[count
] = new_split
;
136 txn_splits
[count
+ 1] = NULL
;
138 DB( g_print("- %d splits\n", da_splits_count(txn_splits
)) );
142 guint
da_splits_clone(Split
*stxn_splits
[], Split
*dtxn_splits
[])
146 DB( g_print("da_splits_clone\n") );
148 count
= da_splits_count(stxn_splits
);
151 dtxn_splits
[i
] = da_split_record_clone(stxn_splits
[i
]);
155 dtxn->flags |= OF_SPLIT;*/
157 DB( g_print(" clone %p -> %p, %d splits\n", stxn_splits
, dtxn_splits
, count
) );
163 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
165 guint
da_splits_parse(Split
*ope_splits
[], gchar
*cats
, gchar
*amounts
, gchar
*memos
)
167 gchar
**cat_a
, **amt_a
, **mem_a
;
173 DB( g_print(" split parse %s :: %s :: %s\n", cats
, amounts
, memos
) );
175 cat_a
= g_strsplit (cats
, "||", 0);
176 amt_a
= g_strsplit (amounts
, "||", 0);
177 mem_a
= g_strsplit (memos
, "||", 0);
179 count
= g_strv_length(amt_a
);
180 if( (count
== g_strv_length(cat_a
)) && (count
== g_strv_length(mem_a
)) )
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
);
193 g_warning("invalid split parse");
205 guint
da_splits_tostring(Split
*ope_splits
[], gchar
**cats
, gchar
**amounts
, gchar
**memos
)
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
);
213 count
= da_splits_count(ope_splits
);
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
);
222 g_string_append(cat_a
, "||");
223 g_string_append(amt_a
, "||");
224 g_string_append(mem_a
, "||");
228 *cats
= g_string_free(cat_a
, FALSE
);
229 *amounts
= g_string_free(amt_a
, FALSE
);
230 *memos
= g_string_free(mem_a
, FALSE
);
235 void split_cat_consistency (Split
*txn_splits
[])
239 // check split category #1340142
240 nbsplit
= da_splits_count(txn_splits
);
241 for(i
=0;i
<nbsplit
;i
++)
243 if(da_cat_get(txn_splits
[i
]->kcat
) == NULL
)
245 g_warning("split consistency: fixed invalid split cat %d", txn_splits
[i
]->kcat
);
246 txn_splits
[i
]->kcat
= 0;
This page took 0.052989 seconds and 4 git commands to generate.