-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSeparatedOutput.h
302 lines (248 loc) · 8.97 KB
/
SeparatedOutput.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* SeparatedOutput
*
* Contact: Jeff Maddalon ([email protected])
*
* Copyright (c) 2014-2021 United States Government as represented by
* the National Aeronautics and Space Administration. No copyright
* is claimed in the United States under Title 17, U.S.Code. All Other
* Rights Reserved.
*/
#ifndef SEPARATEDOUTPUT_H
#define SEPARATEDOUTPUT_H
#include "Units.h"
#include "ErrorLog.h"
#include "ErrorReporter.h"
#include "ParameterReader.h"
#include "Quad.h"
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <stdexcept>
namespace larcfm {
/**
* <p>A class to writes a separated value file (separated by commas, spaces, or tabs).<p>
* only one file can be created from an object.</p>
*
* <p>
* Future: handle a memory buffer, standard output/error, file, socket?
* </p>
*/
class SeparatedOutput : public ErrorReporter {
private:
std::ostream* writer;
ErrorLog error;
bool header; // has header line been written?
std::vector<std::string> header_str; // header line
bool bunits; // Should units line be written?
std::vector<std::string> units_str; // Units type
std::vector<std::string> line_str; // raw line
long size_l;
int column_count;
int header_count;
char delim;
std::string space;
std::string comment_char;
std::string empty;
std::vector<std::string> comments;
std::vector<std::string> params;
void init();
public:
/** Create an "empty" separated output */
SeparatedOutput();
/** Create a new SeparatedOutput from the given writer
* @param w writer object
* */
explicit SeparatedOutput(std::ostream* w);
// /** Copy Constructor. This should not be used. */
// SeparatedOutput(const SeparatedOutput& x);
/** Assignment Operator. */
SeparatedOutput& operator=(const SeparatedOutput& x);
void close();
/** Return the heading for the given column
* @param i column index
* @return heading
* */
std::string getHeading(int i);
/** Return the number of rows written
* @return number of rows
* */
long length();
/** Return the number of columns
* @return number of columns
* */
long size();
/**
* Should the output units be placed in the output file?
* @param output if true, then the units should be displayed
*/
void setOutputUnits(bool output);
/**
* Set the heading for the given column number, columns begin at 0.
* @param i the column number
* @param name the name of this column heading
* @param unit the unit for this column. If you don't know, then use "unspecified"
*/
void setHeading(int i, const std::string& name, const std::string& unit);
/**
* Add the given heading (and unit) to the next column
* @param name the name of this column heading
* @param unit the unit for this column. If you don't know, then use "unspecified"
*/
void addHeading(const std::string& name, const std::string& unit);
/**
* Add the given heading (and unit) to the next column
* @param names the name of this column heading
* @param units the unit for this column. If you don't know, then use "unspecified"
*/
void addHeading(const std::vector<std::string>& names, const std::vector<std::string>& units);
/**
* Add the given heading (and unit) to the next column
* @param names_and_units an array containing an alternating list of heading names and heading units. The length of this list must be even.
*/
void addHeading(const std::vector<std::string>& names_and_units);
/**
* Find the index of the column with given heading. If
* the heading is not found, then -1 is returned.
* Note: If you are getting some oddly large indexes, there are probably some nonstandard characters in the input.
* @param heading name of heading
* @param caseSensitive false if ignoring case
* @return index of heading
*/
int findHeading(const std::string& heading, bool caseSensitive);
/**
* Find the index of the column with any of the given headings. If none of
* the given headings is found, then -1 is returned. This tries to find the
* first heading, and if it finds it then returns that index. If it doesn't
* find it, it moves to the next heading, etc.
* Note: If you are getting some oddly large indexes, there are probably some nonstandard characters in the input.
* @param heading1 name of heading
* @param heading2 alternate name of heading
* @param caseSensitive false if ignore case
* @return index of heading
*/
int findHeading(const std::string& heading1, const std::string& heading2, bool caseSensitive);
/**
* Find the index of the column with any of the given headings. If none of
* the given headings is found, then -1 is returned. This tries to find the
* first heading, and if it finds it then returns that index. If it doesn't
* find it, it moves to the next heading, etc.
* Note: If you are getting some oddly large indexes, there are probably some nonstandard characters in the input.
* @param heading1 name of heading
* @param heading2 alternate name of heading
* @param heading3 alternate name of heading
* @param caseSensitive false if ignore heading
* @return index of heading
*/
int findHeading(const std::string& heading1, const std::string& heading2, const std::string& heading3, bool caseSensitive);
/**
* Find the index of the column with any of the given headings. If none of
* the given headings is found, then -1 is returned. This tries to find the
* first heading, and if it finds it then returns that index. If it doesn't
* find it, it moves to the next heading, etc.
* Note: If you are getting some oddly large indexes, there are probably some nonstandard characters in the input.
* @param heading1 name of heading
* @param heading2 alternate name of heading
* @param heading3 alternate name of heading
* @param heading4 alternate name of heading
* @param caseSensitive false if ignore heading
* @return index of heading
*/
int findHeading(const std::string& heading1, const std::string& heading2, const std::string& heading3, const std::string& heading4, bool caseSensitive);
/**
* Returns the units string for the i-th column. If an invalid
* column is entered, then "unspecified" is returned.
* @param i column index
* @return unit
*/
std::string getUnit(int i);
/**
* Sets the next column value equal to the given value. The value is in internal units.
*/
void setColumn(int i, double val);
/**
* Sets the next column value equal to the given value. The value is in internal units.
*/
void setColumn(double val);
/**
* Sets the next column value equal to the given value.
* @param i index of column
* @param val value
*/
void setColumn(int i, const std::string& val);
/**
* Adds the given value to the next column.
* @param val value
*/
void addColumn(const std::string& val);
// /**
// * Adds each of the given values to the next columns.
// */
// void addColumn(OutputList ol);
/**
* Adds each of the given values to the next columns.
* @param vals list of values
*/
void addColumn(const std::vector<std::string>& vals);
/**
* Sets the column delimiter to a tab
*/
void setColumnDelimiterTab();
/**
* Sets the column delimiter to a comma
*/
void setColumnDelimiterComma();
/**
* Sets the column delimiter to a space. If a space is used as a
* separator then the empty value should be set (see setEmptyValue).
*/
void setColumnDelimiterSpace();
/**
* Sets the number of extra spaces after the delimiter
* @param num number of spaces
*/
void setColumnSpace(int num);
/**
* The value to be displayed if a column is "skipped". Empty values are only added inside a line, not at the end.
* @param e value to indicate empty
*/
void setEmptyValue(const std::string& e);
/**
* Set the code indicating the start of a comment.
* @param c comment character
*/
void setCommentCharacter(const std::string& c);
/**
* Additively set parameters. (This does not delete existing parameters, but will overwrite them.)
* @param pr parameter object
*/
void setParameters(const ParameterData& pr);
void setParameter(const std::string& key, const std::string& value);
/**
* Clear all parameters.
*/
void clearParameters();
/**
* Add the following line to the comments.
* @param c comment string
*/
void addComment(const std::string& c);
/**
* Writes a line of the output. The first call to writeLine will write the column headings, units, etc.
*/
void writeLine();
void flush();
private:
void print_line(const std::vector<std::string>& vals); // throws IOException;
public:
std::string toString() const;
// ErrorReporter Interface Methods
bool hasError() const;
bool hasMessage() const;
std::string getMessage();
std::string getMessageNoClear() const;
};
}
#endif //SEPARATEDOUTPUT_H