X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fhomebank;a=blobdiff_plain;f=src%2Fhb-import-csv.c;h=497641c598675bc461feeb0a9337e9756356d4b4;hp=02f62b238b6fc4d2a61a3307755df6e3d878e5de;hb=5499ff44ef50b751b58f27fd13594f7dd4f959b7;hpb=cd13d9691c46c2b2d6d459e9e6a76bed1c21b7a6 diff --git a/src/hb-import-csv.c b/src/hb-import-csv.c index 02f62b2..497641c 100644 --- a/src/hb-import-csv.c +++ b/src/hb-import-csv.c @@ -1,5 +1,5 @@ /* HomeBank -- Free, easy, personal accounting for everyone. - * Copyright (C) 1995-2018 Maxime DOYEN + * Copyright (C) 1995-2019 Maxime DOYEN * * This file is part of HomeBank. * @@ -39,6 +39,18 @@ extern struct HomeBank *GLOBALS; extern struct Preferences *PREFS; +static gint csvtype[7] = { + CSV_DATE, + CSV_INT, + CSV_STRING, + CSV_STRING, + CSV_STRING, + CSV_DOUBLE, + CSV_STRING, + }; + + + static gchar *hb_csv_strndup (gchar *str, gsize n) { gchar *new_str; @@ -72,14 +84,14 @@ gchar *twoquote; } -static gchar *hb_csv_find_delimiter(gchar *string) +static gchar *hb_csv_find_delimiter(gchar *string, gchar delimiter) { gchar *s = string; gboolean enclosed = FALSE; while( *s != '\0' ) { - if( *s == ';' && enclosed == FALSE ) + if( (*s == delimiter) && (enclosed == FALSE) ) break; if( *s == '\"' ) @@ -94,7 +106,7 @@ gboolean enclosed = FALSE; } -gboolean hb_csv_row_valid(gchar **str_array, guint nbcolumns, gint *csvtype) +static gboolean hb_csv_row_valid(gchar **str_array, guint nbcolumns, gint *csvtype) { gboolean valid = TRUE; guint i; @@ -162,7 +174,7 @@ csvend: } -gchar **hb_csv_row_get(gchar *string, gchar *delimiter, gint max_tokens) +static gchar **hb_csv_row_get(gchar *string, gchar delimiter, gint max_tokens) { GSList *string_list = NULL, *slist; gchar **str_array, *s; @@ -170,18 +182,15 @@ guint n = 0; gchar *remainder; g_return_val_if_fail (string != NULL, NULL); - g_return_val_if_fail (delimiter != NULL, NULL); - g_return_val_if_fail (delimiter[0] != '\0', NULL); + g_return_val_if_fail (delimiter != '\0', NULL); if (max_tokens < 1) max_tokens = G_MAXINT; remainder = string; - s = hb_csv_find_delimiter (remainder); + s = hb_csv_find_delimiter (remainder, delimiter); if (s) { - gsize delimiter_len = strlen (delimiter); - while (--max_tokens && s && *s != '\0') { gsize len; @@ -191,8 +200,8 @@ gchar *remainder; DB( g_print(" stored=[%s]\n", (gchar *)string_list->data) ); n++; - remainder = s + delimiter_len; - s = hb_csv_find_delimiter (remainder); + remainder = s + 1; + s = hb_csv_find_delimiter (remainder, delimiter); } } if (*string) @@ -217,45 +226,56 @@ gchar *remainder; } -GList *homebank_csv_import(gchar *filename, ImportContext *ictx) +static gchar hb_csv_get_separator(void) +{ +static const gchar sep[] = PRF_DTEX_CSVSEP_BUFFER; + return sep[PREFS->dtex_csvsep]; +} + + +gboolean hb_csv_test_line(gchar *rawline) +{ +gchar **str_array; +gchar sep; +gboolean isvalid = FALSE; + + hb_string_strip_crlf(rawline); + sep = hb_csv_get_separator(); + str_array = hb_csv_row_get(rawline, sep, 8); + isvalid = hb_csv_row_valid(str_array, 8, csvtype); + + g_strfreev (str_array); + + return isvalid; +} + + +GList *homebank_csv_import(ImportContext *ictx, GenFile *genfile) { GIOChannel *io; -GList *list = NULL; -static gint csvtype[7] = { - CSV_DATE, - CSV_INT, - CSV_STRING, - CSV_STRING, - CSV_STRING, - CSV_DOUBLE, - CSV_STRING, - }; +//GList *list = NULL; DB( g_print("\n[import] homebank csv\n") ); - io = g_io_channel_new_file(filename, "r", NULL); + io = g_io_channel_new_file(genfile->filepath, "r", NULL); if(io != NULL) { gchar *tmpstr; + gchar sep; gsize length; gint io_stat; gboolean isvalid; gint count = 0; gint error = 0; - Account *tmp_acc; - Payee *payitem; - Category *catitem; + GenAcc *newacc; GError *err = NULL; - gchar *accname = g_strdup_printf(_("(account %d)"), da_acc_get_max_key() + 1); - tmp_acc = import_create_account(accname, NULL); - g_free(accname); - + newacc = hb_import_gen_acc_get_next(ictx, FILETYPE_CSV_HB, NULL, NULL); - if( ictx->encoding != NULL ) + if( genfile->encoding != NULL ) { - g_io_channel_set_encoding(io, ictx->encoding, NULL); + g_io_channel_set_encoding(io, genfile->encoding, NULL); } for(;;) @@ -276,14 +296,16 @@ static gint csvtype[7] = { count++; + sep = hb_csv_get_separator(); + hb_string_strip_crlf(tmpstr); - DB( g_print("\n (row-%04d) ->|%s|<-\n", count, tmpstr) ); + str_array = hb_csv_row_get(tmpstr, sep, 8); // 0:date; 1:paymode; 2:info; 3:payee, 4:wording; 5:amount; 6:category; 7:tags - str_array = hb_csv_row_get(tmpstr, ";", 8); isvalid = hb_csv_row_valid(str_array, 8, csvtype); - DB( g_print(" valid %d, '%s'\n", isvalid, tmpstr) ); + DB( g_print("\n (row-%04d) ->|%s|<-\n", count, tmpstr) ); + DB( g_print(" valid %d, '%s'\n", isvalid, tmpstr) ); if( !isvalid ) { @@ -293,60 +315,30 @@ static gint csvtype[7] = { } else { - Transaction *newope = da_transaction_malloc(); - - //DB( g_print(" ->%s\n", tmpstr ) ); - - newope->date = hb_date_get_julian(str_array[0], ictx->datefmt); - if( newope->date == 0 ) - { - g_warning ("csv parse: line %d, parse date failed", count); - ictx->cnt_err_date++; - } - - newope->paymode = atoi(str_array[1]); - newope->info = g_strdup(str_array[2]); - - /* payee */ - g_strstrip(str_array[3]); - payitem = da_pay_get_by_name(str_array[3]); - if(payitem == NULL) - { - payitem = da_pay_malloc(); - payitem->name = g_strdup(str_array[3]); - payitem->imported = TRUE; - da_pay_append(payitem); - - if( payitem->imported == TRUE ) - ictx->cnt_new_pay += 1; - } - - newope->kpay = payitem->key; - newope->memo = g_strdup(str_array[4]); - newope->amount = hb_qif_parser_get_amount(str_array[5]); - - /* category */ - g_strstrip(str_array[6]); - catitem = da_cat_append_ifnew_by_fullname(str_array[6], TRUE); - if( catitem != NULL ) - { - newope->kcat = catitem->key; - - if( catitem->imported == TRUE && catitem->key > 0 ) - ictx->cnt_new_cat += 1; - } - - /* tags */ - transaction_tags_parse(newope, str_array[7]); - - - newope->kacc = tmp_acc->key; - //newope->kxferacc = accnum; - - newope->flags |= OF_ADDED; - - if( newope->amount > 0) - newope->flags |= OF_INCOME; + GenTxn *newope = da_gen_txn_malloc();; + + DB( g_print(" adding txn\n" ) ); + + /* convert to generic transaction */ + newope->date = g_strdup(str_array[0]); + newope->paymode = atoi(str_array[1]); + //todo: reinforce controls here + // csv file are standalone, so no way to link a target txn + //added 5.1.8 forbid to import 5=internal xfer + if(newope->paymode == PAYMODE_INTXFER) + newope->paymode = PAYMODE_XFER; + newope->rawinfo = g_strdup(str_array[2]); + newope->rawpayee = g_strdup(g_strstrip(str_array[3])); + newope->rawmemo = g_strdup(str_array[4]); + newope->amount = hb_qif_parser_get_amount(str_array[5]); + newope->category = g_strdup(g_strstrip(str_array[6])); + newope->tags = g_strdup(str_array[7]); + newope->account = g_strdup(newacc->name); + + /* todo: move this eval date valid */ + //guint32 juliantmp = hb_date_get_julian(str_array[0], ictx->datefmt); + ///if( juliantmp == 0 ) + // ictx->cnt_err_date++; /* DB( g_print(" storing %s : %s : %s :%s : %s : %s : %s : %s\n", @@ -356,7 +348,7 @@ static gint csvtype[7] = { ) ); */ - list = g_list_append(list, newope); + da_gen_txn_append(ictx, newope); g_strfreev (str_array); } @@ -376,7 +368,7 @@ static gint csvtype[7] = { } - return list; + return ictx->gen_lst_txn; }