LibOFX

ofx2qif.c
Go to the documentation of this file.
1 /***************************************************************************
2 ofx2qif.c
3 -------------------
4 copyright : (C) 2002 by Benoit Gr�goire
5 email : benoitg@coeus.ca
6 ***************************************************************************/
32 /***************************************************************************
33 * *
34 * This program is free software; you can redistribute it and/or modify *
35 * it under the terms of the GNU General Public License as published by *
36 * the Free Software Foundation; either version 2 of the License, or *
37 * (at your option) any later version. *
38 * *
39 ***************************************************************************/
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 #include "libofx.h"
45
46 #define QIF_FILE_MAX_SIZE 256000
47
48 int ofx_proc_transaction_cb(const struct OfxTransactionData data, void * transaction_data)
49 {
50 char dest_string[255];
51 char trans_buff[4096];
52 struct tm temp_tm;
53 char trans_list_buff[QIF_FILE_MAX_SIZE];
54
55 trans_list_buff[0]='\0';
56
57 if(data.date_posted_valid==true){
58 temp_tm = *localtime(&(data.date_posted));
59 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");
60 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
61 }
62 if(data.amount_valid==true){
63 sprintf(trans_buff, "T%.2f%s",data.amount,"\n");
64 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
65 }
66 if(data.check_number_valid==true){
67 sprintf(trans_buff, "N%s%s",data.check_number,"\n");
68 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
69 }
70 else if(data.reference_number_valid==true){
71 sprintf(trans_buff, "N%s%s",data.reference_number,"\n");
72 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
73 }
74 if(data.name_valid==true){
75 sprintf(trans_buff, "P%s%s",data.name,"\n");
76 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
77 }
78 if(data.memo_valid==true){
79 sprintf(trans_buff, "M%s%s",data.memo,"\n");
80 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
81 }
82 /* Add PAYEE and ADRESS here once supported by the library */
83
84
85 if(data.transactiontype_valid==true){
86 switch(data.transactiontype){
87 case OFX_CREDIT: strncpy(dest_string, "Generic credit", sizeof(dest_string));
88 break;
89 case OFX_DEBIT: strncpy(dest_string, "Generic debit", sizeof(dest_string));
90 break;
91 case OFX_INT: strncpy(dest_string, "Interest earned or paid (Note: Depends on signage of amount)", sizeof(dest_string));
92 break;
93 case OFX_DIV: strncpy(dest_string, "Dividend", sizeof(dest_string));
94 break;
95 case OFX_FEE: strncpy(dest_string, "FI fee", sizeof(dest_string));
96 break;
97 case OFX_SRVCHG: strncpy(dest_string, "Service charge", sizeof(dest_string));
98 break;
99 case OFX_DEP: strncpy(dest_string, "Deposit", sizeof(dest_string));
100 break;
101 case OFX_ATM: strncpy(dest_string, "ATM debit or credit (Note: Depends on signage of amount)", sizeof(dest_string));
102 break;
103 case OFX_POS: strncpy(dest_string, "Point of sale debit or credit (Note: Depends on signage of amount)", sizeof(dest_string));
104 break;
105 case OFX_XFER: strncpy(dest_string, "Transfer", sizeof(dest_string));
106 break;
107 case OFX_CHECK: strncpy(dest_string, "Check", sizeof(dest_string));
108 break;
109 case OFX_PAYMENT: strncpy(dest_string, "Electronic payment", sizeof(dest_string));
110 break;
111 case OFX_CASH: strncpy(dest_string, "Cash withdrawal", sizeof(dest_string));
112 break;
113 case OFX_DIRECTDEP: strncpy(dest_string, "Direct deposit", sizeof(dest_string));
114 break;
115 case OFX_DIRECTDEBIT: strncpy(dest_string, "Merchant initiated debit", sizeof(dest_string));
116 break;
117 case OFX_REPEATPMT: strncpy(dest_string, "Repeating payment/standing order", sizeof(dest_string));
118 break;
119 case OFX_OTHER: strncpy(dest_string, "Other", sizeof(dest_string));
120 break;
121 default : strncpy(dest_string, "Unknown transaction type", sizeof(dest_string));
122 break;
123 }
124 sprintf(trans_buff, "L%s%s",dest_string,"\n");
125 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
126 }
127 strcpy(trans_buff, "^\n");
128 strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
129 fputs(trans_list_buff,stdout);
130 return 0;
131 }/* end ofx_proc_transaction() */
132
133 int ofx_proc_statement_cb(const struct OfxStatementData data, void * statement_data)
134 {
135 struct tm temp_tm;
136
137 printf("!Account\n");
138 if(data.account_id_valid==true){
139 /* Use the account id as the qif name of the account */
140 printf("N%s%s",data.account_id,"\n");
141 }
142 if(data.account_ptr->account_type_valid==true)
143 {
144 switch(data.account_ptr->account_type){
145 case OFX_CHECKING : printf("TBank\n");
146 break;
147 case OFX_SAVINGS : printf("TBank\n");
148 break;
149 case OFX_MONEYMRKT : printf("TOth A\n");
150 break;
151 case OFX_CREDITLINE : printf("TOth L\n");
152 break;
153 case OFX_CMA : printf("TOth A\n");
154 break;
155 case OFX_CREDITCARD : printf("TCCard\n");
156 break;
157 default: perror("WRITEME: ofx_proc_account() This is an unknown account type!");
158 }
159 }
160 printf("DOFX online account\n");
161
162 if(data.ledger_balance_date_valid==true){
163 temp_tm = *localtime(&(data.ledger_balance_date));
164 printf("/%d%s%d%s%d%s", temp_tm.tm_mday, "/", temp_tm.tm_mon+1, "/", temp_tm.tm_year+1900, "\n");
165 }
166 if(data.ledger_balance_valid==true){
167 printf("$%.2f%s",data.ledger_balance,"\n");
168 }
169 printf("^\n");
170 /*The transactions will follow, here is the header */
171 if(data.account_ptr->account_type_valid==true){
172 switch(data.account_ptr->account_type){
173 case OFX_CHECKING : printf("!Type:Bank\n");
174 break;
175 case OFX_SAVINGS : printf("!Type:Bank\n");
176 break;
177 case OFX_MONEYMRKT : printf("!Type:Oth A\n");
178 break;
179 case OFX_CREDITLINE : printf("!Type:Oth L\n");
180 break;
181 case OFX_CMA : printf("!Type:Oth A\n");
182 break;
183 case OFX_CREDITCARD : printf("!Type:CCard\n");
184 break;
185 default: perror("WRITEME: ofx_proc_account() This is an unknown account type!");
186 }
187 }
188
189 return 0;
190 }/* end ofx_proc_statement() */
191
192 int ofx_proc_account_cb(const struct OfxAccountData data, void * account_data)
193 {
194 char dest_string[255]="";
195
196
197 // strncat(trans_list_buff, dest_string, QIF_FILE_MAX_SIZE - strlen(trans_list_buff));
198 fputs(dest_string,stdout);
199 return 0;
200 }/* end ofx_proc_account() */
201
202 int main (int argc, char *argv[])
203 {
204 ofx_PARSER_msg = false;
205 ofx_DEBUG_msg = false;
206 ofx_WARNING_msg = false;
207 ofx_ERROR_msg = false;
208 ofx_INFO_msg = false;
209 ofx_STATUS_msg = false;
210
211 LibofxContextPtr libofx_context = libofx_get_new_context();
212 ofx_set_statement_cb(libofx_context, ofx_proc_statement_cb, 0);
213 ofx_set_account_cb(libofx_context, ofx_proc_account_cb, 0);
214 ofx_set_transaction_cb(libofx_context, ofx_proc_transaction_cb, 0);
215
216 if(argc >= 2){
217 libofx_proc_file(libofx_context, argv[1], OFX);
218 }
219 return libofx_free_context(libofx_context);
220 }
221
222
223
224
225
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