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 _WIN32
# define DIRSEP "\\"
/* MSWin calls it _mkgmtime instead of timegm */
# define timegm(tm) _mkgmtime(tm)
#else
# define DIRSEP "/"
#endif
std::string CharStringtostring(const SGMLApplication::CharString source)
{
// The CharString type might have multi-byte characters if SP_MULTI_BYTE was defined
std::string result;
result.resize(source.len);
for (size_t i = 0; i < source.len; i++)
{
result[i] = static_cast<char>(source.ptr[i]);
}
return result;
}
void AppendCharStringtostring(const SGMLApplication::CharString source, std::string &dest)
{
const std::string toBeAppended = CharStringtostring(source);
dest.append(toBeAppended);
}
time_t ofxdate_to_time_t(const std::string& ofxdate)
{
if (ofxdate.empty())
{
message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string is 0 length!");
return 0;
}
std::string ofxdate_whole =
ofxdate.substr(0, ofxdate.find_first_not_of("0123456789"));
if (ofxdate_whole.size() < 8)
{
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 std::time(NULL);
}
struct tm time;
memset(&time, 0, sizeof(tm));
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() < 14)
{
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
{
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());
}
if (time.tm_hour + time.tm_min + time.tm_sec == 0)
{
time.tm_hour = 10;
time.tm_min = 59;
time.tm_sec = 0;
return timegm(&time);
}
std::string::size_type startidx = ofxdate.find("[");
if (startidx != std::string::npos)
{
startidx++;
std::string::size_type endidx = ofxdate.find(":", startidx) - 1;
std::string offset_str = ofxdate.substr(startidx, (endidx - startidx) + 1);
float ofx_gmt_offset = atof(offset_str.c_str());
std::time_t temptime = std::time(nullptr);
static const double secs_per_hour = 3600.0;
time.tm_sec -= static_cast<int>(ofx_gmt_offset * secs_per_hour);
return timegm(&time);
}
/* No timezone, assume GMT */
return timegm(&time);
}
double ofxamount_to_double(const std::string ofxamount)
{
//Replace commas and decimal points for atof()
std::string::size_type idx;
std::string tmp = ofxamount;
idx = tmp.find(',');
if (idx == std::string::npos)
{
idx = tmp.find('.');
}
if (idx != std::string::npos)
{
tmp.replace(idx, 1, 1, ((localeconv())->decimal_point)[0]);
}
return atof(tmp.c_str());
}
std::string strip_whitespace(const std::string para_string)
{
size_t index;
size_t i;
std::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,carriage 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) != std::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) != std::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)) != std::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 _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;
}
strip_whitespace
std::string strip_whitespace(const std::string para_string)
Sanitize a string coming from OpenSP.
Definition: ofx_utilities.cpp:153
ERROR
@ ERROR
Definition: messages.hh:41
ofx_utilities.hh
Various simple functions for type conversion & al.
ofxamount_to_double
double ofxamount_to_double(const std::string ofxamount)
Convert OFX amount of money to double float.
Definition: ofx_utilities.cpp:130
message_out
int message_out(OfxMsgType error_type, const std::string message)
Message output function.
Definition: messages.cpp:67
ofxdate_to_time_t
time_t ofxdate_to_time_t(const std::string &ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
Definition: ofx_utilities.cpp:68
AppendCharStringtostring
void AppendCharStringtostring(const SGMLApplication::CharString source, std::string &dest)
Append an OpenSP CharString to an existing C++ STL string.
Definition: ofx_utilities.cpp:52
WARNING
@ WARNING
Definition: messages.hh:40
messages.hh
Message IO functionality.
CharStringtostring
std::string CharStringtostring(const SGMLApplication::CharString source)
Convert OpenSP CharString to a C++ STL string.
Definition: ofx_utilities.cpp:40
DEBUG4
@ DEBUG4
Definition: messages.hh:36