Nemo  2.3.56
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 ()
 
virtual bool read (const char *stream)=0
 Read/parse params & args from a file or a string or an R object. More...
 
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) {}
Provides interface to read input parameters from various sources and parses them.
Definition: paramsparser.h:50

◆ ~StreamParser()

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

Member Function Documentation

◆ eatLine()

void StreamParser::eatLine ( istream &  IN,
int l_count 
)
static
1709{
1710 char c;
1711 while(IN.get(c) && IN.good() && c != EOL && !IN.eof());
1712 ++l_count;
1713}
#define EOL
Definition: paramsparser.cc:48

References EOL.

Referenced by readArguments(), and readUntilCharacter().

+ Here is the caller graph for this function:

◆ read()

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

Implements ParamsParser.

Reimplemented in FileParser, and BinaryFileParser.

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

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

References eatLine(), EOL, fatal(), readUntilCharacter(), removeComment(), and removeSpaceAndComment().

Referenced by Param::getArgumentFromFile(), and read().

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

References eatLine(), EOL, fatal(), readUntilCharacter(), removeComment(), and removeSpaceAndComment().

Referenced by ParamsParser::getBlockArgument(), readArguments(), and readUntilCharacter().

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

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

References EOL, and removeSpaceAndComment().

Referenced by read(), readArguments(), readUntilCharacter(), and removeSpaceAndComment().

+ Here is the caller graph for this function:

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

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

References EOL, and removeComment().

Referenced by ParamsParser::getArguments(), read(), readArguments(), readUntilCharacter(), and removeComment().

+ Here is the caller graph for this function:

◆ replaceCR()

void StreamParser::replaceCR ( string &  stream,
const char  rpl = '\n' 
)
1718{
1719 size_t pos = 0;
1720
1721 while ( (pos = stream.find_first_of('\r', pos)) != string::npos) {
1722 stream[pos] = rpl;
1723 }
1724}

Referenced by read().

+ Here is the caller graph for this function:

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

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR