-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_struct_writer.cc
More file actions
123 lines (117 loc) · 3.92 KB
/
safe_struct_writer.cc
File metadata and controls
123 lines (117 loc) · 3.92 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
// x----------------------x
// | safe_struct_writer.cc |
// x----------------------x
#include <fstream>
#include <vector>
#include <iostream>
#include "di/Account.h"
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cerr << "Expected exactly 3 args\n";
std::exit(EXIT_FAILURE);
}
std::ifstream fin(argv[1]);
if (!fin)
{
std::cerr << "Can't open file: " << strerror(errno) << '\n';
std::exit(EXIT_FAILURE);
}
std::ofstream fout(argv[2], std::ios::binary);
if (!fout)
{
std::cerr << "Can't open write file: " << strerror(errno) << '\n';
std::exit(EXIT_FAILURE);
}
constexpr int k_buffer_len{ 100 };
char buffer[k_buffer_len];
for (std::string line, sep; getline(fin, line);)
{
di::Account account{buffer, k_buffer_len};
int i{};
for (std::istringstream in{line}; getline(in, sep, ',');++i)
{
switch (i)
{
case 0:
account.sequencingId(atoll(sep.c_str()));
break;
case 1:
account.accountId(atoll(sep.c_str()));
break;
case 2:
account.participant().participantId(atoll(sep.c_str()));
break;
case 3:
{
di::KycLevelType::Value type{ di::KycLevelType::invalid };
if (sep == "id_verified")
{
type = di::KycLevelType::idVerified;
}
else if (sep == "address_verified")
{
type = di::KycLevelType::addressVerified;
}
account.participant().kycLevel().kycLevelType( type );
}
break;
case 4:
{
di::AccountStatusType::Value type{ di::AccountStatusType::invalid };
if (sep == "active")
{
type = di::AccountStatusType::active;
}
else if (sep == "inactive")
{
type = di::AccountStatusType::inactive;
}
account.accountStatus().accountStatusType( type );
}
break;
case 5:
{
std::string sep_tag;
std::vector<std::string> vals;
for (std::istringstream in(sep); std::getline(in, sep_tag, ';');)
{
vals.emplace_back(sep_tag);
}
di::Account::Tags& tags = account.tagsCount(static_cast<uint16_t>(vals.size()));
for (auto val: vals)
{
tags.next().putTag(std::move(val));
}
}
break;
case 6:
{
di::SelfTradingRuleType::Value type{ di::SelfTradingRuleType::invalid };
if (sep == "valid")
{
type = di::SelfTradingRuleType::valid;
}
account.selfTradingRule().selfTradingRuleType( type );
}
break;
case 7:
{
di::CancellationPolicyType::Value type{ di::CancellationPolicyType::invalid };
if (sep == "valid")
{
type = di::CancellationPolicyType::valid;
}
account.cancellationPolicy().cancellationPolicyType( type );
}
break;
default:
std::cerr << "Incorrect record: " << line << '\n';
std::exit(EXIT_FAILURE);
}
}
fout.write(buffer, k_buffer_len);
}
return EXIT_SUCCESS;
}