This class parses strings to variables. The syntax is compatible with Matlab and Octave. It can be used in several different ways. The following test program and test data file gives several examples:
#ifndef PARSER_TEST_FILE
int main()
{
cerr << "PARSER_TEST_FILE not defined. Test skipped." << endl;
return 1;
}
#else
int main(int argc, char *argv[])
{
int a;
double b;
string c;
ivec d;
vec e;
svec f;
bvec g;
imat h;
mat i;
smat j;
bool l, m;
string n;
cout << "Use the Parser class on a parameter file:" << boolalpha << endl;
p.
init(
string(PARSER_TEST_FILE));
cout << "a = " << a << endl;
cout << "c = " << c << endl;
cout << "d = " << d << endl;
cout << "e = " << e << endl;
cout << "f = " << f << endl;
cout << "g = " << g << endl;
cout << "h = " << h << endl;
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;
cout << "l = " << l << endl;
cout << "m = " << m << endl;
cout << "n = " << n << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on the command line input:" << endl;
cout << "Example: > ./parser_test.run \"a=3; b=-1.2; d=[0 1 2 45];\"" << endl << endl;
cout << "Use the following code:" << endl;
cout << " p.init(argc,argv);" << endl;
cout << " a = p.get_int(\"a\"); cout << \"a = \" << a << endl;" << endl;
cout << " b = p.get_double(\"b\"); cout << \"b = \" << b << endl;" << endl;
cout << " d = p.get_ivec(\"d\"); cout << \"d = \" << d << endl;" << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on a char pointer (usually the command line input):" << endl;
char argv1_0[] = "a=345";
char argv1_1[] = "b=1.001";
char argv1_2[] = "c=\"Hello Bird\"";
char argv1_3[] = "d=[1,2,3,-1,-2,-3]";
int argc1 = 4;
char *argv1[4] = { argv1_0, argv1_1, argv1_2, argv1_3 };
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on a parameter file and a char pointer:" << endl;
char argv2_0[] = "a=345";
char argv2_1[] = "d=[1,-2,3,-4,5,-6,7,-8]";
int argc2 = 2;
char *argv2[2] = { argv2_0, argv2_1 };
if (argc == 0) {
p.
init(
string(PARSER_TEST_FILE), argc2, argv2);
}
else {
p.
init(
string(PARSER_TEST_FILE), argc, argv);
}
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "d = " << d << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser class on an Array of strings:" << endl;
parser_data(0) = "a=-11";
parser_data(1) = "b=3.14";
parser_data(2) = "c=\"Hello Nerd\"";
parser_data(3) = "d=[0,1,2,0,3,4,7]";
cout << "------------------------------------------------------------" << endl;
cout << "Use the Parser::get() method on a parameter file:" << endl;
p.
init(
string(PARSER_TEST_FILE));
bool r = true;
int s = 7;
double t = 3.14;
std::string u = "test string";
cout << "------------------------------------------------------------" << endl;
cout << "Check if variables exist in the Parser:" << endl;
cout <<
"p.exist(\"a\") = " << p.
exist(
"a") << endl;
cout <<
"p.exist(\"aa\") = " << p.
exist(
"aa") << endl;
cout << "------------------------------------------------------------" << endl;
return 0;
}
#endif
void set_array(Array< T > &a, const char *values)
Assign a C-style string to an Array<T>. T must have istream operator>> defined.
double get_double(const std::string &name, int num=-1)
Interpret variable name as a double.
svec get_svec(const std::string &name, int num=-1)
Interpret variable name as a svec.
mat get_mat(const std::string &name, int num=-1)
Interpret variable name as a mat.
int get_int(const std::string &name, int num=-1)
Interpret variable name as an integer.
ivec get_ivec(const std::string &name, int num=-1)
Interpret variable name as a ivec.
bvec get_bvec(const std::string &name, int num=-1)
Interpret variable name as a bvec.
imat get_imat(const std::string &name, int num=-1)
Interpret variable name as a imat.
bool get_bool(const std::string &name, int num=-1)
Interpret variable name as a bool.
void init(const std::string &filename)
Initialization function. Sets input file name.
smat get_smat(const std::string &name, int num=-1)
Interpret variable name as a smat.
std::string get_string(const std::string &name, int num=-1)
Interpret variable name as a string.
bool get(T &var, const std::string &name, int num=-1)
Get variable value if name can be found (and return true), otherwise keep old value (and return false...
bool exist(const std::string &name)
Check is name exists in the file. Returns true if the name is found and false otherwise.
vec get_vec(const std::string &name, int num=-1)
Interpret variable name as a vec.
bmat get_bmat(const std::string &name, int num=-1)
Interpret variable name as a bmat.
Include file for the IT++ base module.
Mat< bin > bmat
bin matrix
%---------------------------------------------------------------------
% Test data file for the parser_test.cpp program
%---------------------------------------------------------------------
% Lines that begins with % are ignored. Expressions that ends with ';'
% are not printed while parsing.
a = 4; %Comments may be put at the end of each line
% You can give several values for the same parameter name. Each
% alternative value must be on a new row. Select the value you
% want with the num parameter in the get_double member function.
% Use:
% Parser p("filename.txt");
% double b = p.get_double("b",1);
% to select the value 2.36 below.
b = 2.35
2.36
2.37
% Several expressions may be put on the same row (separated by , or ;)
% c is a string, d is an integer vector. Spaces and/or commas separate
% the vector elements.
c = "Hello World"; d =[1,2,3 4,3 2, -1];
% This a vector.
e=[1.2,2,3.33 4.01,3.2 2, -1.2];
% This is a short vector.
f=[1,2,3 4,3 2, -1];
% This is a binary vector.
g=[0 1 0 0 1];
% This an integer matrix. Spaces and/or commas separate the colums. Semicolons separate rows.
h=[1 2 3;4 5 6];
% Expressions can continue over several rows by inserting '...' at the end of the rows
% (as in Matlab). This is a matrix.
i=[...
1.0, 2 ; ...
-3 4.2 ...
] ;
% This is a short matrix.
j=[1 -2 -3;4 -5 6];
% This is a binary matrix.
k=[0 1 0 1;1 0 1 0;0 0 0 0;1 1 1 1];
% These are boolean variables:
l= 0;
m= true;
% This is a string with single instead of double quotes (For Matlab compatibility)
n='Hello World';
% This is an Array<Array<cvec> >
% In Matlab, the elements defined below are accessible as
% o{1}{1}, o{1}{2} and o{2}{1}; note the curly brackets.
% For complex numbers, both 1+2i and (1,2) are allowed formats,
% but only the first one is Matlab compatible.
o = {{[1+2i 3+4i] [5+6i]} {[7 8 9]}}
Beside the type-specific get methods, like get_int(), there is a templated get method called get(), that can handle all variable types that have an istream operator defined, e.g. Arrays. The get() method takes the variable value as an input/output parameter. If the variable name cannot be found, the old value is kept. Here is an example: