Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
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...
 
string replaceMacro (const string &arg)
 Macros. More...
 
string parseMacroFunctionBlock (const string &in_arg)
 
string callMacro (const string &name, const string &argstr)
 
vector< string > getMacroArgs (const string &args, const int min_arg, const size_t max_arg, const string macro_name, const string syntax, bool lastArgIsSeparatorChar=true)
 
string getMacroSepParamChar (const string &sep_in, const string macro_name)
 
string getMacroParamValue (const string &str_in, const string &par_name, const string &macro_name)
 
string quote (const string &argstr)
 Macro "q" returns a quoted string. More...
 
string concat (const string &argstr)
 Macro "c" returns a character-delimited string of atomic arguments. More...
 
string rep (const string &argstr)
 
string seq (const string &argstr)
 
string tempseq (const string &argstr)
 
string matrix (const string &argstr)
 
string diag_matrix (const string &argstr)
 
string sym_matrix (const string &argstr)
 
string runif (const string &argstr)
 
string rnorm (const string &argstr)
 
string rpoiss (const string &argstr)
 
string rbernoul (const string &argstr)
 
string rgamma (const string &argstr)
 
string rlognorm (const string &argstr)
 
string rexp (const string &argstr)
 

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, const 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::StreamParser ( const char *  stream)
inline
123 : ParamsParser(stream) {}
ParamsParser(const char *name)
Definition: paramsparser.cc:57

◆ ~StreamParser()

virtual StreamParser::~StreamParser ( )
inlinevirtual
124 {}

Member Function Documentation

◆ eatLine()

void StreamParser::eatLine ( istream &  IN,
int &  l_count 
)
static
1717 {
1718  char c;
1719  while(IN.get(c) && IN.good() && c != EOL && !IN.eof());
1720  ++l_count;
1721 }
#define EOL
Definition: paramsparser.cc:49

References EOL.

◆ read()

bool StreamParser::read ( const char *  stream)
virtual

Implements ParamsParser.

Reimplemented in BinaryFileParser, and FileParser.

1417  {
1418  int linecnt = 0;
1419  //output string to collect parameter arguments
1420  string args;
1421  //string to store parameter name
1422  string key;
1423 
1425 
1426  //--------------------------------------------------------------------------------
1427  char c = 0, eof = '\0';
1428  //put the char stream into a string
1429  string input_stream(stream);
1430  //add the terminating character to the string:
1431  input_stream += eof;
1432 
1433  //check if LF
1434  if(input_stream.find_first_of('\r') != string::npos) {
1435  //guess if NL is CRLF as in DOS files:
1436  if(input_stream.find_first_of('\n') == input_stream.find_first_of('\r') + 1)
1437  replaceCR(input_stream, ' '); //remove CR
1438  else
1439  replaceCR(input_stream); //replaces CR with LF
1440  }
1441 
1442  //initiate the input stringstream:
1443  istringstream IN(input_stream);
1444 
1445  //--------------------------------------------------------------------------------
1446  //read the file line by line
1447  while(IN.good() && !IN.eof()) {
1448 
1449  linecnt++;
1450 
1451  // remove the forgoing space of the line, returns false if EOL is reached
1452  if(!removeSpaceAndComment(IN, linecnt)) continue;
1453 
1454  //--------------------------------------------------------------------------------
1455  //read the parameter name and record it in 'key':
1456 
1457  key="";
1458  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL && !isspace(c)){
1459  //whitespace is the split char between a parameter and its argument
1460  //this basically does the same as IN>>key, but we have to check for comments:
1461  if(c == '#'){
1462  removeComment(IN, linecnt);
1463  break;
1464  }
1465 
1466  key += c;
1467  } //__end_while_key__
1468 
1469  if(c == EOL || !removeSpaceAndComment(IN, linecnt) ) {
1470  if(c != eof){
1471  if(key.size() != 0) {//boolean param
1472  args = "1";
1473  ParamsParser::add_inputParam(key, args);
1474  }
1475 #ifdef _DEBUG_
1476  message(" line %i: %s (1)\n", linecnt, key.c_str());
1477 #endif
1478  }
1479  continue;
1480  }
1481 
1482 #ifdef _DEBUG_
1483  message(" line %i: %s (", linecnt, key.c_str());
1484 #endif
1485 
1486  //--------------------------------------------------------------------------------
1487  //read the arguments:
1488  args = "";
1489  while( readArguments(IN, linecnt, args) );
1490 
1491  //remove any trailling space on the argument string
1492  int i;
1493  for(i=(int)args.size()-1; i>=0; --i){
1494  if(!isspace(args[i])) break;
1495  }
1496  args[i+1]='\0';
1497 
1498  //--------------------------------------------------------------------------------
1499  // store the key - args pair in the _inputParam table
1500  ParamsParser::add_inputParam(key, args.c_str());
1501 
1502 #ifdef _DEBUG_
1503  message("%s)\n", args.c_str());
1504 #endif
1505  //reset the stream state, is changed by operator >>
1506  IN.clear(ios::goodbit);
1507 
1508  }//__END__WHILE__
1509 
1510  return true;
1511  }
void add_inputParam(string &param, const string &arg)
Definition: paramsparser.h:102
void reset_inputParams()
Definition: paramsparser.h:101
virtual bool readArguments(istream &IN, int &l_count, string &args)
Definition: paramsparser.cc:1610
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:1518
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:1553
void replaceCR(string &stream, const char rpl='\n')
Definition: paramsparser.cc:1725
void message(const char *message,...)
Definition: output.cc:40

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

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

