Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

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 "messages.hh"
00025 #include "ofx_utilities.hh"
00026 
00027 using namespace std;
00031 /*ostream &operator<<(ostream &os, SGMLApplication::CharString s)
00032   {
00033   for (size_t i = 0; i < s.len; i++)
00034   {
00035   os << ((char *)(s.ptr))[i*sizeof(SGMLApplication::Char)];
00036   }
00037   return os;
00038   }*/
00039 
00040 /*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
00041   {
00042   for (size_t i = 0; i < s.len; i++)
00043   {//cout<<i;
00044   os << wchar_t(s.ptr[i*MULTIPLY4]);  
00045   }
00046   return os;
00047   }            */
00048 
00049 /*wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
00050   {
00051   size_t i;
00052   for (i = 0; i < source.len; i++)
00053   {
00054   dest[i]+=wchar_t(source.ptr[i*sizeof(SGMLApplication::Char)*(sizeof(char)/sizeof(wchar_t))]);
00055   }
00056   return dest;
00057   }*/
00058 
00059 string CharStringtostring(const SGMLApplication::CharString source, string &dest)
00060 {
00061   size_t i;
00062   dest.assign("");//Empty the provided string
00063   //  cout<<"Length: "<<source.len<<"sizeof(Char)"<<sizeof(SGMLApplication::Char)<<endl;
00064   for (i = 0; i < source.len; i++){
00065     dest+=(char)(((source.ptr)[i]));  
00066     //    cout<<i<<" "<<(char)(((source.ptr)[i]))<<endl; 
00067   }
00068   return dest;
00069 }
00070 
00071 string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
00072 {
00073   size_t i;
00074   for (i = 0; i < source.len; i++)
00075     {
00076       dest+=(char)(((source.ptr)[i]));
00077     }
00078   return dest;
00079 }
00080 
00084 time_t ofxdate_to_time_t(const string ofxdate)
00085 {
00086   struct tm time;
00087   double local_offset; 
00088   float ofx_gmt_offset;
00089   char timezone[4];
00090   time_t temptime;
00091   std::time(&temptime);
00092   local_offset = difftime(mktime(localtime(&temptime)), mktime(gmtime(&temptime)));
00093   
00094   if(ofxdate.size()!=0){
00095     time.tm_year=atoi(ofxdate.substr(0,4).c_str())-1900;
00096     time.tm_mon=atoi(ofxdate.substr(4,2).c_str())-1;
00097     time.tm_mday=atoi(ofxdate.substr(6,2).c_str());
00098     /* if exact time is specified */
00099     if(ofxdate.size()>8) {
00100       time.tm_hour=atoi(ofxdate.substr(8,2).c_str());
00101       time.tm_min=atoi(ofxdate.substr(10,2).c_str());
00102       time.tm_sec=atoi(ofxdate.substr(12,2).c_str());
00103     }
00104     else{
00105       time.tm_hour=12;
00106       time.tm_min=0;
00107       time.tm_sec=0;
00108     }
00109     
00110    
00111     
00112     /* Check if the timezone has been specified */
00113     string::size_type startidx = ofxdate.find("[");
00114     string::size_type endidx;
00115     if(startidx!=string::npos){
00116       startidx++;
00117       endidx = ofxdate.find(":", startidx)-1;
00118       ofx_gmt_offset=atof(ofxdate.substr(startidx,(endidx-startidx)+1).c_str());
00119       startidx = endidx+2;
00120       strncpy(timezone,ofxdate.substr(startidx,3).c_str(),4);
00121     }
00122     else{
00123       ofx_gmt_offset=0;
00124       strcpy(timezone, "GMT");
00125     }
00126     /* Correct the time for the timezone */
00127     time.tm_sec = time.tm_sec + (int)(local_offset - (ofx_gmt_offset*60*60));//Convert from fractionnal hours to seconds
00128   }
00129   else{
00130     message_out(ERROR, "ofxdate_to_time_t():Unable to convert time, string is 0 length!");
00131   }
00132   return mktime(&time);
00133 }
00134 
00139 double ofxamount_to_double(const string ofxamount)
00140 {
00141   //Replace commas and decimal points for atof()
00142   string::size_type idx;
00143   string tmp = ofxamount;
00144 
00145   idx = tmp.find(',');
00146   if(idx==string::npos){
00147     idx = tmp.find('.');
00148   }
00149   
00150   if(idx!=string::npos){
00151     tmp.replace(idx,1,1,((localeconv())->decimal_point)[0]);
00152   }
00153 
00154   return atof(tmp.c_str());
00155 }
00156 
00160 string strip_whitespace(const string para_string)
00161 {
00162   size_t index;
00163   size_t i;
00164   string temp_string = para_string;
00165   char *whitespace = " \b\f\n\r\t\v";
00166   char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
00167   message_out(DEBUG4,"strip_whitespace() Before: |"+temp_string+"|");
00168   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++);
00169   temp_string.erase(0,i);//Strip leading whitespace
00170   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--);
00171   temp_string.erase(i+1,temp_string.size()-(i+1));//Strip trailing whitespace
00172   
00173 while ((index = temp_string.find_first_of(abnormal_whitespace))!=string::npos)
00174   {
00175     temp_string.erase(index,1);//Strip leading whitespace
00176   };
00177  
00178  message_out(DEBUG4,"strip_whitespace() After:  |"+temp_string+"|");
00179  
00180  return temp_string;
00181 }

Generated on Sat Apr 12 18:35:07 2003 for LibOFX by doxygen1.2.18