26#ifndef __readColumns_hpp__
27#define __readColumns_hpp__
34#include "../mxlib.hpp"
35#include "../mxError.hpp"
39#define MX_READCOL_MISSINGVALSTR "-99"
46template <
char delim = ' ',
char eol = '\n'>
47void readcol(
char *sin,
int sz )
49 static_cast<void>( sin );
50 static_cast<void>( sz );
55template <
char delim =
' ',
char eol =
'\n',
typename arrT,
typename... arrTs>
56void readcol(
char *sin,
int sz, arrT &array, arrTs &...arrays )
62 int l = strlen( sin );
68 while( isspace( sin[i] ) && sin[i] != eol && i < l )
76 array.push_back( convertFromString<typename arrT::value_type>(
"" ) );
80 std::stringstream sinstr( sin );
82 std::getline( sinstr, str, delim );
85 if( str[str.size() - 1] == eol )
87 str.erase( str.size() - 1 );
92 array.push_back( convertFromString<typename arrT::value_type>( MX_READCOL_MISSINGVALSTR ) );
96 array.push_back( convertFromString<typename arrT::value_type>( str ) );
99 sin += ( str.size() + 1 ) *
sizeof(
char );
100 sz -= ( str.size() + 1 ) *
sizeof(
char );
102 readcol<delim, eol>( sin, sz, arrays... );
137template <
char delim =
' ',
char comment =
'#',
char eol =
'\n',
typename... arrTs>
152 mxPError(
"readColumns", errno,
"Occurred while opening " + fname +
" for reading." );
156 mxError(
"readColumns", MXE_FILEOERR,
"Occurred while opening " + fname +
" for reading." );
162 char *line =
new char[lineSize];
167 fin.getline( line, lineSize - 1, eol );
170 int l = strlen( line );
178 while( line[i] != comment )
200 readcol<delim, eol>( line, strlen( line ), arrays... );
206 if( fin.bad() && !fin.fail() )
210 mxPError(
"readColumns", errno,
"Occurred while reading from " + fname +
"." );
214 mxError(
"readColumns", MXE_FILERERR,
"Occurred while reading from " + fname +
"." );
226 mxPError(
"readColumns", errno,
"Occurred while closing " + fname +
"." );
230 mxError(
"readColumns", MXE_FILECERR,
"Occurred while closing " + fname +
"." );
253 template <
typename T>
254 void push_back(
const T &arg )
int readColumns(const std::string &fname, arrTs &...arrays)
Read in columns from a text file.
Utilities for working with strings.
A dummy class to allow mx::readColumns to skip a column(s) in a file without requiring memory allocat...
std::string value_type
value_type is defined as std::string so that no conversions take place.