ofx2qif.c
Go to the documentation of this file.
/***************************************************************************
ofx2qif.c
-------------------
copyright : (C) 2002 by Benoit Gr�goire
email : benoitg@coeus.ca
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "libofx.h"
#define QIF_FILE_MAX_SIZE 256000
int ofx_proc_transaction_cb(const struct OfxTransactionData data, void * transaction_data)
{
char dest_string[255];
char trans_buff[4096];
struct tm temp_tm;
char trans_list_buff[QIF_FILE_MAX_SIZE];
trans_list_buff[0]='\0';
if(data.date_posted_valid==true){
temp_tm = *localtime(&(data.date_posted));
sprintf(trans_buff, "D%d%s%d%s%d%s", temp_tm.tm_mday, "/", temp_tm.tm_mon+1, "/", temp_tm.tm_year+1900, "\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
if(data.amount_valid==true){
sprintf(trans_buff, "T%.2f%s",data.amount,"\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
if(data.check_number_valid==true){
sprintf(trans_buff, "N%s%s",data.check_number,"\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
else if(data.reference_number_valid==true){
sprintf(trans_buff, "N%s%s",data.reference_number,"\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
if(data.name_valid==true){
sprintf(trans_buff, "P%s%s",data.name,"\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
if(data.memo_valid==true){
sprintf(trans_buff, "M%s%s",data.memo,"\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
/* Add PAYEE and ADRESS here once supported by the library */
if(data.transactiontype_valid==true){
switch(data.transactiontype){
case OFX_CREDIT: strncpy(dest_string, "Generic credit", sizeof(dest_string));
break;
case OFX_DEBIT: strncpy(dest_string, "Generic debit", sizeof(dest_string));
break;
case OFX_INT: strncpy(dest_string, "Interest earned or paid (Note: Depends on signage of amount)", sizeof(dest_string));
break;
case OFX_DIV: strncpy(dest_string, "Dividend", sizeof(dest_string));
break;
case OFX_FEE: strncpy(dest_string, "FI fee", sizeof(dest_string));
break;
case OFX_SRVCHG: strncpy(dest_string, "Service charge", sizeof(dest_string));
break;
case OFX_DEP: strncpy(dest_string, "Deposit", sizeof(dest_string));
break;
case OFX_ATM: strncpy(dest_string, "ATM debit or credit (Note: Depends on signage of amount)", sizeof(dest_string));
break;
case OFX_POS: strncpy(dest_string, "Point of sale debit or credit (Note: Depends on signage of amount)", sizeof(dest_string));
break;
case OFX_XFER: strncpy(dest_string, "Transfer", sizeof(dest_string));
break;
case OFX_CHECK: strncpy(dest_string, "Check", sizeof(dest_string));
break;
case OFX_PAYMENT: strncpy(dest_string, "Electronic payment", sizeof(dest_string));
break;
case OFX_CASH: strncpy(dest_string, "Cash withdrawal", sizeof(dest_string));
break;
case OFX_DIRECTDEP: strncpy(dest_string, "Direct deposit", sizeof(dest_string));
break;
case OFX_DIRECTDEBIT: strncpy(dest_string, "Merchant initiated debit", sizeof(dest_string));
break;
case OFX_REPEATPMT: strncpy(dest_string, "Repeating payment/standing order", sizeof(dest_string));
break;
case OFX_OTHER: strncpy(dest_string, "Other", sizeof(dest_string));
break;
default : strncpy(dest_string, "Unknown transaction type", sizeof(dest_string));
break;
}
sprintf(trans_buff, "L%s%s",dest_string,"\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
}
strcpy(trans_buff, "^\n");
strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
fputs(trans_list_buff,stdout);
return 0;
}/* end ofx_proc_transaction() */
int ofx_proc_statement_cb(const struct OfxStatementData data, void * statement_data)
{
struct tm temp_tm;
printf("!Account\n");
if(data.account_id_valid==true){
/* Use the account id as the qif name of the account */
printf("N%s%s",data.account_id,"\n");
}
if(data.account_ptr->account_type_valid==true)
{
switch(data.account_ptr->account_type){
case OFX_CHECKING : printf("TBank\n");
break;
case OFX_SAVINGS : printf("TBank\n");
break;
case OFX_MONEYMRKT : printf("TOth A\n");
break;
case OFX_CREDITLINE : printf("TOth L\n");
break;
case OFX_CMA : printf("TOth A\n");
break;
case OFX_CREDITCARD : printf("TCCard\n");
break;
default: perror("WRITEME: ofx_proc_account() This is an unknown account type!");
}
}
printf("DOFX online account\n");
if(data.ledger_balance_date_valid==true){
temp_tm = *localtime(&(data.ledger_balance_date));
printf("/%d%s%d%s%d%s", temp_tm.tm_mday, "/", temp_tm.tm_mon+1, "/", temp_tm.tm_year+1900, "\n");
}
if(data.ledger_balance_valid==true){
printf("$%.2f%s",data.ledger_balance,"\n");
}
printf("^\n");
/*The transactions will follow, here is the header */
if(data.account_ptr->account_type_valid==true){
switch(data.account_ptr->account_type){
case OFX_CHECKING : printf("!Type:Bank\n");
break;
case OFX_SAVINGS : printf("!Type:Bank\n");
break;
case OFX_MONEYMRKT : printf("!Type:Oth A\n");
break;
case OFX_CREDITLINE : printf("!Type:Oth L\n");
break;
case OFX_CMA : printf("!Type:Oth A\n");
break;
case OFX_CREDITCARD : printf("!Type:CCard\n");
break;
default: perror("WRITEME: ofx_proc_account() This is an unknown account type!");
}
}
return 0;
}/* end ofx_proc_statement() */
int ofx_proc_account_cb(const struct OfxAccountData data, void * account_data)
{
char dest_string[255]="";
// strncat(trans_list_buff, dest_string, QIF_FILE_MAX_SIZE - strlen(trans_list_buff));
fputs(dest_string,stdout);
return 0;
}/* end ofx_proc_account() */
int main (int argc, char *argv[])
{
ofx_PARSER_msg = false;
ofx_DEBUG_msg = false;
ofx_WARNING_msg = false;
ofx_ERROR_msg = false;
ofx_INFO_msg = false;
ofx_STATUS_msg = false;
LibofxContextPtr libofx_context = libofx_get_new_context();
ofx_set_statement_cb(libofx_context, ofx_proc_statement_cb, 0);
ofx_set_account_cb(libofx_context, ofx_proc_account_cb, 0);
ofx_set_transaction_cb(libofx_context, ofx_proc_transaction_cb, 0);
if(argc >= 2){
libofx_proc_file(libofx_context, argv[1], OFX);
}
return libofx_free_context(libofx_context);
}
OFX_INT
Definition: inc/libofx.h:626
OFX_OTHER
Definition: inc/libofx.h:640
OfxTransactionData::date_posted
time_t date_posted
Definition: inc/libofx.h:747
OfxStatementData::ledger_balance_date
time_t ledger_balance_date
Definition: inc/libofx.h:1083
OfxAccountData
An abstraction of an account.
Definition: inc/libofx.h:276
OFX_POS
Definition: inc/libofx.h:632
OFX_REPEATPMT
Definition: inc/libofx.h:639
OFX_ATM
Definition: inc/libofx.h:631
OfxTransactionData::reference_number
char reference_number[OFX_REFERENCE_NUMBER_LENGTH]
Definition: inc/libofx.h:791
OfxTransactionData::check_number
char check_number[OFX_CHECK_NUMBER_LENGTH]
Definition: inc/libofx.h:786
OfxTransactionData::transactiontype_valid
int transactiontype_valid
Definition: inc/libofx.h:698
OfxTransactionData::name
char name[OFX_TRANSACTION_NAME_LENGTH]
Definition: inc/libofx.h:801
OFX_CHECK
Definition: inc/libofx.h:634
OFX_FEE
Definition: inc/libofx.h:628
OFX
Definition: inc/libofx.h:129
OFX_CASH
Definition: inc/libofx.h:636
OfxTransactionData::memo
char memo[OFX_MEMO2_LENGTH]
Definition: inc/libofx.h:805
ofx_STATUS_msg
int ofx_STATUS_msg
Definition: build/libofx-0.10.0/lib/messages.cpp:37
ofx_WARNING_msg
int ofx_WARNING_msg
Definition: build/libofx-0.10.0/lib/messages.cpp:39
libofx_get_new_context
LibofxContextPtr libofx_get_new_context()
Initialise the library and return a new context.
Definition: build/libofx-0.10.0/lib/context.cpp:169
ofx_PARSER_msg
int ofx_PARSER_msg
Definition: build/libofx-0.10.0/lib/messages.cpp:30
OfxStatementData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:1072
ofx_set_statement_cb
void ofx_set_statement_cb(LibofxContextPtr ctx, LibofxProcStatementCallback cb, void *user_data)
Definition: build/libofx-0.10.0/lib/context.cpp:229
ofx_set_account_cb
void ofx_set_account_cb(LibofxContextPtr ctx, LibofxProcAccountCallback cb, void *user_data)
Definition: build/libofx-0.10.0/lib/context.cpp:202
OFX_CREDIT
Definition: inc/libofx.h:624
OFX_DEP
Definition: inc/libofx.h:630
OfxStatementData::account_ptr
struct OfxAccountData * account_ptr
Definition: inc/libofx.h:1074
OFX_SRVCHG
Definition: inc/libofx.h:629
OFX_DIRECTDEBIT
Definition: inc/libofx.h:638
OFX_PAYMENT
Definition: inc/libofx.h:635
OfxTransactionData::amount
double amount
Definition: inc/libofx.h:720
ofx_ERROR_msg
int ofx_ERROR_msg
Definition: build/libofx-0.10.0/lib/messages.cpp:40
ofx_set_transaction_cb
void ofx_set_transaction_cb(LibofxContextPtr ctx, LibofxProcTransactionCallback cb, void *user_data)
Definition: build/libofx-0.10.0/lib/context.cpp:220
ofx_INFO_msg
int ofx_INFO_msg
Definition: build/libofx-0.10.0/lib/messages.cpp:38
OFX_DEBIT
Definition: inc/libofx.h:625
main
int main(int argc, char *argv[])
Definition: ofxdump.cpp:1146
OfxTransactionData
An abstraction of a transaction in an account.
Definition: inc/libofx.h:682
OFX_XFER
Definition: inc/libofx.h:633
libofx_proc_file
int libofx_proc_file(LibofxContextPtr libofx_context, const char *p_filename, enum LibofxFileFormat ftype)
libofx_proc_file is the entry point of the library.
Definition: build/libofx-0.10.0/lib/file_preproc.cpp:67
OFX_DIV
Definition: inc/libofx.h:627
ofx_DEBUG_msg
int ofx_DEBUG_msg
Definition: build/libofx-0.10.0/lib/messages.cpp:31
OfxStatementData::ledger_balance
double ledger_balance
Definition: inc/libofx.h:1080
OFX_DIRECTDEP
Definition: inc/libofx.h:637
libofx_free_context
int libofx_free_context(LibofxContextPtr)
Free all ressources used by this context.
Definition: build/libofx-0.10.0/lib/context.cpp:174
OfxStatementData
An abstraction of an account statement.
Definition: inc/libofx.h:1057