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   int local_offset; 
00088   float ofx_gmt_offset;
00089   char timezone[4];
00090   time_t temptime;
00091   std::time(&temptime);
00092   local_offset = (localtime(&temptime))->tm_gmtoff;
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 + (local_offset - ((int)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 
00138 double ofxamount_to_double(const string ofxamount)
00139 {
00140   //Replace commas with decimal points for atof()
00141   string::size_type idx;
00142   string tmp = ofxamount;
00143   idx = tmp.find(',');
00144   if(idx!=string::npos){
00145     tmp.replace(idx,1,1,'.');
00146   }
00147   return atof(tmp.c_str());
00148 }
00149 
00153 string strip_whitespace(const string para_string)
00154 {
00155   size_t index;
00156   size_t i;
00157   string temp_string = para_string;
00158   char *whitespace = " \b\f\n\r\t\v";
00159   char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
00160   message_out(DEBUG4,"strip_whitespace() Before: |"+temp_string+"|");
00161   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++);
00162   temp_string.erase(0,i);//Strip leading whitespace
00163   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--);
00164   temp_string.erase(i+1,temp_string.size()-(i+1));//Strip trailing whitespace
00165   
00166 while ((index = temp_string.find_first_of(abnormal_whitespace))!=string::npos)
00167   {
00168     temp_string.erase(index,1);//Strip leading whitespace
00169   };
00170  
00171  message_out(DEBUG4,"strip_whitespace() After:  |"+temp_string+"|");
00172  
00173  return temp_string;
00174 }

Generated on Mon Feb 3 12:16:44 2003 for LibOFX by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002