1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
std::string& trim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
s.erase(s.find_last_not_of(t) + 1);
return s;
}
bool error(size_t number, const std::string& messge)
{
std::cout << "Error on line " << number;
std::cout << ": " << message << '\n';
return false; // always return error
}
bool command(const std::string& line, size_t number)
{
std::istringstream iss(line);
std::string cmd;
if(!(iss >> cmd))
return error(number, "Command expected.");
if(cmd == "RUN") return run();
else if(cmd == "SEND") return send();
else if(cmd == "WAIT")
{
int time;
if(!(iss >> time))
return error(number, "Command WAIT expected parameter: WAIT n");
return wait(time);
}
else if(cmd == "SPD")
{
double value;
if(!(iss >> value))
return error(number, "Command SPD expected parameter: SPD n");
return spd(value);
}
else if(cmd == "POINT")
{
double x, y;
if(!(iss >> x >> y))
return error(number, "Command POINT expected parameters: POINT x y");
return point(x, y);
}
return error(number, "Unknown command.");
}
int main()
{
std::ifstream ifs("config.txt");
size_t number;
std::string line;
while(std::getline(ifs, line))
{
++number;
if(!trim(line).empty() && line[0] == '$')
command(line.substr(1), number);
}
}
|