#include "probe.hpp" #include #include #include #include #include #include namespace { bool horizontal(char value) { return value == ' ' || value == '\t' || value == '\r'; } std::vector tokens(const std::string& line) { std::vector result; std::string current; for (char value : line) { if (horizontal(value)) { if (!current.empty()) { result.push_back(current); current.clear(); } } else { current.push_back(value); } } if (!current.empty()) { result.push_back(current); } return result; } bool integer(const std::string& text, std::int64_t& value) { if (text.empty()) { return false; } std::size_t index = 0; bool negative = false; if (text[index] == '+' || text[index] == '-') { negative = text[index] == '-'; ++index; } if (index == text.size()) { return false; } std::uint64_t magnitude = 0; const std::uint64_t limit = negative ? 9223372036854775808ULL : 9223372036854775807ULL; for (; index < text.size(); ++index) { char digit = text[index]; if (digit < '0' || digit > '9') { return false; } unsigned number = static_cast(digit - '0'); if (magnitude > (limit - number) / 10) { return false; } magnitude = magnitude * 10 + number; } if (negative && magnitude == 9223372036854775808ULL) { value = std::numeric_limits::min(); } else { value = negative ? -static_cast(magnitude) : static_cast(magnitude); } return true; } bool narrow(std::int64_t value) { return value >= std::numeric_limits::min() && value <= std::numeric_limits::max(); } void emit(const std::vector& values) { for (std::size_t i = 0; i < values.size(); ++i) { if (i != 0) { std::cout << ' '; } std::cout << values[i]; } std::cout << '\n'; } void append(std::vector& row, const std::vector& extra) { row.insert(row.end(), extra.begin(), extra.end()); } bool run(probe::Registry& registry, const std::vector& words) { std::vector args; for (std::size_t i = 1; i < words.size(); ++i) { std::int64_t value; if (!integer(words[i], value)) { return false; } args.push_back(value); } const std::string& command = words[0]; if (command == "RESET" && args.size() == 1) { emit({0, registry.reset(args[0]) ? 1 : 0, registry.now()}); return true; } if (command == "ADV" && args.size() == 1) { emit({3, registry.advance(args[0]) ? 1 : 0, registry.now()}); return true; } for (std::int64_t value : args) { if (!narrow(value)) { return false; } } if (command == "SET" && args.size() == 4) { int id = static_cast(args[0]); bool ok = registry.set(id, static_cast(args[1]), static_cast(args[2]), static_cast(args[3])); std::vector row = {1, ok ? 1 : 0}; append(row, probe::snapshot_fields(registry.view(id))); emit(row); return true; } if (command == "ASK" && args.size() == 3) { probe::Query query{static_cast(args[0]), static_cast(args[1]), static_cast(args[2])}; int verdict = registry.ask(query); std::vector row = {2, verdict, query.id, query.strict, query.evidence}; append(row, probe::snapshot_fields(registry.view(query.id))); emit(row); return true; } if (command == "VIEW" && args.size() == 1) { std::vector row = {4}; append(row, probe::snapshot_fields(registry.view(static_cast(args[0])))); emit(row); return true; } if (command == "DROP" && args.size() == 1) { bool ok = registry.drop(static_cast(args[0])); std::vector row = {5, ok ? 1 : 0}; append(row, probe::total_fields(registry.totals())); emit(row); return true; } if (command == "SUMMARY" && args.empty()) { std::vector row = {6, registry.now()}; append(row, probe::total_fields(registry.totals())); emit(row); return true; } return false; } } int main() { probe::Registry registry; std::string line; while (std::getline(std::cin, line)) { auto words = tokens(line); if (words.empty()) { continue; } if (!run(registry, words)) { return 2; } } return std::cin.bad() ? 2 : 0; }