LibOFX
ofx_utilities.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ofx_util.cpp
3  -------------------
4  copyright : (C) 2002 by Benoit Gr�goire
5  email : benoitg@coeus.ca
6  ***************************************************************************/
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 #include <config.h>
19 #include <iostream>
20 #include <assert.h>
21 
22 #include "ParserEventGeneratorKit.h"
23 #include "SGMLApplication.h"
24 #include <ctime>
25 #include <cstdlib>
26 #include <string>
27 #include <locale.h>
28 #include "messages.hh"
29 #include "ofx_utilities.hh"
30 
31 #ifdef __WIN32__
32 # define DIRSEP "\\"
33 /* MSWin calls it _mkgmtime instead of timegm */
34 # define timegm(tm) _mkgmtime(tm)
35 #else
36 # define DIRSEP "/"
37 #endif
38 
39 
40 using namespace std;
44 /*ostream &operator<<(ostream &os, SGMLApplication::CharString s)
45  {
46  for (size_t i = 0; i < s.len; i++)
47  {
48  os << ((char *)(s.ptr))[i*sizeof(SGMLApplication::Char)];
49  }
50  return os;
51  }*/
52 
53 /*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
54  {
55  for (size_t i = 0; i < s.len; i++)
56  {//cout<<i;
57  os << wchar_t(s.ptr[i*MULTIPLY4]);
58  }
59  return os;
60  } */
61 
62 /*wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
63  {
64  size_t i;
65  for (i = 0; i < source.len; i++)
66  {
67  dest[i]+=wchar_t(source.ptr[i*sizeof(SGMLApplication::Char)*(sizeof(char)/sizeof(wchar_t))]);
68  }
69  return dest;
70  }*/
71 
72 string CharStringtostring(const SGMLApplication::CharString source, string &dest)
73 {
74  size_t i;
75  dest.assign("");//Empty the provided string
76  // cout<<"Length: "<<source.len<<"sizeof(Char)"<<sizeof(SGMLApplication::Char)<<endl;
77  for (i = 0; i < source.len; i++)
78  {
79  dest += (char)(((source.ptr)[i]));
80  // cout<<i<<" "<<(char)(((source.ptr)[i]))<<endl;
81  }
82  return dest;
83 }
84 
85 string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
86 {
87  size_t i;
88  for (i = 0; i < source.len; i++)
89  {
90  dest += (char)(((source.ptr)[i]));
91  }
92  return dest;
93 }
94 
105  time_t ofxdate_to_time_t(const string& ofxdate)
106 {
107  if (ofxdate.empty())
108  {
109  message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string is 0 length!");
110  return 0;
111  }
112  string ofxdate_whole =
113  ofxdate.substr(0, ofxdate.find_first_not_of("0123456789"));
114 
115  if (ofxdate_whole.size() < 8)
116  {
117  message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string " + ofxdate + " is not in proper YYYYMMDDHHMMSS.XXX[gmt offset:tz name] format!");
118  return std::time(NULL);
119  }
120 
121  struct tm time;
122  memset(&time, 0, sizeof(tm));
123  time.tm_year = atoi(ofxdate_whole.substr(0, 4).c_str()) - 1900;
124  time.tm_mon = atoi(ofxdate_whole.substr(4, 2).c_str()) - 1;
125  time.tm_mday = atoi(ofxdate_whole.substr(6, 2).c_str());
126 
127  if (ofxdate_whole.size() < 14)
128  {
129  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!");
130  }
131  else
132  {
133  time.tm_hour = atoi(ofxdate_whole.substr(8, 2).c_str());
134  time.tm_min = atoi(ofxdate_whole.substr(10, 2).c_str());
135  time.tm_sec = atoi(ofxdate_whole.substr(12, 2).c_str());
136  }
137 
138  if (time.tm_hour + time.tm_min + time.tm_sec == 0)
139  {
140  time.tm_hour = 10;
141  time.tm_min = 59;
142  time.tm_sec = 0;
143  return timegm(&time);
144  }
145 
146  string::size_type startidx = ofxdate.find("[");
147  if (startidx != string::npos)
148  {
149  startidx++;
150  string::size_type endidx = ofxdate.find(":", startidx) - 1;
151  string offset_str = ofxdate.substr(startidx, (endidx - startidx) + 1);
152  float ofx_gmt_offset = atof(offset_str.c_str());
153  std::time_t temptime = std::time(nullptr);
154  static const double secs_per_hour = 3600.0;
155  time.tm_sec -= static_cast<int>(ofx_gmt_offset * secs_per_hour);
156  return timegm(&time);
157  }
158 
159  /* No timezone, assume GMT */
160  return timegm(&time);
161 }
162 
167 double ofxamount_to_double(const string ofxamount)
168 {
169  //Replace commas and decimal points for atof()
170  string::size_type idx;
171  string tmp = ofxamount;
172 
173  idx = tmp.find(',');
174  if (idx == string::npos)
175  {
176  idx = tmp.find('.');
177  }
178 
179  if (idx != string::npos)
180  {
181  tmp.replace(idx, 1, 1, ((localeconv())->decimal_point)[0]);
182  }
183 
184  return atof(tmp.c_str());
185 }
186 
190 string strip_whitespace(const string para_string)
191 {
192  size_t index;
193  size_t i;
194  string temp_string = para_string;
195  if (temp_string.empty())
196  return temp_string; // so that size()-1 is allowed below
197 
198  const char *whitespace = " \b\f\n\r\t\v";
199  const char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,carriage return, horizontal and vertical tabs
200  message_out(DEBUG4, "strip_whitespace() Before: |" + temp_string + "|");
201 
202  for (i = 0;
203  i <= temp_string.size()
204  && temp_string.find_first_of(whitespace, i) == i
205  && temp_string.find_first_of(whitespace, i) != string::npos;
206  i++);
207  temp_string.erase(0, i); //Strip leading whitespace
208 
209  for (i = temp_string.size() - 1;
210  (i > 0)
211  && (temp_string.find_last_of(whitespace, i) == i)
212  && (temp_string.find_last_of(whitespace, i) != string::npos);
213  i--);
214  temp_string.erase(i + 1, temp_string.size() - (i + 1)); //Strip trailing whitespace
215 
216  while ((index = temp_string.find_first_of(abnormal_whitespace)) != string::npos)
217  {
218  temp_string.erase(index, 1); //Strip leading whitespace
219  };
220 
221  message_out(DEBUG4, "strip_whitespace() After: |" + temp_string + "|");
222 
223  return temp_string;
224 }
225 
226 
227 std::string get_tmp_dir()
228 {
229  // Tries to mimic the behaviour of
230  // http://developer.gnome.org/doc/API/2.0/glib/glib-Miscellaneous-Utility-Functions.html#g-get-tmp-dir
231  char *var;
232  var = getenv("TMPDIR");
233  if (var) return var;
234  var = getenv("TMP");
235  if (var) return var;
236  var = getenv("TEMP");
237  if (var) return var;
238 #ifdef __WIN32__
239  return "C:\\";
240 #else
241  return "/tmp";
242 #endif
243 }
244 
245 int mkTempFileName(const char *tmpl, char *buffer, unsigned int size)
246 {
247 
248  std::string tmp_dir = get_tmp_dir();
249 
250  strncpy(buffer, tmp_dir.c_str(), size);
251  assert((strlen(buffer) + strlen(tmpl) + 2) < size);
252  strcat(buffer, DIRSEP);
253  strcat(buffer, tmpl);
254  return 0;
255 }
256 
257 
258 
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: messages.cpp:61
Message IO functionality.
@ ERROR
Definition: messages.hh:34
@ WARNING
Definition: messages.hh:33
@ DEBUG4
Definition: messages.hh:29
string strip_whitespace(const string para_string)
Sanitize a string coming from OpenSP.
string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
Append an OpenSP CharString to an existing C++ STL string.
time_t ofxdate_to_time_t(const string &ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
double ofxamount_to_double(const string ofxamount)
Convert OFX amount of money to double float.
string CharStringtostring(const SGMLApplication::CharString source, string &dest)
Convert OpenSP CharString to a C++ STL string.
Various simple functions for type conversion & al.