LibOFX
ofx_container_main.cpp
Go to the documentation of this file.
1/***************************************************************************
2 ofx_container_main.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
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <string>
24#include <iostream>
25#include "ParserEventGeneratorKit.h"
26#include "messages.hh"
27#include "libofx.h"
28#include "ofx_containers.hh"
29
30OfxMainContainer::OfxMainContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
31 OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
32{
33
34//statement_tree_top=statement_tree.insert(statement_tree_top, NULL);
35//security_tree_top=security_tree.insert(security_tree_top, NULL);
36
37}
38OfxMainContainer::~OfxMainContainer()
39{
40 message_out(DEBUG, "Entering the main container's destructor");
41 tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
42
43 while (tmp != security_tree.end())
44 {
45 message_out(DEBUG, "Deleting " + (*tmp)->type);
46 delete (*tmp);
47 ++tmp;
48 }
49 tmp = account_tree.begin();
50 while (tmp != account_tree.end())
51 {
52 message_out(DEBUG, "Deleting " + (*tmp)->type);
53 delete (*tmp);
54 ++tmp;
55 }
56}
57int OfxMainContainer::add_container(OfxGenericContainer * container)
58{
59 message_out(DEBUG, "OfxMainContainer::add_container for element " + container->tag_identifier + "; destroying the generic container");
60 /* Call gen_event anyway, it could be a status container or similar */
61 container->gen_event();
62 if (container != this) {
63 delete container;
64 }
65 return 0;
66}
67
68int OfxMainContainer::add_container(OfxSecurityContainer * container)
69{
70 message_out(DEBUG, "OfxMainContainer::add_container, adding a security");
71 security_tree.insert(security_tree.begin(), container);
72 return true;
73
74
75}
76
77int OfxMainContainer::add_container(OfxAccountContainer * container)
78{
79 message_out(DEBUG, "OfxMainContainer::add_container, adding an account");
80 if ( account_tree.size() == 0)
81 {
82 message_out(DEBUG, "OfxMainContainer::add_container, account is the first account");
83 account_tree.insert(account_tree.begin(), container);
84 }
85 else
86 {
87 message_out(DEBUG, "OfxMainContainer::add_container, account is not the first account");
89 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
90 account_tree.insert_after(tmp, container);
91 }
92 return true;
93}
94
95int OfxMainContainer::add_container(OfxStatementContainer * container)
96{
97 message_out(DEBUG, "OfxMainContainer::add_container, adding a statement");
99 //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
100 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
101
102 if (account_tree.is_valid(tmp))
103 {
104 message_out(DEBUG, "1: tmp is valid, Accounts are present");
105 tree<OfxGenericContainer *>::iterator child = account_tree.begin(tmp); // FIXME: Variable "child" is unused. Is there an error below?
106 if (account_tree.number_of_children(tmp) != 0)
107 {
108 message_out(DEBUG, "There are already children for this account");
109 account_tree.insert(tmp.begin(), container);
110
111 }
112 else
113 {
114 message_out(DEBUG, "There are no children for this account");
115 account_tree.append_child(tmp, container);
116 }
117 container->add_account(&( ((OfxAccountContainer *)(*tmp))->data));
118 return true;
119 }
120 else
121 {
122 message_out(ERROR, "OfxMainContainer::add_container, no accounts are present (tmp is invalid)");
123 return false;
124 }
125}
126
127int OfxMainContainer::add_container(OfxTransactionContainer * container)
128{
129 message_out(DEBUG, "OfxMainContainer::add_container, adding a transaction");
130
131 if ( account_tree.size() != 0)
132 {
134 //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
135 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
136 if (account_tree.is_valid(tmp))
137 {
138 message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
139 account_tree.append_child(tmp, container);
140 container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
141 return true;
142 }
143 else
144 {
145 message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
146 return false;
147 }
148 }
149 else
150 {
151 message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
152 return false;
153 }
154}
155
156int OfxMainContainer::add_container(OfxPositionContainer * container)
157{
158 message_out(DEBUG, "OfxMainContainer::add_container, adding a position");
159
160 if ( account_tree.size() != 0)
161 {
163 //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
164 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
165 if (account_tree.is_valid(tmp))
166 {
167 message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
168 account_tree.append_child(tmp, container);
169 container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
170 return true;
171 }
172 else
173 {
174 message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
175 return false;
176 }
177 }
178 else
179 {
180 message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
181 return false;
182 }
183}
184
186{
187 message_out(DEBUG, "Begin walking the trees of the main container to generate events");
188 tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
189 //cerr<<"security_tree.size(): "<<security_tree.size()<<endl;
190 int i = 0;
191 while (tmp != security_tree.end())
192 {
193 message_out(DEBUG, "Looping...");
194 //cerr <<i<<endl;
195 i++;
196 (*tmp)->gen_event();
197 ++tmp;
198 }
199 tmp = account_tree.begin();
200 //cerr<<account_tree.size()<<endl;
201 i = 0;
202 while (tmp != account_tree.end())
203 {
204 //cerr<< "i="<<i<<"; depth="<<account_tree.depth(tmp)<<endl;
205 i++;
206 (*tmp)->gen_event();
207 ++tmp;
208 }
209 message_out(DEBUG, "End walking the trees of the main container to generate events");
210
211 return true;
212}
213
214OfxSecurityData * OfxMainContainer::find_security(std::string unique_id)
215{
216 message_out(DEBUG, "OfxMainContainer::find_security() Begin.");
217
219 OfxSecurityData * retval = NULL;
220 while (tmp != security_tree.end() && retval == NULL)
221 {
222 if (((OfxSecurityContainer*)(*tmp))->data.unique_id == unique_id)
223 {
224 message_out(DEBUG, (std::string)"Security " + ((OfxSecurityContainer*)(*tmp))->data.unique_id + " found.");
225 retval = &((OfxSecurityContainer*)(*tmp))->data;
226 }
227 ++tmp;
228 }
229 return retval;
230}
Represents a bank account or a credit card account.
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
std::string tag_identifier
virtual int gen_event()
Generate libofx.h events.
int gen_event()
Generate libofx.h events.
Represents an investment position, such as a stock or bond.
Represents a security, such as a stock or bond.
Represents a statement for either a bank account or a credit card account.
Represents a generic transaction.
Definition: tree.hh:107
iter insert_after(iter position, const T &x)
Insert node as next sibling of node pointed to by position.
Definition: tree.hh:890
bool is_valid(const iterator_base &) const
Determine whether the iterator is an 'end' iterator and thus not actually pointing to a node.
Definition: tree.hh:1523
iter append_child(iter position)
Insert empty node as last child of node pointed to by position.
Definition: tree.hh:742
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: tree.hh:581
iter insert(iter position, const T &x)
Insert node as previous sibling of node pointed to by position.
Definition: tree.hh:829
unsigned int number_of_children(const iterator_base &) const
Count the number of children of node at position.
Definition: tree.hh:1442
int size() const
Count the total number of nodes.
Definition: tree.hh:1408
unsigned int number_of_siblings(const iterator_base &) const
Count the number of 'next' siblings of node at iterator.
Definition: tree.hh:1458
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: tree.hh:587
Main header file containing the LibOfx API.
int message_out(OfxMsgType error_type, const std::string message)
Message output function.
Definition: messages.cpp:67
Message IO functionality.
@ DEBUG
Definition: messages.hh:25
@ ERROR
Definition: messages.hh:34
LibOFX internal object code.
An abstraction of a security, such as a stock, mutual fund, etc.
Definition: libofx.h:370