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 #else
34 # define DIRSEP "/"
35 #endif
36 
37 
38 using namespace std;
42 /*ostream &operator<<(ostream &os, SGMLApplication::CharString s)
43  {
44  for (size_t i = 0; i < s.len; i++)
45  {
46  os << ((char *)(s.ptr))[i*sizeof(SGMLApplication::Char)];
47  }
48  return os;
49  }*/
50 
51 /*wostream &operator<<(wostream &os, SGMLApplication::CharString s)
52  {
53  for (size_t i = 0; i < s.len; i++)
54  {//cout<<i;
55  os << wchar_t(s.ptr[i*MULTIPLY4]);
56  }
57  return os;
58  } */
59 
60 /*wchar_t* CharStringtowchar_t(SGMLApplication::CharString source, wchar_t *dest)
61  {
62  size_t i;
63  for (i = 0; i < source.len; i++)
64  {
65  dest[i]+=wchar_t(source.ptr[i*sizeof(SGMLApplication::Char)*(sizeof(char)/sizeof(wchar_t))]);
66  }
67  return dest;
68  }*/
69 
70 string CharStringtostring(const SGMLApplication::CharString source, string &dest)
71 {
72  size_t i;
73  dest.assign("");//Empty the provided string
74  // cout<<"Length: "<<source.len<<"sizeof(Char)"<<sizeof(SGMLApplication::Char)<<endl;
75  for (i = 0; i < source.len; i++)
76  {
77  dest += (char)(((source.ptr)[i]));
78  // cout<<i<<" "<<(char)(((source.ptr)[i]))<<endl;
79  }
80  return dest;
81 }
82 
83 string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
84 {
85  size_t i;
86  for (i = 0; i < source.len; i++)
87  {
88  dest += (char)(((source.ptr)[i]));
89  }
90  return dest;
91 }
92 
103  time_t ofxdate_to_time_t(const string& ofxdate)
104 {
105  if (ofxdate.empty())
106  {
107  message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string is 0 length!");
108  return 0;
109  }
110  string ofxdate_whole =
111  ofxdate.substr(0, ofxdate.find_first_not_of("0123456789"));
112 
113  if (ofxdate_whole.size() < 8)
114  {
115  message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string " + ofxdate + " is not in proper YYYYMMDDHHMMSS.XXX[gmt offset:tz name] format!");
116  return std::time(NULL);
117  }
118 
119  struct tm time;
120  memset(&time, 0, sizeof(tm));
121  time.tm_year = atoi(ofxdate_whole.substr(0, 4).c_str()) - 1900;
122  time.tm_mon = atoi(ofxdate_whole.substr(4, 2).c_str()) - 1;
123  time.tm_mday = atoi(ofxdate_whole.substr(6, 2).c_str());
124 
125  if (ofxdate_whole.size() < 14)
126  {
127  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!");
128  }
129  else
130  {
131  time.tm_hour = atoi(ofxdate_whole.substr(8, 2).c_str());
132  time.tm_min = atoi(ofxdate_whole.substr(10, 2).c_str());
133  time.tm_sec = atoi(ofxdate_whole.substr(12, 2).c_str());
134  }
135 
136  if (time.tm_hour + time.tm_min + time.tm_sec == 0)
137  {
138  time.tm_hour = 10;
139  time.tm_min = 59;
140  time.tm_sec = 0;
141  return timegm(&time);
142  }
143 
144  string::size_type startidx = ofxdate.find("[");
145  if (startidx != string::npos)
146  {
147  startidx++;
148  string::size_type endidx = ofxdate.find(":", startidx) - 1;
149  string offset_str = ofxdate.substr(startidx, (endidx - startidx) + 1);
150  float ofx_gmt_offset = atof(offset_str.c_str());
151  std::time_t temptime = std::time(nullptr);
152  static const double secs_per_hour = 3600.0;
153  time.tm_sec += static_cast<int>(ofx_gmt_offset * secs_per_hour);
154  return timegm(&time);
155  }
156 
157  /* No timezone, assume GMT */
158  return timegm(&time);
159 }
160 
165 double ofxamount_to_double(const string ofxamount)
166 {
167  //Replace commas and decimal points for atof()
168  string::size_type idx;
169  string tmp = ofxamount;
170 
171  idx = tmp.find(',');
172  if (idx == string::npos)
173  {
174  idx = tmp.find('.');
175  }
176 
177  if (idx != string::npos)
178  {
179  tmp.replace(idx, 1, 1, ((localeconv())->decimal_point)[0]);
180  }
181 
182  return atof(tmp.c_str());
183 }
184 
188 string strip_whitespace(const string para_string)
189 {
190  size_t index;
191  size_t i;
192  string temp_string = para_string;
193  if (temp_string.empty())
194  return temp_string; // so that size()-1 is allowed below
195 
196  const char *whitespace = " \b\f\n\r\t\v";
197  const char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
198  message_out(DEBUG4, "strip_whitespace() Before: |" + temp_string + "|");
199 
200  for (i = 0;
201  i <= temp_string.size()
202  && temp_string.find_first_of(whitespace, i) == i
203  && temp_string.find_first_of(whitespace, i) != string::npos;
204  i++);
205  temp_string.erase(0, i); //Strip leading whitespace
206 
207  for (i = temp_string.size() - 1;
208  (i > 0)
209  && (temp_string.find_last_of(whitespace, i) == i)
210  && (temp_string.find_last_of(whitespace, i) != string::npos);
211  i--);
212  temp_string.erase(i + 1, temp_string.size() - (i + 1)); //Strip trailing whitespace
213 
214  while ((index = temp_string.find_first_of(abnormal_whitespace)) != string::npos)
215  {
216  temp_string.erase(index, 1); //Strip leading whitespace
217  };
218 
219  message_out(DEBUG4, "strip_whitespace() After: |" + temp_string + "|");
220 
221  return temp_string;
222 }
223 
224 
225 std::string get_tmp_dir()
226 {
227  // Tries to mimic the behaviour of
228  // http://developer.gnome.org/doc/API/2.0/glib/glib-Miscellaneous-Utility-Functions.html#g-get-tmp-dir
229  char *var;
230  var = getenv("TMPDIR");
231  if (var) return var;
232  var = getenv("TMP");
233  if (var) return var;
234  var = getenv("TEMP");
235  if (var) return var;
236 #ifdef __WIN32__
237  return "C:\\";
238 #else
239  return "/tmp";
240 #endif
241 }
242 
243 int mkTempFileName(const char *tmpl, char *buffer, unsigned int size)
244 {
245 
246  std::string tmp_dir = get_tmp_dir();
247 
248  strncpy(buffer, tmp_dir.c_str(), size);
249  assert((strlen(buffer) + strlen(tmpl) + 2) < size);
250  strcat(buffer, DIRSEP);
251  strcat(buffer, tmpl);
252  return 0;
253 }
254 
255 
256 
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.