GnuCashew ~ Web Application compatible with GnuCash sql data files.
GCW
Loading...
Searching...
No Matches
Commodities.cpp
Go to the documentation of this file.
1#line 2 "src/Dbo/Commodities/Commodities.cpp"
2
3#include <fstream>
4
5#include "../Glb/Core.h"
6#include "../App.h"
7#include "../../3rd/RapidXML/rapidxml.hpp"
8#include "Commodities.h"
9
10namespace {
11
12/*
13** This is a simple heap dynamic buffer, that deletes when it goes out of scope.
14*/
15class DynBuf
16{
17 public:
18
19 char * buffer;
20
21 DynBuf( int size )
22 {
23 buffer = new char[size];
24 }
25
26 ~DynBuf()
27 {
28 delete buffer;
29 }
30};
31
32}
33
34const char * GCW::Dbo::Commodities::s_tableName = "commodities";
35
36const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::guid = "guid" ; // text(32) PRIMARY KEY NOT NULL
37const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::nameSpace = "namespace" ; // text(2048) NOT NULL
38const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::mnemonic = "mnemonic" ; // text(2048) NOT NULL
41const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::fraction = "fraction" ; // integer NOT NULL
42const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::quote_flag = "quote_flag" ; // integer NOT NULL
45
46auto
48load( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
49{
51
52 if( _guid != "" )
53 {
54
55 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
56
57 try
58 {
59 retVal =
60 GCW::app()-> gnucashew_session().load< GCW::Dbo::Commodities::Item >( _guid )
61 ;
62 }
63 catch( std::exception & e )
64 {
65 std::cout << __FILE__ << ":" << __LINE__ << " " << e.what() << std::endl;
66 }
67 }
68
69 return retVal;
70
71} // endload( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
72
73auto
75byGuid( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
76{
77 return GCW::Dbo::Commodities::load( _guid );
78
79} // endbyGuid( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
80
81
82
83auto
85getIso4217Commodities()-> std::vector< Commodity_t >
86{
87 std::vector< Commodity_t > retVal;
88
89 /*ยท
90 ** Open the file and read it all in to memory
91 **
92 */
93 std::ifstream inFile;
94 inFile.open( wApp-> docRoot() + "/iso-4217-currencies.xml" );
95
96 if( inFile.fail() )
97 return retVal;
98
99 inFile.seekg( 0, std::ios::end );
100 std::streampos length = inFile.tellg();
101 inFile.seekg( 0, std::ios::beg );
102
103 /*
104 ** make a little empty space in the buffer for adding
105 ** zero terminated strings, or whatever it is that the
106 ** rapid xml does to the content to manipulate it. Note
107 ** that it's possible to cause rapidxml to not modify
108 ** the buffer at all, but then every string that gets pulled
109 ** out of the buffer has to be tested for actual length
110 ** and any strings derived from that need to include that
111 ** length parameter.
112 **
113 */
114 DynBuf db(((int)length)+10000);
115 inFile.read( db.buffer, length );
116 inFile.close();
117
118 db.buffer[length] = 0;
119
120 /*
121 ** Parse-in the XML file
122 **
123 */
124 rapidxml::xml_document<> doc;
125 try
126 {
127 doc.parse<0>( db.buffer );
128 }
129 catch( std::exception & err )
130 {
131 std::cout << __FILE__ << ":" << __LINE__ << " " << err.what() << std::endl;
132 return retVal;
133 }
134
135 auto _processCurrency = [&]( rapidxml::xml_node<> * topNode )
136 {
137 for( auto node = topNode-> first_node(); node; node = node-> next_sibling() )
138 {
139 Commodity_t commodity =
140 {
141 node-> first_attribute( "isocode" )-> value(),
142 node-> first_attribute( "fullname" )-> value(),
143 node-> first_attribute( "unitname" )-> value(),
144 node-> first_attribute( "partname" )-> value(),
145 node-> first_attribute( "namespace" )-> value(),
146 node-> first_attribute( "exchange-code" )-> value(),
147 node-> first_attribute( "parts-per-unit" )-> value(),
148 node-> first_attribute( "smallest-fraction" )-> value(),
149 node-> first_attribute( "local-symbol" )-> value()
150 };
151
152 retVal.push_back( commodity );
153
154 } // endfor( auto node = topNode-> first_node(); node; node = node-> next_sibling() )
155
156 }; // endauto _processObservation = [=]( rapidxml::xml_node<> * topNode )
157
158 for( auto node = doc.first_node(); node; node = node-> next_sibling() )
159 {
160 if( std::string(node-> name()) == "currencylist" )
161 _processCurrency( node );
162 }
163
164 return retVal;
165
166} // endgetIso4217Commodities()-> std::vector< Commodity_t >
167
168
169
170
const char * Field
const Wt::WFormModel::Field cusip
const Wt::WFormModel::Field fullname
const Wt::WFormModel::Field nameSpace
const Wt::WFormModel::Field guid
const Wt::WFormModel::Field fraction
const Wt::WFormModel::Field mnemonic
const Wt::WFormModel::Field quote_source
const Wt::WFormModel::Field quote_tz
const Wt::WFormModel::Field quote_flag
const char * s_tableName
auto byGuid(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
auto getIso4217Commodities() -> std::vector< Commodity_t >
Get Commodities.
auto load(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
App * app()
Definition App.cpp:75