36 #include <boost/filesystem.hpp> 37 #include <type_traits> 66 void add_key(
const std::string &keyword,
void (Z::*
function)())
68 std::cout <<
"Adding a keyword to the list of possible parameters: " << keyword <<
"..." << std::flush;
71 std::shared_ptr<ParserKey0Arg<Z> > key = std::make_shared<ParserKey0Arg<Z> >();
73 key->set_function(
function);
76 std::cout <<
" done.\n" << std::flush;
80 template<
class Y,
class A>
81 void add_key(
const std::string &keyword,
void (Y::*
function)(A))
83 std::cout <<
"Adding a keyword to the list of possible parameters: " << keyword <<
"..." << std::flush;
86 std::shared_ptr<ParserKey1Arg<Z,A> > key = std::make_shared<ParserKey1Arg<Z,A> >();
88 key->set_function(
function);
91 std::cout <<
" done.\n" << std::flush;
95 template<
class Y,
class A,
class B>
96 void add_key(
const std::string &keyword,
void (Y::*
function)(A, B))
98 std::cout <<
"Adding a keyword to the list of possible parameters: " << keyword <<
"..." << std::flush;
101 std::shared_ptr<ParserKey2Arg<Z,A,B> > key = std::make_shared<ParserKey2Arg<Z,A,B> >();
103 key->set_function(
function);
104 _keys[keyword] = key;
106 std::cout <<
" done.\n" << std::flush;
128 if (this->
is_end(line))
break;
134 std::cout <<
"\nReading the line: \"" << line <<
"\"...\n";
140 std::shared_ptr<ParserKeyBase<Z> > key;
144 key->get_arguments(line);
147 key->call_function();
163 throw std::runtime_error(
"Unable to open registration parameter file:\n\t" +
_filename);
166 std::cout <<
"\nSuccessfully opened the parameter file: " <<
_filename <<
"\n";
170 void get_line(std::ifstream &file, std::string &line)
174 if (!getline(file,line)) {
176 throw std::runtime_error(
"Error reading parameter file. Maybe the end of the file was reached but no \"End\" statement was found.");
180 line.erase(
remove(line.begin(),line.end(),
' '),line.end());
181 line.erase(
remove(line.begin(),line.end(),
'\t'),line.end());
184 line = line.substr(0, line.find(
';'));
192 std::transform(line.begin(), line.end(), line.begin(), ::tolower);
195 if (line.substr(0,3) ==
"end") {
196 std::cout <<
"\nThe end line was reached. Parsing will stop.\n\n";
208 if (line.find(
":=") == -1)
return false;
217 auto index_end = line.find(
":=");
219 std::string keyword = line.substr(0, index_end);
221 std::cout <<
"\tKeyword: " << keyword <<
"\n";
224 return line.substr(0, index_end);
231 typename std::map<std::string, std::shared_ptr<ParserKeyBase<Z> > >::iterator it;
232 it =
_keys.find(keyword);
235 if (it ==
_keys.end()) {
236 throw std::runtime_error(
"Error. Key not found in list of possible keywords.");
240 std::cout <<
"\tMatching key was found.\n";
252 std::map<std::string, std::shared_ptr<ParserKeyBase<Z> > >
_keys;
std::string get_keyword(std::string &line)
Get the keyword from a line.
Definition: Parser.h:214
virtual ~Parser()
Destructor.
Definition: Parser.h:57
void set_object(std::shared_ptr< Z > object)
Set object.
Definition: Parser.h:60
Parser keys for 0, 1 and 2 arguments.
void set_filename(std::string filename)
Set filename.
Definition: Parser.h:63
void add_key(const std::string &keyword, void(Z::*function)())
Add key with 0 arguments.
Definition: Parser.h:66
std::shared_ptr< ParserKeyBase< Z > > get_key_sptr(std::string &keyword)
Get a key from its keyword.
Definition: Parser.h:228
void parse()
Parse.
Definition: Parser.h:110
std::shared_ptr< Z > _object
Object.
Definition: Parser.h:248
std::map< std::string, std::shared_ptr< ParserKeyBase< Z > > > _keys
Map of keys.
Definition: Parser.h:252
bool contains_parameter(std::string &line)
Does the line contain a parameter?
Definition: Parser.h:205
Abstract data container.
Definition: GeometricalInfo.cpp:141
Parser()
Constructor.
Definition: Parser.h:54
void add_key(const std::string &keyword, void(Y::*function)(A, B))
Add key with 2 arguments - we need to template Y in case Z is the derived class and Y is a parent cla...
Definition: Parser.h:96
std::string _filename
Filename.
Definition: Parser.h:250
void get_line(std::ifstream &file, std::string &line)
Get line.
Definition: Parser.h:170
bool is_end(std::string line)
Is the end of the file?
Definition: Parser.h:189
void open_file(std::ifstream &file)
Open file.
Definition: Parser.h:155
void add_key(const std::string &keyword, void(Y::*function)(A))
Add key with 1 argument - we need to template Y in case Z is the derived class and Y is a parent clas...
Definition: Parser.h:81