Nemo  2.4.1
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)
 
string expandMacros (const string &arg)
 Public entry point to expand macros in a string, used to expand macros that were preserved by P() once the random seed has been set (see SimRunner::expandPreservedMacros). More...
 
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)
 
string sample (const string &argstr)
 
string sort (const string &argstr)
 
string round (const string &argstr)
 
string ceiling (const string &argstr)
 
string floor (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
149 : ParamsParser(stream) {}
ParamsParser(const char *name)
Definition: paramsparser.cc:56

◆ ~StreamParser()

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

Member Function Documentation

◆ eatLine()

void StreamParser::eatLine ( istream &  IN,
int &  l_count 
)
static
2041 {
2042  char c;
2043  while(IN.get(c) && IN.good() && c != EOL && !IN.eof());
2044  ++l_count;
2045 }
#define EOL
Definition: paramsparser.cc:48

References EOL.

◆ read()

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

Implements ParamsParser.

Reimplemented in BinaryFileParser, and FileParser.

1741  {
1742  int linecnt = 0;
1743  //output string to collect parameter arguments
1744  string args;
1745  //string to store parameter name
1746  string key;
1747 
1749 
1750  //--------------------------------------------------------------------------------
1751  char c = 0, eof = '\0';
1752  //put the char stream into a string
1753  string input_stream(stream);
1754  //add the terminating character to the string:
1755  input_stream += eof;
1756 
1757  //check if LF
1758  if(input_stream.find_first_of('\r') != string::npos) {
1759  //guess if NL is CRLF as in DOS files:
1760  if(input_stream.find_first_of('\n') == input_stream.find_first_of('\r') + 1)
1761  replaceCR(input_stream, ' '); //remove CR
1762  else
1763  replaceCR(input_stream); //replaces CR with LF
1764  }
1765 
1766  //initiate the input stringstream:
1767  istringstream IN(input_stream);
1768 
1769  //--------------------------------------------------------------------------------
1770  //read the file line by line
1771  while(IN.good() && !IN.eof()) {
1772 
1773  linecnt++;
1774 
1775  // remove the forgoing space of the line, returns false if EOL is reached
1776  if(!removeSpaceAndComment(IN, linecnt)) continue;
1777 
1778  //--------------------------------------------------------------------------------
1779  //read the parameter name and record it in 'key':
1780 
1781  key="";
1782  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL && !isspace(c)){
1783  //whitespace is the split char between a parameter and its argument
1784  //this basically does the same as IN>>key, but we have to check for comments:
1785  if(c == '#'){
1786  removeComment(IN, linecnt);
1787  break;
1788  }
1789 
1790  key += c;
1791  } //__end_while_key__
1792 
1793  if(c == EOL || !removeSpaceAndComment(IN, linecnt) ) {
1794  if(c != eof){
1795  if(key.size() != 0) {//boolean param
1796  args = "1";
1797  ParamsParser::add_inputParam(key, args);
1798  }
1799 #ifdef _DEBUG_
1800  message(" line %i: %s (1)\n", linecnt, key.c_str());
1801 #endif
1802  }
1803  continue;
1804  }
1805 
1806 #ifdef _DEBUG_
1807  message(" line %i: %s (", linecnt, key.c_str());
1808 #endif
1809 
1810  //--------------------------------------------------------------------------------
1811  //read the arguments:
1812  args = "";
1813  while( readArguments(IN, linecnt, args) );
1814 
1815  //remove any trailling space on the argument string
1816  int i;
1817  for(i=(int)args.size()-1; i>=0; --i){
1818  if(!isspace(args[i])) break;
1819  }
1820  args[i+1]='\0';
1821 
1822  //--------------------------------------------------------------------------------
1823  // store the key - args pair in the _inputParam table
1824  ParamsParser::add_inputParam(key, args.c_str());
1825 
1826 #ifdef _DEBUG_
1827  message("%s)\n", args.c_str());
1828 #endif
1829  //reset the stream state, is changed by operator >>
1830  IN.clear(ios::goodbit);
1831 
1832  }//__END__WHILE__
1833 
1834  return true;
1835  }
void add_inputParam(string &param, const string &arg)
Definition: paramsparser.h:114
void reset_inputParams()
Definition: paramsparser.h:113
virtual bool readArguments(istream &IN, int &l_count, string &args)
Definition: paramsparser.cc:1934
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:1842
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:1877
void replaceCR(string &stream, const char rpl='\n')
Definition: paramsparser.cc:2049
void message(const char *message,...)
Definition: output.cc:39

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

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

+ Here is the caller graph for this function:

◆ readArguments()

