-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformatters.hpp
212 lines (170 loc) · 6.3 KB
/
formatters.hpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* \file src/formatters.hpp
*
* \brief Column Value Formatters
*
* \author Olivia Quinet
*/
#ifndef _CPP_SAS7BDAT_SRC_FORMATTERS_HPP_
#define _CPP_SAS7BDAT_SRC_FORMATTERS_HPP_
#include <limits>
#include <string>
namespace cppsas7bdat {
namespace INTERNAL {
namespace FORMATTER {
using Type = Column::Type;
struct IFormatter {
const size_t offset{0};
const size_t length{0};
const Type type{Type::unknown};
IFormatter(const size_t _offset, const size_t _length, const Type _type)
: offset(_offset), length(_length), type(_type) {}
SV get_string([[maybe_unused]] const void *_p) const noexcept { return {}; }
NUMBER get_number([[maybe_unused]] const void *_p) const noexcept {
return std::numeric_limits<NUMBER>::quiet_NaN();
}
INTEGER get_integer([[maybe_unused]] const void *_p) const noexcept {
return {};
}
DATETIME get_datetime([[maybe_unused]] const void *_p) const noexcept {
return {};
}
DATE get_date([[maybe_unused]] const void *_p) const noexcept { return {}; }
TIME get_time([[maybe_unused]] const void *_p) const noexcept {
return TIME(boost::posix_time::not_a_date_time);
}
STRING to_string([[maybe_unused]] const void *_p) const { return {}; }
const uint8_t *data(const void *_p) const noexcept {
const uint8_t *p = reinterpret_cast<const uint8_t *>(_p) + offset;
return p;
}
};
struct NoFormatter : public IFormatter {
NoFormatter(const size_t _offset, const size_t _length)
: IFormatter(_offset, _length, Type::unknown) {
D(spdlog::debug("NoFormatter: {}:{}\n", offset, length));
}
};
struct StringFormatter : public IFormatter {
StringFormatter(const size_t _offset, const size_t _length)
: IFormatter(_offset, _length, Type::string) {
D(spdlog::debug("StringFormatter\n"));
}
SV get_string(const void *_p) const noexcept {
return INTERNAL::get_string_trim_0(data(_p), length);
}
STRING to_string(const void *_p) const { return STRING(get_string(_p)); }
};
struct SmallIntegerFormatter : public IFormatter {
SmallIntegerFormatter(const size_t _offset, const size_t _length)
: IFormatter(_offset, _length, Type::integer) {
D(spdlog::debug("SmallIntegerFormatter\n"));
}
INTEGER get_integer(const void *_p) const noexcept { return *data(_p); }
NUMBER get_number(const void *_p) const noexcept { return get_integer(_p); }
STRING to_string(const void *_p) const {
return std::to_string(get_integer(_p));
}
};
template <Endian _endian, typename _Tp>
struct IntegerFormatter : public IFormatter {
IntegerFormatter(const size_t _offset, const size_t _length)
: IFormatter(_offset, _length, Type::integer) {
D(spdlog::debug("IntegerFormatter\n"));
}
INTEGER get_integer(const void *_p) const noexcept {
return INTERNAL::get_val<_endian, _Tp>(data(_p));
}
NUMBER get_number(const void *_p) const noexcept { return get_integer(_p); }
STRING to_string(const void *_p) const {
return std::to_string(get_integer(_p));
}
};
template <Endian _endian> struct DoubleFormatter : public IFormatter {
DoubleFormatter(const size_t _offset, const size_t _length,
const Type _type = Type::number)
: IFormatter(_offset, _length, _type) {
D(spdlog::debug("DoubleFormatter\n"));
}
NUMBER get_number([[maybe_unused]] const void *_p) const noexcept {
return INTERNAL::get_double<_endian>(data(_p));
}
STRING to_string(const void *_p) const {
return std::to_string(get_number(_p));
}
};
template <Endian _endian, int _nbits>
struct IncompleteDoubleFormatter : public IFormatter {
IncompleteDoubleFormatter(const size_t _offset, const size_t _length)
: IFormatter(_offset, _length, Type::number) {
D(spdlog::debug("IncompleteDoubleFormatter\n"));
}
NUMBER get_number([[maybe_unused]] const void *_p) const noexcept {
return INTERNAL::get_incomplete_double<_endian, _nbits>(data(_p));
}
STRING to_string(const void *_p) const {
return std::to_string(get_number(_p));
}
};
template <Endian _endian>
struct DateTimeFormatter : public DoubleFormatter<_endian> {
DateTimeFormatter(const size_t _offset, const size_t _length)
: DoubleFormatter<_endian>(_offset, _length, Type::datetime) {
D(spdlog::debug("DateTimeFormatter\n"));
}
DATETIME get_datetime([[maybe_unused]] const void *_p) const noexcept {
return INTERNAL::get_datetime_from_epoch(
DoubleFormatter<_endian>::get_number(_p));
}
DATE get_date([[maybe_unused]] const void *_p) const noexcept {
return get_datetime(_p).date();
}
TIME get_time([[maybe_unused]] const void *_p) const noexcept {
return get_datetime(_p).time_of_day();
}
STRING to_string(const void *_p) const {
return cppsas7bdat::to_string(get_datetime(
_p)); // boost::posix_time::to_iso_extended_string(get_datetime(_p));
}
};
template <Endian _endian>
struct DateFormatter : public DoubleFormatter<_endian> {
DateFormatter(const size_t _offset, const size_t _length)
: DoubleFormatter<_endian>(_offset, _length, Type::date) {
D(spdlog::debug("DateTimeFormatter\n"));
}
DATE get_date([[maybe_unused]] const void *_p) const noexcept {
return INTERNAL::get_date_from_epoch(
DoubleFormatter<_endian>::get_number(_p));
}
DATETIME get_datetime([[maybe_unused]] const void *_p) const noexcept {
return DATETIME(get_date(_p), {});
}
TIME get_time([[maybe_unused]] const void *_p) const noexcept { return {}; }
STRING to_string(const void *_p) const {
return cppsas7bdat::to_string(get_date(
_p)); // boost::gregorian::to_iso_extended_string(get_date(_p));
}
};
template <Endian _endian>
struct TimeFormatter : public DoubleFormatter<_endian> {
TimeFormatter(const size_t _offset, const size_t _length)
: DoubleFormatter<_endian>(_offset, _length, Type::time) {
D(spdlog::debug("DateTimeFormatter\n"));
}
TIME get_time([[maybe_unused]] const void *_p) const noexcept {
return INTERNAL::get_time_from_epoch(
DoubleFormatter<_endian>::get_number(_p));
}
DATETIME get_datetime([[maybe_unused]] const void *_p) const noexcept {
return DATETIME({}, get_time(_p));
}
STRING to_string(const void *_p) const {
return cppsas7bdat::to_string(
get_time(_p)); // boost::posix_time::to_simple_string(get_time(_p));
}
};
} // namespace FORMATTER
} // namespace INTERNAL
} // namespace cppsas7bdat
#endif