# ofx_utilities.cpp

Go to the documentation of this file.

    00001 /***************************************************************************
    00002                           ofx_util.cpp
    00003                              -------------------
    00004     copyright            : (C) 2002 by Benoit Grégoire
    00005     email                : bock@step.polymtl.ca
    00006  ***************************************************************************/
    00010 /***************************************************************************
    00011  *                                                                         *
    00012  *   This program is free software; you can redistribute it and/or modify  *
    00013  *   it under the terms of the GNU General Public License as published by  *
    00014  *   the Free Software Foundation; either version 2 of the License, or     *
    00015  *   (at your option) any later version.                                   *
    00016  *                                                                         *
    00017  ***************************************************************************/
    00018 #include <config.h>
    00019 #include <iostream>
    00020 #include "ParserEventGeneratorKit.h"
    00021 #include "SGMLApplication.h"
    00022 #include <time.h>
    00023 #include <string>
    00024 #include <locale.h>
    00025 #include "messages.hh"
    00026 #include "ofx_utilities.hh"
    00027
    00028 using namespace std;
    00032 /*ostream &operator<<(ostream &os, SGMLApplication::CharString s)
    00033   {
    00034   for (size_t i = 0; i < s.len; i++)
    00035   {
    00036   os << ((char *)(s.ptr))[i*sizeof(SGMLApplication::Char)];
    00037   }
    00038   return os;
    00039   }*/
    00040
    00041 /*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
    00042   {
    00043   for (size_t i = 0; i < s.len; i++)
    00044   {//cout<<i;
    00045   os << wchar_t(s.ptr[i*MULTIPLY4]);
    00046   }
    00047   return os;
    00048   }            */
    00049
    00050 /*wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
    00051   {
    00052   size_t i;
    00053   for (i = 0; i < source.len; i++)
    00054   {
    00055   dest[i]+=wchar_t(source.ptr[i*sizeof(SGMLApplication::Char)*(sizeof(char)/sizeof(wchar_t))]);
    00056   }
    00057   return dest;
    00058   }*/
    00059
    00060 string CharStringtostring(const SGMLApplication::CharString source, string &dest)
    00061 {
    00062   size_t i;
    00063   dest.assign("");//Empty the provided string
    00064   //  cout<<"Length: "<<source.len<<"sizeof(Char)"<<sizeof(SGMLApplication::Char)<<endl;
    00065   for (i = 0; i < source.len; i++){
    00066     dest+=(char)(((source.ptr)[i]));
    00067     //    cout<<i<<" "<<(char)(((source.ptr)[i]))<<endl;
    00068   }
    00069   return dest;
    00070 }
    00071
    00072 string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
    00073 {
    00074   size_t i;
    00075   for (i = 0; i < source.len; i++)
    00076     {
    00077       dest+=(char)(((source.ptr)[i]));
    00078     }
    00079   return dest;
    00080 }
    00081
    00097 time_t ofxdate_to_time_t(const string ofxdate)
    00098 {
    00099   struct tm time;
    00100   double local_offset; /* in seconds */
    00101   float ofx_gmt_offset; /* in fractionnal hours */
    00102   char timezone[4]; /* Original timezone: the library does not expose this value*/
    00103   char exact_time_specified = false;
    00104   char time_zone_specified = false;
    00105
    00106   time_t temptime;
    00107   std::time(&temptime);
    00108   local_offset = difftime(mktime(localtime(&temptime)), mktime(gmtime(&temptime)));
    00109
    00110   if(ofxdate.size()!=0){
    00111     time.tm_year=atoi(ofxdate.substr(0,4).c_str())-1900;
    00112     time.tm_mon=atoi(ofxdate.substr(4,2).c_str())-1;
    00113     time.tm_mday=atoi(ofxdate.substr(6,2).c_str());
    00114     if(ofxdate.size()>8) {
    00115     /* if exact time is specified */
    00116 exact_time_specified = true;
    00117       time.tm_hour=atoi(ofxdate.substr(8,2).c_str());
    00118       time.tm_min=atoi(ofxdate.substr(10,2).c_str());
    00119       time.tm_sec=atoi(ofxdate.substr(12,2).c_str());
    00120     }
    00121
    00122     /* Check if the timezone has been specified */
    00123     string::size_type startidx = ofxdate.find("[");
    00124     string::size_type endidx;
    00125     if(startidx!=string::npos){
    00126       /* Time zone was specified */
    00127       time_zone_specified = true;
    00128       startidx++;
    00129       endidx = ofxdate.find(":", startidx)-1;
    00130       ofx_gmt_offset=atof(ofxdate.substr(startidx,(endidx-startidx)+1).c_str());
    00131       startidx = endidx+2;
    00132       strncpy(timezone,ofxdate.substr(startidx,3).c_str(),4);
    00133     }
    00134     else{
    00135       /* Time zone was not specified, assume GMT (provisionnaly) in case exact time is specified */
    00136       ofx_gmt_offset=0;
    00137       strcpy(timezone, "GMT");
    00138     }
    00139
    00140     if(time_zone_specified == true)
    00141       {
    00142         /* If the timezone is specified always correct the timezone */
    00143         /* If the timezone is not specified, but the exact time is, correct the timezone, assuming GMT following the spec */
    00144         /* Correct the time for the timezone */
    00145         time.tm_sec = time.tm_sec + (int)(local_offset - (ofx_gmt_offset*60*60));//Convert from fractionnal hours to seconds
    00146       }
    00147     else if (exact_time_specified == false)
    00148       {
    00149         /*Time zone data missing and exact time not specified, diverge from the OFX spec ans assume 11h59 local time */
    00150        time.tm_hour=11;
    00151        time.tm_min=59;
    00152        time.tm_sec=0;
    00153       }
    00154   }
    00155   else{
    00156     message_out(ERROR, "ofxdate_to_time_t():  Unable to convert time, string is 0 length!");
    00157   }
    00158   return mktime(&time);
    00159 }
    00160
    00165 double ofxamount_to_double(const string ofxamount)
    00166 {
    00167   //Replace commas and decimal points for atof()
    00168   string::size_type idx;
    00169   string tmp = ofxamount;
    00170
    00171   idx = tmp.find(',');
    00172   if(idx==string::npos){
    00173     idx = tmp.find('.');
    00174   }
    00175
    00176   if(idx!=string::npos){
    00177     tmp.replace(idx,1,1,((localeconv())->decimal_point)[0]);
    00178   }
    00179
    00180   return atof(tmp.c_str());
    00181 }
    00182
    00186 string strip_whitespace(const string para_string)
    00187 {
    00188   size_t index;
    00189   size_t i;
    00190   string temp_string = para_string;
    00191   char *whitespace = " \b\f\n\r\t\v";
    00192   char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
    00193   message_out(DEBUG4,"strip_whitespace() Before: |"+temp_string+"|");
    00194   for(i=0;i<=temp_string.size()&&temp_string.find_first_of(whitespace, i)==i&&temp_string.find_first_of(whitespace, i)!=string::npos;i++);
    00195   temp_string.erase(0,i);//Strip leading whitespace
    00196   for(i=temp_string.size()-1;(i>=0)&&(temp_string.find_last_of(whitespace, i)==i)&&(temp_string.find_last_of(whitespace, i)!=string::npos);i--);
    00197   temp_string.erase(i+1,temp_string.size()-(i+1));//Strip trailing whitespace
    00198
    00199 while ((index = temp_string.find_first_of(abnormal_whitespace))!=string::npos)
    00200   {
    00201     temp_string.erase(index,1);//Strip leading whitespace
    00202   };
    00203
    00204  message_out(DEBUG4,"strip_whitespace() After:  |"+temp_string+"|");
    00205
    00206  return temp_string;
    00207 }