ofx_utilities.cpp
Go to the documentation of this file.
/***************************************************************************
ofx_util.cpp
-------------------
copyright : (C) 2002 by Benoit Gr�goire
email : benoitg@coeus.ca
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <config.h>
#include <iostream>
#include <assert.h>
#include "ParserEventGeneratorKit.h"
#include "SGMLApplication.h"
#include <ctime>
#include <cstdlib>
#include <string>
#include <locale.h>
#include "messages.hh"
#include "ofx_utilities.hh"
#ifdef OS_WIN32
# define DIRSEP "\\"
#else
# define DIRSEP "/"
#endif
using namespace std;
/*ostream &operator<<(ostream &os, SGMLApplication::CharString s)
{
for (size_t i = 0; i < s.len; i++)
{
os << ((char *)(s.ptr))[i*sizeof(SGMLApplication::Char)];
}
return os;
}*/
/*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
{
for (size_t i = 0; i < s.len; i++)
{//cout<<i;
os << wchar_t(s.ptr[i*MULTIPLY4]);
}
return os;
} */
/*wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
{
size_t i;
for (i = 0; i < source.len; i++)
{
dest[i]+=wchar_t(source.ptr[i*sizeof(SGMLApplication::Char)*(sizeof(char)/sizeof(wchar_t))]);
}
return dest;
}*/
string CharStringtostring(const SGMLApplication::CharString source, string &dest)
{
size_t i;
dest.assign("");//Empty the provided string
// cout<<"Length: "<<source.len<<"sizeof(Char)"<<sizeof(SGMLApplication::Char)<<endl;
for (i = 0; i < source.len; i++)
{
dest += (char)(((source.ptr)[i]));
// cout<<i<<" "<<(char)(((source.ptr)[i]))<<endl;
}
return dest;
}
string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
{
size_t i;
for (i = 0; i < source.len; i++)
{
dest += (char)(((source.ptr)[i]));
}
return dest;
}
time_t ofxdate_to_time_t(const string ofxdate)
{
struct tm time;
double local_offset; /* in seconds */
float ofx_gmt_offset; /* in fractional hours */
char timezone[4]; /* Original timezone: the library does not expose this value*/
char exact_time_specified = false;
char time_zone_specified = false;
string ofxdate_whole;
time_t temptime;
time.tm_isdst = daylight; // initialize dst setting
std::time(&temptime);
local_offset = difftime(mktime(localtime(&temptime)), mktime(gmtime(&temptime))) + (3600 * daylight);
if (ofxdate.size() != 0)
{
ofxdate_whole = ofxdate.substr(0, ofxdate.find_first_not_of("0123456789"));
if (ofxdate_whole.size() >= 8)
{
time.tm_year = atoi(ofxdate_whole.substr(0, 4).c_str()) - 1900;
time.tm_mon = atoi(ofxdate_whole.substr(4, 2).c_str()) - 1;
time.tm_mday = atoi(ofxdate_whole.substr(6, 2).c_str());
if (ofxdate_whole.size() > 8)
{
if (ofxdate_whole.size() == 14)
{
/* if exact time is specified */
exact_time_specified = true;
time.tm_hour = atoi(ofxdate_whole.substr(8, 2).c_str());
time.tm_min = atoi(ofxdate_whole.substr(10, 2).c_str());
time.tm_sec = atoi(ofxdate_whole.substr(12, 2).c_str());
}
else
{
message_out(WARNING, "ofxdate_to_time_t(): Successfully parsed date part, but unable to parse time part of string " + ofxdate_whole + ". It is not in proper YYYYMMDDHHMMSS.XXX[gmt offset:tz name] format!");
}
}
}
else
{
/* Catch invalid string format */
message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string " + ofxdate + " is not in proper YYYYMMDDHHMMSS.XXX[gmt offset:tz name] format!");
return mktime(&time);
}
/* Check if the timezone has been specified */
string::size_type startidx = ofxdate.find("[");
string::size_type endidx;
if (startidx != string::npos)
{
/* Time zone was specified */
time_zone_specified = true;
startidx++;
endidx = ofxdate.find(":", startidx) - 1;
ofx_gmt_offset = atof(ofxdate.substr(startidx, (endidx - startidx) + 1).c_str());
startidx = endidx + 2;
strncpy(timezone, ofxdate.substr(startidx, 3).c_str(), 4);
}
else
{
/* Time zone was not specified, assume GMT (provisionnaly) in case exact time is specified */
ofx_gmt_offset = 0;
strcpy(timezone, "GMT");
}
if (time_zone_specified == true)
{
/* If the timezone is specified always correct the timezone */
/* If the timezone is not specified, but the exact time is, correct the timezone, assuming GMT following the spec */
/* Correct the time for the timezone */
time.tm_sec = time.tm_sec + (int)(local_offset - (ofx_gmt_offset * 60 * 60)); //Convert from fractionnal hours to seconds
}
else if (exact_time_specified == false)
{
/*Time zone data missing and exact time not specified, diverge from the OFX spec ans assume 11h59 local time */
time.tm_hour = 11;
time.tm_min = 59;
time.tm_sec = 0;
}
return mktime(&time);
}
else
{
message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string is 0 length!");
return 0; // MUST RETURN ZERO here because otherwise the uninitialized &time will be returned!
}
return mktime(&time);
}
double ofxamount_to_double(const string ofxamount)
{
//Replace commas and decimal points for atof()
string::size_type idx;
string tmp = ofxamount;
idx = tmp.find(',');
if (idx == string::npos)
{
idx = tmp.find('.');
}
if (idx != string::npos)
{
tmp.replace(idx, 1, 1, ((localeconv())->decimal_point)[0]);
}
return atof(tmp.c_str());
}
string strip_whitespace(const string para_string)
{
size_t index;
size_t i;
string temp_string = para_string;
if (temp_string.empty())
return temp_string; // so that size()-1 is allowed below
const char *whitespace = " \b\f\n\r\t\v";
const char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
message_out(DEBUG4, "strip_whitespace() Before: |" + temp_string + "|");
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++);
temp_string.erase(0, i); //Strip leading whitespace
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--);
temp_string.erase(i + 1, temp_string.size() - (i + 1)); //Strip trailing whitespace
while ((index = temp_string.find_first_of(abnormal_whitespace)) != string::npos)
{
temp_string.erase(index, 1); //Strip leading whitespace
};
message_out(DEBUG4, "strip_whitespace() After: |" + temp_string + "|");
return temp_string;
}
std::string get_tmp_dir()
{
// Tries to mimic the behaviour of
// http://developer.gnome.org/doc/API/2.0/glib/glib-Miscellaneous-Utility-Functions.html#g-get-tmp-dir
char *var;
var = getenv("TMPDIR");
if (var) return var;
var = getenv("TMP");
if (var) return var;
var = getenv("TEMP");
if (var) return var;
#ifdef OS_WIN32
return "C:\\";
#else
return "/tmp";
#endif
}
int mkTempFileName(const char *tmpl, char *buffer, unsigned int size)
{
std::string tmp_dir = get_tmp_dir();
strncpy(buffer, tmp_dir.c_str(), size);
assert((strlen(buffer) + strlen(tmpl) + 2) < size);
strcat(buffer, DIRSEP);
strcat(buffer, tmpl);
return 0;
}
AppendCharStringtostring
string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
Append an OpenSP CharString to an existing C++ STL string.
Definition: ofx_utilities.cpp:83
WARNING
Definition: messages.hh:33
DEBUG4
Definition: messages.hh:29
message_out
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: messages.cpp:60
ofxdate_to_time_t
time_t ofxdate_to_time_t(const string ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
Definition: ofx_utilities.cpp:108
ofxamount_to_double
double ofxamount_to_double(const string ofxamount)
Convert OFX amount of money to double float.
Definition: ofx_utilities.cpp:205
CharStringtostring
string CharStringtostring(const SGMLApplication::CharString source, string &dest)
Convert OpenSP CharString to a C++ STL string.
Definition: ofx_utilities.cpp:70
ofx_utilities.hh
Various simple functions for type conversion & al.
ERROR
Definition: messages.hh:34
strip_whitespace
string strip_whitespace(const string para_string)
Sanitize a string coming from OpenSP.
Definition: ofx_utilities.cpp:228
messages.hh
Message IO functionality.