mt4hstcvt
/
hstfmt.cpp
97 строк · 2.4 Кб
1#include <hstfmt.h>
2
3namespace hst {
4
5dataset::dataset(const std::string& path)
6{
7m.f.open(path, std::ios_base::binary);
8if (!m.f) throw dataset_error("file open error");
9m.f.read(reinterpret_cast<char*>(&m.hdr), sizeof(header));
10if (!m.f) throw dataset_error("file read error");
11
12if (version() != MT4_509 && version() != MT4_574)
13throw dataset_error("file version error");
14
15m.f.seekg(0, std::ios_base::end);
16size_t flen = m.f.tellg();
17flen -= sizeof(header);
18m.count = flen / elem_size();
19}
20
21void dataset::update()
22{
23m.f.seekg(0, std::ios_base::beg);
24if (!m.f) throw dataset_error("file position error");
25m.f.read(reinterpret_cast<char*>(&m.hdr), sizeof(header));
26if (!m.f) throw dataset_error("file read error");
27
28if (version() != MT4_509 && version() != MT4_574)
29throw dataset_error("file version error");
30
31m.f.seekg(0, std::ios_base::end);
32size_t flen = m.f.tellg();
33flen -= sizeof(header);
34m.count = flen / elem_size();
35}
36
37bar dataset::at(size_t index)
38{
39if (index >= m.count)
40throw dataset_error("file indexing error");
41
42return this->operator[](index);
43}
44
45bar dataset::operator[](size_t index)
46{
47
48std::streamoff off = elem_size() * index + sizeof(header);
49m.f.seekg(off, std::ios_base::beg);
50if (!m.f) throw dataset_error("file position error");
51
52bar result{};
53
54if (version() == MT4_574)
55{
56m.f.read(reinterpret_cast<char*>(&result), sizeof(bar));
57}
58else
59{
60bar_old tmp{};
61m.f.read(reinterpret_cast<char*>(&tmp), sizeof(bar_old));
62result.ctm = static_cast<datetime64>(tmp.ctm);
63result.volume = static_cast<decltype(result.volume)>(tmp.volume);
64}
65
66if (!m.f) throw dataset_error("file read error");
67
68return result;
69}
70
71void dataset::each(std::function<void(const bar&)> f)
72{
73m.f.seekg(sizeof(header), std::ios_base::beg);
74if (!m.f) throw dataset_error("file position error");
75
76bar result{};
77while (!m.f.eof())
78{
79if (version() == MT4_574)
80{
81m.f.read(reinterpret_cast<char*>(&result), sizeof(bar));
82}
83else
84{
85bar_old tmp{};
86m.f.read(reinterpret_cast<char*>(&tmp), sizeof(bar_old));
87result.ctm = static_cast<datetime64>(tmp.ctm);
88result.volume = static_cast<decltype(result.volume)>(tmp.volume);
89}
90
91if (m.f.eof()) break;
92
93f(result);
94}
95}
96
97} // namespace
98