- Main Page
- Namespaces
- Data Structures
- Files

# 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 <assert.h>
    00021
    00022 #include "ParserEventGeneratorKit.h"
    00023 #include "SGMLApplication.h"
    00024 #include <time.h>
    00025 #include <string>
    00026 #include <locale.h>
    00027 #include "messages.hh"
    00028 #include "ofx_utilities.hh"
    00029
    00030 #ifdef OS_WIN32
    00031 # define DIRSEP "\\"
    00032 #else
    00033 # define DIRSEP "/"
    00034 #endif
    00035
    00036
    00037 using namespace std;
    00041 /*ostream &operator<<(ostream &os, SGMLApplication::CharString s)
    00042   {
    00043   for (size_t i = 0; i < s.len; i++)
    00044   {
    00045   os << ((char *)(s.ptr))[i*sizeof(SGMLApplication::Char)];
    00046   }
    00047   return os;
    00048   }*/
    00049
    00050 /*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
    00051   {
    00052   for (size_t i = 0; i < s.len; i++)
    00053   {//cout<<i;
    00054   os << wchar_t(s.ptr[i*MULTIPLY4]);
    00055   }
    00056   return os;
    00057   }            */
    00058
    00059 /*wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
    00060   {
    00061   size_t i;
    00062   for (i = 0; i < source.len; i++)
    00063   {
    00064   dest[i]+=wchar_t(source.ptr[i*sizeof(SGMLApplication::Char)*(sizeof(char)/sizeof(wchar_t))]);
    00065   }
    00066   return dest;
    00067   }*/
    00068
    00069 string CharStringtostring(const SGMLApplication::CharString source, string &dest)
    00070 {
    00071   size_t i;
    00072   dest.assign("");//Empty the provided string
    00073   //  cout<<"Length: "<<source.len<<"sizeof(Char)"<<sizeof(SGMLApplication::Char)<<endl;
    00074   for (i = 0; i < source.len; i++){
    00075     dest+=(char)(((source.ptr)[i]));
    00076     //    cout<<i<<" "<<(char)(((source.ptr)[i]))<<endl;
    00077   }
    00078   return dest;
    00079 }
    00080
    00081 string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
    00082 {
    00083   size_t i;
    00084   for (i = 0; i < source.len; i++)
    00085     {
    00086       dest+=(char)(((source.ptr)[i]));
    00087     }
    00088   return dest;
    00089 }
    00090
    00106 time_t ofxdate_to_time_t(const string ofxdate)
    00107 {
    00108   struct tm time;
    00109   double local_offset; /* in seconds */
    00110   float ofx_gmt_offset; /* in fractionnal hours */
    00111   char timezone[4]; /* Original timezone: the library does not expose this value*/
    00112   char exact_time_specified = false;
    00113   char time_zone_specified = false;
    00114
    00115   time_t temptime;
    00116   std::time(&temptime);
    00117   local_offset = difftime(mktime(localtime(&temptime)), mktime(gmtime(&temptime)));
    00118
    00119   if(ofxdate.size()!=0){
    00120     time.tm_year=atoi(ofxdate.substr(0,4).c_str())-1900;
    00121     time.tm_mon=atoi(ofxdate.substr(4,2).c_str())-1;
    00122     time.tm_mday=atoi(ofxdate.substr(6,2).c_str());
    00123     if(ofxdate.size()>8) {
    00124     /* if exact time is specified */
    00125 exact_time_specified = true;
    00126       time.tm_hour=atoi(ofxdate.substr(8,2).c_str());
    00127       time.tm_min=atoi(ofxdate.substr(10,2).c_str());
    00128       time.tm_sec=atoi(ofxdate.substr(12,2).c_str());
    00129     }
    00130
    00131     /* Check if the timezone has been specified */
    00132     string::size_type startidx = ofxdate.find("[");
    00133     string::size_type endidx;
    00134     if(startidx!=string::npos){
    00135       /* Time zone was specified */
    00136       time_zone_specified = true;
    00137       startidx++;
    00138       endidx = ofxdate.find(":", startidx)-1;
    00139       ofx_gmt_offset=atof(ofxdate.substr(startidx,(endidx-startidx)+1).c_str());
    00140       startidx = endidx+2;
    00141       strncpy(timezone,ofxdate.substr(startidx,3).c_str(),4);
    00142     }
    00143     else{
    00144       /* Time zone was not specified, assume GMT (provisionnaly) in case exact time is specified */
    00145       ofx_gmt_offset=0;
    00146       strcpy(timezone, "GMT");
    00147     }
    00148
    00149     if(time_zone_specified == true)
    00150       {
    00151         /* If the timezone is specified always correct the timezone */
    00152         /* If the timezone is not specified, but the exact time is, correct the timezone, assuming GMT following the spec */
    00153         /* Correct the time for the timezone */
    00154         time.tm_sec = time.tm_sec + (int)(local_offset - (ofx_gmt_offset*60*60));//Convert from fractionnal hours to seconds
    00155       }
    00156     else if (exact_time_specified == false)
    00157       {
    00158         /*Time zone data missing and exact time not specified, diverge from the OFX spec ans assume 11h59 local time */
    00159        time.tm_hour=11;
    00160        time.tm_min=59;
    00161        time.tm_sec=0;
    00162       }
    00163   }
    00164   else{
    00165     message_out(ERROR, "ofxdate_to_time_t():  Unable to convert time, string is 0 length!");
    00166   }
    00167   return mktime(&time);
    00168 }
    00169
    00174 double ofxamount_to_double(const string ofxamount)
    00175 {
    00176   //Replace commas and decimal points for atof()
    00177   string::size_type idx;
    00178   string tmp = ofxamount;
    00179
    00180   idx = tmp.find(',');
    00181   if(idx==string::npos){
    00182     idx = tmp.find('.');
    00183   }
    00184
    00185   if(idx!=string::npos){
    00186     tmp.replace(idx,1,1,((localeconv())->decimal_point)[0]);
    00187   }
    00188
    00189   return atof(tmp.c_str());
    00190 }
    00191
    00195 string strip_whitespace(const string para_string)
    00196 {
    00197   size_t index;
    00198   size_t i;
    00199   string temp_string = para_string;
    00200   const char *whitespace = " \b\f\n\r\t\v";
    00201   const char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
    00202   message_out(DEBUG4,"strip_whitespace() Before: |"+temp_string+"|");
    00203   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++);
    00204   temp_string.erase(0,i);//Strip leading whitespace
    00205   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--);
    00206   temp_string.erase(i+1,temp_string.size()-(i+1));//Strip trailing whitespace
    00207
    00208 while ((index = temp_string.find_first_of(abnormal_whitespace))!=string::npos)
    00209   {
    00210     temp_string.erase(index,1);//Strip leading whitespace
    00211   };
    00212
    00213  message_out(DEBUG4,"strip_whitespace() After:  |"+temp_string+"|");
    00214
    00215  return temp_string;
    00216 }
    00217
    00218
    00219 int mkTempFileName(const char *tmpl, char *buffer, unsigned int size) {
    00220   const char *tmp_dir;
    00221
    00222   tmp_dir = getenv ("TMPDIR");
    00223   if (!tmp_dir)
    00224     tmp_dir = getenv ("TMP");
    00225   if (!tmp_dir)
    00226     tmp_dir = getenv ("TEMP");
    00227
    00228   if (!tmp_dir)
    00229     {
    00230 #ifdef OS_WIN32
    00231       tmp_dir = "C:\\";
    00232 #else
    00233       tmp_dir = "/tmp";
    00234 #endif  /* !OS_WIN32 */
    00235     }
    00236
    00237   strncpy(buffer, tmp_dir, size);
    00238   assert((strlen(buffer)+strlen(tmpl)+2)<size);
    00239   strcat(buffer, DIRSEP);
    00240   strcat(buffer, tmpl);
    00241   return 0;
    00242 }
    00243
    00244
    00245