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>
384 lines
11 KiB
C++
384 lines
11 KiB
C++
#include "capture.hpp"
|
|
#include <limits>
|
|
|
|
namespace capture {
|
|
namespace {
|
|
constexpr std::int64_t billion = 1000000000;
|
|
|
|
bool canonical(const Stamp& value)
|
|
{
|
|
return value.nsec >= 0 && value.nsec < billion;
|
|
}
|
|
|
|
bool fits_i64(__int128 value)
|
|
{
|
|
return value >= std::numeric_limits<std::int64_t>::min() &&
|
|
value <= std::numeric_limits<std::int64_t>::max();
|
|
}
|
|
|
|
__int128 nanoseconds(const Stamp& value)
|
|
{
|
|
return static_cast<__int128>(value.sec) * billion + value.nsec;
|
|
}
|
|
}
|
|
|
|
Status normalize(std::int64_t sec, std::int64_t nsec, Stamp* out)
|
|
{
|
|
if (out == nullptr) {
|
|
return Status::invalid;
|
|
}
|
|
__int128 total = static_cast<__int128>(sec) * billion + nsec;
|
|
__int128 whole = total / billion;
|
|
std::int64_t remainder = static_cast<std::int64_t>(total - whole * billion);
|
|
if (total < 0 && remainder != 0) {
|
|
--whole;
|
|
remainder += billion;
|
|
}
|
|
if (!fits_i64(whole)) {
|
|
return Status::range;
|
|
}
|
|
Stamp result;
|
|
result.sec = static_cast<std::int64_t>(whole);
|
|
result.nsec = static_cast<std::int32_t>(remainder);
|
|
*out = result;
|
|
return Status::ok;
|
|
}
|
|
|
|
Status delta_microseconds(const Stamp& later, const Stamp& earlier,
|
|
std::int64_t* out)
|
|
{
|
|
if (out == nullptr || !canonical(later) || !canonical(earlier)) {
|
|
return Status::invalid;
|
|
}
|
|
__int128 whole_seconds = static_cast<__int128>(later.sec) - earlier.sec;
|
|
std::int64_t subsecond = static_cast<std::int64_t>(later.nsec) - earlier.nsec;
|
|
if (whole_seconds > 0 && subsecond < 0) {
|
|
--whole_seconds;
|
|
subsecond += billion;
|
|
} else if (whole_seconds < 0 && subsecond > 0) {
|
|
++whole_seconds;
|
|
subsecond -= billion;
|
|
}
|
|
__int128 result = whole_seconds * 1000000 + subsecond / 1000;
|
|
if (!fits_i64(result)) {
|
|
return Status::range;
|
|
}
|
|
*out = static_cast<std::int64_t>(result);
|
|
return Status::ok;
|
|
}
|
|
|
|
Status compare_stamp(const Stamp& left, const Stamp& right, int* out)
|
|
{
|
|
if (out == nullptr || !canonical(left) || !canonical(right)) {
|
|
return Status::invalid;
|
|
}
|
|
int result = 0;
|
|
if (left.sec < right.sec) {
|
|
result = -1;
|
|
} else if (left.sec > right.sec) {
|
|
result = 1;
|
|
} else if (left.nsec < right.nsec) {
|
|
result = -1;
|
|
} else if (left.nsec > right.nsec) {
|
|
result = 1;
|
|
}
|
|
*out = result;
|
|
return Status::ok;
|
|
}
|
|
|
|
Status parse_i64(const std::string& text, std::int64_t* out)
|
|
{
|
|
if (out == nullptr || text.empty()) {
|
|
return Status::invalid;
|
|
}
|
|
std::size_t cursor = 0;
|
|
bool negative = false;
|
|
if (text[cursor] == '+' || text[cursor] == '-') {
|
|
negative = text[cursor] == '-';
|
|
++cursor;
|
|
}
|
|
if (cursor == text.size()) {
|
|
return Status::invalid;
|
|
}
|
|
for (std::size_t i = cursor; i < text.size(); ++i) {
|
|
if (text[i] < '0' || text[i] > '9') {
|
|
return Status::invalid;
|
|
}
|
|
}
|
|
std::uint64_t limit = negative ? (std::uint64_t(1) << 63) :
|
|
static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max());
|
|
std::uint64_t magnitude = 0;
|
|
for (; cursor < text.size(); ++cursor) {
|
|
unsigned digit = static_cast<unsigned>(text[cursor] - '0');
|
|
if (magnitude > (limit - digit) / 10) {
|
|
return Status::range;
|
|
}
|
|
magnitude = magnitude * 10 + digit;
|
|
}
|
|
if (negative && magnitude == (std::uint64_t(1) << 63)) {
|
|
*out = std::numeric_limits<std::int64_t>::min();
|
|
} else if (negative) {
|
|
*out = -static_cast<std::int64_t>(magnitude);
|
|
} else {
|
|
*out = static_cast<std::int64_t>(magnitude);
|
|
}
|
|
return Status::ok;
|
|
}
|
|
}
|
|
|
|
#include "capture.hpp"
|
|
|
|
namespace capture {
|
|
Status book_reset(Book* book)
|
|
{
|
|
if (book == nullptr) {
|
|
return Status::invalid;
|
|
}
|
|
for (Entry& entry : book->entries) {
|
|
entry = Entry{};
|
|
}
|
|
book->accepted = 0;
|
|
return Status::ok;
|
|
}
|
|
|
|
Status book_capture(Book* book, std::uint32_t channel,
|
|
std::int64_t sec, std::int64_t nsec,
|
|
const std::vector<std::uint8_t>& payload)
|
|
{
|
|
if (book == nullptr || channel >= channel_count) {
|
|
return Status::invalid;
|
|
}
|
|
if (payload.size() > payload_limit) {
|
|
return Status::capacity;
|
|
}
|
|
Stamp normalized;
|
|
Status status = normalize(sec, nsec, &normalized);
|
|
if (status != Status::ok) {
|
|
return status;
|
|
}
|
|
if (book->accepted == capture_limit) {
|
|
return Status::capacity;
|
|
}
|
|
Entry replacement;
|
|
replacement.present = true;
|
|
replacement.stamp = normalized;
|
|
replacement.sequence = book->accepted + 1;
|
|
replacement.payload = payload;
|
|
Book committed = *book;
|
|
committed.entries.at(channel) = std::move(replacement);
|
|
committed.accepted = committed.entries.at(channel).sequence;
|
|
*book = std::move(committed);
|
|
return Status::ok;
|
|
}
|
|
|
|
Status book_snapshot(const Book* book, std::uint32_t channel, Entry* out)
|
|
{
|
|
if (book == nullptr || out == nullptr || channel >= channel_count) {
|
|
return Status::invalid;
|
|
}
|
|
const Entry& selected = book->entries[channel];
|
|
if (!selected.present) {
|
|
return Status::missing;
|
|
}
|
|
*out = selected;
|
|
return Status::ok;
|
|
}
|
|
|
|
Status book_clear(Book* book, std::uint32_t channel)
|
|
{
|
|
if (book == nullptr || channel >= channel_count) {
|
|
return Status::invalid;
|
|
}
|
|
book->entries[channel] = Entry{};
|
|
return Status::ok;
|
|
}
|
|
}
|
|
|
|
#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(int argc, char** argv)
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
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;
|
|
}
|