LibOFX: ofx_container_main.cpp Source File

LibOFX

lib/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
30 OfxMainContainer::OfxMainContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, 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 }
38 OfxMainContainer::~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 }
57 int 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 delete container;
63 return 0;
64 }
65
66 int OfxMainContainer::add_container(OfxSecurityContainer * container)
67 {
68 message_out(DEBUG, "OfxMainContainer::add_container, adding a security");
69 security_tree.insert(security_tree.begin(), container);
70 return true;
71
72
73 }
74
75 int OfxMainContainer::add_container(OfxAccountContainer * container)
76 {
77 message_out(DEBUG, "OfxMainContainer::add_container, adding an account");
78 if ( account_tree.size() == 0)
79 {
80 message_out(DEBUG, "OfxMainContainer::add_container, account is the first account");
81 account_tree.insert(account_tree.begin(), container);
82 }
83 else
84 {
85 message_out(DEBUG, "OfxMainContainer::add_container, account is not the first account");
86 tree<OfxGenericContainer *>::sibling_iterator tmp = account_tree.begin();
87 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
88 account_tree.insert_after(tmp, container);
89 }
90 return true;
91 }
92
93 int OfxMainContainer::add_container(OfxStatementContainer * container)
94 {
95 message_out(DEBUG, "OfxMainContainer::add_container, adding a statement");
96 tree<OfxGenericContainer *>::sibling_iterator tmp = account_tree.begin();
97 //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
98 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
99
100 if (account_tree.is_valid(tmp))
101 {
102 message_out(DEBUG, "1: tmp is valid, Accounts are present");
103 tree<OfxGenericContainer *>::iterator child = account_tree.begin(tmp); // FIXME: Variable "child" is unused. Is there an error below?
104 if (account_tree.number_of_children(tmp) != 0)
105 {
106 message_out(DEBUG, "There are already children for this account");
107 account_tree.insert(tmp.begin(), container);
108
109 }
110 else
111 {
112 message_out(DEBUG, "There are no children for this account");
113 account_tree.append_child(tmp, container);
114 }
115 container->add_account(&( ((OfxAccountContainer *)(*tmp))->data));
116 return true;
117 }
118 else
119 {
120 message_out(ERROR, "OfxMainContainer::add_container, no accounts are present (tmp is invalid)");
121 return false;
122 }
123 }
124
125 int OfxMainContainer::add_container(OfxTransactionContainer * container)
126 {
127 message_out(DEBUG, "OfxMainContainer::add_container, adding a transaction");
128
129 if ( account_tree.size() != 0)
130 {
131 tree<OfxGenericContainer *>::sibling_iterator tmp = account_tree.begin();
132 //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
133 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
134 if (account_tree.is_valid(tmp))
135 {
136 message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
137 account_tree.append_child(tmp, container);
138 container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
139 return true;
140 }
141 else
142 {
143 message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
144 return false;
145 }
146 }
147 else
148 {
149 message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
150 return false;
151 }
152 }
153
154 int OfxMainContainer::add_container(OfxPositionContainer * container)
155 {
156 message_out(DEBUG, "OfxMainContainer::add_container, adding a position");
157
158 if ( account_tree.size() != 0)
159 {
160 tree<OfxGenericContainer *>::sibling_iterator tmp = account_tree.begin();
161 //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
162 tmp += (account_tree.number_of_siblings(tmp)); //Find last account
163 if (account_tree.is_valid(tmp))
164 {
165 message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
166 account_tree.append_child(tmp, container);
167 container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
168 return true;
169 }
170 else
171 {
172 message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
173 return false;
174 }
175 }
176 else
177 {
178 message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
179 return false;
180 }
181 }
182
183 int OfxMainContainer::gen_event()
184 {
185 message_out(DEBUG, "Begin walking the trees of the main container to generate events");
186 tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
187 //cerr<<"security_tree.size(): "<<security_tree.size()<<endl;
188 int i = 0;
189 while (tmp != security_tree.end())
190 {
191 message_out(DEBUG, "Looping...");
192 //cerr <<i<<endl;
193 i++;
194 (*tmp)->gen_event();
195 ++tmp;
196 }
197 tmp = account_tree.begin();
198 //cerr<<account_tree.size()<<endl;
199 i = 0;
200 while (tmp != account_tree.end())
201 {
202 //cerr<< "i="<<i<<"; depth="<<account_tree.depth(tmp)<<endl;
203 i++;
204 (*tmp)->gen_event();
205 ++tmp;
206 }
207 message_out(DEBUG, "End walking the trees of the main container to generate events");
208
209 return true;
210 }
211
212 OfxSecurityData * OfxMainContainer::find_security(string unique_id)
213 {
214 message_out(DEBUG, "OfxMainContainer::find_security() Begin.");
215
216 tree<OfxGenericContainer *>::sibling_iterator tmp = security_tree.begin();
217 OfxSecurityData * retval = NULL;
218 while (tmp != security_tree.end() && retval == NULL)
219 {
220 if (((OfxSecurityContainer*)(*tmp))->data.unique_id == unique_id)
221 {
222 message_out(DEBUG, (string)"Security " + ((OfxSecurityContainer*)(*tmp))->data.unique_id + " found.");
223 retval = &((OfxSecurityContainer*)(*tmp))->data;
224 }
225 ++tmp;
226 }
227 return retval;
228 }
OfxSecurityContainer
Represents a security, such as a stock or bond.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:204
OfxGenericContainer
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:33
tree::begin
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: build/libofx-0.10.0/lib/tree.hh:580
OfxTransactionContainer
Represents a generic transaction.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:245
tree::number_of_siblings
unsigned int number_of_siblings(const iterator_base &) const
Count the number of &#39;next&#39; siblings of node at iterator.
Definition: build/libofx-0.10.0/lib/tree.hh:1457
LibofxContext
Definition: build/libofx-0.10.0/lib/context.hh:25
tree::sibling_iterator
Iterator which traverses only the nodes which are siblings of each other.
Definition: build/libofx-0.10.0/lib/tree.hh:230
tree::insert_after
iter insert_after(iter position, const T &x)
Insert node as next sibling of node pointed to by position.
Definition: build/libofx-0.10.0/lib/tree.hh:889
message_out
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: build/libofx-0.10.0/lib/messages.cpp:61
tree::number_of_children
unsigned int number_of_children(const iterator_base &) const
Count the number of children of node at position.
Definition: build/libofx-0.10.0/lib/tree.hh:1441
tree::end
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: build/libofx-0.10.0/lib/tree.hh:586
ofx_containers.hh
LibOFX internal object code.
OfxGenericContainer::tag_identifier
string tag_identifier
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:37
OfxStatementContainer
Represents a statement for either a bank account or a credit card account.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:157
messages.hh
Message IO functionality.
OfxGenericContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: build/libofx-0.10.0/lib/ofx_container_generic.cpp:68
tree::append_child
iter append_child(iter position)
Insert empty node as last child of node pointed to by position.
Definition: build/libofx-0.10.0/lib/tree.hh:741
tree::insert
iter insert(iter position, const T &x)
Insert node as previous sibling of node pointed to by position.
Definition: build/libofx-0.10.0/lib/tree.hh:828
OfxPositionContainer
Represents an investment position, such as a stock or bond.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:224
OfxSecurityData
An abstraction of a security, such as a stock, mutual fund, etc.
Definition: inc/libofx.h:358
DEBUG
Definition: build/libofx-0.10.0/lib/messages.hh:25
tree::pre_order_iterator
Depth-first iterator, first accessing the node, then its children.
Definition: build/libofx-0.10.0/lib/tree.hh:161
tree::is_valid
bool is_valid(const iterator_base &) const
Determine whether the iterator is an &#39;end&#39; iterator and thus not actually pointing to a node...
Definition: build/libofx-0.10.0/lib/tree.hh:1522
ERROR
Definition: build/libofx-0.10.0/lib/messages.hh:34
tree::size
int size() const
Count the total number of nodes.
Definition: build/libofx-0.10.0/lib/tree.hh:1407
OfxAccountContainer
Represents a bank account or a credit card account.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:180
OfxMainContainer::gen_event
int gen_event()
Generate libofx.h events.
Definition: build/libofx-0.10.0/lib/ofx_container_main.cpp:183