Nemo  2.3.46
StreamParser Class Reference

Read parameters from a text buffer. More...

#include <paramsparser.h>

+ Inheritance diagram for StreamParser:
+ Collaboration diagram for StreamParser:

Public Member Functions

 StreamParser (const char *stream)
 
virtual ~StreamParser ()
 
virtual bool read (const char *stream)
 
virtual bool readArguments (istream &IN, int &l_count, string &args)
 
void replaceCR (string &stream, const char rpl= '\n')
 
- Public Member Functions inherited from ParamsParser
 ParamsParser (const char *name)
 
virtual ~ParamsParser ()
 
void setName (const char *name)
 
map< string, string > get_inputParams ()
 
map< string, vector< string > > & getParsedParameters (const char *stream_name)
 
map< string, vector< string > > & getParsedParameters ()
 
map< string, string > & getParameters (const char *stream_name)
 
map< string, string > & getParameters ()
 
void parse ()
 Builds the _parsedParams from the _inputParams. More...
 

Static Public Member Functions

static bool removeComment (istream &IN, int &l_count)
 Recusively removes comments until the end of a line/of the file, or of a block comment is reached. More...
 
static bool removeSpaceAndComment (istream &IN, int &l_count, bool keepLast=false)
 Removes whitespace char on a line until a non-ws or EOL is reached. More...
 
static string readUntilCharacter (istream &IN, int &l_count, char &start_c, const char end_c)
 
static void eatLine (istream &IN, int &l_count)
 
- Static Public Member Functions inherited from ParamsParser
static void getBlockArgument (istream &IN, char &c, string &arg)
 
static void getArguments (string &arg_str, vector< string > &arg_vect)
 

Additional Inherited Members

- Protected Member Functions inherited from ParamsParser
void reset_inputParams ()
 
void add_inputParam (string &param, const string &arg)
 

Detailed Description

Read parameters from a text buffer.

Constructor & Destructor Documentation

StreamParser::StreamParser ( const char *  stream)
inline
83 : ParamsParser(stream) {}
ParamsParser(const char *name)
Definition: paramsparser.h:44
virtual StreamParser::~StreamParser ( )
inlinevirtual
84 {}

Member Function Documentation

void StreamParser::eatLine ( istream &  IN,
int l_count 
)
static

References EOL.

489 {
490  char c;
491  while(IN.get(c) && IN.good() && c != EOL && !IN.eof());
492  ++l_count;
493 }
#define EOL
Definition: paramsparser.cc:39
bool StreamParser::read ( const char *  stream)
virtual

Implements ParamsParser.

Reimplemented in BinaryFileParser, and FileParser.

References ParamsParser::add_inputParam(), EOL, message(), and ParamsParser::reset_inputParams().

Referenced by FileParser::read(), and BinaryFileParser::read().

180  {
181  int linecnt = 0;
182  //output string to collect parameter arguments
183  string args;
184  //string to store parameter name
185  string key;
186 
188 
189  //--------------------------------------------------------------------------------
190  char c = 0, eof = '\0';
191  //put the char stream into a string
192  string input_stream(stream);
193  //add the terminating character to the string:
194  input_stream += eof;
195 
196  //check if LF
197  if(input_stream.find_first_of('\r') != string::npos) {
198  //guess if NL is CRLF as in DOS files:
199  if(input_stream.find_first_of('\n') == input_stream.find_first_of('\r') + 1)
200  replaceCR(input_stream, ' '); //remove CR
201  else
202  replaceCR(input_stream); //replaces CR with LF
203  }
204 
205  //initiate the input stringstream:
206  istringstream IN(input_stream);
207 
208  //--------------------------------------------------------------------------------
209  //read the file line by line
210  while(IN.good() && !IN.eof()) {
211 
212  linecnt++;
213 
214  // remove the forgoing space of the line, returns false if EOL is reached
215  if(!removeSpaceAndComment(IN, linecnt)) continue;
216 
217  key="";
218  //read the parameter name:
219  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL && !isspace(c)){
220  //whitespace is the split char between a parameter and its argument
221  //this basically does the same as IN>>key, but we have to chek for comments:
222  if(c == '#'){
223  removeComment(IN, linecnt);
224  break;
225  }
226 
227  key += c;
228  } //__end_while_key__
229 
230  if(c == EOL || !removeSpaceAndComment(IN, linecnt) ) {
231  if(c != eof){
232  if(key.size() != 0) {//boolean param
233  args = "1";
234  ParamsParser::add_inputParam(key, args);
235  }
236 #ifdef _DEBUG_
237  message(" line %i: %s (1)\n", linecnt, key.c_str());
238 #endif
239  }
240  continue;
241  }
242  //remove whitespace before arguments
243 // if( !removeSpaceAndComment(IN, linecnt) ) continue;
244 
245 #ifdef _DEBUG_
246  message(" line %i: %s (", linecnt, key.c_str());
247 #endif
248 
249  //read the arguments:
250  args = "";
251  while( readArguments(IN, linecnt, args) );
252 
253  //remove any trailling space on the argument string
254  int i;
255  for(i=(int)args.size()-1; i>=0; --i){
256  if(!isspace(args[i])) break;
257  }
258  args[i+1]='\0';
259 
260  ParamsParser::add_inputParam(key, args.c_str());
261 
262 #ifdef _DEBUG_
263  message("%s)\n", args.c_str());
264 #endif
265  //reset the stream state, is changed by operator >>
266  IN.clear(ios::goodbit);
267 
268  }//__END__WHILE__
269 
270  return true;
271  }
void message(const char *message,...)
Definition: output.cc:40
static bool removeSpaceAndComment(istream &IN, int &l_count, bool keepLast=false)
Removes whitespace char on a line until a non-ws or EOL is reached.
Definition: paramsparser.cc:278
#define EOL
Definition: paramsparser.cc:39
virtual bool readArguments(istream &IN, int &l_count, string &args)
Definition: paramsparser.cc:370
void replaceCR(string &stream, const char rpl= '\n')
Definition: paramsparser.cc:497
void add_inputParam(string &param, const string &arg)
Definition: paramsparser.h:67
static bool removeComment(istream &IN, int &l_count)
Recusively removes comments until the end of a line/of the file, or of a block comment is reached...
Definition: paramsparser.cc:313
void reset_inputParams()
Definition: paramsparser.h:66
bool StreamParser::readArguments ( istream &  IN,
int l_count,
string &  args 
)
virtual

