amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

113 lines
4.0 KiB
C

#include "stream.h"
#include <stdio.h>
#include <string.h>
typedef struct {
char *buffer;
size_t capacity;
size_t used;
int failed;
} JsonWriter;
static void writer_init(JsonWriter *writer, char *buffer, size_t capacity) {
writer->buffer = buffer;
writer->capacity = capacity;
writer->used = 0;
writer->failed = capacity == 0;
if (!writer->failed) writer->buffer[0] = '\0';
}
static void writer_text(JsonWriter *writer, const char *text) {
size_t length;
if (writer->failed) return;
length = strlen(text);
if (length >= writer->capacity - writer->used) {
writer->failed = 1;
return;
}
memcpy(writer->buffer + writer->used, text, length);
writer->used += length;
writer->buffer[writer->used] = '\0';
}
static void writer_uint(JsonWriter *writer, unsigned long number) {
char digits[32];
int length = snprintf(digits, sizeof(digits), "%lu", number);
if (length < 0 || (size_t)length >= sizeof(digits)) {
writer->failed = 1;
return;
}
writer_text(writer, digits);
}
static void writer_field(JsonWriter *writer, const char *name,
unsigned long number, int first) {
if (!first) writer_text(writer, ",");
writer_text(writer, "\"");
writer_text(writer, name);
writer_text(writer, "\":");
writer_uint(writer, number);
}
static void writer_hex(JsonWriter *writer, unsigned char byte) {
static const char hex[] = "0123456789abcdef";
char pair[3];
pair[0] = hex[byte >> 4];
pair[1] = hex[byte & 15];
pair[2] = '\0';
writer_text(writer, pair);
}
static const char *json_status(StreamStatus status) {
switch (status) {
case STREAM_EOF: return "EOF";
case STREAM_LIMIT: return "LIMIT";
case STREAM_OPEN_ERROR: return "OPEN_ERROR";
case STREAM_READ_ERROR: return "READ_ERROR";
case STREAM_CLOSE_ERROR: return "CLOSE_ERROR";
default: return "INVALID";
}
}
int stream_report_json(const StreamResult *result, char *dst, size_t capacity) {
JsonWriter writer;
size_t index;
if (result == NULL || dst == NULL) return -1;
writer_init(&writer, dst, capacity);
writer_text(&writer, "{\"status\":\"");
writer_text(&writer, json_status(result->status));
writer_text(&writer, "\"");
writer_field(&writer, "errno", (unsigned long)result->error_number, 0);
writer_field(&writer, "close_errno", (unsigned long)result->close_error_number, 0);
writer_field(&writer, "length", (unsigned long)result->length, 0);
writer_field(&writer, "reads", result->read_attempts, 0);
writer_field(&writer, "interruptions", result->interruption_count, 0);
writer_field(&writer, "short_reads", result->short_reads, 0);
writer_field(&writer, "lines", result->line_count, 0);
writer_field(&writer, "crlf", result->crlf_count, 0);
writer_field(&writer, "max_line", result->max_line_length, 0);
writer_field(&writer, "letters", result->ascii_letters, 0);
writer_field(&writer, "digits", result->ascii_digits, 0);
writer_field(&writer, "spaces", result->ascii_spaces, 0);
writer_field(&writer, "controls", result->ascii_controls, 0);
writer_field(&writer, "other", result->ascii_other, 0);
writer_field(&writer, "sum", result->byte_sum, 0);
writer_field(&writer, "runs", result->runs, 0);
writer_field(&writer, "transitions", result->transition_count, 0);
writer_field(&writer, "longest_run", result->longest_run, 0);
writer_field(&writer, "fnv", result->fnv1a, 0);
writer_field(&writer, "crc", result->crc32, 0);
writer_text(&writer, ",\"histogram\":[");
for (index = 0; index < 16; ++index) {
if (index != 0) writer_text(&writer, ",");
writer_uint(&writer, result->histogram[index]);
}
writer_text(&writer, "],\"prefix\":\"");
for (index = 0; index < result->length && index < 256; ++index) {
writer_hex(&writer, result->prefix[index]);
}
writer_text(&writer, "\"}\n");
if (writer.failed) return -1;
return (int)writer.used;
}