You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.9 KiB
C++
88 lines
1.9 KiB
C++
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
void callback(char command, char arg, int parsed_number) {
|
|
cout << "command: " << command << " | ";
|
|
cout << "arg: " << arg << " | ";
|
|
cout << "parsed_number: " << parsed_number << " | ";
|
|
cout << endl;
|
|
}
|
|
|
|
int parse_string(char input[]) {
|
|
char current_char = input[0];
|
|
char command = ' ';
|
|
char arg = ' ';
|
|
char current_number_string[4] = {' ', ' ', ' ', '\0'};
|
|
|
|
int line_position = 0;
|
|
int total_position = 0;
|
|
int current_number_len = 0;
|
|
int parsed_number = 0;
|
|
|
|
while (current_char != '\0') {
|
|
if (line_position == 0) {
|
|
switch (current_char) {
|
|
case 'f':
|
|
case 't':
|
|
case 'b':
|
|
command = current_char;
|
|
break;
|
|
|
|
default:
|
|
cout << "invalid command. " << endl;
|
|
return 1;
|
|
}
|
|
} else {
|
|
switch (current_char) {
|
|
case '0' ... '9':
|
|
if (current_number_len < 4) {
|
|
current_number_len++;
|
|
current_number_string[current_number_len - 1] = current_char;
|
|
}
|
|
break;
|
|
|
|
case 'r':
|
|
case 'g':
|
|
case 'b':
|
|
case 't':
|
|
arg = current_char;
|
|
break;
|
|
|
|
case ' ':
|
|
if (current_number_len > 0) parsed_number = stoi(current_number_string);
|
|
if (arg != ' ') callback(command, arg, parsed_number);
|
|
|
|
arg = ' ';
|
|
current_number_len = 0;
|
|
fill(begin(current_number_string), end(current_number_string), ' ');
|
|
current_number_string[3] = '\0';
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
if (current_number_len > 0) parsed_number = stoi(current_number_string);
|
|
callback(command, arg, parsed_number);
|
|
|
|
command = arg = ' ';
|
|
line_position = parsed_number = current_number_len = 0;
|
|
fill(begin(current_number_string), end(current_number_string), ' ');
|
|
current_number_string[3] = '\0';
|
|
|
|
break;
|
|
|
|
default:
|
|
cout << "invalid syntax." << endl;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (current_char != '\n') line_position++;
|
|
|
|
total_position++;
|
|
current_char = input[total_position];
|
|
}
|
|
|
|
line_position = 0;
|
|
return 0;
|
|
} |