-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_writer.h
163 lines (156 loc) · 5.45 KB
/
csv_writer.h
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
#ifndef CSV_WRITER_H
#define CSV_WRITER_H
#include <map>
#include <string>
#include <sstream>
#include <functional>
#include <Arduino.h>
enum class Type {
INT,
UINT,
LONG,
ULONG,
LLONG,
ULLONG,
FLOAT,
DOUBLE,
UINT8,
UINT16,
UINT32,
UINT64,
INT16,
INT32
};
class smartCsv {
private:
std::map<std::string, void*> values;
std::map<std::string, Type> types;
// std::map<std::string, float> scales;
public:
smartCsv(const std::initializer_list<std::tuple<std::string, void*, Type>>& entries) {
for (const auto& entry : entries) {
values[std::get<0>(entry)] = std::get<1>(entry);
types[std::get<0>(entry)] = std::get<2>(entry);
// scales[std::get<0>(entry)] = std::get<3>(entry);
}
}
String getKeysAsCSVRow() {
String result;
for (const auto& pair : values) {
result += pair.first.c_str();
result += ",";
}
result.remove(result.length() - 1); // Remove last comma
result += "\n";
return result;
}
String getValuesAsCSVRow() {
String result;
for (const auto& pair : values) {
switch (types[pair.first]) {
case Type::INT:
result += *reinterpret_cast<int*>(pair.second);
break;
case Type::UINT:
result += *reinterpret_cast<unsigned int*>(pair.second);
break;
case Type::LONG:
result += *reinterpret_cast<long*>(pair.second);
break;
case Type::ULONG:
result += *reinterpret_cast<unsigned long*>(pair.second);
break;
case Type::LLONG:
result += *reinterpret_cast<long long*>(pair.second);
break;
case Type::ULLONG:
result += *reinterpret_cast<unsigned long long*>(pair.second);
break;
case Type::FLOAT:
result += *reinterpret_cast<float*>(pair.second);
break;
case Type::DOUBLE:
result += *reinterpret_cast<double*>(pair.second);
break;
case Type::UINT64:
result += *reinterpret_cast<uint64_t*>(pair.second);
break;
case Type::INT16:
result += *reinterpret_cast<int16_t*>(pair.second);
break;
case Type::UINT8:
result += *reinterpret_cast<uint8_t*>(pair.second);
break;
case Type::UINT16:
result += *reinterpret_cast<uint16_t*>(pair.second);
break;
case Type::UINT32:
result += *reinterpret_cast<uint32_t*>(pair.second);
break;
case Type::INT32:
result += *reinterpret_cast<int32_t*>(pair.second);
break;
}
result += ",";
}
result.remove(result.length() - 1); // Remove last comma
result += "\n";
return result;
}
String getValuesAndKeysAsCSVRow() {
String result;
for (const auto& pair : values) {
result += pair.first.c_str();
result += ": ";
switch (types[pair.first]) {
case Type::INT:
result += *reinterpret_cast<int*>(pair.second);
break;
case Type::UINT:
result += *reinterpret_cast<unsigned int*>(pair.second);
break;
case Type::LONG:
result += *reinterpret_cast<long*>(pair.second);
break;
case Type::ULONG:
result += *reinterpret_cast<unsigned long*>(pair.second);
break;
case Type::LLONG:
result += *reinterpret_cast<long long*>(pair.second);
break;
case Type::ULLONG:
result += *reinterpret_cast<unsigned long long*>(pair.second);
break;
case Type::FLOAT:
result += *reinterpret_cast<float*>(pair.second);
break;
case Type::DOUBLE:
result += *reinterpret_cast<double*>(pair.second);
break;
case Type::UINT64:
result += *reinterpret_cast<uint64_t*>(pair.second);
break;
case Type::INT16:
result += *reinterpret_cast<int16_t*>(pair.second);
break;
case Type::UINT8:
result += *reinterpret_cast<uint8_t*>(pair.second);
break;
case Type::UINT16:
result += *reinterpret_cast<uint16_t*>(pair.second);
break;
case Type::UINT32:
result += *reinterpret_cast<uint32_t*>(pair.second);
break;
case Type::INT32:
result += *reinterpret_cast<int32_t*>(pair.second);
break;
}
result += "\n";
}
result.remove(result.length() - 1); // Remove last comma
result += "\n";
return result;
}
};
#endif