References EOL, and fatal().

Referenced by Param::getArgumentFromFile().

371 {
372  char c;
373 
374  //read one byte at a time
375  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL){
376 
377  if(c == '\\')
378  eatLine(IN, l_count);
379  //block delimiters: the block will the be parsed in Param
380  else if(c == '{')
381  args += readUntilCharacter(IN, l_count, c, '}');
382  else if(c == '(')
383  args += readUntilCharacter(IN, l_count, c, ')');
384  else if(c == '[')
385  args += readUntilCharacter(IN, l_count, c, ']');
386  else if(c == '\"')
387  args += readUntilCharacter(IN, l_count, c, '\"');
388  //comment delimiter:
389  else if(c == '#') {
390  if(!removeComment(IN, l_count))
391  return false; // reached end of line
392 
393  //argument in external file
394  } else if (c == '&') {
395 
396  //IN.putback(c);
397  //read the file name
398  string file;
399  int extcnt = 1;
400  while(IN.get(c) && IN.good() && !IN.eof() && !isspace(c)){
401  if(c=='#'){ // check if a comment is included
402  IN.putback(c); // comment
403  if(!removeSpaceAndComment(IN, l_count)) break;
404  }
405  file += c;
406  }
407  if(c==EOL || isspace(c)) IN.putback(c);
408 
409  //open the external file and read the parameter argument from it
410  //put the argument in a dummy string, this is only as a check
411 // ifstream EXT(file.c_str());
412 // string dummy;
413 
414 // if(!EXT) fatal("External parameter file '%s' could not be found!\n", file.c_str());
415 
416 // while(readArguments(EXT, extcnt, dummy));
417 
418 // EXT.close();
419 
420  //put the file name back into the arg, will be processed later
421  args = "&" + file;
422 
423  } else
424  args += c;
425 
426  }//end while read args
427 
428  if(c == EOL || IN.eof()) return false;
429 
430  if(!IN.good()) //this may happen if an external file doesn't end with a \n
431  fatal("problem reading input file; line %i appears corrupted, skipping line\n",l_count);
432 
433  return true;
434 }
static bool removeSpaceAndComment(istream &IN, int &l_count, bool keepLast=false)
Removes whitespace char on a line until a non-ws or EOL is reached.
Definition: paramsparser.cc:278
void fatal(const char *str,...)
Definition: output.cc:90
#define EOL
Definition: paramsparser.cc:39
static bool removeComment(istream &IN, int &l_count)
Recusively removes comments until the end of a line/of the file, or of a block comment is reached...
Definition: paramsparser.cc:313
static void eatLine(istream &IN, int &l_count)
Definition: paramsparser.cc:488
static string readUntilCharacter(istream &IN, int &l_count, char &start_c, const char end_c)
Definition: paramsparser.cc:438
string StreamParser::readUntilCharacter ( istream &  IN,
int l_count,
char &  start_c,
const char  end_c 
)
static

References EOL, and fatal().

Referenced by ParamsParser::getBlockArgument().

