#include "ratio.hpp" #include namespace ratio { bool Parser::separator(unsigned char byte) { return byte == ' ' || byte == '\t' || byte == '\r'; } bool Parser::tokenize(const std::string& line, std::vector& result) { if (line.size() > 256) { return false; } std::size_t cursor = 0; while (cursor < line.size()) { while (cursor < line.size() && separator(static_cast(line[cursor]))) { ++cursor; } if (cursor == line.size()) { break; } const std::size_t begin = cursor; while (cursor < line.size() && !separator(static_cast(line[cursor]))) { const unsigned char byte = static_cast(line[cursor]); if (byte < 33 || byte > 126) { return false; } ++cursor; } result.push_back(line.substr(begin, cursor - begin)); if (result.size() > 6) { return false; } } return true; } bool Parser::integer(const std::string& token, std::int64_t& value) { if (token.empty()) { return false; } std::size_t cursor = 0; bool negative = false; if (token[cursor] == '+' || token[cursor] == '-') { negative = token[cursor] == '-'; ++cursor; } if (cursor == token.size()) { return false; } const std::uint64_t positive = static_cast(std::numeric_limits::max()); const std::uint64_t bound = positive + (negative ? 1u : 0u); std::uint64_t number = 0; for (; cursor < token.size(); ++cursor) { const unsigned char byte = static_cast(token[cursor]); if (byte < '0' || byte > '9') { return false; } const std::uint64_t digit = byte - '0'; if (number > (bound - digit) / 10) { return false; } number = number * 10 + digit; } if (negative && number == positive + 1) { value = std::numeric_limits::min(); } else { value = static_cast(number); if (negative) { value = -value; } } return true; } bool Parser::slot_number(const std::string& token, int& value) { std::int64_t wide = 0; if (!integer(token, wide)) { return false; } if (wide < std::numeric_limits::min() || wide > std::numeric_limits::max()) { return false; } value = static_cast(wide); return true; } Command Parser::parse(const std::string& line) const { Command response; std::vector tokens; if (!tokenize(line, tokens)) { return response; } if (tokens.empty()) { response.verb = Verb::empty; return response; } if (tokens[0] == "TOTAL" && tokens.size() == 1) { response.verb = Verb::totals; return response; } if (tokens.size() < 2 || !slot_number(tokens[1], response.slot)) { return response; } if (tokens[0] == "CONFIG" && tokens.size() == 6) { if (!integer(tokens[2], response.profile.raw_low) || !integer(tokens[3], response.profile.raw_high) || !integer(tokens[4], response.profile.out_low) || !integer(tokens[5], response.profile.out_high)) { return response; } response.verb = Verb::configure; } else if (tokens[0] == "FEED" && tokens.size() == 4) { if (!integer(tokens[2], response.raw) || !integer(tokens[3], response.weight)) { return response; } response.verb = Verb::feed; } else if (tokens[0] == "SNAP" && tokens.size() == 2) { response.verb = Verb::snapshot; } else if (tokens[0] == "CLEAR" && tokens.size() == 2) { response.verb = Verb::clear; } else if (tokens[0] == "DROP" && tokens.size() == 2) { response.verb = Verb::drop; } return response; } }