ofx_container_position.cpp
Go to the documentation of this file.
/***************************************************************************
ofx_container_position.cpp
-------------------
copyright : (C) 2016 by Jeff Lundblad
email : jeffjl.kde@outlook.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include "messages.hh"
#include "libofx.h"
#include "ofx_containers.hh"
#include "ofx_utilities.hh"
extern OfxMainContainer * MainContainer;
/***************************************************************************
* OfxPositionContainer *
***************************************************************************/
OfxPositionContainer::OfxPositionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
{
memset(&data, 0, sizeof(data));
type = "POSITION";
}
OfxPositionContainer::~OfxPositionContainer()
{
}
void OfxPositionContainer::add_attribute(const std::string identifier, const std::string value)
{
if (identifier == "UNIQUEID")
{
ASSIGN_STRNCPY(data.unique_id, value);
}
else if (identifier == "UNIQUEIDTYPE")
{
ASSIGN_STRNCPY(data.unique_id_type, value);
}
else if (identifier == "HELDINACCT")
{
if (value == "CASH")
{
ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_CASH);
}
else if (value == "MARGIN")
{
ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_MARGIN);
}
else if (value == "SHORT")
{
ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_SHORT);
}
else if (value == "OTHER")
{
ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_OTHER);
}
}
else if (identifier == "POSTYPE")
{
if (value == "SHORT")
{
ASSIGN(data.position_type, data.OFX_POSITION_SHORT);
}
else if (value == "LONG")
{
ASSIGN(data.position_type, data.OFX_POSITION_LONG);
}
}
else if (identifier == "UNITS")
{
ASSIGN(data.units, ofxamount_to_double(value));
}
else if (identifier == "UNITPRICE")
{
ASSIGN(data.unit_price, ofxamount_to_double(value));
}
else if (identifier == "MKTVAL")
{
ASSIGN(data.market_value, ofxamount_to_double(value));
}
else if (identifier == "DTPRICEASOF")
{
ASSIGN(data.date_unit_price, ofxdate_to_time_t(value));
}
else if (identifier == "CURRATE")
{
ASSIGN(data.currency_ratio, ofxamount_to_double(value));
}
else if (identifier == "CURSYM")
{
ASSIGN_STRNCPY(data.currency, value);
}
else if (identifier == "CURRENCY")
{
ASSIGN(data.amounts_are_foreign_currency, false);
}
else if (identifier == "ORIGCURRENCY")
{
ASSIGN(data.amounts_are_foreign_currency, true);
}
else if (identifier == "MEMO")
{
ASSIGN_STRNCPY(data.memo, value);
}
else if (identifier == "INV401KSOURCE")
{
if (value == "PRETAX")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_PRETAX);
}
else if (value == "AFTERTAX")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_AFTERTAX);
}
else if (value == "MATCH")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_MATCH);
}
else if (value == "PROFITSHARING")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_PROFITSHARING);
}
else if (value == "ROLLOVER")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_ROLLOVER);
}
else if (value == "OTHERVEST")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_OTHERVEST);
}
else if (value == "OTHERNONVEST")
{
ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_OTHERNONVEST);
}
}
else
{
/* Redirect unknown identifiers to the base class */
OfxGenericContainer::add_attribute(identifier, value);
}
}
int OfxPositionContainer::gen_event()
{
if (data.unique_id_valid == true && MainContainer != NULL)
{
data.security_data_ptr = MainContainer->find_security(data.unique_id);
if (data.security_data_ptr != NULL)
{
data.security_data_valid = true;
}
}
libofx_context->positionCallback(data);
return true;
}
void OfxPositionContainer::add_account(OfxAccountData * account_data)
{
if (account_data->account_id_valid == true)
{
data.account_ptr = account_data;
ASSIGN_STRNCPY(data.account_id, std::string(account_data->account_id));
}
}
int OfxPositionContainer::add_to_main_tree()
{
if (MainContainer != NULL)
{
return MainContainer->add_container(this);
}
else
{
return false;
}
}
OfxGenericContainer::type
std::string type
Definition: ofx_containers.hh:41
OfxPositionData::OFX_HELDINACCT_OTHER
@ OFX_HELDINACCT_OTHER
Definition: inc/libofx.h:1203
ASSIGN
#define ASSIGN(DEST, VALUE)
Definition: ofx_utilities.hh:45
OfxPositionContainer::add_to_main_tree
virtual int add_to_main_tree()
Add this container to the main tree.
Definition: ofx_container_position.cpp:181
OfxPositionData::units
double units
Definition: inc/libofx.h:1216
OfxPositionData::OFX_HELDINACCT_MARGIN
@ OFX_HELDINACCT_MARGIN
Definition: inc/libofx.h:1201
OfxPositionContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: ofx_container_position.cpp:158
ASSIGN_STRNCPY
#define ASSIGN_STRNCPY(DEST, VALUE)
Definition: ofx_utilities.hh:52
OfxAccountData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:301
OfxAccountData
An abstraction of an account.
Definition: inc/libofx.h:287
OfxMainContainer
The root container. Created by the <OFX> OFX element or by the export functions.
Definition: ofx_containers.hh:289
OfxPositionData::market_value
double market_value
Definition: inc/libofx.h:1224
OfxPositionData::currency_ratio
double currency_ratio
Definition: inc/libofx.h:1259
OfxPositionData::account_ptr
struct OfxAccountData * account_ptr
Definition: inc/libofx.h:1178
ofx_utilities.hh
Various simple functions for type conversion & al.
OfxPositionData::OFX_HELDINACCT_SHORT
@ OFX_HELDINACCT_SHORT
Definition: inc/libofx.h:1202
OfxGenericContainer
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
Definition: ofx_containers.hh:31
OfxGenericContainer::add_attribute
virtual void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_container_generic.cpp:57
ofxamount_to_double
double ofxamount_to_double(const std::string ofxamount)
Convert OFX amount of money to double float.
Definition: ofx_utilities.cpp:130
LibofxContext
Definition: context.hh:23
ofx_containers.hh
LibOFX internal object code.
OfxPositionData::OFX_HELDINACCT_CASH
@ OFX_HELDINACCT_CASH
Definition: inc/libofx.h:1200
OfxPositionData::currency
char currency[OFX_CURRENCY_LENGTH]
Definition: inc/libofx.h:1255
OfxPositionData::unique_id_type
char unique_id_type[OFX_UNIQUE_ID_TYPE_LENGTH]
Definition: inc/libofx.h:1192
OfxPositionContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_container_position.cpp:45
ofxdate_to_time_t
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.
Definition: ofx_utilities.cpp:68
OfxPositionData::amounts_are_foreign_currency
int amounts_are_foreign_currency
Definition: inc/libofx.h:1265
OfxPositionData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:1175
OfxPositionData::unit_price
double unit_price
Definition: inc/libofx.h:1220
OfxPositionData::security_data_ptr
struct OfxSecurityData * security_data_ptr
Definition: inc/libofx.h:1269
OfxPositionData::unique_id
char unique_id[OFX_UNIQUE_ID_LENGTH]
Definition: inc/libofx.h:1190
OfxPositionData::date_unit_price
time_t date_unit_price
Definition: inc/libofx.h:1228
messages.hh
Message IO functionality.
OfxPositionData::memo
char memo[OFX_MEMO_LENGTH]
Definition: inc/libofx.h:1238