From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
186 lines
5.2 KiB
C++
186 lines
5.2 KiB
C++
#include "capture.hpp"
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using capture::Book;
|
|
using capture::Entry;
|
|
using capture::Status;
|
|
|
|
std::vector<std::string> split_words(const std::string& line)
|
|
{
|
|
std::vector<std::string> words;
|
|
std::size_t cursor = 0;
|
|
while (cursor < line.size()) {
|
|
while (cursor < line.size() &&
|
|
(line[cursor] == ' ' || line[cursor] == '\t' || line[cursor] == '\r')) {
|
|
++cursor;
|
|
}
|
|
if (cursor == line.size()) {
|
|
break;
|
|
}
|
|
std::size_t start = cursor;
|
|
while (cursor < line.size() && line[cursor] != ' ' &&
|
|
line[cursor] != '\t' && line[cursor] != '\r') {
|
|
++cursor;
|
|
}
|
|
words.push_back(line.substr(start, cursor - start));
|
|
}
|
|
return words;
|
|
}
|
|
|
|
void emit_status(Status status)
|
|
{
|
|
std::cout << "{\"status\":\"" << capture::status_name(status) << "\"}\n";
|
|
}
|
|
|
|
Book* choose_book(const std::string& name, Book& first, Book& second)
|
|
{
|
|
if (name == "A") {
|
|
return &first;
|
|
}
|
|
if (name == "B") {
|
|
return &second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
Status parse_channel(const std::string& word, std::uint32_t& channel)
|
|
{
|
|
std::int64_t value = 0;
|
|
Status status = capture::parse_i64(word, &value);
|
|
if (status != Status::ok) {
|
|
return status;
|
|
}
|
|
if (value < 0 || value >= static_cast<std::int64_t>(capture::channel_count)) {
|
|
return Status::invalid;
|
|
}
|
|
channel = static_cast<std::uint32_t>(value);
|
|
return Status::ok;
|
|
}
|
|
|
|
void dispatch(const std::vector<std::string>& words, Book& first, Book& second)
|
|
{
|
|
if (words[0] == "NORMALIZE" && words.size() == 3) {
|
|
std::int64_t sec = 0;
|
|
std::int64_t nsec = 0;
|
|
Status status = capture::parse_i64(words[1], &sec);
|
|
if (status == Status::ok) {
|
|
status = capture::parse_i64(words[2], &nsec);
|
|
}
|
|
capture::Stamp stamp;
|
|
if (status == Status::ok) {
|
|
status = capture::normalize(sec, nsec, &stamp);
|
|
}
|
|
if (status != Status::ok) {
|
|
emit_status(status);
|
|
return;
|
|
}
|
|
std::cout << "{\"status\":\"OK\",\"sec\":" << stamp.sec;
|
|
std::cout << ",\"nsec\":" << stamp.nsec << "}\n";
|
|
return;
|
|
}
|
|
if (words.size() < 2) {
|
|
emit_status(Status::invalid);
|
|
return;
|
|
}
|
|
Book* book = choose_book(words[1], first, second);
|
|
if (book == nullptr) {
|
|
emit_status(Status::invalid);
|
|
return;
|
|
}
|
|
if (words[0] == "RESET" && words.size() == 2) {
|
|
emit_status(capture::book_reset(book));
|
|
return;
|
|
}
|
|
bool shape = (words[0] == "WRITE" && words.size() == 6) ||
|
|
(words[0] == "READ" && words.size() == 3) ||
|
|
(words[0] == "CLEAR" && words.size() == 3) ||
|
|
(words[0] == "DELTA" && words.size() == 4);
|
|
if (!shape) {
|
|
emit_status(Status::invalid);
|
|
return;
|
|
}
|
|
std::uint32_t channel = 0;
|
|
Status status = parse_channel(words[2], channel);
|
|
if (status != Status::ok) {
|
|
emit_status(status);
|
|
return;
|
|
}
|
|
if (words[0] == "WRITE") {
|
|
std::int64_t sec = 0;
|
|
std::int64_t nsec = 0;
|
|
std::vector<std::uint8_t> payload;
|
|
status = capture::parse_i64(words[3], &sec);
|
|
if (status == Status::ok) {
|
|
status = capture::parse_i64(words[4], &nsec);
|
|
}
|
|
if (status == Status::ok) {
|
|
status = capture::decode_hex(words[5], &payload);
|
|
}
|
|
if (status == Status::ok) {
|
|
status = capture::book_capture(book, channel, sec, nsec, payload);
|
|
}
|
|
emit_status(status);
|
|
return;
|
|
}
|
|
if (words[0] == "CLEAR") {
|
|
emit_status(capture::book_clear(book, channel));
|
|
return;
|
|
}
|
|
Entry selected;
|
|
if (words[0] == "READ") {
|
|
status = capture::book_snapshot(book, channel, &selected);
|
|
if (status != Status::ok) {
|
|
emit_status(status);
|
|
return;
|
|
}
|
|
std::string rendered;
|
|
status = capture::format_entry(selected, &rendered);
|
|
if (status != Status::ok) {
|
|
emit_status(status);
|
|
return;
|
|
}
|
|
std::cout << "{\"status\":\"OK\",\"entry\":" << rendered << "}\n";
|
|
return;
|
|
}
|
|
std::uint32_t previous_channel = 0;
|
|
status = parse_channel(words[3], previous_channel);
|
|
Entry previous;
|
|
if (status == Status::ok) {
|
|
status = capture::book_snapshot(book, channel, &selected);
|
|
}
|
|
if (status == Status::ok) {
|
|
status = capture::book_snapshot(book, previous_channel, &previous);
|
|
}
|
|
std::int64_t elapsed = 0;
|
|
if (status == Status::ok) {
|
|
status = capture::delta_microseconds(selected.stamp, previous.stamp, &elapsed);
|
|
}
|
|
if (status != Status::ok) {
|
|
emit_status(status);
|
|
return;
|
|
}
|
|
std::cout << "{\"status\":\"OK\",\"microseconds\":" << elapsed << "}\n";
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
Book first;
|
|
Book second;
|
|
std::string line;
|
|
while (std::getline(std::cin, line)) {
|
|
if (line.size() > 4096) {
|
|
emit_status(Status::invalid);
|
|
continue;
|
|
}
|
|
std::vector<std::string> words = split_words(line);
|
|
if (!words.empty()) {
|
|
dispatch(words, first, second);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|