-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathLoadMktSymbols.cpp
119 lines (97 loc) · 4.07 KB
/
LoadMktSymbols.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/************************************************************************
* Copyright(c) 2012, One Unified. All rights reserved. *
* email: [email protected] *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
// Started 2012/10/14
#include <stdexcept>
#include <fstream>
#include "CurlGetMktSymbols.h"
#include "UnzipMktSymbols.h"
#include "ParseMktSymbolDiskFile.h"
#include "ValidateMktSymbolLine.h"
#include "LoadMktSymbols.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
namespace detail {
// shared between debug and release
const std::string sFileNameMarketSymbolsText( "../mktsymbols_v2.txt" );
const std::string sFileNameMarketSymbolsBinary( "../symbols.ser" );
}
typedef MarketSymbol::TableRowDef trd_t;
void LoadMktSymbols( InMemoryMktSymbolList& symbols, MktSymbolLoadType::Enum e, bool bSaveTextToDisk, const std::string& sName ) {
// valid combinations:
// bDownload t t f
// bLoadTextFromDisk f f t
// bSaveTextToDisk f t f
if (
( MktSymbolLoadType::Download == e ) ||
( ( MktSymbolLoadType::LoadTextFromDisk == e ) && !bSaveTextToDisk )
) {
}
else {
throw std::runtime_error( "illegal option combination" );
}
symbols.Clear();
ValidateMktSymbolLine validator;
validator.SetOnProcessLine( MakeDelegate( &symbols, &InMemoryMktSymbolList::InsertParsedStructure ) );
switch ( e ) {
case MktSymbolLoadType::Download:
try {
CurlGetMktSymbols cgms;
UnZipMktSymbolsFile uzmsf;
UnZipMktSymbolsFile::pUnZippedFile_t pUnZippedFile = uzmsf.UnZip( cgms.Buffer(), cgms.Size() );
if ( bSaveTextToDisk ) {
std::ofstream file;
//char* name = "mktsymbols_v2.txt";
std::cout << "Writing Symbol File " << std::endl;
file.open( sName.c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary );
if ( file.bad() ) {
throw std::runtime_error( "can't open output file" );
}
else {
file.write( pUnZippedFile.get(), uzmsf.UnZippedFileSize() );
file.close();
}
}
std::cout << "Processing Contents" << std::endl;
const char* pBegin = pUnZippedFile.get();
const char* pEnd = pBegin + uzmsf.UnZippedFileSize();
validator.ParseHeaderLine( pBegin, pEnd );
while ( pBegin != pEnd ) {
validator.Parse( pBegin, pEnd );
}
}
catch( ... ) {
std::cout << "Some Sort of failure in Download" << std::endl;
}
break;
case MktSymbolLoadType::LoadTextFromDisk:
typedef ParseMktSymbolDiskFile diskfile_t;
try {
diskfile_t diskfile;
diskfile.SetOnProcessLine( MakeDelegate( &validator, &ValidateMktSymbolLine::Parse<diskfile_t::iterator_t> ) );
diskfile.Run( detail::sFileNameMarketSymbolsText );
}
catch (...) {
std::cout << "Some sort of failure on disk read" << std::endl;
}
break;
}
validator.SetOnProcessHasOption( MakeDelegate( &symbols, &InMemoryMktSymbolList::HandleSymbolHasOption ) );
validator.SetOnUpdateOptionUnderlying( MakeDelegate( &symbols, &InMemoryMktSymbolList::HandleUpdateOptionUnderlying ) );
validator.PostProcess();
validator.Summary();
}
} // namespace iqfeed
} // namespace tf
} // namespace ou