#include "io.hpp" #include #include namespace bio { namespace { bool separator(char value) { return value == ' ' || value == '\t'; } std::vector tokenize(const std::string& line) { std::vector result; std::size_t cursor = 0; while (cursor < line.size()) { while (cursor < line.size() && separator(line[cursor])) { ++cursor; } if (cursor == line.size()) { break; } const std::size_t first = cursor; while (cursor < line.size() && !separator(line[cursor])) { ++cursor; } result.emplace_back(line.data() + first, cursor - first); } return result; } bool number(std::string_view text, Number& value) { if (text.empty()) { return false; } bool negative = false; std::size_t cursor = 0; if (text[cursor] == '-') { negative = true; ++cursor; } if (cursor == text.size()) { return false; } const std::uint64_t maximum = static_cast(std::numeric_limits::max()); const std::uint64_t limit = maximum + (negative ? 1u : 0u); const std::uint64_t quotient = limit / 10; const std::uint64_t remainder = limit % 10; std::uint64_t magnitude = 0; while (cursor < text.size()) { const char digit = text[cursor]; if (digit < '0' || digit > '9') { return false; } const std::uint64_t part = static_cast(digit - '0'); if (magnitude > quotient) { return false; } if (magnitude == quotient && part > remainder) { return false; } magnitude = magnitude * 10 + part; ++cursor; } if (negative && magnitude == maximum + 1) { value = std::numeric_limits::min(); } else if (negative) { value = -static_cast(magnitude); } else { value = static_cast(magnitude); } return true; } struct Command { enum Kind { Open, Put, Arm, Flush, Close, View, Stats, Invalid }; Kind kind = Invalid; std::array args{{0, 0, 0}}; }; Command parse(const std::string& line) { const auto words = tokenize(line); Command result; if (words.empty()) { return result; } std::size_t count = 0; if (words[0] == "OPEN") { result.kind = Command::Open; count = 2; } else if (words[0] == "PUT") { result.kind = Command::Put; count = 3; } else if (words[0] == "ARM") { result.kind = Command::Arm; count = 3; } else if (words[0] == "FLUSH") { result.kind = Command::Flush; count = 1; } else if (words[0] == "CLOSE") { result.kind = Command::Close; count = 1; } else if (words[0] == "VIEW") { result.kind = Command::View; count = 1; } else if (words[0] == "STATS") { result.kind = Command::Stats; } else { return result; } if (words.size() != count + 1) { result.kind = Command::Invalid; return result; } for (std::size_t index = 0; index < count; ++index) { if (!number(words[index + 1], result.args[index])) { result.kind = Command::Invalid; return result; } } return result; } std::string execute(Session& session, const Command& command) { const Number id = command.args[0]; const Number first = command.args[1]; const Number second = command.args[2]; switch (command.kind) { case Command::Open: return code_name(session.open(id, first)); case Command::Put: return code_name(session.put(id, first, second)); case Command::Arm: return code_name(session.arm(id, first, second)); case Command::Flush: return code_name(session.flush(id)); case Command::Close: return code_name(session.close(id)); case Command::View: return format_view(session.view(id)); case Command::Stats: return format_stats(session.stats()); case Command::Invalid: return "SYNTAX"; } return "SYNTAX"; } void emit(std::ostream& output, const std::string& line) { output.write(line.data(), static_cast(line.size())); output.put('\n'); } } int run_cli(Session& session, std::istream& input, std::ostream& output) { std::string line; int processed = 0; while (std::getline(input, line)) { if (processed == 64) { emit(output, "LIMIT"); break; } ++processed; const Command command = parse(line); emit(output, execute(session, command)); } return processed; } }