26 #include <queso/AsciiTable.h>
31 template <
class V,
class M>
35 unsigned int numExtraCols,
36 const std::vector<bool>* extraColIsString,
37 const std::string& fileName)
41 m_numCols (1+numExtraCols),
42 m_colIsString (1,true),
43 m_fileName (fileName),
55 if (extraColIsString == NULL) {
59 unsigned int maxJ = std::min(numExtraCols,(
unsigned int) extraColIsString->size());
60 for (
unsigned int j = 0; j < maxJ; ++j) {
76 template <
class V,
class M>
79 for (
unsigned int j = 0; j < m_doubleColumns.size(); ++j) {
80 if (m_doubleColumns[j])
delete m_doubleColumns[j];
82 for (
unsigned int j = 0; j < m_stringColumns.size(); ++j) {
83 if (m_stringColumns[j])
delete m_stringColumns[j];
88 template <
class V,
class M>
92 unsigned int maxCharsPerLine = 512;
94 std::ifstream ifs(m_fileName.c_str());
98 unsigned int numLines = std::count(std::istreambuf_iterator<char>(ifs),
99 std::istreambuf_iterator<char>(),
104 ifs.seekg(0,std::ios_base::beg);
105 unsigned int lineId = 0;
106 unsigned int numValidLines = 0;
107 std::string tempString;
108 while ((lineId < numLines) && (ifs.eof() ==
false)) {
110 queso_require_msg(!(iRC),
"failed reading during the determination of the number of valid lines");
115 if (tempString[0] !=
'#') numValidLines++;
117 ifs.ignore(maxCharsPerLine,
'\n');
120 if (m_numRows != numValidLines) {
121 char errorExplanation[512];
122 sprintf(errorExplanation,
"number of valid lines (%u) in ASCII table file does not match number of rows (%u)",numValidLines,m_numRows);
126 if (m_env.subDisplayFile()) {
127 *m_env.subDisplayFile() <<
"ASCII table file '" << m_fileName
128 <<
"' has " << numLines
129 <<
" lines and specifies " << numValidLines
134 for (
unsigned int j=0; j < m_numCols; ++j) {
135 if (m_colIsString[j]) {
136 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
137 *m_env.subDisplayFile() <<
"Column j = " << j
138 <<
" is a columns of strings"
144 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
145 *m_env.subDisplayFile() <<
"Column j = " << j
146 <<
" is a columns of doubles"
149 m_doubleColumns[j] =
new V(m_env,*m_map);
154 ifs.seekg(0,std::ios_base::beg);
156 unsigned int validLineId = 0;
157 std::string tmpString;
158 while ((lineId < numLines) && (ifs.eof() ==
false)) {
160 bool endOfLineAchieved =
false;
163 queso_require_msg(!(iRC),
"failed reading a first column during the valid lines reading loop");
166 if (tmpString[0] ==
'#') {
167 if (!endOfLineAchieved) ifs.ignore(maxCharsPerLine,
'\n');
172 firstColumn(validLineId,0) = tmpString;
175 if (validLineId >= numValidLines) {
176 char errorExplanation[512];
177 sprintf(errorExplanation,
"validLineId (%u) got too large during reading of ASCII table file",validLineId);
181 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
182 *m_env.subDisplayFile() <<
"Just read a string: table[" << validLineId
184 <<
"] = " << firstColumn(validLineId,0)
188 for (
unsigned int j=1; j < m_numCols; ++j) {
189 queso_require_msg(!(endOfLineAchieved),
"failed reading all columns in a valid line");
190 if (m_colIsString[j]) {
194 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
195 *m_env.subDisplayFile() <<
"Just read a string: table[" << validLineId
197 <<
"] = " << arrayOfStrings(validLineId,0)
204 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
205 *m_env.subDisplayFile() <<
"Just read a double: table[" << validLineId
207 <<
"] = " << (*m_doubleColumns[j])[validLineId]
212 if (!endOfLineAchieved) ifs.ignore(maxCharsPerLine,
'\n');
220 if (m_env.displayVerbosity() >= 5) {
221 if (m_env.subDisplayFile()) {
222 *m_env.subDisplayFile() <<
"Finished reading table '" << m_fileName
223 <<
"'. Its contents per column are:"
225 *m_env.subDisplayFile() << *
this;
226 *m_env.subDisplayFile() << std::endl;
234 template <
class V,
class M>
241 template <
class V,
class M>
248 template <
class V,
class M>
256 return *m_stringColumns[j];
259 template <
class V,
class M>
267 return *m_doubleColumns[j];
271 template <
class V,
class M>
275 for (
unsigned int j = 0; j < m_numCols; ++j) {
276 if ((m_stringColumns[j] != NULL))
queso_require_msg(!(m_doubleColumns[j]),
"column is not null on both possible ways");
277 if ((m_stringColumns[j] == NULL))
queso_require_msg(m_doubleColumns[j],
"column is null on both possible ways");
279 os <<
"\nContents of table '" << m_fileName
283 if (m_stringColumns[j] != NULL) {
284 os << *m_stringColumns[j];
292 os << *m_doubleColumns[j];
300 template<
class V,
class M>
301 std::ostream& operator<<(std::ostream& os, const AsciiTable<V,M>& obj)
unsigned int displayVerbosity() const
const DistArray< std::string > & stringColumn(unsigned int j) const
Returns the string stored in column j.
unsigned int numRows() const
Returns the number of rows in the table.
std::vector< DistArray< std::string > * > m_stringColumns
const V & doubleColumn(unsigned int j) const
Returns the value (double) stored in column j.
void readColumnsFromFile()
void print(std::ostream &os) const
Prints the table.
std::vector< bool > m_colIsString
#define queso_error_msg(msg)
const BaseEnvironment & m_env
#define queso_require_equal_to_msg(expr1, expr2, msg)
#define queso_require_msg(asserted, msg)
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
#define queso_require_less_msg(expr1, expr2, msg)
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
std::vector< V * > m_doubleColumns
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
unsigned int numCols() const
Returns the number of columns in the table.
AsciiTable(const BaseEnvironment &env, unsigned int numRows, unsigned int numExtraCols, const std::vector< bool > *extraColIsString, const std::string &fileName)
Default constructor.