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 
time_t ledger_balance_date
Definition: inc/libofx.h:1083
An abstraction of an account.
Definition: inc/libofx.h:276
char reference_number[OFX_REFERENCE_NUMBER_LENGTH]
Definition: inc/libofx.h:791
char check_number[OFX_CHECK_NUMBER_LENGTH]
Definition: inc/libofx.h:786
char name[OFX_TRANSACTION_NAME_LENGTH]
Definition: inc/libofx.h:801
char memo[OFX_MEMO2_LENGTH]
Definition: inc/libofx.h:805
LibofxContextPtr libofx_get_new_context()
Initialise the library and return a new context.
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:1072
void ofx_set_statement_cb(LibofxContextPtr ctx, LibofxProcStatementCallback cb, void *user_data)
void ofx_set_account_cb(LibofxContextPtr ctx, LibofxProcAccountCallback cb, void *user_data)
struct OfxAccountData * account_ptr
Definition: inc/libofx.h:1074
void ofx_set_transaction_cb(LibofxContextPtr ctx, LibofxProcTransactionCallback cb, void *user_data)
int main(int argc, char *argv[])
Definition: ofxdump.cpp:1146
An abstraction of a transaction in an account.
Definition: inc/libofx.h:682
int libofx_proc_file(LibofxContextPtr libofx_context, const char *p_filename, enum LibofxFileFormat ftype)
libofx_proc_file is the entry point of the library.
double ledger_balance
Definition: inc/libofx.h:1080
int libofx_free_context(LibofxContextPtr)
Free all ressources used by this context.
An abstraction of an account statement.
Definition: inc/libofx.h:1057