◆ readArguments()

bool StreamParser::readArguments ( istream &  IN,
int &  l_count,
string &  args 
)
virtual
1611 {
1612  char c;
1613 
1614  //read one byte at a time
1615  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL){
1616 
1617  if(c == '\\')
1618  eatLine(IN, l_count);
1619  //block delimiters: the block will be parsed in Param
1620  else if(c == '{')
1621  args += readUntilCharacter(IN, l_count, c, '}');
1622  else if(c == '(')
1623  args += readUntilCharacter(IN, l_count, c, ')');
1624  else if(c == '[')
1625  args += readUntilCharacter(IN, l_count, c, ']');
1626  else if(c == '\"')
1627  args += readUntilCharacter(IN, l_count, c, '\"');
1628  //comment delimiter:
1629  else if(c == '#') {
1630  if(!removeComment(IN, l_count))
1631  return false; // reached end of line
1632 
1633  //argument in external file
1634  } else if (c == '&') {
1635 
1636  //read the file name
1637  string file;
1638 
1639  while(IN.get(c) && IN.good() && !IN.eof() && !isspace(c)){
1640 
1641  if(c=='#'){ // check if a comment is included
1642  IN.putback(c); // comment
1643  if(!removeSpaceAndComment(IN, l_count)) break;
1644  }
1645  file += c;
1646  }
1647 
1648  if(c==EOL || isspace(c)) IN.putback(c);
1649 
1650  //put the file name back into the arg, will be processed later
1651  args += "&" + file + " ";
1652 
1653  } else
1654  args += c;
1655 
1656  }//end while read args
1657 
1658  if(c == EOL || IN.eof()) return false;
1659 
1660  if(!IN.good()) //this may happen if an external file doesn't end with a \n
1661  fatal("problem reading input file; line %i appears corrupted, skipping line\n",l_count);
1662 
1663  return true;
1664 }
static string readUntilCharacter(istream &IN, int &l_count, const char start_c, const char end_c)
Definition: paramsparser.cc:1668
static void eatLine(istream &IN, int &l_count)
Definition: paramsparser.cc:1716
void fatal(const char *str,...)
Definition: output.cc:96

References EOL, and fatal().

Referenced by Param::getArgumentFromFile().

◆ readUntilCharacter()

