CsvTransExport - generic code cleanup

- declare variables on use
- more concise tests and loops
This commit is contained in:
Geert Janssens 2023-02-14 12:45:56 +01:00
parent 003f379d88
commit c6a93903b3

View File

@ -29,6 +29,7 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <stdbool.h>
#include "gnc-commodity.h" #include "gnc-commodity.h"
#include "gnc-ui-util.h" #include "gnc-ui-util.h"
@ -422,7 +423,7 @@ add_price (gchar *so_far, Split *split, gboolean t_void, CsvExportInfo *info)
/******************************************************************************/ /******************************************************************************/
static gchar* static gchar*
make_simple_trans_line (Account *acc, Transaction *trans, Split *split, CsvExportInfo *info) make_simple_trans_line (Transaction *trans, Split *split, CsvExportInfo *info)
{ {
gboolean t_void = xaccTransGetVoidStatus (trans); gboolean t_void = xaccTransGetVoidStatus (trans);
@ -459,7 +460,7 @@ make_split_part (gchar* exp_line, Split *split, gboolean t_void, CsvExportInfo *
} }
static gchar* static gchar*
make_complex_trans_line (Account *acc, Transaction *trans, Split *split, CsvExportInfo *info) make_complex_trans_line (Transaction *trans, Split *split, CsvExportInfo *info)
{ {
gchar *exp_line = g_strdup(""); gchar *exp_line = g_strdup("");
exp_line = add_date (exp_line, trans, info); exp_line = add_date (exp_line, trans, info);
@ -493,21 +494,18 @@ make_complex_split_line (Transaction *trans, Split *split, CsvExportInfo *info)
static static
void account_splits (CsvExportInfo *info, Account *acc, FILE *fh ) void account_splits (CsvExportInfo *info, Account *acc, FILE *fh )
{ {
GSList *p1, *p2;
GList *splits;
QofBook *book;
// Setup the query for normal transaction export // Setup the query for normal transaction export
if (info->export_type == XML_EXPORT_TRANS) if (info->export_type == XML_EXPORT_TRANS)
{ {
info->query = qof_query_create_for (GNC_ID_SPLIT); info->query = qof_query_create_for (GNC_ID_SPLIT);
book = gnc_get_current_book(); QofBook *book = gnc_get_current_book();
qof_query_set_book (info->query, book); qof_query_set_book (info->query, book);
/* Sort by transaction date */ /* Sort by transaction date */
p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED); GSList *p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED);
p1 = g_slist_prepend (p1, SPLIT_TRANS); p1 = g_slist_prepend (p1, SPLIT_TRANS);
p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT); GSList *p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT);
qof_query_set_sort_order (info->query, p1, p2, NULL); qof_query_set_sort_order (info->query, p1, p2, NULL);
xaccQueryAddSingleAccountMatch (info->query, acc, QOF_QUERY_AND); xaccQueryAddSingleAccountMatch (info->query, acc, QOF_QUERY_AND);
@ -515,83 +513,61 @@ void account_splits (CsvExportInfo *info, Account *acc, FILE *fh )
} }
/* Run the query */ /* Run the query */
for (splits = qof_query_run (info->query); splits; splits = splits->next) for (GList *splits = qof_query_run (info->query); splits; splits = splits->next)
{ {
Split *split; Split *split = splits->data;
Transaction *trans;
SplitList *s_list;
GList *node;
Split *t_split;
int nSplits;
int cnt;
gchar *line;
split = splits->data;
trans = xaccSplitGetParent (split);
nSplits = xaccTransCountSplits (trans);
s_list = xaccTransGetSplitList (trans);
// Look for trans already exported in trans_list // Look for trans already exported in trans_list
if (g_list_find (info->trans_list, trans) != NULL) Transaction *trans = xaccSplitGetParent (split);
if (g_list_find (info->trans_list, trans))
continue; continue;
// Look for blank split // Look for blank split
if (xaccSplitGetAccount (split) == NULL) Account *split_acc = xaccSplitGetAccount (split);
if (!split_acc)
continue; continue;
// This will be a simple layout equivalent to a single line register view.
if (info->simple_layout) if (info->simple_layout)
{ {
line = make_simple_trans_line (acc, trans, split, info); // Write line in simple layout, equivalent to a single line register view
gchar *line = make_simple_trans_line (trans, split, info);
/* Write to file */ info->failed = !write_line_to_file (fh, line);
if (!write_line_to_file (fh, line))
{
info->failed = TRUE;
break;
}
g_free (line); g_free (line);
if (info->failed)
break;
continue; continue;
} }
// Complex Transaction Line. // Write complex Transaction Line.
line = make_complex_trans_line (acc, trans, split, info); gchar *line = make_complex_trans_line (trans, split, info);
info->failed = !write_line_to_file (fh, line);
/* Write to file */
if (!write_line_to_file (fh, line))
{
info->failed = TRUE;
break;
}
g_free (line); g_free (line);
if (info->failed)
break;
/* Loop through the list of splits for the Transaction */ /* Loop through the list of splits for the Transaction */
node = s_list; for (GList *node = xaccTransGetSplitList (trans); node; node = node->next)
cnt = 0;
while ((cnt < nSplits) && (info->failed == FALSE))
{ {
t_split = node->data; Split *t_split = node->data;
// base split is already written on the trans_line // base split is already written on the trans_line
if (split != t_split) if (split == t_split)
{ continue;
// Complex Split Line.
line = make_complex_split_line (trans, t_split, info);
if (!write_line_to_file (fh, line))
info->failed = TRUE;
g_free (line); // Write complex Split Line.
} line = make_complex_split_line (trans, t_split, info);
info->failed = !write_line_to_file (fh, line);
cnt++; g_free (line);
node = node->next; if (info->failed)
break;
} }
info->trans_list = g_list_prepend (info->trans_list, trans); // add trans to trans_list info->trans_list = g_list_prepend (info->trans_list, trans); // add trans to trans_list
} }
if (info->export_type == XML_EXPORT_TRANS) if (info->export_type == XML_EXPORT_TRANS)
qof_query_destroy (info->query); qof_query_destroy (info->query);
g_list_free (splits);
} }