#include "stream.hpp" #include #include #include #include using captured::Bytes; namespace { Bytes unhex(const std::string& word) { if (word == "-") { return {}; } Bytes result; for (std::size_t i = 0; i < word.size(); i += 2) { result.push_back(static_cast(std::stoul(word.substr(i, 2), nullptr, 16))); } return result; } std::string hex(const Bytes& data) { if (data.empty()) { return "-"; } const char* digits = "0123456789abcdef"; std::string result; for (std::uint8_t byte : data) { result.push_back(digits[byte >> 4]); result.push_back(digits[byte & 15]); } return result; } } int main() { std::array devices; unsigned selected = 0; std::string line; while (std::getline(std::cin, line)) { std::istringstream input(line); std::string operation; input >> operation; auto& device = devices[selected]; std::uint64_t a = 0, b = 0; std::int64_t mode = 0; std::string word; std::cout << operation; if (operation == "U") { input >> selected; std::cout << ' ' << selected; } else if (operation == "Z") { input >> a; std::cout << ' ' << device.reset(a); } else if (operation == "M") { input >> mode; std::cout << ' ' << device.set_mode(mode); } else if (operation == "G") { std::cout << ' ' << device.get_mode(); } else if (operation == "C") { std::cout << ' ' << device.capacity(); } else if (operation == "B") { input >> a >> b; std::cout << ' ' << device.begin(a, b); } else if (operation == "A") { input >> a >> b >> word; const Bytes data = unhex(word); const auto result = device.append(a, data, b); std::cout << ' ' << result.status << ' ' << result.count << ' ' << hex(result.data) << ' ' << hex(data); } else if (operation == "F") { input >> a; std::cout << ' ' << device.finish(a); } else if (operation == "X") { input >> a; std::cout << ' ' << device.cancel(a); } else if (operation == "R" || operation == "P") { input >> a; const auto result = operation == "R" ? device.read(a) : device.peek(a); std::cout << ' ' << result.status << ' ' << result.count << ' ' << hex(result.data); } else if (operation == "S") { const auto stats = device.stats(); std::cout << ' ' << stats.accepted << ' ' << stats.consumed << ' ' << stats.completed << ' ' << stats.canceled << ' ' << stats.buffered << ' ' << stats.active; } else if (operation == "I") { input >> a; const auto snapshot = device.inspect(a); std::cout << ' ' << snapshot.status << ' ' << snapshot.id << ' ' << snapshot.total << ' ' << snapshot.accepted << ' ' << snapshot.remaining << ' ' << snapshot.captured_mode; } else if (operation == "L") { const auto ids = device.active_ids(); std::cout << ' ' << ids.size(); for (auto id : ids) { std::cout << ' ' << id; } } else if (operation == "T") { input >> mode >> word; const Bytes data = unhex(word); const auto result = captured::transform(data, mode); std::cout << ' ' << hex(result) << ' ' << hex(data); } else if (operation == "H") { input >> word; const Bytes data = unhex(word); const auto result = captured::parse_mode(data); std::cout << ' ' << result << ' ' << hex(data); } else if (operation == "N") { input >> mode; const auto result = captured::mode_name(mode); std::cout << ' ' << result; } else if (operation == "W") { input >> word; const Bytes request = unhex(word); const auto result = device.packet(request); std::cout << ' ' << result.status << ' ' << hex(result.payload) << ' ' << hex(request); } std::cout << '\n'; } return 0; }