-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix2json.cpp
More file actions
158 lines (131 loc) · 4.15 KB
/
fix2json.cpp
File metadata and controls
158 lines (131 loc) · 4.15 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<iomanip>
#include<unistd.h>
#include<quickfix/Application.h>
#include<quickfix/MessageCracker.h>
using namespace std;
void wrap(const string& value, FIX::TYPE::Type& datatype) {
switch (datatype) {
case FIX::TYPE::Price:
case FIX::TYPE::Int:
case FIX::TYPE::Amt:
case FIX::TYPE::Qty:
case FIX::TYPE::SeqNum:
case FIX::TYPE::Length:
case FIX::TYPE::Float:
cout << value;
break;
default:
cout << "\"" << value << "\"";
break;
}
}
void print_section(const FIX::FieldMap& map, FIX::DataDictionary& dictionary) {
FIX::FieldMap::iterator i = map.begin();
int fields = const_cast<FIX::FieldMap&>(map).getFieldCount(); int count = 0;
for (; i != map.end(); ++i) {
string name;
dictionary.getFieldName((*i).first, name);
if (name.empty()) {
stringstream ss;
ss << (*i).first;
name = ss.str();
}
// Get the field type
FIX::TYPE::Type type = FIX::TYPE::Unknown;
dictionary.getFieldType( (*i).first, type);
cout << "\"" << name << "\":";
wrap((*i).second.getString(), type);
cout << "" << (++count < fields ? "," : "");
}
FIX::FieldMap::g_iterator j = map.g_begin();
for(; j != map.g_end(); ++j)
{
cout << ",\"" << j->first << "\":{";
vector<FIX::FieldMap*>::const_iterator k;
for(k = j->second.begin(); k != j->second.end(); ++k) {
print_section( (*(*k)), dictionary);
}
cout << "}";
}
}
void process(string& s, FIX::DataDictionary& dictionary, bool validate, bool use_namespace) {
try {
const FIX::Message m(s, dictionary, validate);
FIX::MsgType msg_type;
const string messageType = m.getHeader().getField( msg_type ).getString() ;
FIX::TargetCompID target_id;
const string target = m.getHeader().getField( target_id ).getString();
FIX::SenderCompID sender_id;
const string sender = m.getHeader().getField( sender_id ).getString();
string n;
dictionary.getValueName( msg_type.getField(), msg_type, n);
if (n.empty()) n = "Unknown (" + messageType + ")";
cout << "{ \"type\":\"" << messageType << "\",\"sender\":\"" << sender << "\",\"target\":\"" << target << "\"";
cout << "," << (use_namespace ? "\"header\":{" : "");
print_section(m.getHeader(), dictionary);
cout << (use_namespace ? "}" : "");
cout << (m.getHeader().totalFields() > 0 ? "," : "") << (use_namespace ? "\"fields\":{" : "");
print_section(m, dictionary);
cout << (use_namespace ? "}" : "");
cout << (static_cast<FIX::FieldMap>(m).getFieldCount() > 0 ? "," : "") << (use_namespace ? "\"trailer\":{" : "");
print_section(m.getTrailer(), dictionary);
cout << (use_namespace ? "}}" : "}") << endl;
} catch (FIX::Exception& e) {
cerr << "Caught exception: " << e.detail << endl;
}
};
void usage() {
cout << "fix2json [options]" << endl;
cout << "where options are: " << endl;
cout << "-d <dictionary path> : use specified FIX dictionary" << endl;
cout << "-v : enable validation" << endl;
cout << "-s : disable stdio syncing (experimental)" << endl;
cout << "-n : dont namespace fix header/fields/trailer" << endl;
cout << "-h : print this message()";
exit(1);
}
int main(int argc, char* argv[]) {
std::vector<string> lines;
const string opts("spd:nh");
bool validate(false);
bool use_namespace(true);
string dictionary_path;
int opt = 0;
opt = getopt(argc, argv, opts.c_str());
while (opt != -1) {
switch (opt) {
case 'd':
dictionary_path = optarg;
break;
case 'v':
validate = true;
break;
case 's':
::std::ios_base::sync_with_stdio(false);
break;
case 'n':
use_namespace = false;
break;
case 'h':
usage();
break;
}
opt = getopt(argc, argv, opts.c_str());
}
if (dictionary_path.empty())
dictionary_path = "./FIX44.xml";
FIX::DataDictionary dictionary(dictionary_path);
string line;
while (getline(cin, line)) {
size_t pos = line.find("8=FIX");
if (pos != string::npos && pos > 0) {
line = line.substr(pos, string::npos);
process(line, dictionary, validate, use_namespace);
}
}
}