bool StreamParser::readArguments ( istream &  IN,
int &  l_count,
string &  args 
)
virtual
1935 {
1936  char c;
1937 
1938  //read one byte at a time
1939  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL){
1940 
1941  if(c == '\\')
1942  eatLine(IN, l_count);
1943  //block delimiters: the block will be parsed in Param
1944  else if(c == '{')
1945  args += readUntilCharacter(IN, l_count, c, '}');
1946  else if(c == '(')
1947  args += readUntilCharacter(IN, l_count, c, ')');
1948  else if(c == '[')
1949  args += readUntilCharacter(IN, l_count, c, ']');
1950  else if(c == '\"')
1951  args += readUntilCharacter(IN, l_count, c, '\"');
1952  //comment delimiter:
1953  else if(c == '#') {
1954  if(!removeComment(IN, l_count))
1955  return false; // reached end of line
1956 
1957  //argument in external file
1958  } else if (c == '&') {
1959 
1960  //read the file name
1961  string file;
1962 
1963  while(IN.get(c) && IN.good() && !IN.eof() && !isspace(c)){
1964 
1965  if(c=='#'){ // check if a comment is included
1966  IN.putback(c); // comment
1967  if(!removeSpaceAndComment(IN, l_count)) break;
1968  }
1969  file += c;
1970  }
1971 
1972  if(c==EOL || isspace(c)) IN.putback(c);
1973 
1974  //put the file name back into the arg, will be processed later
1975  args += "&" + file + " ";
1976 
1977  } else
1978  args += c;
1979 
1980  }//end while read args
1981 
1982  if(c == EOL || IN.eof()) return false;
1983 
1984  if(!IN.good()) //this may happen if an external file doesn't end with a \n
1985  fatal("problem reading input file; line %i appears corrupted, skipping line\n",l_count);
1986 
1987  return true;
1988 }
static string readUntilCharacter(istream &IN, int &l_count, const char start_c, const char end_c)
Definition: paramsparser.cc:1992
static void eatLine(istream &IN, int &l_count)
Definition: paramsparser.cc:2040
void fatal(const char *str,...)
Definition: output.cc:99

References EOL, and fatal().

Referenced by Param::getArgumentFromFile().

+ Here is the caller graph for this function:

◆ readUntilCharacter()

string StreamParser::readUntilCharacter ( istream &  IN,
int &  l_count,
const char  start_c,
const char  end_c 
)
static
1993 {
1994  string out;
1995  char c;
1996  bool closed = false;
1997 
1998  out += start_c;
1999 
2000  //cout << "start block : "<<start_c<<endl;
2001  while (IN.get(c) && IN.good() && !IN.eof() ) {
2002 
2003  if(c == EOL) //a block can span several lines
2004 
2005  ++l_count;
2006 
2007  else if(c == end_c) {
2008 
2009  out += c;
2010  //remove trailing spaces but keep last one, it's the arg seperator
2011  if(!removeSpaceAndComment(IN, l_count, true)) {
2012  IN.putback(EOL); //we've reached eol, put it back
2013  }
2014 
2015  closed = true;
2016  break;
2017 
2018  } else if(c == start_c) {//nested blocks
2019 
2020  out += readUntilCharacter(IN, l_count, start_c, end_c);
2021 
2022  } else if(c == '\\') {//line continuation within a block
2023 
2024  eatLine(IN, l_count);
2025 
2026  } else if(c == '#') {
2027 
2028  if(!removeComment(IN, l_count)) l_count++;
2029 
2030  } else out += c;
2031  } //__end_while__
2032 
2033  if(!closed) fatal("missing closing character '%c' on line %i.\n", end_c, l_count);
2034 
2035  return out;
2036 }

References EOL, and fatal().

Referenced by ParamsParser::getBlockArgument().

+ Here is the caller graph for this function:

◆ 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)

1878 {
1879  char c;
1880 
1881  //remember: we enter the loop with c = '#'
1882  //check if next char is the start of a block comment:
1883  bool isBlock = (IN.peek() == '/');
1884  bool prevIsComment = true;
1885 
1886  while(IN.get(c) && IN.good() && !IN.eof()){
1887 
1888  //break if EOL && next line not a comment, continue otherwise:
1889  if (c == EOL){
1890  //check if next line is also a comment, start the loop again:
1891  if (IN.peek() == '#') {
1892 
1893  IN.get(c);
1894  prevIsComment = true;
1895  ++l_count;
1896  continue;
1897 
1898  //continue if within a block comment:
1899  } else if( isBlock ) {
1900  ++l_count;
1901  continue;
1902  //next line not a comment, get out of the loop:
1903  } else
1904  return false;
1905  }
1906 
1907  //check if we had the block str '#/' within a commented line
1908  if (c == '/'){
1909 
1910  if(IN.peek() == '#') {
1911  IN.get(c);
1912 
1913  if(isBlock)
1914  //block termination string '/#', remove trailing space
1915  //note: the string '#/#' is also considered as a terminating string
1916  return removeSpaceAndComment(IN, l_count);
1917 
1918  } else if(prevIsComment) {
1919  //we've got '#/', a block comment may start anywhere on a comment line
1920  isBlock = true;
1921  }
1922  }
1923 
1924  if(c == '#') prevIsComment = true;
1925  else prevIsComment = false;
1926 
1927  } //__END_WHILE__
1928 
1929  return false;
1930 }

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.

1843 {
1844  char c;
1845 
1846  while(IN.get(c) && IN.good() && !IN.eof() && c != EOL) {
1847 
1848  if(!isspace(c)){
1849 
1850  if(c=='#') return removeComment(IN, l_count); // recursively remove comment
1851 
1852  IN.putback(c); //this is a parameter: put the character back
1853 
1854  if(keepLast) {
1855  //get back one char:
1856  IN.unget();
1857  IN.get(c); //read it again and check if whitespace:
1858  if(isspace(c)) IN.putback(c);
1859  }
1860 
1861  return true;
1862  }
1863  }
1864  return false;
1865 }

References EOL.

Referenced by ParamsParser::getArguments().

+ Here is the caller graph for this function:

◆ replaceCR()

void StreamParser::replaceCR ( string &  stream,
const char  rpl = '\n' 
)
2050 {
2051  size_t pos = 0;
2052 
2053  while ( (pos = stream.find_first_of('\r', pos)) != string::npos) {
2054  stream[pos] = rpl;
2055  }
2056 }

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

Generated for Nemo v2.4.1 by  doxygen 1.9.1

Catalogued on GSR