LibOFX
build/libofx-0.10.0/lib/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 
93 /* Since we don't know for an arbitrary date-time whether or not it represents daylight time and we have to tell mktime something, we tell it yes if the current environment's TZ uses daylight time. If mktime finds that the supplied date-time isn't in daylight time for that time zone, it will adjust the struct tm to reflect the same time-point in not-daylight time and correct the value of tm_isdst. We check for that change and if it happens restore timeptr's date and time values and call mktime again with the correct value of tm_isdst. */
94 static time_t
95 checked_mktime(struct tm* timeptr)
96 {
97  int is_dst = timeptr->tm_isdst;
98  int min = timeptr->tm_min;
99  int hour = timeptr->tm_hour;
100  int mday = timeptr->tm_mday;
101  int mon = timeptr->tm_mon;
102  int year = timeptr->tm_year;
103  time_t result = mktime(timeptr);
104  if (is_dst == timeptr->tm_isdst) // mktime didn't change it, OK to use the result.
105  return result;
106 
107  //Restore the date & time to what it was, but use the new isdst value:
108  timeptr->tm_min = min;
109  timeptr->tm_hour = hour;
110  timeptr->tm_mday = mday;
111  timeptr->tm_mon = mon;
112  timeptr->tm_year = year;
113  return mktime(timeptr);
114 }
115 
131 time_t ofxdate_to_time_t(const string ofxdate)
132 {
133  struct tm time;
134  double local_offset; /* in seconds */
135  float ofx_gmt_offset; /* in fractional hours */
136  char timezone[4]; /* Original timezone: the library does not expose this value*/
137  char exact_time_specified = false;
138  char time_zone_specified = false;
139  string ofxdate_whole;
140  time_t temptime;
141 
142  std::time(&temptime);
143  local_offset = difftime(mktime(localtime(&temptime)), mktime(gmtime(&temptime)));
144  /* daylight is set to 1 if the timezone indicated by the environment (either TZ or /etc/localtime) uses daylight savings time (aka summer time). We use it here to provisionally set tm_isdst. */
145  time.tm_isdst = daylight;
146 
147  if (ofxdate.size() != 0)
148  {
149  ofxdate_whole = ofxdate.substr(0, ofxdate.find_first_not_of("0123456789"));
150  if (ofxdate_whole.size() >= 8)
151  {
152  time.tm_year = atoi(ofxdate_whole.substr(0, 4).c_str()) - 1900;
153  time.tm_mon = atoi(ofxdate_whole.substr(4, 2).c_str()) - 1;
154  time.tm_mday = atoi(ofxdate_whole.substr(6, 2).c_str());
155 
156  if (ofxdate_whole.size() > 8)
157  {
158  if (ofxdate_whole.size() == 14)
159  {
160  /* if exact time is specified */
161  exact_time_specified = true;
162  time.tm_hour = atoi(ofxdate_whole.substr(8, 2).c_str());
163  time.tm_min = atoi(ofxdate_whole.substr(10, 2).c_str());
164  time.tm_sec = atoi(ofxdate_whole.substr(12, 2).c_str());
165  }
166  else
167  {
168  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!");
169  }
170  }
171 
172  }
173  else
174  {
175  /* Catch invalid string format */
176  message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string " + ofxdate + " is not in proper YYYYMMDDHHMMSS.XXX[gmt offset:tz name] format!");
177  return checked_mktime(&time);
178  }
179 
180 
181  /* Check if the timezone has been specified */
182  string::size_type startidx = ofxdate.find("[");
183  string::size_type endidx;
184  if (startidx != string::npos)
185  {
186  /* Time zone was specified */
187  time_zone_specified = true;
188  startidx++;
189  endidx = ofxdate.find(":", startidx) - 1;
190  ofx_gmt_offset = atof(ofxdate.substr(startidx, (endidx - startidx) + 1).c_str());
191  startidx = endidx + 2;
192  strncpy(timezone, ofxdate.substr(startidx, 3).c_str(), 4);
193  }
194  else
195  {
196  /* Time zone was not specified, assume GMT (provisionnaly) in case exact time is specified */
197  ofx_gmt_offset = 0;
198  time.tm_isdst = 0; //GMT doesn't use daylight time.
199  strcpy(timezone, "GMT");
200  }
201 
202  if (time_zone_specified == true)
203  {
204  /* If the timezone is specified always correct the timezone */
205  /* If the timezone is not specified, but the exact time is, correct the timezone, assuming GMT following the spec */
206  /* Correct the time for the timezone */
207  time.tm_sec = time.tm_sec + (int)(local_offset - (ofx_gmt_offset * 60 * 60)); //Convert from fractionnal hours to seconds
208  }
209  else if (exact_time_specified == false)
210  {
211  /*Time zone data missing and exact time not specified, diverge from the OFX spec ans assume 11h59 local time */
212  time.tm_hour = 11;
213  time.tm_min = 59;
214  time.tm_sec = 0;
215  }
216  return checked_mktime(&time);
217  }
218  else
219  {
220  message_out(ERROR, "ofxdate_to_time_t(): Unable to convert time, string is 0 length!");
221  return 0; // MUST RETURN ZERO here because otherwise the uninitialized &time will be returned!
222  }
223  return checked_mktime(&time);
224 }
225 
230 double ofxamount_to_double(const string ofxamount)
231 {
232  //Replace commas and decimal points for atof()
233  string::size_type idx;
234  string tmp = ofxamount;
235 
236  idx = tmp.find(',');
237  if (idx == string::npos)
238  {
239  idx = tmp.find('.');
240  }
241 
242  if (idx != string::npos)
243  {
244  tmp.replace(idx, 1, 1, ((localeconv())->decimal_point)[0]);
245  }
246 
247  return atof(tmp.c_str());
248 }
249 
253 string strip_whitespace(const string para_string)
254 {
255  size_t index;
256  size_t i;
257  string temp_string = para_string;
258  if (temp_string.empty())
259  return temp_string; // so that size()-1 is allowed below
260 
261  const char *whitespace = " \b\f\n\r\t\v";
262  const char *abnormal_whitespace = "\b\f\n\r\t\v";//backspace,formfeed,newline,cariage return, horizontal and vertical tabs
263  message_out(DEBUG4, "strip_whitespace() Before: |" + temp_string + "|");
264 
265  for (i = 0;
266  i <= temp_string.size()
267  && temp_string.find_first_of(whitespace, i) == i
268  && temp_string.find_first_of(whitespace, i) != string::npos;
269  i++);
270  temp_string.erase(0, i); //Strip leading whitespace
271 
272  for (i = temp_string.size() - 1;
273  (i > 0)
274  && (temp_string.find_last_of(whitespace, i) == i)
275  && (temp_string.find_last_of(whitespace, i) != string::npos);
276  i--);
277  temp_string.erase(i + 1, temp_string.size() - (i + 1)); //Strip trailing whitespace
278 
279  while ((index = temp_string.find_first_of(abnormal_whitespace)) != string::npos)
280  {
281  temp_string.erase(index, 1); //Strip leading whitespace
282  };
283 
284  message_out(DEBUG4, "strip_whitespace() After: |" + temp_string + "|");
285 
286  return temp_string;
287 }
288 
289 
290 std::string get_tmp_dir()
291 {
292  // Tries to mimic the behaviour of
293  // http://developer.gnome.org/doc/API/2.0/glib/glib-Miscellaneous-Utility-Functions.html#g-get-tmp-dir
294  char *var;
295  var = getenv("TMPDIR");
296  if (var) return var;
297  var = getenv("TMP");
298  if (var) return var;
299  var = getenv("TEMP");
300  if (var) return var;
301 #ifdef __WIN32__
302  return "C:\\";
303 #else
304  return "/tmp";
305 #endif
306 }
307 
308 int mkTempFileName(const char *tmpl, char *buffer, unsigned int size)
309 {
310 
311  std::string tmp_dir = get_tmp_dir();
312 
313  strncpy(buffer, tmp_dir.c_str(), size);
314  assert((strlen(buffer) + strlen(tmpl) + 2) < size);
315  strcat(buffer, DIRSEP);
316  strcat(buffer, tmpl);
317  return 0;
318 }
319 
320 
321 
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.
string strip_whitespace(const string para_string)
Sanitize a string coming from OpenSP.
time_t ofxdate_to_time_t(const string ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
int message_out(OfxMsgType error_type, const string message)
Message output function.
string AppendCharStringtostring(const SGMLApplication::CharString source, string &dest)
Append an OpenSP CharString to an existing C++ STL string.
Message IO functionality.