1 #ifndef ASDMVALUESPARSER_H
2 #define ASDMVALUESPARSER_H
35 #include <boost/algorithm/string/trim.hpp>
36 #include <boost/tokenizer.hpp>
82 return "ASDMValuesParserException : " +
message;
87 static std::istringstream
iss;
88 static std::ostringstream
oss;
94 iss >> v;
if (
iss.fail() || (
iss.get(c) && c !=
' ')) {
96 oss <<
"Error while reading the string to be parsed : '" <<
iss.str() <<
"'.";
103 static T
parse(
const std::string& s) {
112 static std::vector<T>
parse1D(
const std::string& s) {
121 oss <<
"The first field of a 1D array representation should be '1', I found '" << ndim <<
"' in '" << s <<
"'.";
128 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue <<
"'.";
132 std::vector<T> result(nvalue);
134 for (
int i = 0; i < nvalue; i++) {
143 static std::vector<std::vector<T> >
parse2D(
const std::string& s) {
153 oss <<
"The first field of a 2D array representation should be '2', I found '" << ndim <<
"' in '" << s <<
"'.";
160 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue1 <<
"'.";
167 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue2 <<
"'.";
171 std::vector<std::vector<T> > result(nvalue1);
173 for (
int i = 0; i < nvalue1; i++) {
174 std::vector<T> v(nvalue2);
175 for (
int j = 0; j < nvalue2; j++) {
185 static std::vector<std::vector<std::vector<T> > >
parse3D(
const std::string& s) {
197 oss <<
"The first field of a 3D array representation should be '3', I found '" << ndim <<
"' in '" << s <<
"'.";
204 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue1 <<
"'.";
211 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue2 <<
"'.";
218 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue3 <<
"'.";
222 std::vector<std::vector<std::vector<T> > > result(nvalue1);
224 for (
int i = 0; i < nvalue1; i++) {
225 std::vector<std::vector<T> >vv(nvalue2);
226 for (
int j = 0; j < nvalue2; j++) {
227 std::vector<T> v(nvalue3);
228 for (
int k = 0; k < nvalue3; k++) {
240 static std::vector<std::vector<std::vector<std::vector<T> > > >
parse4D(
const std::string& s) {
252 oss <<
"The first field of a 3D array representation should be '4', I found '" << ndim <<
"' in '" << s <<
"'.";
259 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue1 <<
"'.";
266 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue2 <<
"'.";
273 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue3 <<
"'.";
280 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue4 <<
"'.";
284 std::vector<std::vector<std::vector<std::vector<T> > > > result(nvalue1);
286 for (
int i = 0; i < nvalue1; i++) {
287 std::vector<std::vector<std::vector<T> > > vvv(nvalue2);
288 for (
int j = 0; j < nvalue2; j++) {
289 std::vector<std::vector<T> > vv(nvalue3);
290 for (
int k = 0; k < nvalue3; k++) {
291 std::vector<T> v(nvalue4);
292 for (
int l = 0; l < nvalue4; l++) {
305 static std::string
parse(
const std::string& s);
306 static std::vector<std::string>
parse1D(
const std::string& s);
307 static std::vector<std::vector<std::string > >
parse2D(
const std::string& s);
308 static std::vector<std::vector<std::vector<std::string > > >
parse3D(
const std::string& s);
310 static std::vector<std::string>
parseQuoted(
const std::string& s);
332 oss <<
"The first field of a 1D array representation should be '1', I found '" << ndim <<
"' in '" << s <<
"'.";
333 throw ASDMValuesParserException(
oss.str());
339 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue <<
"'.";
340 throw ASDMValuesParserException(
oss.str());
343 std::string remains; getline(
iss,remains);
344 #ifndef WITHOUT_BOOST
345 std::vector<std::string> result =
parseQuoted(boost::trim_left_copy(remains));
347 std::vector<std::string> result =
parseQuoted(asdm::ltrim_copy(remains));
349 if (nvalue > (
int) result.size()) {
351 oss <<
"Error while reading the string to be parsed : '" <<
iss.str() <<
"'.";
352 throw ASDMValuesParserException(
oss.str());
367 oss <<
"The first field of a 2D array representation should be '2', I found '" << ndim <<
"' in '" << s <<
"'.";
368 throw ASDMValuesParserException(
oss.str());
374 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue1 <<
"'.";
375 throw ASDMValuesParserException(
oss.str());
381 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue2 <<
"'.";
382 throw ASDMValuesParserException(
oss.str());
385 std::string remains; getline(
iss,remains);
386 #ifndef WITHOUT_BOOST
387 std::vector<std::string> v_s =
parseQuoted(boost::trim_left_copy(remains));
389 std::vector<std::string> v_s =
parseQuoted(asdm::ltrim_copy(remains));
391 if (nvalue1 * nvalue2 > (
int) v_s.size()) {
393 oss <<
"Error while reading the string to be parsed : '" <<
iss.str() <<
"'.";
394 throw ASDMValuesParserException(
oss.str());
397 std::vector<std::vector<std::string> > result(nvalue1);
399 for (
unsigned int i = 0; i < result.size(); i++) {
401 result[i].assign(v_s.begin()+start, v_s.begin()+start+nvalue2);
417 oss <<
"The first field of a 2D array representation should be '2', I found '" << ndim <<
"' in '" << s <<
"'.";
418 throw ASDMValuesParserException(
oss.str());
424 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue1 <<
"'.";
425 throw ASDMValuesParserException(
oss.str());
431 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue2 <<
"'.";
432 throw ASDMValuesParserException(
oss.str());
438 oss <<
"The number of values along one dimension of an array must be expressed by a strictly positive integer.I found '" << nvalue3 <<
"'.";
439 throw ASDMValuesParserException(
oss.str());
442 std::string remains; getline(
iss,remains);
443 #ifndef WITHOUT_BOOST
444 std::vector<std::string> v_s =
parseQuoted(boost::trim_left_copy(remains));
446 std::vector<std::string> v_s =
parseQuoted(asdm::ltrim_copy(remains));
448 if (nvalue1 * nvalue2 * nvalue3 > (
int) v_s.size()) {
450 oss <<
"Error while reading the string to be parsed : '" <<
iss.str() <<
"'.";
451 throw ASDMValuesParserException(
oss.str());
454 std::vector<std::vector<std::string> > plane(nvalue2);
455 std::vector<std::vector<std::vector<std::string> > > result(nvalue1, plane);
457 for (
unsigned int i = 0; i < (
unsigned int) nvalue1; i++) {
458 for (
unsigned int j = 0; j < (
unsigned int) nvalue2; j++) {
459 result[i][j].assign(v_s.begin()+start, v_s.begin()+start+nvalue3);
467 #ifndef WITHOUT_BOOST
468 std::string separator1(
"\\");
469 std::string separator2(
" ");
470 std::string separator3(
"\"");
472 boost::escaped_list_separator<char> els(separator1,separator2,separator3);
473 boost::tokenizer<boost::escaped_list_separator<char> > tok(s, els);
474 std::vector<std::string> result(tok.begin(), tok.end());
477 std::vector<std::string> result;
485 std::string nullString;
487 bool quoted, escaped;
488 quoted = escaped =
false;
506 result.push_back(token);
519 result.push_back(token);
Elements::const_iterator const_iterator
static std::vector< std::vector< T > > parse2D(const std::string &s)
static std::istringstream iss
static std::vector< T > parse1D(const std::string &s)
ASDMValuesParserException()
An empty contructor.
LatticeExprNode ndim(const LatticeExprNode &expr)
1-argument function to get the dimensionality of a lattice.
static std::vector< std::vector< std::vector< std::vector< T > > > > parse4D(const std::string &s)
static std::vector< std::vector< std::vector< T > > > parse3D(const std::string &s)
static std::vector< std::string > parseQuoted(const std::string &s)
std::string getMessage() const
Returns the message associated to this exception.
const Double c
Fundamental physical constants (SI units):
static T parse(const std::string &s)
A class to represent an exception thrown during the parsing of the representation of a basic type val...
virtual ~ASDMValuesParserException()
The destructor.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
static std::ostringstream oss