26 #ifndef __readColumns_hpp__
27 #define __readColumns_hpp__
34 #include "../mxlib.hpp"
35 #include "../mxError.hpp"
39 #define MX_READCOL_MISSINGVALSTR "-99"
46 template<
char delim=' ',
char eol='\n'>
47 void readcol(
char * sin,
int sz)
49 static_cast<void>(sin);
50 static_cast<void>(sz);
55 template<
char delim=
' ',
char eol=
'\n',
typename arrT,
typename... arrTs>
56 void readcol(
char * sin,
int sz, arrT & array, arrTs &... arrays)
67 while( isspace(sin[i]) && sin[i] != eol && i < l) ++i;
74 array.push_back(convertFromString<typename arrT::value_type>(
""));
78 std::stringstream sinstr(sin);
80 std::getline(sinstr, str, delim);
83 if( str[str.size()-1] == eol)
85 str.erase(str.size()-1);
90 array.push_back(convertFromString<typename arrT::value_type>(MX_READCOL_MISSINGVALSTR));
94 array.push_back(convertFromString<typename arrT::value_type>(str));
97 sin += ( str.size()+1)*
sizeof(
char);
98 sz -= ( str.size()+1)*
sizeof(
char);
100 readcol<delim,eol>(sin, sz, arrays...);
138 template<
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);
177 while(line[i] != comment )
197 readcol<delim,eol>(line, strlen(line), arrays...);
203 if(fin.bad() && !fin.fail())
207 mxPError(
"readColumns", errno,
"Occurred while reading from " + fname +
".");
211 mxError(
"readColumns", MXE_FILERERR,
"Occurred while reading from " + fname +
".");
223 mxPError(
"readColumns", errno,
"Occurred while closing " + fname +
".");
227 mxError(
"readColumns", MXE_FILECERR,
"Occurred while closing " + fname +
".");
253 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.