#include "activation.hpp" #include #include #include #include #include #include namespace { using activation::Command; using activation::Op; bool integer(const std::string& text, int& value) { if (text.empty()) { return false; } std::size_t cursor = 0; if (text[0] == '-' || text[0] == '+') { cursor = 1; } if (cursor == text.size()) { return false; } for (std::size_t index = cursor; index < text.size(); ++index) { if (text[index] < '0' || text[index] > '9') { return false; } } try { const long long parsed = std::stoll(text); if (parsed < std::numeric_limits::min() || parsed > std::numeric_limits::max()) { return false; } value = static_cast(parsed); return true; } catch (...) { return false; } } Command parse(const std::vector& words) { Command command; struct Entry { const char* word; Op op; int arguments; }; const std::array entries = {{ {"open", Op::open, 2}, {"close", Op::close, 1}, {"reserve", Op::reserve, 2}, {"gate", Op::gate, 2}, {"ungate", Op::ungate, 2}, {"release", Op::release, 2}, {"activate", Op::activate, 2}, {"deactivate", Op::deactivate, 2}, {"arm-p", Op::arm_p, 2}, {"arm-e", Op::arm_e, 2}, {"clear", Op::clear, 1} }}; for (const Entry& entry : entries) { if (words.front() != entry.word) { continue; } if (words.size() != static_cast(entry.arguments + 1)) { return command; } int first = 0; int second = 0; if (!integer(words[1], first)) { return command; } if (entry.arguments == 2 && !integer(words[2], second)) { return command; } command.op = entry.op; command.a = first; command.b = second; return command; } return command; } void print(const activation::Observation& observation) { std::cout << "{\"rc\":" << observation.rc; std::cout << ",\"token\":" << observation.token; std::cout << ",\"providers\":["; bool comma = false; for (const auto& view : observation.providers) { if (comma) { std::cout << ','; } comma = true; std::cout << '[' << view.handles << ',' << view.prepared << ','; std::cout << view.enabled << ',' << view.prepare_on << ','; std::cout << view.prepare_off << ',' << view.enable_on << ','; std::cout << view.enable_off << ',' << view.prepare_calls << ','; std::cout << view.enable_calls << ',' << view.pending_prepare << ','; std::cout << view.pending_enable << ']'; } std::cout << "],\"handles\":["; comma = false; for (const auto& view : observation.handles) { if (comma) { std::cout << ','; } comma = true; std::cout << '[' << view.token << ',' << view.client << ','; std::cout << view.provider << ',' << view.prepared << ','; std::cout << view.enabled << ']'; } std::cout << "],\"events\":["; comma = false; for (const auto& event : observation.events) { if (comma) { std::cout << ','; } comma = true; std::cout << '[' << event.kind << ',' << event.provider << ','; std::cout << event.token << ']'; } std::cout << "]}\n"; } } int main() { activation::Engine engine; std::string line; while (std::getline(std::cin, line)) { std::istringstream stream(line); std::vector words; std::string word; while (stream >> word) { words.push_back(word); } if (words.empty()) { continue; } engine.execute(parse(words)); print(engine.observe()); } return 0; }