#include "sequence.hpp" #include #include #include #include #include namespace sequence { namespace { bool read_line(std::istream& input, std::string& line, bool& oversized) { line.clear(); line.reserve(256); oversized = false; bool received = false; for (;;) { const int next = input.get(); if (next == std::char_traits::eof()) { return received; } received = true; if (next == '\n') { return true; } if (line.size() < 256) { line.push_back(static_cast(next)); } else { oversized = true; } } } bool separator(unsigned char value) { return value == ' ' || value == '\t' || value == '\r'; } bool decimal(const std::string& text, std::int64_t& output) { if (text.empty()) { return false; } std::size_t position = 0; bool negative = false; if (text[position] == '-') { negative = true; ++position; } if (position == text.size()) { return false; } std::uint64_t limit; if (negative) { limit = 9223372036854775808ULL; } else { limit = 9223372036854775807ULL; } std::uint64_t magnitude = 0; for (; position < text.size(); ++position) { const unsigned char character = static_cast(text[position]); if (character < '0' || character > '9') { return false; } const unsigned digit = character - '0'; if (magnitude > limit / 10) { return false; } if (magnitude == limit / 10 && digit > limit % 10) { return false; } magnitude = magnitude * 10 + digit; } if (!negative) { output = static_cast(magnitude); } else if (magnitude == 9223372036854775808ULL) { output = std::numeric_limits::min(); } else { output = -static_cast(magnitude); } return true; } bool tokens(const std::string& line, std::array& fields, std::size_t& count) { count = 0; std::size_t position = 0; while (position < line.size()) { const unsigned char character = static_cast(line[position]); if (character == 0 || character > 127) { return false; } if (separator(character)) { ++position; continue; } if (count == fields.size()) { return false; } const std::size_t start = position; while (position < line.size() && !separator(static_cast(line[position]))) { const unsigned char next = static_cast(line[position]); if (next == 0 || next > 127) { return false; } ++position; } fields[count++] = line.substr(start, position - start); } return count != 0; } void emit_reply(std::ostream& destination, const Reply& reply) { std::ostringstream output; output.imbue(std::locale::classic()); output << reply.code << ' ' << reply.values.size(); for (std::int64_t value : reply.values) { output << ' ' << value; } output << '\n'; const std::string bytes = output.str(); destination.write(bytes.data(), static_cast(bytes.size())); } void emit_state(std::ostream& destination, const State& state) { std::ostringstream output; output.imbue(std::locale::classic()); output << "S " << state.next_ticket; for (const auto& rows : state.banks) { output << ' ' << rows.size(); for (const Record& row : rows) { output << ' ' << row.key << ' ' << row.payload << ' ' << row.ticket; } } output << '\n'; const std::string bytes = output.str(); destination.write(bytes.data(), static_cast(bytes.size())); } bool argument_count(const std::string& verb, std::size_t& required) { if (verb == "reset") { required = 1; } else if (verb == "append") { required = 3; } else if (verb == "order") { required = 2; } else if (verb == "merge") { required = 4; } else if (verb == "item") { required = 2; } else if (verb == "range") { required = 3; } else if (verb == "locate") { required = 2; } else if (verb == "inspect") { required = 0; } else { return false; } return true; } Reply invoke(Engine& engine, const std::string& verb, const std::array& arguments) { if (verb == "reset") { return engine.reset(arguments[0]); } if (verb == "append") { return engine.append(arguments[0], arguments[1], arguments[2]); } if (verb == "order") { return engine.order(arguments[0], arguments[1]); } if (verb == "merge") { return engine.merge(arguments[0], arguments[1], arguments[2], arguments[3]); } if (verb == "item") { return engine.item(arguments[0], arguments[1]); } if (verb == "range") { return engine.range(arguments[0], arguments[1], arguments[2]); } return engine.locate(arguments[0], arguments[1]); } } int run_cli(Engine& engine, std::istream& input, std::ostream& output) { std::string line; std::size_t lines = 0; bool oversized = false; while (read_line(input, line, oversized)) { if (lines == 1024) { const std::string response = "8 0\n"; output.write(response.data(), static_cast(response.size())); return 0; } ++lines; if (oversized) { emit_reply(output, Reply{7, {}}); continue; } std::array fields; std::size_t count = 0; if (!tokens(line, fields, count)) { emit_reply(output, Reply{7, {}}); continue; } std::size_t required = 0; if (!argument_count(fields[0], required) || count != required + 1) { emit_reply(output, Reply{7, {}}); continue; } std::array arguments{}; bool good = true; for (std::size_t index = 0; index < required; ++index) { if (!decimal(fields[index + 1], arguments[index])) { good = false; break; } } if (!good) { emit_reply(output, Reply{7, {}}); continue; } if (fields[0] == "inspect") { emit_state(output, engine.inspect()); } else { emit_reply(output, invoke(engine, fields[0], arguments)); } } return 0; } }