LibOFX
ofx_containers_misc.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ofx_proc_rs.cpp
3  -------------------
4  copyright : (C) 2002 by Benoit GrĂ©goire
5  email : benoitg@coeus.ca
6 ***************************************************************************/
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <iostream>
27 #include <stdlib.h>
28 #include <string>
29 #include "messages.hh"
30 #include "libofx.h"
31 #include "ofx_error_msg.hh"
32 #include "ofx_utilities.hh"
33 #include "ofx_containers.hh"
34 
35 extern OfxMainContainer * MainContainer;
36 
37 /***************************************************************************
38  * OfxDummyContainer *
39  ***************************************************************************/
40 
41 OfxDummyContainer::OfxDummyContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
42  OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
43 {
44  type = "DUMMY";
45  message_out(INFO, "Created OfxDummyContainer to hold unsupported aggregate " + para_tag_identifier);
46 }
47 void OfxDummyContainer::add_attribute(const std::string identifier, const std::string value)
48 {
49  message_out(DEBUG, "OfxDummyContainer for " + tag_identifier + " ignored a " + identifier + " (" + value + ")");
50 }
51 
52 /***************************************************************************
53  * OfxPushUpContainer *
54  ***************************************************************************/
55 
56 OfxPushUpContainer::OfxPushUpContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
57  OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
58 {
59  type = "PUSHUP";
60  message_out(DEBUG, "Created OfxPushUpContainer to hold aggregate " + tag_identifier);
61 }
62 void OfxPushUpContainer::add_attribute(const std::string identifier, const std::string value)
63 {
64  //message_out(DEBUG, "OfxPushUpContainer for "+tag_identifier+" will push up a "+identifier+" ("+value+") to a "+ parentcontainer->type + " container");
65  if (parentcontainer)
66  parentcontainer->add_attribute(identifier, value);
67 }
68 
69 /***************************************************************************
70  * OfxStatusContainer *
71  ***************************************************************************/
72 
73 OfxStatusContainer::OfxStatusContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
74  OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
75 {
76  memset(&data, 0, sizeof(data));
77  type = "STATUS";
78  if (parentcontainer != NULL)
79  {
80  ASSIGN_STRNCPY(data.ofx_element_name, parentcontainer->tag_identifier);
81  }
82 
83 }
84 OfxStatusContainer::~OfxStatusContainer()
85 {
86  message_out(DEBUG, "Entering the status's container's destructor");
87 
88  libofx_context->statusCallback(data);
89 
90  if ( data.server_message_valid )
91  delete [] data.server_message;
92 }
93 
94 void OfxStatusContainer::add_attribute(const std::string identifier, const std::string value)
95 {
96  ErrorMsg error_msg;
97 
98  if ( identifier == "CODE")
99  {
100  ASSIGN(data.code, atoi(value.c_str()));
101  error_msg = find_error_msg(data.code);
102  data.name = error_msg.name;//memory is already allocated
103  data.description = error_msg.description;//memory is already allocated
104  }
105  else if (identifier == "SEVERITY")
106  {
107  data.severity_valid = true;
108  if (value == "INFO")
109  {
110  data.severity = OfxStatusData::INFO;
111  }
112  else if (value == "WARN")
113  {
114  data.severity = OfxStatusData::WARN;
115  }
116  else if (value == "ERROR")
117  {
118  data.severity = OfxStatusData::ERROR;
119  }
120  else
121  {
122  message_out(ERROR, "WRITEME: Unknown severity " + value + " inside a " + type + " container");
123  data.severity_valid = false;
124  }
125  }
126  else if ((identifier == "MESSAGE") || (identifier == "MESSAGE2"))
127  {
128  data.server_message = new char[value.length() + 1];
129  strcpy(data.server_message, value.c_str());
130  data.server_message_valid = true;
131  }
132  else
133  {
134  /* Redirect unknown identifiers to the base class */
135  OfxGenericContainer::add_attribute(identifier, value);
136  }
137 }
138 
139 
140 
141 /***************************************************************************
142  * OfxBalanceContainer (does not directly abstract a object in libofx.h) *
143  ***************************************************************************/
144 
145 OfxBalanceContainer::OfxBalanceContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
146  OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
147 {
148  amount_valid = false;
149  date_valid = false;
150  margin_balance_valid = false;
151  short_balance_valid = false;
152  buying_power_valid = false;
153  type = "BALANCE";
154 }
155 
156 OfxBalanceContainer::~OfxBalanceContainer()
157 {
158  if (parentcontainer->type == "STATEMENT")
159  {
160  ((OfxStatementContainer*)parentcontainer)->add_balance(this);
161  }
162  else
163  {
164  message_out (ERROR, "I completed a " + type + " element, but I haven't found a suitable parent to save it");
165  }
166 }
167 void OfxBalanceContainer::add_attribute(const std::string identifier, const std::string value)
168 {
169  if (identifier == "BALAMT" ||
170  identifier == "AVAILCASH" || // from <INVBAL>
171  identifier == "CASHBAL") // from <INV401KBAL>
172  {
174  }
175  else if (identifier == "MARGINBALANCE")
176  {
177  ASSIGN(margin_balance, ofxamount_to_double(value));
178  }
179  else if (identifier == "SHORTBALANCE")
180  {
181  ASSIGN(short_balance, ofxamount_to_double(value));
182  }
183  else if (identifier == "BUYPOWER")
184  {
185  ASSIGN(buying_power, ofxamount_to_double(value));
186  }
187  else if (identifier == "DTASOF")
188  {
189  ASSIGN(date, ofxdate_to_time_t(value));
190  }
191  else
192  {
193  /* Redirect unknown identifiers to the base class */
194  OfxGenericContainer::add_attribute(identifier, value);
195  }
196 }
197 
198 /***************************************************************************
199  * OfxInv401kContainer *
200  * This container is only used to throw away the multiple possible *
201  * <DTSTART>, <DTASOF> and <DTEND> elements that can occur under <INV401K> *
202  * so they don't corrupt the statement dates. *
203 ***************************************************************************/
204 
205 OfxInv401kContainer::OfxInv401kContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
206  OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
207 {
208  type = "INV401K";
209  message_out(INFO, "Created OfxInv401kContainer to hold unsupported aggregate " + para_tag_identifier);
210 }
211 void OfxInv401kContainer::add_attribute(const std::string identifier, const std::string value)
212 {
213  if (identifier == "DTSTART" || identifier == "DTEND" || identifier == "DTASOF")
214  {
215  message_out(DEBUG, "OfxInv401kContainer for " + tag_identifier + " ignored a " + identifier + " (" + value + ")");
216  }
217  else
218  {
219  /* Redirect unknown identifiers to the base class */
220  OfxGenericContainer::add_attribute(identifier, value);
221  }
222 }
223 
OfxGenericContainer::type
std::string type
Definition: ofx_containers.hh:41
OfxBalanceContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_containers_misc.cpp:167
ASSIGN
#define ASSIGN(DEST, VALUE)
Definition: ofx_utilities.hh:45
OfxStatusContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_containers_misc.cpp:94
ASSIGN_STRNCPY
#define ASSIGN_STRNCPY(DEST, VALUE)
Definition: ofx_utilities.hh:52
OfxMainContainer
The root container. Created by the <OFX> OFX element or by the export functions.
Definition: ofx_containers.hh:289
OfxBalanceContainer::amount
double amount
Definition: ofx_containers.hh:126
ErrorMsg::name
const char * name
Definition: ofx_error_msg.hh:33
OfxStatusData::server_message
char * server_message
Definition: inc/libofx.h:258
OfxPushUpContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_containers_misc.cpp:62
ofx_error_msg.hh
OFX error code management functionality.
ERROR
@ ERROR
Definition: messages.hh:41
ErrorMsg::description
const char * description
Definition: ofx_error_msg.hh:34
OfxStatusData::ERROR
@ ERROR
Definition: inc/libofx.h:249
OfxStatusData::INFO
@ INFO
Definition: inc/libofx.h:247
find_error_msg
const ErrorMsg find_error_msg(int param_code)
Retrieve error code descriptions.
Definition: ofx_error_msg.hh:130
ofx_utilities.hh
Various simple functions for type conversion & al.
OfxStatusData::name
const char * name
Definition: inc/libofx.h:240
OfxGenericContainer
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
Definition: ofx_containers.hh:31
OfxInv401kContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_containers_misc.cpp:211
OfxGenericContainer::add_attribute
virtual void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_container_generic.cpp:57
ofxamount_to_double
double ofxamount_to_double(const std::string ofxamount)
Convert OFX amount of money to double float.
Definition: ofx_utilities.cpp:130
LibofxContext
Definition: context.hh:23
OfxStatusData::WARN
@ WARN
Definition: inc/libofx.h:248
ofx_containers.hh
LibOFX internal object code.
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
INFO
@ INFO
Definition: messages.hh:39
OfxStatementContainer
Represents a statement for either a bank account or a credit card account.
Definition: ofx_containers.hh:155
ErrorMsg
An abstraction of an OFX error code sent by an OFX server.
Definition: ofx_error_msg.hh:23
OfxDummyContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_containers_misc.cpp:47
OfxBalanceContainer::date
time_t date
Definition: ofx_containers.hh:128
OfxStatusData::description
const char * description
Definition: inc/libofx.h:241
OfxStatusData::code
int code
Definition: inc/libofx.h:239
messages.hh
Message IO functionality.
OfxGenericContainer::tag_identifier
std::string tag_identifier
Definition: ofx_containers.hh:42
DEBUG
@ DEBUG
Definition: messages.hh:32