25 #include <queso/AsciiTable.h>
30 template <
class V,
class M>
34 unsigned int numExtraCols,
35 const std::vector<bool>* extraColIsString,
36 const std::string& fileName)
40 m_numCols (1+numExtraCols),
41 m_colIsString (1,true),
42 m_fileName (fileName),
54 if (extraColIsString == NULL) {
58 unsigned int maxJ = std::min(numExtraCols,(
unsigned int) extraColIsString->size());
59 for (
unsigned int j = 0; j < maxJ; ++j) {
75 template <
class V,
class M>
78 for (
unsigned int j = 0; j < m_doubleColumns.size(); ++j) {
79 if (m_doubleColumns[j])
delete m_doubleColumns[j];
81 for (
unsigned int j = 0; j < m_stringColumns.size(); ++j) {
82 if (m_stringColumns[j])
delete m_stringColumns[j];
87 template <
class V,
class M>
91 unsigned int maxCharsPerLine = 512;
93 std::ifstream ifs(m_fileName.c_str());
96 "AsciiTable<V,M>::readColumnsFromFile()",
97 "file was not found");
100 unsigned int numLines = std::count(std::istreambuf_iterator<char>(ifs),
101 std::istreambuf_iterator<char>(),
106 ifs.seekg(0,std::ios_base::beg);
107 unsigned int lineId = 0;
108 unsigned int numValidLines = 0;
109 std::string tempString;
110 while ((lineId < numLines) && (ifs.eof() ==
false)) {
114 "AsciiTable<V,M>::readColumnsFromFile()",
115 "failed reading during the determination of the number of valid lines");
120 if (tempString[0] !=
'#') numValidLines++;
122 ifs.ignore(maxCharsPerLine,
'\n');
126 "AsciiTable<V,M>::readColumnsFromFile()",
127 "the first number of lines read is nonconsistent");
128 if (m_numRows != numValidLines) {
129 char errorExplanation[512];
130 sprintf(errorExplanation,
"number of valid lines (%u) in ASCII table file does not match number of rows (%u)",numValidLines,m_numRows);
133 "AsciiTable<V,M>::readColumnsFromFile()",
137 if (m_env.subDisplayFile()) {
138 *m_env.subDisplayFile() <<
"ASCII table file '" << m_fileName
139 <<
"' has " << numLines
140 <<
" lines and specifies " << numValidLines
145 for (
unsigned int j=0; j < m_numCols; ++j) {
146 if (m_colIsString[j]) {
147 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
148 *m_env.subDisplayFile() <<
"Column j = " << j
149 <<
" is a columns of strings"
155 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
156 *m_env.subDisplayFile() <<
"Column j = " << j
157 <<
" is a columns of doubles"
160 m_doubleColumns[j] =
new V(m_env,*m_map);
165 ifs.seekg(0,std::ios_base::beg);
167 unsigned int validLineId = 0;
168 std::string tmpString;
169 while ((lineId < numLines) && (ifs.eof() ==
false)) {
171 bool endOfLineAchieved =
false;
176 "AsciiTable<V,M>::readColumnsFromFile()",
177 "failed reading a first column during the valid lines reading loop");
180 if (tmpString[0] ==
'#') {
181 if (!endOfLineAchieved) ifs.ignore(maxCharsPerLine,
'\n');
186 firstColumn(validLineId,0) = tmpString;
189 if (validLineId >= numValidLines) {
190 char errorExplanation[512];
191 sprintf(errorExplanation,
"validLineId (%u) got too large during reading of ASCII table file",validLineId);
194 "AsciiTable<V,M>::readColumnsFromFile()",
198 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
199 *m_env.subDisplayFile() <<
"Just read a string: table[" << validLineId
201 <<
"] = " << firstColumn(validLineId,0)
205 for (
unsigned int j=1; j < m_numCols; ++j) {
208 "AsciiTable<V,M>::readColumnsFromFile()",
209 "failed reading all columns in a valid line");
210 if (m_colIsString[j]) {
215 "AsciiTable<V,M>::readColumnsFromFile()",
216 "failed reading a string column in a valid line");
217 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
218 *m_env.subDisplayFile() <<
"Just read a string: table[" << validLineId
220 <<
"] = " << arrayOfStrings(validLineId,0)
228 "AsciiTable<V,M>::readColumnsFromFile()",
229 "failed reading a double column in a valid line");
230 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
231 *m_env.subDisplayFile() <<
"Just read a double: table[" << validLineId
233 <<
"] = " << (*m_doubleColumns[j])[validLineId]
238 if (!endOfLineAchieved) ifs.ignore(maxCharsPerLine,
'\n');
245 "AsciiTable<V,M>::readColumnsFromFile()",
246 "the second number of lines read is not consistent");
249 "AsciiTable<V,M>::readColumnsFromFile()",
250 "the number of valid lines just read is not consistent");
252 if (m_env.displayVerbosity() >= 5) {
253 if (m_env.subDisplayFile()) {
254 *m_env.subDisplayFile() <<
"Finished reading table '" << m_fileName
255 <<
"'. Its contents per column are:"
257 *m_env.subDisplayFile() << *
this;
258 *m_env.subDisplayFile() << std::endl;
266 template <
class V,
class M>
273 template <
class V,
class M>
280 template <
class V,
class M>
286 "AsciiTable<V,M>::stringColumn()",
291 "AsciiTable<V,M>::stringColumn()",
292 "string column is not ready");
294 return *m_stringColumns[j];
297 template <
class V,
class M>
303 "AsciiTable<V,M>::doubleColumn()",
308 "AsciiTable<V,M>::doubleColumn()",
309 "double column is not ready");
311 return *m_doubleColumns[j];
315 template <
class V,
class M>
319 for (
unsigned int j = 0; j < m_numCols; ++j) {
322 "AsciiTable<V,M>::print()",
323 "column is not null on both possible ways");
326 "AsciiTable<V,M>::print()",
327 "column is null on both possible ways");
329 os <<
"\nContents of table '" << m_fileName
333 if (m_stringColumns[j] != NULL) {
334 os << *m_stringColumns[j];
342 os << *m_doubleColumns[j];
350 template<
class V,
class M>
351 std::ostream& operator<<(std::ostream& os, const AsciiTable<V,M>& obj)
std::vector< bool > m_colIsString
const DistArray< std::string > & stringColumn(unsigned int j) const
Returns the string stored in column j.
void print(std::ostream &os) const
Prints the table.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
unsigned int numRows() const
Returns the number of rows in the table.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
const BaseEnvironment & m_env
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
std::vector< DistArray< std::string > * > m_stringColumns
const V & doubleColumn(unsigned int j) const
Returns the value (double) stored in column j.
AsciiTable(const BaseEnvironment &env, unsigned int numRows, unsigned int numExtraCols, const std::vector< bool > *extraColIsString, const std::string &fileName)
Default constructor.
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
std::vector< V * > m_doubleColumns
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
unsigned int displayVerbosity() const
unsigned int numCols() const
Returns the number of columns in the table.
void readColumnsFromFile()