#include "model.hpp" #include #include #include #include #include namespace { using lineirq::Controller; using lineirq::Counters; using lineirq::Reply; using lineirq::Snapshot; using lineirq::View; struct Command { std::string verb; std::vector numbers; bool valid = false; }; bool separator(char c) { return c == ' ' || c == '\t' || c == '\r'; } std::vector split(const std::string& input) { std::vector words; std::size_t cursor = 0; while (cursor < input.size()) { while (cursor < input.size() && separator(input[cursor])) { ++cursor; } if (cursor == input.size()) { break; } const std::size_t start = cursor; while (cursor < input.size() && !separator(input[cursor])) { ++cursor; } words.push_back(input.substr(start, cursor - start)); } return words; } bool integer(const std::string& word, long long& output) { if (word.empty()) { return false; } std::size_t cursor = 0; bool negative = false; if (word[cursor] == '+' || word[cursor] == '-') { negative = word[cursor] == '-'; ++cursor; } if (cursor == word.size()) { return false; } const unsigned long long positive = static_cast(std::numeric_limits::max()); const unsigned long long limit = negative ? positive + 1ULL : positive; unsigned long long value = 0; for (; cursor < word.size(); ++cursor) { const unsigned char character = static_cast(word[cursor]); if (character < '0' || character > '9') { return false; } const unsigned digit = character - '0'; if (value > (limit - digit) / 10ULL) { return false; } value = value * 10ULL + digit; } if (negative && value == positive + 1ULL) { output = std::numeric_limits::min(); } else if (negative) { output = -static_cast(value); } else { output = static_cast(value); } return true; } int arguments(const std::string& verb) { if (verb == "reset") { return 1; } if (verb == "install") { return 4; } if (verb == "remove") { return 2; } if (verb == "disable") { return 2; } if (verb == "enable") { return 2; } if (verb == "pulse") { return 3; } if (verb == "level") { return 3; } if (verb == "countset") { return 3; } if (verb == "enter") { return 1; } if (verb == "finish") { return 2; } if (verb == "inspect") { return 2; } if (verb == "count") { return 2; } if (verb == "ready") { return 2; } if (verb == "totals") { return 1; } if (verb == "snapshot") { return 1; } return -1; } Command parse(const std::string& input) { Command command; const std::vector words = split(input); if (words.empty()) { return command; } command.verb = words[0]; const int arity = arguments(command.verb); if (arity < 0) { return command; } if (words.size() != static_cast(arity + 1)) { return command; } for (std::size_t index = 1; index < words.size(); ++index) { long long number = 0; if (!integer(words[index], number)) { return command; } command.numbers.push_back(number); } if (command.numbers[0] < 0 || command.numbers[0] > 1) { return command; } command.valid = true; return command; } void print_view(const View& line) { std::cout << ' ' << line.live; std::cout << ' ' << line.mode; std::cout << ' ' << line.priority; std::cout << ' ' << line.depth; std::cout << ' ' << line.pending; std::cout << ' ' << line.active; std::cout << ' ' << line.asserted; std::cout << ' ' << line.received; std::cout << ' ' << line.handled; std::cout << ' ' << line.entries; } void print_counters(const Counters& counters) { for (std::uint64_t value : counters) { std::cout << ' ' << value; } } void print_snapshot(char tag, int bank, const Snapshot& snapshot) { std::cout << tag << ' ' << bank; print_counters(snapshot.counters); for (const View& line : snapshot.lines) { print_view(line); } std::cout << '\n'; } Reply apply(Controller& controller, const Command& command, View& view) { const std::string& verb = command.verb; const std::vector& numbers = command.numbers; if (verb == "reset") { return controller.reset(); } if (verb == "install") { return controller.install(numbers[1], numbers[2], numbers[3]); } if (verb == "remove") { return controller.remove(numbers[1]); } if (verb == "disable") { return controller.disable(numbers[1]); } if (verb == "enable") { return controller.enable(numbers[1]); } if (verb == "pulse") { return controller.pulse(numbers[1], numbers[2]); } if (verb == "level") { return controller.level(numbers[1], numbers[2]); } if (verb == "countset") { return controller.countset(numbers[1], numbers[2]); } if (verb == "enter") { return controller.enter(); } if (verb == "finish") { return controller.finish(numbers[1]); } if (verb == "inspect") { return controller.inspect(numbers[1], view); } if (verb == "count") { return controller.count(numbers[1]); } if (verb == "ready") { return controller.ready(numbers[1]); } if (verb == "totals" || verb == "snapshot") { return Reply{}; } return Reply{-8, 0}; } void query(Controller& controller, const Command& command, const View& view) { if (command.verb == "inspect") { std::cout << 'V'; print_view(view); std::cout << '\n'; } if (command.verb == "totals") { std::cout << 'T'; print_counters(controller.totals()); std::cout << '\n'; } if (command.verb == "snapshot") { print_snapshot('Q', static_cast(command.numbers[0]), controller.snapshot()); } } } int main() { std::array controllers; std::string input; while (std::getline(std::cin, input)) { const Command command = parse(input); Reply reply{-8, 0}; View view; if (command.valid) { Controller& controller = controllers[command.numbers[0]]; reply = apply(controller, command, view); } std::cout << "R " << 0 << ' ' << reply.value << '\n'; if (command.valid) { Controller& controller = controllers[command.numbers[0]]; query(controller, command, view); } for (int bank = 0; bank < 2; ++bank) { print_snapshot('S', bank, controllers[bank].snapshot()); } } return 0; }