One proposal, I use map to allow easily add more functions
#include <iostream>
#include <fstream>
#include <string>
#include <map>
void fA() {
std::cout << "fA called" << std::endl;
}
void fB(std::string b) {
std::cout << "fB(" << b << ") called" << std::endl;
}
int main() {
std::ifstream file;
file.open("test.txt");
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
size_t p1, p2;
if (((p1 = line.find('(')) == std::string::npos) ||
((p2 = line.find(')', p1+1)) == std::string::npos)) // can check last char is ')' too
std::cout << "invalid line" << line << std::endl;
else {
std::string fn = line.substr(0, p1);
static const std::map<std::string, void (*)()> noarg = { {"FA", fA} };
static const std::map<std::string, void (*)(std::string)> stringarg = { {"FB", fB} };
std::map<std::string, void (*)()>::const_iterator itno = noarg.find(fn);
std::map<std::string, void (*)(std::string)>::const_iterator itstr = stringarg.find(fn);
if ((itno = noarg.find(fn)) != noarg.end())
// may check there is no arg in call
itno->second();
else if ((itstr = stringarg.find(fn)) != stringarg.end()) {
if (((p1 = line.find('"', p1+1)) == std::string::npos) ||
((p2 = line.find('"', p1+1)) == std::string::npos))
std::cout << "no string arg in " << line << std::endl;
else
// may check there is only one arg
itstr->second(line.substr(p1+1, p2 - p1 -1));
}
else
std::cout << "unmanaged function " << fn << std::endl;
}
}
}
file.close();
}
Compilation and execution:
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra f.cc
pi@raspberrypi:/tmp $ cat test.txt
FA()
FB("aze")
FC
FC(
FC()
FB(123)
pi@raspberrypi:/tmp $ ./a.out
fA called
fB(aze) called
invalid lineFC
invalid lineFC(
unmanaged function FC
no string arg in FB(123)