LibOFX: ofx_container_main.cpp Source File

LibOFX

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

fx-0.9.10/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);
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::gen_event()
155 {
156 message_out(DEBUG, "Begin walking the trees of the main container to generate events");
157 tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
158 //cerr<<"security_tree.size(): "<<security_tree.size()<<endl;
159 int i = 0;
160 while (tmp != security_tree.end())
161 {
162 message_out(DEBUG, "Looping...");
163 //cerr <<i<<endl;
164 i++;
165 (*tmp)->gen_event();
166 ++tmp;
167 }
168 tmp = account_tree.begin();
169 //cerr<<account_tree.size()<<endl;
170 i = 0;
171 while (tmp != account_tree.end())
172 {
173 //cerr<< "i="<<i<<"; depth="<<account_tree.depth(tmp)<<endl;
174 i++;
175 (*tmp)->gen_event();
176 ++tmp;
177 }
178 message_out(DEBUG, "End walking the trees of the main container to generate events");
179
180 return true;
181 }
182
183 OfxSecurityData * OfxMainContainer::find_security(string unique_id)
184 {
185 message_out(DEBUG, "OfxMainContainer::find_security() Begin.");
186
187 tree<OfxGenericContainer *>::sibling_iterator tmp = security_tree.begin();
188 OfxSecurityData * retval = NULL;
189 while (tmp != security_tree.end() && retval == NULL)
190 {
191 if (((OfxSecurityContainer*)(*tmp))->data.unique_id == unique_id)
192 {
193 message_out(DEBUG, (string)"Security " + ((OfxSecurityContainer*)(*tmp))->data.unique_id + " found.");
194 retval = &((OfxSecurityContainer*)(*tmp))->data;
195 }
196 ++tmp;
197 }
198 return retval;
199 }
tree::number_of_children
unsigned int number_of_children(const iterator_base &) const
Count the number of children of node at position.
Definition: tree.hh:1441
OfxSecurityContainer
Represents a security, such as a stock or bond.
Definition: ofx_containers.hh:181
OfxGenericContainer
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
Definition: ofx_containers.hh:33
OfxTransactionContainer
Represents a generic transaction.
Definition: ofx_containers.hh:201
LibofxContext
Definition: context.hh:25
messages.hh
Message IO functionality.
tree::end
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: tree.hh:586
tree::size
int size() const
Count the total number of nodes.
Definition: tree.hh:1407
tree::insert_after
iter insert_after(iter position, const T &x)
Insert node as next sibling of node pointed to by position.
Definition: tree.hh:889
message_out
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: messages.cpp:60
tree::begin
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: tree.hh:580
DEBUG
Definition: messages.hh:25
tree::is_valid
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:1522
ERROR
Definition: messages.hh:34
OfxGenericContainer::tag_identifier
string tag_identifier
Definition: ofx_containers.hh:37
OfxStatementContainer
Represents a statement for either a bank account or a credit card account.
Definition: ofx_containers.hh:134
tree::number_of_siblings
unsigned int number_of_siblings(const iterator_base &) const
Count the number of 'next' siblings of node at iterator.
Definition: tree.hh:1457
OfxGenericContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: 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: tree.hh:741
tree::insert
iter insert(iter position, const T &x)
Insert node as previous sibling of node pointed to by position.
Definition: tree.hh:828
tree
Definition: tree.hh:105
OfxSecurityData
An abstraction of a security, such as a stock, mutual fund, etc.
Definition: inc/libofx.h:351
OfxAccountContainer
Represents a bank account or a credit card account.
Definition: ofx_containers.hh:157
OfxMainContainer::gen_event
int gen_event()
Generate libofx.h events.
Definition: ofx_container_main.cpp:154
ofx_containers.hh
LibOFX internal object code.