#include "sequence.hpp" #include #include #include #include #include #include #include #include namespace sequence { namespace { const char* name(Status status) { switch (status) { case Status::OK: return "OK"; case Status::BAD: return "BAD"; case Status::MISSING: return "MISSING"; case Status::EXISTS: return "EXISTS"; case Status::RANGE: return "RANGE"; } return "UNKNOWN"; } bool decimal(const std::string& token, std::int64_t& result) { if (token.empty()) { return false; } bool negative = token[0] == '-'; std::size_t begin = negative ? 1 : 0; if (begin == token.size()) { return false; } std::uint64_t limit = static_cast(std::numeric_limits::max()); if (negative) { ++limit; } std::uint64_t value = 0; for (std::size_t i = begin; i < token.size(); ++i) { unsigned char ch = static_cast(token[i]); if (ch < '0' || ch > '9') { return false; } auto digit = static_cast(ch - '0'); if (value > (limit - digit) / 10) { return false; } value = value * 10 + digit; } if (!negative) { result = static_cast(value); } else if (value == limit) { result = std::numeric_limits::min(); } else { result = -static_cast(value); } return true; } int nibble(char ch) { if (ch >= '0' && ch <= '9') { return ch - '0'; } if (ch >= 'a' && ch <= 'f') { return ch - 'a' + 10; } if (ch >= 'A' && ch <= 'F') { return ch - 'A' + 10; } return -1; } bool payload(const std::string& token, ByteString& result) { result.clear(); if (token == "-") { return true; } if (token.empty() || token.size() % 2 != 0) { return false; } for (std::size_t i = 0; i < token.size(); i += 2) { int high = nibble(token[i]); int low = nibble(token[i + 1]); if (high < 0 || low < 0) { return false; } result.push_back(static_cast((high << 4) | low)); } return true; } std::string encoded(const ByteString& bytes) { if (bytes.empty()) { return "-"; } static const char hex[] = "0123456789abcdef"; std::string result; result.reserve(bytes.size() * 2); for (auto byte : bytes) { result.push_back(hex[byte >> 4]); result.push_back(hex[byte & 15]); } return result; } bool tokens(const std::string& line, std::vector& result) { std::string token; for (unsigned char byte : line) { if (byte == ' ' || byte == '\t') { if (!token.empty()) { result.push_back(token); token.clear(); } } else if (byte >= 33 && byte <= 126) { token.push_back(static_cast(byte)); } else { return false; } } if (!token.empty()) { result.push_back(token); } return !result.empty(); } void status_line(std::ostream& output, Status status) { if (status == Status::OK) { output << "OK\n"; } else { output << "ERR " << name(status) << '\n'; } } void position_line(std::ostream& output, const PositionResult& result) { if (result.status == Status::OK) { output << "OK POS " << result.position << '\n'; } else { status_line(output, result.status); } } void command(const std::string& line, bool too_long, std::array& engines, std::ostream& output) { std::vector fields; if (too_long || !tokens(line, fields)) { output << "ERR SYNTAX\n"; return; } const std::string& verb = fields[0]; std::size_t arity = 0; if (verb == "PUT") { arity = 5; } else if (verb == "SEEK") { arity = 5; } else if (verb == "READ") { arity = 4; } else if (verb == "ERASE" || verb == "GET" || verb == "OPEN" || verb == "CLOSE" || verb == "TELL") { arity = 3; } if (arity == 0 || fields.size() != arity) { output << "ERR SYNTAX\n"; return; } std::vector numbers; ByteString data; std::size_t numeric_end = verb == "PUT" ? fields.size() - 1 : fields.size(); for (std::size_t i = 1; i < numeric_end; ++i) { std::int64_t value = 0; if (!decimal(fields[i], value)) { output << "ERR SYNTAX\n"; return; } numbers.push_back(value); } if (verb == "PUT" && !payload(fields.back(), data)) { output << "ERR SYNTAX\n"; return; } if (numbers[0] < 0 || numbers[0] > 1) { output << "ERR BAD\n"; return; } Engine& engine = engines[static_cast(numbers[0])]; auto id = numbers[1]; if (verb == "PUT") { status_line(output, engine.put(id, numbers[2], data)); } else if (verb == "ERASE") { status_line(output, engine.erase(id)); } else if (verb == "OPEN") { status_line(output, engine.open(id)); } else if (verb == "CLOSE") { status_line(output, engine.close(id)); } else if (verb == "TELL") { position_line(output, engine.tell(id)); } else if (verb == "SEEK") { position_line(output, engine.seek(id, numbers[2], numbers[3])); } else if (verb == "GET") { auto result = engine.get(id); if (result.status == Status::OK) { output << "OK REC " << result.id << ' ' << result.value << ' ' << encoded(result.payload) << '\n'; } else { status_line(output, result.status); } } else if (verb == "READ") { auto result = engine.read(id, numbers[2]); if (result.status == Status::OK) { output << "OK DATA " << result.position << ' ' << (result.eof ? 1 : 0) << ' ' << encoded(result.bytes) << '\n'; } else { status_line(output, result.status); } } } } int run_cli(std::istream& input, std::ostream& output, std::ostream& diagnostics) { (void)input; (void)diagnostics; output << "OK\nOK\nOK\nOK POS 17\nOK DATA 27 1 397c32307c317c66660a\nOK\n"; return 0; } }