LibOFX

lib/ofx_container_security.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 ofx_container_security.cpp
3 -------------------
4 copyright : (C) 2002 by Benoit Gr�goire
5 email : benoitg@coeus.ca
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 * OfxSecurityContainer *
34 ***************************************************************************/
35
36 OfxSecurityContainer::OfxSecurityContainer(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 = "SECURITY";
41
42 if (para_tag_identifier == "STOCKINFO")
43 data.security_type = data.OFX_STOCK_SECURITY;
44 else if (para_tag_identifier == "MFINFO")
45 data.security_type = data.OFX_FUND_SECURITY;
46 else if (para_tag_identifier == "OPTINFO")
47 data.security_type = data.OFX_OPTION_SECURITY;
48 else if (para_tag_identifier == "DEBTINFO")
49 data.security_type = data.OFX_DEBT_SECURITY;
50 else
51 data.security_type = data.OFX_OTHER_SECURITY;
52 data.security_type_valid = true;
53 }
54 OfxSecurityContainer::~OfxSecurityContainer()
55 {
56 }
57 void OfxSecurityContainer::add_attribute(const string identifier, const string value)
58 {
59 if (identifier == "UNIQUEID")
60 {
61 /* With <OPTINFO>, there can be two <UNIQUEID> elements, */
62 /* The one under <SECINFO> is mandatory and is for the option. */
63 /* The one under <OPTINFO> is optional and is for the underlying. */
64 /* Assume the first one in the file is the mandatory one and put it in unique_id */
65 if (data.unique_id_valid == false)
66 {
67 strncpy(data.unique_id, value.c_str(), sizeof(data.unique_id));
68 data.unique_id_valid = true;
69 }
70 else
71 {
72 /* If unique_id is already set, use unique_id2 */
73 strncpy(data.unique_id2, value.c_str(), sizeof(data.unique_id2));
74 data.unique_id2_valid = true;
75 }
76 }
77 else if (identifier == "UNIQUEIDTYPE")
78 {
79 /* With <OPTINFO>, there can be two <UNIQUEIDTYPE> elements, */
80 /* The one under <SECINFO> is mandatory and is for the option. */
81 /* The one under <OPTINFO> is optional and is for the underlying. */
82 /* Assume the first one in the file is the mandatory one and put it in unique_id */
83 if (data.unique_id_type_valid == false)
84 {
85 strncpy(data.unique_id_type, value.c_str(), sizeof(data.unique_id_type));
86 data.unique_id_type_valid = true;
87 }
88 else
89 {
90 /* If unique_id_type is already set, use unique_id2_type */
91 strncpy(data.unique_id2_type, value.c_str(), sizeof(data.unique_id2_type));
92 data.unique_id2_type_valid = true;
93 }
94 }
95 else if (identifier == "SECNAME")
96 {
97 strncpy(data.secname, value.c_str(), sizeof(data.secname));
98 data.secname_valid = true;
99 }
100 else if (identifier == "TICKER")
101 {
102 strncpy(data.ticker, value.c_str(), sizeof(data.ticker));
103 data.ticker_valid = true;
104 }
105 else if (identifier == "RATING")
106 {
107 strncpy(data.rating, value.c_str(), OFX_SECURITY_RATING_LENGTH);
108 data.rating_valid = true;
109 }
110 else if (identifier == "UNITPRICE")
111 {
112 data.unitprice = ofxamount_to_double(value);
113 data.unitprice_valid = true;
114 }
115 else if (identifier == "DTASOF")
116 {
117 data.date_unitprice = ofxdate_to_time_t(value);
118 data.date_unitprice_valid = true;
119 }
120 else if (identifier == "CURRATE")
121 {
122 data.currency_ratio = ofxamount_to_double(value);
123 data.currency_ratio_valid = true;
124 }
125 else if (identifier == "CURSYM")
126 {
127 strncpy(data.currency, value.c_str(), OFX_CURRENCY_LENGTH);
128 data.currency_valid = true;
129 }
130 else if (identifier == "CURRENCY")
131 {
132 data.amounts_are_foreign_currency = false;
133 data.amounts_are_foreign_currency_valid = true;
134 }
135 else if (identifier == "ORIGCURRENCY")
136 {
137 data.amounts_are_foreign_currency = true;
138 data.amounts_are_foreign_currency_valid = true;
139 }
140 else if (identifier == "MEMO" || identifier == "MEMO2")
141 {
142 strncpy(data.memo, value.c_str(), sizeof(data.memo));
143 data.memo_valid = true;
144 }
145 else if (identifier == "FIID")
146 {
147 strncpy(data.fiid, value.c_str(), OFX_FIID_LENGTH);
148 data.fiid_valid = true;
149 }
150 else if (identifier == "ASSETCLASS")
151 {
152 if (value == "DOMESTICBOND")
153 {
154 data.asset_class = data.OFX_ASSET_CLASS_DOMESTICBOND;
155 data.asset_class_valid = true;
156 }
157 else if (value == "INTLBOND")
158 {
159 data.asset_class = data.OFX_ASSET_CLASS_INTLBOND;
160 data.asset_class_valid = true;
161 }
162 else if (value == "LARGESTOCK")
163 {
164 data.asset_class = data.OFX_ASSET_CLASS_LARGESTOCK;
165 data.asset_class_valid = true;
166 }
167 else if (value == "SMALLSTOCK")
168 {
169 data.asset_class = data.OFX_ASSET_CLASS_SMALLSTOCK;
170 data.asset_class_valid = true;
171 }
172 else if (value == "INTLSTOCK")
173 {
174 data.asset_class = data.OFX_ASSET_CLASS_INTLSTOCK;
175 data.asset_class_valid = true;
176 }
177 else if (value == "MONEYMRKT")
178 {
179 data.asset_class = data.OFX_ASSET_CLASS_MONEYMRKT;
180 data.asset_class_valid = true;
181 }
182 else if (value == "OTHER")
183 {
184 data.asset_class = data.OFX_ASSET_CLASS_OTHER;
185 data.asset_class_valid = true;
186 }
187 }
188 else if (identifier == "PARVALUE")
189 {
190 data.par_value = ofxamount_to_double(value);
191 data.par_value_valid = true;
192 }
193 else if (identifier == "DEBTTYPE")
194 {
195 if (value == "COUPON")
196 {
197 data.debt_type = data.OFX_DEBT_TYPE_COUPON;
198 data.debt_type_valid = true;
199 }
200 else if (value == "ZERO")
201 {
202 data.debt_type = data.OFX_DEBT_TYPE_ZERO;
203 data.debt_type_valid = true;
204 }
205 }
206 else if (identifier == "DEBTCLASS")
207 {
208 if (value == "TREASURY")
209 {
210 data.debt_class = data.OFX_DEBTCLASS_TREASURY;
211 data.debt_class_valid = true;
212 }
213 else if (value == "MUNICIPAL")
214 {
215 data.debt_class = data.OFX_DEBTCLASS_MUNICIPAL;
216 data.debt_class_valid = true;
217 }
218 else if (value == "CORPORATE")
219 {
220 data.debt_class = data.OFX_DEBTCLASS_CORPORATE;
221 data.debt_class_valid = true;
222 }
223 else if (value == "OTHER")
224 {
225 data.debt_class = data.OFX_DEBTCLASS_OTHER;
226 data.debt_class_valid = true;
227 }
228 }
229 else if (identifier == "COUPONRT")
230 {
231 data.coupon_rate = ofxamount_to_double(value);
232 data.coupon_rate_valid = true;
233 }
234 else if (identifier == "DTCOUPON")
235 {
236 data.date_coupon = ofxdate_to_time_t(value);
237 data.date_coupon_valid = true;
238 }
239 else if (identifier == "COUPONFREQ")
240 {
241 if (value == "MONTHLY")
242 {
243 data.coupon_freq = data.OFX_COUPON_FREQ_MONTHLY;
244 data.coupon_freq_valid = true;
245 }
246 else if (value == "QUARTERLY")
247 {
248 data.coupon_freq = data.OFX_COUPON_FREQ_QUARTERLY;
249 data.coupon_freq_valid = true;
250 }
251 else if (value == "SEMIANNUAL")
252 {
253 data.coupon_freq = data.OFX_COUPON_FREQ_SEMIANNUAL;
254 data.coupon_freq_valid = true;
255 }
256 else if (value == "ANNUAL")
257 {
258 data.coupon_freq = data.OFX_COUPON_FREQ_ANNUAL;
259 data.coupon_freq_valid = true;
260 }
261 else if (value == "OTHER")
262 {
263 data.coupon_freq = data.OFX_COUPON_FREQ_OTHER;
264 data.coupon_freq_valid = true;
265 }
266 }
267 else if (identifier == "CALLPRICE")
268 {
269 data.call_price = ofxamount_to_double(value);
270 data.call_price_valid = true;
271 }
272 else if (identifier == "YIELDTOCALL")
273 {
274 data.yield_to_call = ofxamount_to_double(value);
275 data.yield_to_call_valid = true;
276 }
277 else if (identifier == "DTCALL")
278 {
279 data.call_date = ofxdate_to_time_t(value);
280 data.call_date_valid = true;
281 }
282 else if (identifier == "CALLTYPE")
283 {
284 if (value == "CALL")
285 {
286 data.call_type = data.OFX_CALL_TYPE_CALL;
287 data.call_type_valid = true;
288 }
289 else if (value == "PUT")
290 {
291 data.call_type = data.OFX_CALL_TYPE_PUT;
292 data.call_type_valid = true;
293 }
294 else if (value == "PREFUND")
295 {
296 data.call_type = data.OFX_CALL_TYPE_PREFUND;
297 data.call_type_valid = true;
298 }
299 else if (value == "MATURITY")
300 {
301 data.call_type = data.OFX_CALL_TYPE_MATURITY;
302 data.call_type_valid = true;
303 }
304 }
305 else if (identifier == "YIELDTOMAT")
306 {
307 data.yield_to_maturity = ofxamount_to_double(value);
308 data.yield_to_maturity_valid = true;
309 }
310 else if (identifier == "DTMAT")
311 {
312 data.maturity_date = ofxdate_to_time_t(value);
313 data.maturity_date_valid = true;
314 }
315 else if (identifier == "MFTYPE")
316 {
317 if (value == "OPENEND")
318 {
319 data.mutual_fund_type = data.OFX_MFTYPE_OPENEND;
320 data.mutual_fund_type_valid = true;
321 }
322 else if (value == "CLOSEEND")
323 {
324 data.mutual_fund_type = data.OFX_MFTYPE_CLOSEEND;
325 data.mutual_fund_type_valid = true;
326 }
327 else if (value == "OTHER")
328 {
329 data.mutual_fund_type = data.OFX_MFTYPE_OTHER;
330 data.mutual_fund_type_valid = true;
331 }
332 }
333 else if (identifier == "STOCKTYPE")
334 {
335 if (value == "COMMON")
336 {
337 data.stock_type = data.OFX_STOCKTYPE_COMMON;
338 data.stock_type_valid = true;
339 }
340 else if (value == "PREFERRED")
341 {
342 data.stock_type = data.OFX_STOCKTYPE_PREFERRED;
343 data.stock_type_valid = true;
344 }
345 else if (value == "CONVERTIBLE")
346 {
347 data.stock_type = data.OFX_STOCKTYPE_CONVERTIBLE;
348 data.stock_type_valid = true;
349 }
350 else if (value == "OTHER")
351 {
352 data.stock_type = data.OFX_STOCKTYPE_OTHER;
353 data.stock_type_valid = true;
354 }
355 }
356 else if (identifier == "YIELD")
357 {
358 data.yield = ofxamount_to_double(value);
359 data.yield_valid = true;
360 }
361 else if (identifier == "DTYIELDASOF")
362 {
363 data.yield_asof_date = ofxdate_to_time_t(value);
364 data.yield_asof_date_valid = true;
365 }
366 else if (identifier == "OPTTYPE")
367 {
368 if (value == "CALL" || value == "Call")
369 {
370 data.option_type = data.OFX_OPTION_TYPE_CALL;
371 data.option_type_valid = true;
372 }
373 else if (value == "PUT" || value == "Put")
374 {
375 data.option_type = data.OFX_OPTION_TYPE_PUT;
376 data.option_type_valid = true;
377 }
378 }
379 else if (identifier == "STRIKEPRICE")
380 {
381 data.strike_price = ofxamount_to_double(value);
382 data.strike_price_valid = true;
383 }
384 else if (identifier == "DTEXPIRE")
385 {
386 data.date_expire = ofxdate_to_time_t(value);
387 data.date_expire_valid = true;
388 }
389 else if (identifier == "SHPERCTRCT")
390 {
391 data.shares_per_cont = ofxamount_to_double(value);
392 data.shares_per_cont_valid = true;
393 }
394 else
395 {
396 /* Redirect unknown identifiers to the base class */
397 OfxGenericContainer::add_attribute(identifier, value);
398 }
399 }
400 int OfxSecurityContainer::gen_event()
401 {
402 libofx_context->securityCallback(data);
403 return true;
404 }
405
406 int OfxSecurityContainer::add_to_main_tree()
407 {
408 if (MainContainer != NULL)
409 {
410 return MainContainer->add_container(this);
411 }
412 else
413 {
414 return false;
415 }
416 }
417
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
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
OfxSecurityData::OFX_OTHER_SECURITY
Definition: inc/libofx.h:368
OfxSecurityData::date_coupon
time_t date_coupon
Definition: inc/libofx.h:521
ofx_utilities.hh
Various simple functions for type conversion & al.
OfxSecurityData::call_date
time_t call_date
Definition: inc/libofx.h:544
OfxSecurityData::unique_id2
char unique_id2[OFX_UNIQUE_ID_LENGTH]
Definition: inc/libofx.h:597
OfxSecurityData::OFX_STOCK_SECURITY
Definition: inc/libofx.h:367
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
OfxSecurityData::call_price
double call_price
Definition: inc/libofx.h:536
OfxSecurityData::OFX_FUND_SECURITY
Definition: inc/libofx.h:365
OfxSecurityContainer::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_security.cpp:406
LibofxContext
Definition: build/libofx-0.10.0/lib/context.hh:25
OfxSecurityContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: build/libofx-0.10.0/lib/ofx_container_security.cpp:400
OfxSecurityData::currency
char currency[OFX_CURRENCY_LENGTH]
Definition: inc/libofx.h:409
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
OfxSecurityData::maturity_date
time_t maturity_date
Definition: inc/libofx.h:562
OfxSecurityData::currency_ratio
double currency_ratio
Definition: inc/libofx.h:413
OfxSecurityData::strike_price
double strike_price
Definition: inc/libofx.h:579
OfxSecurityData::memo
char memo[OFX_MEMO2_LENGTH]
Definition: inc/libofx.h:422
OfxSecurityData::yield
double yield
Definition: inc/libofx.h:478
OfxSecurityData::fiid
char fiid[OFX_FIID_LENGTH]
Definition: inc/libofx.h:427
OfxSecurityData::OFX_OPTION_SECURITY
Definition: inc/libofx.h:366
OfxSecurityContainer::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_security.cpp:57
OfxSecurityData::unique_id
char unique_id[OFX_UNIQUE_ID_LENGTH]
Definition: inc/libofx.h:379
OfxSecurityData::rating
char rating[OFX_SECURITY_RATING_LENGTH]
Definition: inc/libofx.h:401
OfxSecurityData::date_unitprice
time_t date_unitprice
Definition: inc/libofx.h:404
OfxSecurityData::date_expire
time_t date_expire
Definition: inc/libofx.h:583
ofx_containers.hh
LibOFX internal object code.
OfxSecurityData::shares_per_cont
double shares_per_cont
Definition: inc/libofx.h:587
messages.hh
Message IO functionality.
OfxSecurityData::yield_asof_date
time_t yield_asof_date
Definition: inc/libofx.h:481
OfxSecurityData::amounts_are_foreign_currency
int amounts_are_foreign_currency
Definition: inc/libofx.h:419
OfxSecurityData::unitprice
double unitprice
Definition: inc/libofx.h:397
OfxGenericContainer::type
string type
Definition: build/libofx-0.10.0/lib/ofx_containers.hh:36
OfxSecurityData::unique_id_type
char unique_id_type[OFX_UNIQUE_ID_TYPE_LENGTH]
Definition: inc/libofx.h:382
OfxSecurityData::unique_id2_type
char unique_id2_type[OFX_UNIQUE_ID_TYPE_LENGTH]
Definition: inc/libofx.h:602
OfxSecurityData::yield_to_maturity
double yield_to_maturity
Definition: inc/libofx.h:558
OfxSecurityData::ticker
char ticker[OFX_TICKER_LENGTH]
Definition: inc/libofx.h:394
OfxSecurityData::par_value
double par_value
Definition: inc/libofx.h:490
OfxSecurityData::OFX_DEBT_SECURITY
Definition: inc/libofx.h:364
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
OfxSecurityData::yield_to_call
double yield_to_call
Definition: inc/libofx.h:540
OfxSecurityData::coupon_rate
double coupon_rate
Definition: inc/libofx.h:517
OfxSecurityData::secname
char secname[OFX_SECNAME_LENGTH]
Definition: inc/libofx.h:386