CSVExporter.cxx
Go to the documentation of this file.
1 #include "CSVExporter.h"
2 
3 #include <algorithm>
4 #include <iostream>
5 #include <iomanip>
6 #include <limits>
7 #include <stdexcept>
8 #include <functional>
9 
10 template<typename T, typename S>
12  std::ostream &output,
13  const std::vector<T> &values,
14  bool firstValuePrinted,
15  S separator,
16  std::function<void (std::ostream &, const T&)> printValue
17 )
18 {
19  if (values.empty()) {
20  return firstValuePrinted;
21  }
22 
23  size_t startIdx = 0;
24 
25  if (! firstValuePrinted)
26  {
27  printValue(output, values[0]);
28  startIdx = 1;
29  }
30 
31  for (size_t i = startIdx; i < values.size(); i++)
32  {
33  output << separator;
34  printValue(output, values[i]);
35  }
36 
37  return true;
38 }
39 
41  : ofile(output), initialized(false)
42 {
43  if (! ofile) {
44  throw std::runtime_error("Failed to open output file");
45  }
46 
47  ofile << std::setprecision(std::numeric_limits<double>::max_digits10);
48 }
49 
51 {
52  ofile << std::setprecision(precision);
53 }
54 
56 {
57  bool firstValuePrinted = false;
58 
59  firstValuePrinted = printSeparatedValues<std::string, char>(
60  ofile, scalVarNames, firstValuePrinted, ',',
61  [] (auto &output, const auto &name) { output << name; }
62  );
63 
64  printSeparatedValues<std::string, char>(
65  ofile, vectVarNames, firstValuePrinted, ',',
66  [] (auto &output, const auto &name) { output << name; }
67  );
68 
69  ofile << std::endl;
70 }
71 
72 void CSVExporter::init(const VarDict &vars)
73 {
74  if (scalVarNames.empty() && vectVarNames.empty()) {
75  for (const auto &kval : vars.scalar) {
76  scalVarNames.push_back(kval.first);
77  }
78 
79  for (const auto &kval : vars.vector) {
80  vectVarNames.push_back(kval.first);
81  }
82 
83  std::sort(scalVarNames.begin(), scalVarNames.end());
84  std::sort(vectVarNames.begin(), vectVarNames.end());
85  }
86 
87  printHeader();
88  initialized = true;
89 }
90 
92 {
93  if (! initialized) {
94  scalVarNames.push_back(name);
95  }
96 }
97 
99 {
100  if (! initialized) {
101  vectVarNames.push_back(name);
102  }
103 }
104 
106 {
107  if (! initialized) {
108  init(vars);
109  }
110 
111  bool firstValuePrinted = false;
112 
113  firstValuePrinted = printSeparatedValues<std::string, char>(
114  ofile, scalVarNames, firstValuePrinted, ',',
115  [&vars] (auto &output, const auto &name)
116  {
117  auto it = vars.scalar.find(name);
118  if (it != vars.scalar.end()) {
119  output << it->second;
120  }
121  }
122  );
123 
124  firstValuePrinted = printSeparatedValues<std::string, char>(
125  ofile, vectVarNames, firstValuePrinted, ',',
126  [&vars] (auto &output, const auto &name)
127  {
128  output << "\"";
129  auto it = vars.vector.find(name);
130 
131  if (it != vars.vector.end()) {
132  printSeparatedValues<double, char>(
133  output, it->second, false, ',',
134  [] (auto &output, const auto x) { output << x; }
135  );
136  }
137 
138  output << "\"";
139  }
140  );
141 
142  ofile << std::endl;
143 }
144 
145 
static QCString name
Definition: declinfo.cpp:673
void printHeader()
Definition: CSVExporter.cxx:55
bool initialized
Definition: CSVExporter.h:21
std::ofstream ofile
Definition: CSVExporter.h:13
float precision
Definition: makePolycone.py:48
void exportVars(const VarDict &vars)
std::string string
Definition: nybbler.cc:12
Definition: 044_section.h:5
std::vector< std::string > scalVarNames
Definition: CSVExporter.h:15
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
Definition: VarDict.h:8
void addScalarVar(const std::string &name)
Definition: CSVExporter.cxx:91
void setPrecision(int precision)
Definition: CSVExporter.cxx:50
bool printSeparatedValues(std::ostream &output, const std::vector< T > &values, bool firstValuePrinted, S separator, std::function< void(std::ostream &, const T &)> printValue)
Definition: CSVExporter.cxx:11
void init(const VarDict &vars)
Definition: CSVExporter.cxx:72
std::unordered_map< std::string, std::vector< double > > vector
Definition: VarDict.h:11
void printValue(T val)
Q_UINT16 values[128]
void addVectorVar(const std::string &name)
Definition: CSVExporter.cxx:98
std::vector< std::string > vectVarNames
Definition: CSVExporter.h:16
list x
Definition: train.py:276
CSVExporter(const std::string &output)
Definition: CSVExporter.cxx:40
void function(int client, int *resource, int parblock, int *test, int p)
QTextStream & endl(QTextStream &s)
std::unordered_map< std::string, double > scalar
Definition: VarDict.h:10