LibOFX
ofx_container_position.cpp
Go to the documentation of this file.
1/***************************************************************************
2 ofx_container_position.cpp
3 -------------------
4 copyright : (C) 2016 by Jeff Lundblad
5 email : jeffjl.kde@outlook.com
6***************************************************************************/
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <string>
25#include "messages.hh"
26#include "libofx.h"
27#include "ofx_containers.hh"
28#include "ofx_utilities.hh"
29
30extern OfxMainContainer * MainContainer;
31
32/***************************************************************************
33 * OfxPositionContainer *
34 ***************************************************************************/
35
36OfxPositionContainer::OfxPositionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
37 OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
38{
39 memset(&data, 0, sizeof(data));
40 type = "POSITION";
41}
42OfxPositionContainer::~OfxPositionContainer()
43{
44}
45void OfxPositionContainer::add_attribute(const std::string identifier, const std::string value)
46{
47 if (identifier == "UNIQUEID")
48 {
49 ASSIGN_STRNCPY(data.unique_id, value);
50 }
51 else if (identifier == "UNIQUEIDTYPE")
52 {
53 ASSIGN_STRNCPY(data.unique_id_type, value);
54 }
55 else if (identifier == "HELDINACCT")
56 {
57 if (value == "CASH")
58 {
59 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_CASH);
60 }
61 else if (value == "MARGIN")
62 {
63 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_MARGIN);
64 }
65 else if (value == "SHORT")
66 {
67 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_SHORT);
68 }
69 else if (value == "OTHER")
70 {
71 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_OTHER);
72 }
73 }
74 else if (identifier == "POSTYPE")
75 {
76 if (value == "SHORT")
77 {
78 ASSIGN(data.position_type, data.OFX_POSITION_SHORT);
79 }
80 else if (value == "LONG")
81 {
82 ASSIGN(data.position_type, data.OFX_POSITION_LONG);
83 }
84 }
85 else if (identifier == "UNITS")
86 {
87 ASSIGN(data.units, ofxamount_to_double(value));
88 }
89 else if (identifier == "UNITPRICE")
90 {
91 ASSIGN(data.unit_price, ofxamount_to_double(value));
92 }
93 else if (identifier == "MKTVAL")
94 {
95 ASSIGN(data.market_value, ofxamount_to_double(value));
96 }
97 else if (identifier == "DTPRICEASOF")
98 {
99 ASSIGN(data.date_unit_price, ofxdate_to_time_t(value));
100 }
101 else if (identifier == "CURRATE")
102 {
103 ASSIGN(data.currency_ratio, ofxamount_to_double(value));
104 }
105 else if (identifier == "CURSYM")
106 {
107 ASSIGN_STRNCPY(data.currency, value);
108 }
109 else if (identifier == "CURRENCY")
110 {
111 ASSIGN(data.amounts_are_foreign_currency, false);
112 }
113 else if (identifier == "ORIGCURRENCY")
114 {
115 ASSIGN(data.amounts_are_foreign_currency, true);
116 }
117 else if (identifier == "MEMO")
118 {
119 ASSIGN_STRNCPY(data.memo, value);
120 }
121 else if (identifier == "INV401KSOURCE")
122 {
123 if (value == "PRETAX")
124 {
125 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_PRETAX);
126 }
127 else if (value == "AFTERTAX")
128 {
129 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_AFTERTAX);
130 }
131 else if (value == "MATCH")
132 {
133 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_MATCH);
134 }
135 else if (value == "PROFITSHARING")
136 {
137 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_PROFITSHARING);
138 }
139 else if (value == "ROLLOVER")
140 {
141 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_ROLLOVER);
142 }
143 else if (value == "OTHERVEST")
144 {
145 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_OTHERVEST);
146 }
147 else if (value == "OTHERNONVEST")
148 {
149 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_OTHERNONVEST);
150 }
151 }
152 else
153 {
154 /* Redirect unknown identifiers to the base class */
155 OfxGenericContainer::add_attribute(identifier, value);
156 }
157}
159{
160 if (data.unique_id_valid == true && MainContainer != NULL)
161 {
162 data.security_data_ptr = MainContainer->find_security(data.unique_id);
163 if (data.security_data_ptr != NULL)
164 {
165 data.security_data_valid = true;
166 }
167 }
168 libofx_context->positionCallback(data);
169 return true;
170}
171
172void OfxPositionContainer::add_account(OfxAccountData * account_data)
173{
174 if (account_data->account_id_valid == true)
175 {
176 data.account_ptr = account_data;
177 ASSIGN_STRNCPY(data.account_id, std::string(account_data->account_id));
178 }
179}
180
182{
183 if (MainContainer != NULL)
184 {
185 return MainContainer->add_container(this);
186 }
187 else
188 {
189 return false;
190 }
191}
192
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
virtual void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
The root container. Created by the <OFX> OFX element or by the export functions.
virtual int add_to_main_tree()
Add this container to the main tree.
virtual int gen_event()
Generate libofx.h events.
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Message IO functionality.
LibOFX internal object code.
double ofxamount_to_double(const std::string ofxamount)
Convert OFX amount of money to double float.
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.
Various simple functions for type conversion & al.
#define ASSIGN_STRNCPY(DEST, VALUE)
#define ASSIGN(DEST, VALUE)