-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataserieswritersucc.h
189 lines (161 loc) · 5.17 KB
/
dataserieswritersucc.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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* dataserieswritersucc.h
*
* Created on: Sep 29, 2011
* Author: gerlach
*/
#ifndef DATASERIESWRITERSUCC_H_
#define DATASERIESWRITERSUCC_H_
/*
* write a container (providing an iterator) containing data to an ascii file
* supports a header of the form:
*
* ## blah blah
* ## blah
* # foo1 = bar
* # foo2 = 124
* # foo4 = 28.983
* ## palaver palaver
* value0
* value1
* ...
* etc.
*
* This version allows to successively add data to the file.
*
* Slight modification, less pointer handling [2012.12.14]
*
* add writing of single data entries [2014.11.18]
*
*/
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
#include "metadata.h"
template <class Container>
class DataSeriesWriterSuccessive {
public:
//prepare the file, the following operations will append to it
DataSeriesWriterSuccessive(const std::string& filename, bool appendToFile = false);
//prepare the file at @filename,
//copy the header from @headerSource
DataSeriesWriterSuccessive(
const DataSeriesWriterSuccessive& headerSource,
const std::string& filename,
bool appendToFile = false);
//use the following functions to prepare the header
template <class ValueType>
void addMeta(const std::string& key, ValueType val);
void addMetadataMap(const MetadataMap& meta);
void addHeaderText(const std::string& headerText);
//write meta data and header text to the file
void writeHeader();
//write the whole data from the container to the file
void writeData(const Container& dataSeries);
void writeData(const Container& dataSeries, uint32_t floatPrecision);
//write a single data point to the file
void writeData(typename Container::value_type value);
void writeData(typename Container::value_type value, uint32_t floatPrecision);
// or just a preformatted line:
void writeData(const std::string& line);
private:
std::ofstream output;
std::string header;
};
template <class Container>
DataSeriesWriterSuccessive<Container>::
DataSeriesWriterSuccessive(const std::string& filename, bool appendToFile)
: output(filename.c_str(), appendToFile ? std::ios::app : std::ios::out), header("")
{
if (not output) {
std::cerr << "Could not open file " << filename << " for writing.\n";
std::cerr << "Error code: " << strerror(errno) << "\n";
}
}
template <class Container>
DataSeriesWriterSuccessive<Container>::
DataSeriesWriterSuccessive(
const DataSeriesWriterSuccessive<Container>& headerSource,
const std::string& filename,
bool appendToFile)
: output(filename.c_str(), appendToFile ? std::ios::app : std::ios::out),
header(headerSource.header)
{}
template <class Container>
template <class ValueType>
void DataSeriesWriterSuccessive<Container>::
addMeta(const std::string& key, ValueType val) {
std::stringstream ss;
ss << "# " << key << " = " << val << '\n';
header += ss.str();
}
template <class Container>
void DataSeriesWriterSuccessive<Container>::
addMetadataMap(const MetadataMap& meta) {
header += metadataToString(meta, "# ");
}
template <class Container>
void DataSeriesWriterSuccessive<Container>::
addHeaderText(const std::string& headerText) {
std::stringstream ss(headerText);
std::string line;
while (std::getline(ss, line)) {
header += "## " + line + '\n';
}
}
template <class Container>
void DataSeriesWriterSuccessive<Container>::
writeHeader() {
output << header;
output.flush();
}
template <class Container>
void DataSeriesWriterSuccessive<Container>
::writeData(const Container& data) {
for (auto iter = data.cbegin(); iter != data.cend(); ++iter) {
output << *iter << '\n';
}
output.flush();
}
template <class Container>
void DataSeriesWriterSuccessive<Container>
::writeData(const Container& data, uint32_t floatPrecision) {
output.precision(floatPrecision);
output.setf(std::ios::scientific, std::ios::floatfield);
for (auto iter = data.cbegin(); iter != data.cend(); ++iter) {
output << *iter << '\n';
}
output.flush();
}
template <class Container>
void DataSeriesWriterSuccessive<Container>
::writeData(typename Container::value_type value) {
output << value << '\n';
output.flush();
}
template <class Container>
void DataSeriesWriterSuccessive<Container>
::writeData(const std::string& line) {
output << line << '\n';
output.flush();
}
template <class Container>
void DataSeriesWriterSuccessive<Container>
::writeData(typename Container::value_type value, uint32_t floatPrecision) {
output.precision(floatPrecision);
output.setf(std::ios::scientific, std::ios::floatfield);
output << value << '\n';
output.flush();
}
typedef DataSeriesWriterSuccessive<std::vector<double>> DoubleVectorWriterSuccessive;
typedef DataSeriesWriterSuccessive<std::vector<int>> IntVectorWriterSuccessive;
#endif /* DATASERIESWRITERSUCC_H_ */