LibOFX: ofx2qif.c Source File

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

# ofx2qif.c

Go to the documentation of this file.

    00001 /***************************************************************************
    00002                                    ofx2qif.c
    00003                              -------------------
    00004     copyright            : (C) 2002 by Benoit Grégoire
    00005     email                : bock@step.polymtl.ca
    00006 ***************************************************************************/
    00032 /***************************************************************************
    00033  *                                                                         *
    00034  *   This program is free software; you can redistribute it and/or modify  *
    00035  *   it under the terms of the GNU General Public License as published by  *
    00036  *   the Free Software Foundation; either version 2 of the License, or     *
    00037  *   (at your option) any later version.                                   *
    00038  *                                                                         *
    00039  ***************************************************************************/
    00040
    00041 #include <stdio.h>
    00042 #include <string.h>
    00043 #include <time.h>
    00044 #include "libofx.h"
    00045
    00046 #define QIF_FILE_MAX_SIZE 256000
    00047
    00048 int ofx_proc_transaction_cb(const struct OfxTransactionData data, void * transaction_data)
    00049 {
    00050   char dest_string[255];
    00051   char trans_buff[4096];
    00052   struct tm temp_tm;
    00053   char trans_list_buff[QIF_FILE_MAX_SIZE];
    00054
    00055   trans_list_buff[0]='\0';
    00056
    00057   if(data.date_posted_valid==true){
    00058     temp_tm = *localtime(&(data.date_posted));
    00059     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");
    00060     strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00061   }
    00062   if(data.amount_valid==true){
    00063     sprintf(trans_buff, "T%.2f%s",data.amount,"\n");
    00064     strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00065   }
    00066   if(data.check_number_valid==true){
    00067     sprintf(trans_buff, "N%s%s",data.check_number,"\n");
    00068     strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00069   }
    00070   else if(data.reference_number_valid==true){
    00071     sprintf(trans_buff, "N%s%s",data.reference_number,"\n");
    00072       strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00073 }
    00074 if(data.name_valid==true){
    00075     sprintf(trans_buff, "P%s%s",data.name,"\n");
    00076         strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00077 }
    00078 if(data.memo_valid==true){
    00079     sprintf(trans_buff, "M%s%s",data.memo,"\n");
    00080         strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00081 }
    00082 /* Add PAYEE and ADRESS here once supported by the library */
    00083
    00084
    00085 if(data.transactiontype_valid==true){
    00086     switch(data.transactiontype){
    00087         case OFX_CREDIT: strncpy(dest_string, "Generic credit", sizeof(dest_string));
    00088         break;
    00089         case OFX_DEBIT: strncpy(dest_string, "Generic debit", sizeof(dest_string));
    00090         break;
    00091         case OFX_INT: strncpy(dest_string, "Interest earned or paid (Note: Depends on signage of amount)", sizeof(dest_string));
    00092         break;
    00093         case OFX_DIV: strncpy(dest_string, "Dividend", sizeof(dest_string));
    00094         break;
    00095         case OFX_FEE: strncpy(dest_string, "FI fee", sizeof(dest_string));
    00096         break;
    00097         case OFX_SRVCHG: strncpy(dest_string, "Service charge", sizeof(dest_string));
    00098         break;
    00099         case OFX_DEP: strncpy(dest_string, "Deposit", sizeof(dest_string));
    00100         break;
    00101         case OFX_ATM: strncpy(dest_string, "ATM debit or credit (Note: Depends on signage of amount)", sizeof(dest_string));
    00102         break;
    00103         case OFX_POS: strncpy(dest_string, "Point of sale debit or credit (Note: Depends on signage of amount)", sizeof(dest_string));
    00104         break;
    00105         case OFX_XFER: strncpy(dest_string, "Transfer", sizeof(dest_string));
    00106         break;
    00107         case OFX_CHECK: strncpy(dest_string, "Check", sizeof(dest_string));
    00108         break;
    00109         case OFX_PAYMENT: strncpy(dest_string, "Electronic payment", sizeof(dest_string));
    00110         break;
    00111         case OFX_CASH: strncpy(dest_string, "Cash withdrawal", sizeof(dest_string));
    00112         break;
    00113         case OFX_DIRECTDEP: strncpy(dest_string, "Direct deposit", sizeof(dest_string));
    00114         break;
    00115         case OFX_DIRECTDEBIT: strncpy(dest_string, "Merchant initiated debit", sizeof(dest_string));
    00116         break;
    00117         case OFX_REPEATPMT: strncpy(dest_string, "Repeating payment/standing order", sizeof(dest_string));
    00118         break;
    00119         case OFX_OTHER: strncpy(dest_string, "Other", sizeof(dest_string));
    00120         break;
    00121         default : strncpy(dest_string, "Unknown transaction type", sizeof(dest_string));
    00122         break;
    00123     }
    00124     sprintf(trans_buff, "L%s%s",dest_string,"\n");
    00125     strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00126 }
    00127  strcpy(trans_buff, "^\n");
    00128  strncat(trans_list_buff, trans_buff, sizeof(trans_list_buff)-1 - strlen(trans_list_buff));
    00129  fputs(trans_list_buff,stdout);
    00130  return 0;
    00131 }/* end ofx_proc_transaction() */
    00132
    00133 int ofx_proc_statement_cb(const struct OfxStatementData data, void * statement_data)
    00134 {
    00135   struct tm temp_tm;
    00136
    00137   printf("!Account\n");
    00138   if(data.account_id_valid==true){
    00139     /* Use the account id as the qif name of the account */
    00140     printf("N%s%s",data.account_id,"\n");
    00141   }
    00142   if(data.account_ptr->account_type_valid==true)
    00143     {
    00144       switch(data.account_ptr->account_type){
    00145       case OFX_CHECKING : printf("TBank\n");
    00146         break;
    00147       case OFX_SAVINGS :  printf("TBank\n");
    00148         break;
    00149       case OFX_MONEYMRKT :  printf("TOth A\n");
    00150         break;
    00151       case OFX_CREDITLINE :  printf("TOth L\n");
    00152         break;
    00153       case OFX_CMA :  printf("TOth A\n");
    00154         break;
    00155       case OFX_CREDITCARD :   printf("TCCard\n");
    00156         break;
    00157       default: perror("WRITEME: ofx_proc_account() This is an unknown account type!");
    00158       }
    00159     }
    00160   printf("DOFX online account\n");
    00161
    00162   if(data.ledger_balance_date_valid==true){
    00163     temp_tm = *localtime(&(data.ledger_balance_date));
    00164     printf("/%d%s%d%s%d%s", temp_tm.tm_mday, "/", temp_tm.tm_mon+1, "/", temp_tm.tm_year+1900, "\n");
    00165   }
    00166   if(data.ledger_balance_valid==true){
    00167     printf("$%.2f%s",data.ledger_balance,"\n");
    00168   }
    00169   printf("^\n");
    00170   /*The transactions will follow, here is the header */
    00171   if(data.account_ptr->account_type_valid==true){
    00172     switch(data.account_ptr->account_type){
    00173     case OFX_CHECKING : printf("!Type:Bank\n");
    00174       break;
    00175     case OFX_SAVINGS : printf("!Type:Bank\n");
    00176       break;
    00177     case OFX_MONEYMRKT : printf("!Type:Oth A\n");
    00178       break;
    00179     case OFX_CREDITLINE : printf("!Type:Oth L\n");
    00180       break;
    00181     case OFX_CMA : printf("!Type:Oth A\n");
    00182       break;
    00183     case OFX_CREDITCARD : printf("!Type:CCard\n");
    00184       break;
    00185     default: perror("WRITEME: ofx_proc_account() This is an unknown account type!");
    00186     }
    00187   }
    00188
    00189   return 0;
    00190 }/* end ofx_proc_statement() */
    00191
    00192 int ofx_proc_account_cb(const struct OfxAccountData data, void * account_data)
    00193 {
    00194   char dest_string[255]="";
    00195
    00196
    00197   //    strncat(trans_list_buff, dest_string, QIF_FILE_MAX_SIZE - strlen(trans_list_buff));
    00198   fputs(dest_string,stdout);
    00199  return 0;
    00200 }/* end ofx_proc_account() */
    00201
    00202 int main (int argc, char *argv[])
    00203 {
    00204 extern int ofx_PARSER_msg;
    00205 extern int ofx_DEBUG_msg;
    00206 extern int ofx_WARNING_msg;
    00207 extern int ofx_ERROR_msg;
    00208 extern int ofx_INFO_msg;
    00209 extern int ofx_STATUS_msg;
    00210  ofx_PARSER_msg = false;
    00211  ofx_DEBUG_msg = false;
    00212  ofx_WARNING_msg = false;
    00213  ofx_ERROR_msg = false;
    00214  ofx_INFO_msg = false;
    00215  ofx_STATUS_msg = false;
    00216
    00217  LibofxContextPtr libofx_context = libofx_get_new_context();
    00218  ofx_set_statement_cb(libofx_context, ofx_proc_statement_cb, 0);
    00219  ofx_set_account_cb(libofx_context, ofx_proc_account_cb, 0);
    00220  ofx_set_transaction_cb(libofx_context, ofx_proc_transaction_cb, 0);
    00221
    00222  if(argc >= 2){
    00223    libofx_proc_file(libofx_context, argv[1], OFX);
    00224  }
    00225  return libofx_free_context(libofx_context);
    00226 }
    00227
    00228
    00229
    00230
    00231