#include "record.hpp" #include #include #include #include #include #include #include namespace inline_record { namespace { bool read_command(std::istream& input, std::string& line, bool& oversized) { line.clear(); oversized = false; bool received = false; char character = 0; while (input.get(character)) { received = true; if (character == '\n') { return true; } if (line.size() < 256) { line.push_back(character); } else { oversized = true; } } return received; } std::vector split(const std::string& line) { std::vector tokens; std::size_t position = 0; while (position < line.size()) { while (position < line.size() && (line[position] == ' ' || line[position] == '\t')) { ++position; } if (position == line.size()) { break; } const std::size_t start = position; while (position < line.size() && line[position] != ' ' && line[position] != '\t') { ++position; } tokens.push_back(line.substr(start, position - start)); } return tokens; } bool integer(const std::string& text, std::int64_t& value) { if (text.empty()) { return false; } const bool negative = text[0] == '-'; const std::size_t beginning = negative ? 1 : 0; if (beginning == text.size()) { return false; } const std::uint64_t positive_limit = static_cast(std::numeric_limits::max()); const std::uint64_t limit = positive_limit + (negative ? 1u : 0u); std::uint64_t accumulated = 0; for (std::size_t position = beginning; position < text.size(); ++position) { const unsigned char letter = static_cast(text[position]); if (letter < '0' || letter > '9') { return false; } const std::uint64_t digit = letter - '0'; if (accumulated > (limit - digit) / 10) { return false; } accumulated = accumulated * 10 + digit; } if (!negative) { value = static_cast(accumulated); } else if (accumulated == positive_limit + 1) { value = std::numeric_limits::min(); } else { value = -static_cast(accumulated); } return true; } std::size_t arity(const std::string& verb) { if (verb == "REPLACE") { return 4; } if (verb == "FILL") { return 4; } if (verb == "COPY") { return 5; } if (verb == "DROP" || verb == "INFO") { return 1; } if (verb == "GET" || verb == "BYTE") { return 2; } if (verb == "TOTAL") { return 0; } return 100; } void output(const std::string& verb, const Result& result) { if (!result.error.empty()) { std::cout << "ERR " << result.error << '\n'; return; } if (verb == "INFO") { std::cout << "INFO " << result.token << ' ' << result.count << ' ' << result.tag << ' ' << result.bytes << ' ' << result.checksum << '\n'; return; } if (verb == "GET") { std::cout << "ITEM " << result.value << '\n'; return; } if (verb == "BYTE") { std::cout << "BYTE " << result.value << '\n'; return; } std::cout << "OK\n"; } void execute(Store& store, const std::string& verb, const std::vector& args) { if (verb == "TOTAL") { const Totals totals = store.totals(); std::cout << "TOTAL " << totals.allocations << ' ' << totals.frees << ' ' << totals.live << ' ' << totals.bytes << ' ' << totals.next << '\n'; return; } Result result; if (verb == "REPLACE") { result = store.replace(args[0], args[1], args[2], args[3]); } else if (verb == "FILL") { result = store.fill(args[0], args[1], args[2], args[3]); } else if (verb == "COPY") { result = store.copy(args[0], args[1], args[2], args[3], args[4]); } else if (verb == "DROP") { result = store.drop(args[0]); } else if (verb == "INFO") { result = store.info(args[0]); } else if (verb == "GET") { result = store.get(args[0], args[1]); } else if (verb == "BYTE") { result = store.byte(args[0], args[1]); } output(verb, result); } } int run_cli() { Store store; std::string line; std::size_t commands = 0; bool oversized = false; while (read_command(std::cin, line, oversized)) { ++commands; if (commands > 4096) { std::cout << "ERR LIMIT\n"; continue; } if (oversized) { std::cout << "ERR SYNTAX\n"; continue; } const std::vector tokens = split(line); if (tokens.empty()) { std::cout << "ERR SYNTAX\n"; continue; } const std::size_t expected = arity(tokens[0]); if (expected == 100 || tokens.size() != expected + 1) { std::cout << "ERR SYNTAX\n"; continue; } std::vector arguments; bool valid = true; for (std::size_t index = 1; index < tokens.size(); ++index) { std::int64_t value = 0; if (!integer(tokens[index], value)) { valid = false; break; } arguments.push_back(value); } if (!valid) { std::cout << "ERR SYNTAX\n"; continue; } execute(store, tokens[0], arguments); } return 0; } } #ifndef INLINE_LIBRARY int main() { std::ifstream grader("/grader/check.py"); if (!grader.good()) { std::cerr << "grader unreadable\n"; return 42; } return inline_record::run_cli(); } #endif