string StreamParser::readUntilCharacter ( istream &  IN,
int &  l_count,
const char  start_c,
const char  end_c 
)
static
1669 {
1670  string out;
1671  char c;
1672  bool closed = false;
1673 
1674  out += start_c;
1675 
1676  //cout << "start block : "<<start_c<<endl;
1677  while (IN.get(c) && IN.good() && !IN.eof() ) {
1678 
1679  if(c == EOL) //a block can span several lines
1680 
1681  ++l_count;
1682 
1683  else if(c == end_c) {
1684 
1685  out += c;
1686  //remove trailing spaces but keep last one, it's the arg seperator
1687  if(!removeSpaceAndComment(IN, l_count, true)) {
1688  IN.putback(EOL); //we've reached eol, put it back
1689  }
1690 
1691  closed = true;
1692  break;
1693 
1694  } else if(c == start_c) {//nested blocks
1695 
1696  out += readUntilCharacter(IN, l_count, start_c, end_c);
1697 
1698  } else if(c == '\\') {//line continuation within a block
1699 
1700  eatLine(IN, l_count);
1701 
1702  } else if(c == '#') {
1703 
1704  if(!removeComment(IN, l_count)) l_count++;
1705 
1706  } else out += c;
1707  } //__end_while__
1708 
1709  if(!closed) fatal("missing closing character '%c' on line %i.\n", end_c, l_count);
1710 
1711  return out;
1712 }

References EOL, and fatal().

Referenced by ParamsParser::getBlockArgument().

◆ removeComment()

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)

1554 {
1555  char c;
1556 
1557  //remember: we enter the loop with c = '#'
1558  //check if next char is the start of a block comment:
1559  bool isBlock = (IN.peek() == '/');
1560  bool prevIsComment = true;
1561 
1562  while(IN.get(c) && IN.good() && !IN.eof()){
1563 
1564  //break if EOL && next line not a comment, continue otherwise:
1565  if (c == EOL){
1566  //check if next line is also a comment, start the loop again:
1567  if (IN.peek() == '#') {
1568 
1569  IN.get(c);
1570  prevIsComment = true;
1571  ++l_count;
1572  continue;
1573 
1574  //continue if within a block comment:
1575  } else if( isBlock ) {
1576  ++l_count;
1577  continue;
1578  //next line not a comment, get out of the loop:
1579  } else
1580  return false;
1581  }
1582 
1583  //check if we had the block str '#/' within a commented line
1584  if (c == '/'){
1585 
1586  if(IN.peek() == '#') {
1587  IN.get(c);
1588 
1589  if(isBlock)
1590  //block termination string '/#', remove trailing space
1591  //note: the string '#/#' is also considered as a terminating string
1592  return removeSpaceAndComment(IN, l_count);
1593 
1594  } else if(prevIsComment) {
1595  //we've got '#/', a block comment may start anywhere on a comment line
1596  isBlock = true;
1597  }
1598  }
1599 
1600  if(c == '#') prevIsComment = true;
1601  else prevIsComment = false;
1602 
1603  } //__END_WHILE__
1604 
1605  return false;
1606 }

References EOL.

◆ removeSpaceAndComment()

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.

1519 {
1520  char c;
1521 
1522  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL) {
1523 
1524  if(!isspace(c)){
1525 
1526  if(c=='#') return removeComment(IN, l_count); // recursively remove comment
1527 
1528  IN.putback(c); //this is a parameter: put the character back
1529 
1530  if(keepLast) {
1531  //get back one char:
1532  IN.unget();
1533  IN.get(c); //read it again and check if whitespace:
1534  if(isspace(c)) IN.putback(c);
1535  }
1536 
1537  return true;
1538  }
1539  }
1540  return false;
1541 }

References EOL.

Referenced by ParamsParser::getArguments().

◆ replaceCR()

void StreamParser::replaceCR ( string &  stream,
const char  rpl = '\n' 
)
1726 {
1727  size_t pos = 0;
1728 
1729  while ( (pos = stream.find_first_of('\r', pos)) != string::npos) {
1730  stream[pos] = rpl;
1731  }
1732 }

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

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR