LibOFX

lib/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
30 extern OfxMainContainer * MainContainer;
31
32 /***************************************************************************
33 * OfxPositionContainer *
34 ***************************************************************************/
35
36 OfxPositionContainer::OfxPositionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, 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 }
42 OfxPositionContainer::~OfxPositionContainer()
43 {
44 }
45 void OfxPositionContainer::add_attribute(const string identifier, const string value)
46 {
47 if (identifier == "UNIQUEID")
48 {
49 strncpy(data.unique_id, value.c_str(), sizeof(data.unique_id));
50 data.unique_id_valid = true;
51 }
52 else if (identifier == "UNIQUEIDTYPE")
53 {
54 strncpy(data.unique_id_type, value.c_str(), sizeof(data.unique_id_type));
55 data.unique_id_type_valid = true;
56 }
57 else if (identifier == "HELDINACCT")
58 {
59 if (value == "CASH")
60 {
61 data.heldinaccount_type = data.OFX_HELDINACCT_CASH;
62 data.heldinaccount_type_valid = true;
63 }
64 else if (value == "MARGIN")
65 {
66 data.heldinaccount_type = data.OFX_HELDINACCT_MARGIN;
67 data.heldinaccount_type_valid = true;
68 }
69 else if (value == "SHORT")
70 {
71 data.heldinaccount_type = data.OFX_HELDINACCT_SHORT;
72 data.heldinaccount_type_valid = true;
73 }
74 else if (value == "OTHER")
75 {
76 data.heldinaccount_type = data.OFX_HELDINACCT_OTHER;
77 data.heldinaccount_type_valid = true;
78 }
79 }
80 else if (identifier == "POSTYPE")
81 {
82 if (value == "SHORT")
83 {
84 data.position_type = data.OFX_POSITION_SHORT;
85 data.position_type_valid = true;
86 }
87 else if (value == "LONG")
88 {
89 data.position_type = data.OFX_POSITION_LONG;
90 data.position_type_valid = true;
91 }
92 }
93 else if (identifier == "UNITS")
94 {
95 data.units = ofxamount_to_double(value);
96 data.units_valid = true;
97 }
98 else if (identifier == "UNITPRICE")
99 {
100 data.unit_price = ofxamount_to_double(value);
101 data.unit_price_valid = true;
102 }
103 else if (identifier == "MKTVAL")
104 {
105 data.market_value = ofxamount_to_double(value);
106 data.market_value_valid = true;
107 }
108 else if (identifier == "DTPRICEASOF")
109 {
110 data.date_unit_price = ofxdate_to_time_t(value);
111 data.date_unit_price_valid = true;
112 }
113 else if (identifier == "CURRATE")
114 {
115 data.currency_ratio = ofxamount_to_double(value);
116 data.currency_ratio_valid = true;
117 }
118 else if (identifier == "CURSYM")
119 {
120 strncpy(data.currency, value.c_str(), sizeof(data.currency));
121 data.currency_valid = true;
122 }
123 else if (identifier == "CURRENCY")
124 {
125 data.amounts_are_foreign_currency = false;
126 data.amounts_are_foreign_currency_valid = true;
127 }
128 else if (identifier == "ORIGCURRENCY")
129 {
130 data.amounts_are_foreign_currency = true;
131 data.amounts_are_foreign_currency_valid = true;
132 }
133 else if (identifier == "MEMO")
134 {
135 strncpy(data.memo, value.c_str(), sizeof(data.memo));
136 data.memo_valid = true;
137 }
138 else if (identifier == "INV401KSOURCE")
139 {
140 if (value == "PRETAX")
141 {
142 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_PRETAX;
143 data.inv_401k_source_valid = true;
144 }
145 else if (value == "AFTERTAX")
146 {
147 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_AFTERTAX;
148 data.inv_401k_source_valid = true;
149 }
150 else if (value == "MATCH")
151 {
152 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_MATCH;
153 data.inv_401k_source_valid = true;
154 }
155 else if (value == "PROFITSHARING")
156 {
157 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_PROFITSHARING;
158 data.inv_401k_source_valid = true;
159 }
160 else if (value == "ROLLOVER")
161 {
162 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_ROLLOVER;
163 data.inv_401k_source_valid = true;
164 }
165 else if (value == "OTHERVEST")
166 {
167 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_OTHERVEST;
168 data.inv_401k_source_valid = true;
169 }
170 else if (value == "OTHERNONVEST")
171 {
172 data.inv_401k_source = data.OFX_401K_POSN_SOURCE_OTHERNONVEST;
173 data.inv_401k_source_valid = true;
174 }
175 }
176 else
177 {
178 /* Redirect unknown identifiers to the base class */
179 OfxGenericContainer::add_attribute(identifier, value);
180 }
181 }
182 int OfxPositionContainer::gen_event()
183 {
184 if (data.unique_id_valid == true && MainContainer != NULL)
185 {
186 data.security_data_ptr = MainContainer->find_security(data.unique_id);
187 if (data.security_data_ptr != NULL)
188 {
189 data.security_data_valid = true;
190 }
191 }
192 libofx_context->positionCallback(data);
193 return true;
194 }
195
196 void OfxPositionContainer::add_account(OfxAccountData * account_data)
197 {
198 if (account_data->account_id_valid == true)
199 {
200 data.account_ptr = account_data;
201 strncpy(data.account_id, account_data->account_id, OFX_ACCOUNT_ID_LENGTH);
202 data.account_id_valid = true;
203 }
204 }
205
206 int OfxPositionContainer::add_to_main_tree()
207 {
208 if (MainContainer != NULL)
209 {
210 return MainContainer->add_container(this);
211 }
212 else
213 {
214 return false;
215 }
216 }
217
OfxPositionContainer::add_attribute
void add_attribute(const string identifier, const string value)
Add data to a container object.
Definition: build/libofx-0.10.0/lib/ofx_container_position.cpp:45
OfxAccountData
An abstraction of an account.
Definition: inc/libofx.h:276
ofxamount_to_double
double ofxamount_to_double(const string ofxamount)
Convert OFX amount of money to double float.
Definition: build/libofx-0.10.0/lib/ofx_utilities.cpp:230
OfxPositionData::currency
char currency[OFX_CURRENCY_LENGTH]
Definition: inc/libofx.h:1244
OfxPositionData::unique_id_type
char unique_id_type[OFX_UNIQUE_ID_TYPE_LENGTH]
Definition: inc/libofx.h:1181
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
OfxPositionData::amounts_are_foreign_currency
int amounts_are_foreign_currency
Definition: inc/libofx.h:1254
OfxAccountData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:290
OfxPositionData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:1164
ofx_utilities.hh
Various simple functions for type conversion & al.
OfxPositionContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: build/libofx-0.10.0/lib/ofx_container_position.cpp:182
OfxPositionData::unit_price
double unit_price
Definition: inc/libofx.h:1209
OfxGenericContainer::add_attribute
virtual void add_attribute(const string identifier, const string value)
Add data to a container object.
Definition: build/libofx-0.10.0/lib/ofx_container_generic.cpp:57
LibofxContext
Definition: build/libofx-0.10.0/lib/context.hh:25
ofxdate_to_time_t
time_t ofxdate_to_time_t(const string ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
Definition: build/libofx-0.10.0/lib/ofx_utilities.cpp:131
OfxPositionData::OFX_HELDINACCT_CASH
Definition: inc/libofx.h:1189
OfxPositionData::date_unit_price
time_t date_unit_price
Definition: inc/libofx.h:1217
OfxPositionData::security_data_ptr
struct OfxSecurityData * security_data_ptr
Definition: inc/libofx.h:1258
OfxPositionData::unique_id
char unique_id[OFX_UNIQUE_ID_LENGTH]
Definition: inc/libofx.h:1179
OfxPositionData::memo
char memo[OFX_MEMO_LENGTH]
Definition: inc/libofx.h:1227
ofx_containers.hh
LibOFX internal object code.
OfxPositionContainer::add_to_main_tree
virtual int add_to_main_tree()
Add this container to the main tree.
Definition: build/libofx-0.10.0/lib/ofx_container_position.cpp:206
OfxPositionData::units
double units
Definition: inc/libofx.h:1205
OfxPositionData::OFX_HELDINACCT_SHORT
Definition: inc/libofx.h:1191
messages.hh
Message IO functionality.
OfxGenericContainer::type
string type
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:36
OfxPositionData::market_value
double market_value
Definition: inc/libofx.h:1213
OfxPositionData::currency_ratio
double currency_ratio
Definition: inc/libofx.h:1248
OfxPositionData::OFX_HELDINACCT_OTHER
Definition: inc/libofx.h:1192
OfxPositionData::OFX_HELDINACCT_MARGIN
Definition: inc/libofx.h:1190
OfxPositionData::account_ptr
struct OfxAccountData * account_ptr
Definition: inc/libofx.h:1167
OfxMainContainer
The root container. Created by the <OFX> OFX element or by the export functions.
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:291