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 <iostream>
00019 #include "ParserEventGeneratorKit.h"
00020 #include "SGMLApplication.h"
00021 #include <time.h>
00022 #include <string>
00023 #include "messages.hh"
00024 #include "ofx_utilities.hh"
00025 
00026 using namespace std;
00027 
00029 
00030 static int get_sp_char_size(const SGMLApplication::Char * ptr)
00031 {
00032   int char_len;
00033   char * c_char_ptr = (char * )ptr;
00034   //cout<<"Contenu: 0"<<c_char_ptr[0]<<" 1"<<char_ptr[1]<<" 2"<<char_ptr[2]<<" 3"<<char_ptr[3]<<endl;
00035   if(c_char_ptr[2]==0&&c_char_ptr[3]==0)
00036     {
00037       char_len = 4;
00038     }
00039   else if(c_char_ptr[1]==0)
00040     {
00041       char_len = 2 ;
00042     }
00043   else
00044     {
00045       char_len = 1;
00046     }
00047   return char_len;
00048 }
00049 
00053 ostream &operator<<(ostream &os, SGMLApplication::CharString s)
00054 {
00055   int char_size = get_sp_char_size(s.ptr);
00056   for (size_t i = 0; i < s.len; i++)
00057     {//cout<<i<<" "<<(unsigned char)(s.ptr[i])<<endl;
00058       os << ((char *)(s.ptr))[i*char_size];
00059     }
00060   return os;
00061 }
00062 
00063 /*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
00064   {
00065   for (size_t i = 0; i < s.len; i++)
00066   {//cout<<i;
00067   os << wchar_t(s.ptr[i*MULTIPLY4]);  
00068   }
00069   return os;
00070   }            */
00071 
00072 wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
00073 {
00074   int char_size = get_sp_char_size(source.ptr);
00075   size_t i;
00076   for (i = 0; i < source.len; i++)
00077     {
00078       dest[i]+=wchar_t(source.ptr[i*char_size*(sizeof(char)/sizeof(wchar_t))]);
00079     }
00080   return dest;
00081 }
00082 
00083 string CharStringtostring(const SGMLApplication::CharString source, string &dest)
00084 {
00085   int char_size = get_sp_char_size(source.ptr);
00086   size_t i;
00087   dest.assign("");//Empty the provided string
00088   for (i = 0; i < source.len; i++){
00089     dest+=((char *)(source.ptr))[i*char_size];  
00090   }
00091   return dest;
00092 }
00093 
00094 string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
00095 {
00096   int char_size = get_sp_char_size(source.ptr);
00097   size_t i;
00098   //cout<<"Length: "<<source.len<<" detected char size: "<<char_size<<endl;
00099   for (i = 0; i < source.len; i++)
00100     {
00101       dest+=((char *)(source.ptr))[i*char_size]; 
00102     }
00103   return dest;
00104 }
00105 
00109 time_t ofxdate_to_time_t(const string ofxdate)
00110 {
00111   struct tm time;
00112   int local_offset; 
00113   float ofx_gmt_offset;
00114   char timezone[4];
00115   time_t temptime;
00116   std::time(&temptime);
00117   local_offset = (localtime(&temptime))->tm_gmtoff;
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 exact time is specified */
00124     if(ofxdate.size()>8) {
00125       time.tm_hour=atoi(ofxdate.substr(8,2).c_str());
00126       time.tm_min=atoi(ofxdate.substr(10,2).c_str());
00127       time.tm_sec=atoi(ofxdate.substr(12,2).c_str());
00128     }
00129     else{
00130       time.tm_hour=12;
00131       time.tm_min=0;
00132       time.tm_sec=0;
00133     }
00134     
00135    
00136     
00137     /* Check if the timezone has been specified */
00138     string::size_type startidx = ofxdate.find("[");
00139     string::size_type endidx;
00140     if(startidx!=string::npos){
00141       startidx++;
00142       endidx = ofxdate.find(":", startidx)-1;
00143       ofx_gmt_offset=atof(ofxdate.substr(startidx,(endidx-startidx)+1).c_str());
00144       startidx = endidx+2;
00145       strncpy(timezone,ofxdate.substr(startidx,3).c_str(),4);
00146     }
00147     else{
00148       ofx_gmt_offset=0;
00149       strcpy(timezone, "GMT");
00150     }
00151     /* Correct the time for the timezone */
00152     time.tm_sec = time.tm_sec + (local_offset - ((int)ofx_gmt_offset*60*60));//Convert from fractionnal hours to seconds
00153   }
00154   else{
00155     message_out(ERROR, "ofxdate_to_time_t():Unable to convert time, string is 0 length!");
00156   }
00157   return mktime(&time);
00158 }
00159 
00163 double ofxamount_to_double(const string ofxamount)
00164 {
00165   //Replace commas with decimal points for atof()
00166   string::size_type idx;
00167   string tmp = ofxamount;
00168   idx = tmp.find(',');
00169   if(idx!=string::npos){
00170     tmp.replace(idx,1,1,'.');
00171   }
00172   return atof(tmp.c_str());
00173 }
00174 
00178 string strip_whitespace(const string para_string)
00179 {
00180   size_t index;
00181   size_t i;
00182   string temp_string = para_string;
00183   char *whitespace = " \b\f\n\r\t\v";
00184   char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
00185   message_out(DEBUG4,"strip_whitespace() Before: |"+temp_string+"|");
00186   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++);
00187   temp_string.erase(0,i);//Strip leading whitespace
00188   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--);
00189   temp_string.erase(i+1,temp_string.size()-(i+1));//Strip trailing whitespace
00190   
00191 while ((index = temp_string.find_first_of(abnormal_whitespace))!=string::npos)
00192   {
00193     temp_string.erase(index,1);//Strip leading whitespace
00194   };
00195  
00196  message_out(DEBUG4,"strip_whitespace() After:  |"+temp_string+"|");
00197  
00198  return temp_string;
00199 }

Generated on Sun Nov 24 20:26:32 2002 for LibOFX by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002