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");
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");
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  {
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  {
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 
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 
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 }
Represents a security, such as a stock or bond.
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Represents a generic transaction.
unsigned int number_of_siblings(const iterator_base &) const
Count the number of &#39;next&#39; siblings of node at iterator.
Iterator which traverses only the nodes which are siblings of each other.
iter insert_after(iter position, const T &x)
Insert node as next sibling of node pointed to by position.
int message_out(OfxMsgType error_type, const string message)
Message output function.
unsigned int number_of_children(const iterator_base &) const
Count the number of children of node at position.
pre_order_iterator end() const
Return iterator to the end of the tree.
LibOFX internal object code.
Represents a statement for either a bank account or a credit card account.
Message IO functionality.
virtual int gen_event()
Generate libofx.h events.
iter append_child(iter position)
Insert empty node as last child of node pointed to by position.
iter insert(iter position, const T &x)
Insert node as previous sibling of node pointed to by position.
Represents an investment position, such as a stock or bond.
An abstraction of a security, such as a stock, mutual fund, etc.
Definition: inc/libofx.h:358
Depth-first iterator, first accessing the node, then its children.
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...
int size() const
Count the total number of nodes.
Represents a bank account or a credit card account.
int gen_event()
Generate libofx.h events.