439 {
440  string out;
441  char c;
442  bool closed = false;
443 
444  out += start_c;
445  //cout << "start block : "<<start_c<<endl;
446  while (IN.get(c) && IN.good() && !IN.eof() ) {
447 
448  if(c == EOL) //a block can span several lines
449 
450  ++l_count;
451 
452  else if(c == end_c) {
453 
454  out += c;
455  //remove trailing spaces but keep last one, it's the arg seperator
456  if(!removeSpaceAndComment(IN, l_count, true)) {
457  IN.putback(EOL); //we've reached eol, put it back
458  }
459 
460  closed = true;
461  break;
462 
463  } else if(c == start_c) {//nested blocks
464 
465  out += readUntilCharacter(IN, l_count, start_c, end_c);
466 
467  } else if(c == '\\') //line continuation within a block
468 
469  eatLine(IN, l_count);
470 
471  else if(c == '#') {
472 
473  if(!removeComment(IN, l_count)) l_count++;
474 
475  } else out += c;
476  } //__end_while__
477  //cout<<out<<endl;
478 
479  if(!closed) fatal("missing closing character '%c' on line %i.\n", end_c, l_count);
480 
481  //cout << "close block : "<< c << endl;
482 
483  return out;
484 }
static bool removeSpaceAndComment(istream &IN, int &l_count, bool keepLast=false)
Removes whitespace char on a line until a non-ws or EOL is reached.
Definition: paramsparser.cc:278
void fatal(const char *str,...)
Definition: output.cc:90
#define EOL
Definition: paramsparser.cc:39
static bool removeComment(istream &IN, int &l_count)
Recusively removes comments until the end of a line/of the file, or of a block comment is reached...
Definition: paramsparser.cc:313
static void eatLine(istream &IN, int &l_count)
Definition: paramsparser.cc:488
static string readUntilCharacter(istream &IN, int &l_count, char &start_c, const char end_c)
Definition: paramsparser.cc:438
bool StreamParser::removeComment ( istream &  IN,
int l_count 
)
static

Recusively removes comments until the end of a line/of the file, or of a block comment is reached.

#: commented line (removed until the end of the line is reached) #/ ... /#: a block comment (may span several lines) Consecutive lines of comments are also removed, even if not part of a block comment. Note: this function always returns false, unless something remains on a line after a block comment (i.e. if removeSpaceAndComment() returns true)

References EOL.

314 {
315  char c;
316 
317  //remember: we enter the loop with c = '#'
318  //check if next char is the start of a block comment:
319  bool isBlock = (IN.peek() == '/');
320  bool prevIsComment = true;
321 
322  while(IN.get(c) && IN.good() && !IN.eof()){
323 
324  //break if EOL && next line not a comment, continue otherwise:
325  if (c == EOL){
326  //check if next line is also a comment, start the loop again:
327  if (IN.peek() == '#') {
328 
329  IN.get(c);
330  prevIsComment = true;
331  ++l_count;
332  continue;
333 
334  //continue if within a block comment:
335  } else if( isBlock ) {
336  ++l_count;
337  continue;
338  //next line not a comment, get out of the loop:
339  } else
340  return false;
341  }
342 
343  //check if we had the block str '#/' within a commented line
344  if (c == '/'){
345 
346  if(IN.peek() == '#') {
347  IN.get(c);
348 
349  if(isBlock)
350  //block termination string '/#', remove trailing space
351  //note: the string '#/#' is also considered as a terminating string
352  return removeSpaceAndComment(IN, l_count);
353 
354  } else if(prevIsComment) {
355  //we've got '#/', a block comment may start anywhere on a comment line
356  isBlock = true;
357  }
358  }
359 
360  if(c == '#') prevIsComment = true;
361  else prevIsComment = false;
362 
363  } //__END_WHILE__
364 
365  return false;
366 }
static bool removeSpaceAndComment(istream &IN, int &l_count, bool keepLast=false)
Removes whitespace char on a line until a non-ws or EOL is reached.
Definition: paramsparser.cc:278
#define EOL
Definition: paramsparser.cc:39
bool StreamParser::removeSpaceAndComment ( istream &  IN,
int l_count,
bool  keepLast = false 
)
static

Removes whitespace char on a line until a non-ws or EOL is reached.

Returns false if EOL or EOF is reached or true otherwise.

References EOL.

Referenced by ParamsParser::getArguments().

279 {
280  char c;
281 
282  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL) {
283 
284  if(!isspace(c)){
285 
286  if(c=='#') return removeComment(IN, l_count); // recursively remove comment
287 
288  IN.putback(c); //this is a parameter: put the character back
289 
290  if(keepLast) {
291  //get back one char:
292  IN.unget();
293  IN.get(c); //read it again and check if whitespace:
294  if(isspace(c)) IN.putback(c);
295  }
296 
297  return true;
298  }
299  }
300  return false;
301 }
#define EOL
Definition: paramsparser.cc:39
static bool removeComment(istream &IN, int &l_count)
Recusively removes comments until the end of a line/of the file, or of a block comment is reached...
Definition: paramsparser.cc:313
void StreamParser::replaceCR ( string &  stream,
const char  rpl = '\n' 
)
498 {
499  size_t pos = 0;
500 
501  while ( (pos = stream.find_first_of('\r', pos)) != string::npos) {
502  stream[pos] = rpl;
503  }
504 }

The documentation for this class was generated from the following files:

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR