#include "latch.hpp" #include #include #include #include namespace { using release::Count; using release::Id; std::vector fields(const std::string &line) { std::vector result; std::size_t start = 0; for (;;) { std::size_t end = line.find('\t', start); result.push_back(end == std::string::npos ? line.substr(start) : line.substr(start, end-start)); if (end == std::string::npos) return result; start = end + 1; } } bool number(const std::string &text, Count &value) { if (text.empty()) return false; for (unsigned char byte : text) if (byte < '0' || byte > '9') return false; Count result = 0; Count limit = std::numeric_limits::max(); for (unsigned char byte : text) { unsigned digit = byte - '0'; if (result > limit / 10 || (result == limit / 10 && digit > limit % 10)) return false; result = result * 10 + digit; } value = result; return true; } int nibble(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1; } bool unhex(const std::string &text, std::string &out) { out.clear(); if (text == "-") return true; if (text.empty() || text.size() % 2) return false; for (std::size_t i = 0; i < text.size(); i += 2) { int a = nibble(text[i]), b = nibble(text[i+1]); if (a < 0 || b < 0) return false; out.push_back(static_cast(a * 16 + b)); } return true; } std::string hex(const std::string &value) { if (value.empty()) return "-"; const char *digits = "0123456789abcdef"; std::string out; for (unsigned char byte : value) { out.push_back(digits[byte >> 4]); out.push_back(digits[byte & 15]); } return out; } void raw_snapshot(const release::Snapshot &v) { std::cout << v.target << ' ' << v.remaining << ' ' << v.open << ' ' << v.revision << ' ' << v.broadcasts << ' ' << v.participants << ' ' << v.completed << ' ' << v.waiting << ' ' << v.released << ' ' << v.canceled << ' ' << v.queued << ' ' << v.next_ticket; } } int main() { release::Latch objects[2]; unsigned selected = 0; std::string line; while (std::getline(std::cin, line)) { if (std::cin.eof()) return 2; if (line.find('\0') != std::string::npos) return 2; auto args = fields(line); const auto &op = args[0]; release::Latch &latch = objects[selected]; Count a = 0, b = 0, c = 0; std::string x, y, z; if (op == "U" && args.size() == 2 && number(args[1], a) && a < 2) { selected = static_cast(a); std::cout << "U " << selected << '\n'; } else if ((op == "R" || op == "C") && args.size() == 2 && number(args[1], a)) { std::cout << op << ' ' << (op == "R" ? latch.reset(a) : latch.count_down(a)) << '\n'; } else if ((op == "J" || op == "D" || op == "W" || op == "X" || op == "P" || op == "A") && args.size() == 2 && number(args[1], a) && a <= UINT32_MAX) { Id id = static_cast(a); int result = op == "J" ? latch.join(id) : op == "D" ? latch.complete(id) : op == "W" ? latch.await(id) : op == "X" ? latch.cancel(id) : op == "P" ? latch.poll(id) : latch.participant(id); std::cout << op << ' ' << result << '\n'; } else if (op == "E" && args.size() == 4 && number(args[1], a) && a <= 2 && number(args[2], b) && b <= UINT32_MAX && number(args[3], c)) { release::WaitInfo info{static_cast(a), c}; int result = latch.inspect(static_cast(b), info); std::cout << "E " << result << ' ' << static_cast(info.state) << ' ' << info.ticket << '\n'; } else if (op == "T" && args.size() == 4 && number(args[1], a) && a <= 2 && number(args[2], b) && unhex(args[3], x)) { release::WaitInfo info{static_cast(a), b}; int result = release::parse_waiter(x, info); std::cout << "T " << result << ' ' << static_cast(info.state) << ' ' << info.ticket << '\n'; } else if (op == "V" && args.size() == 3 && number(args[1], a) && a <= UINT32_MAX && number(args[2], b)) { release::WaitInfo info{release::WaitState::canceled, b}; int result = latch.inspect(static_cast(a), info); std::cout << "V " << result << ' ' << static_cast(info.state) << ' ' << info.ticket << '\n'; } else if (op == "N" && args.size() == 2 && number(args[1], a) && a <= UINT32_MAX) { Id id = static_cast(a); int result = latch.take(id); std::cout << "N " << result << ' ' << id << '\n'; } else if (op == "Q" && args.size() == 1) { auto value = latch.snapshot(); std::cout << "Q "; raw_snapshot(value); std::cout << ' ' << hex(release::format_snapshot(value)) << '\n'; } else if (op == "I" && args.size() == 4 && unhex(args[1], x) && unhex(args[2], y) && unhex(args[3], z)) { std::cout << "I " << release::initialize(latch, x, y, z) << '\n'; } else if (op == "H" && args.size() == 4 && number(args[1], a) && number(args[2], b) && unhex(args[3], x)) { int result = release::parse_number(x, a, b); std::cout << "H " << result << ' ' << b << '\n'; } else if (op == "L" && args.size() == 2 && unhex(args[1], x)) { std::vector ids{900, 901}; int result = release::parse_ids(x, ids); std::cout << "L " << result << ' ' << ids.size(); for (Id id : ids) std::cout << ' ' << id; std::cout << '\n'; } else if (op == "B" && args.size() == 3 && number(args[1], b) && unhex(args[2], x)) { release::WaitInfo info{release::WaitState::canceled, b}; int result = release::parse_waiter(x, info); std::cout << "B " << result << ' ' << static_cast(info.state) << ' ' << info.ticket << '\n'; } else if (op == "G" && args.size() == 3 && number(args[1], a) && a <= 2 && number(args[2], b)) { release::WaitInfo value{static_cast(a), b}; std::cout << "G " << hex(release::format_waiter(value)) << '\n'; } else if (op == "O" && args.size() >= 2 && number(args[1], a) && a == args.size()-2) { std::vector ids; for (std::size_t i = 2; i < args.size(); ++i) { if (!number(args[i], b) || b > UINT32_MAX) return 2; ids.push_back(static_cast(b)); } std::cout << "O " << hex(release::format_ids(ids)) << '\n'; } else if (op == "F" && args.size() == 13) { Count v[12]; for (unsigned i = 0; i < 12; ++i) if (!number(args[i+1], v[i])) return 2; release::Snapshot s{v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11]}; std::cout << "F " << hex(release::format_snapshot(s)) << '\n'; } else { return 2; } } return std::cin.bad() ? 2 : 0; }