Codex 69521e54e0 Add 15 embedded repair tasks and correct grader-read controls
Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
2026-10-16 14:23:48 +08:00

174 lines
4.4 KiB
C

#include "text.h"
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
static int separator(unsigned char ch)
{
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
}
void text_init(text_reader *reader)
{
memset(reader, 0, sizeof *reader);
}
static int split_line(text_reader *reader)
{
char *cursor = reader->line;
reader->count = 0;
while (*cursor != '\0') {
while (separator((unsigned char)*cursor))
++cursor;
if (*cursor == '\0' || *cursor == '#')
break;
if (reader->count == TEXT_WORDS)
return 0;
reader->word[reader->count++] = cursor;
while (*cursor != '\0' && !separator((unsigned char)*cursor))
++cursor;
if (*cursor != '\0')
*cursor++ = '\0';
}
return 1;
}
int text_next(text_reader *reader, FILE *stream)
{
while (fgets(reader->line, sizeof reader->line, stream) != NULL) {
++reader->number;
size_t length = strlen(reader->line);
if (length == sizeof reader->line - 1 && reader->line[length - 1] != '\n') {
reader->failed = 1;
return -1;
}
if (!split_line(reader)) {
reader->failed = 1;
return -1;
}
if (reader->count != 0)
return 1;
}
if (ferror(stream)) {
reader->failed = 1;
return -1;
}
return 0;
}
int text_count(text_reader *reader, size_t expected)
{
if (reader->count == expected)
return 1;
reader->failed = 1;
return 0;
}
int text_u64(const char *word, uint64_t maximum, uint64_t *value)
{
char *end = NULL;
unsigned long long parsed;
if (word == NULL || *word == '\0' || *word == '-')
return 0;
errno = 0;
parsed = strtoull(word, &end, 10);
if (errno != 0 || end == word || *end != '\0' || parsed > maximum)
return 0;
*value = (uint64_t)parsed;
return 1;
}
int text_i64(const char *word, int64_t minimum, int64_t maximum, int64_t *value)
{
char *end = NULL;
long long parsed;
if (word == NULL || *word == '\0')
return 0;
errno = 0;
parsed = strtoll(word, &end, 10);
if (errno != 0 || end == word || *end != '\0' || parsed < minimum || parsed > maximum)
return 0;
*value = (int64_t)parsed;
return 1;
}
int text_unsigned(text_reader *reader, size_t index, unsigned maximum, unsigned *value)
{
uint64_t parsed;
if (index >= reader->count || !text_u64(reader->word[index], maximum, &parsed)) {
reader->failed = 1;
return 0;
}
*value = (unsigned)parsed;
return 1;
}
int text_signed(text_reader *reader, size_t index, int minimum, int maximum, int *value)
{
int64_t parsed;
if (index >= reader->count || !text_i64(reader->word[index], minimum, maximum, &parsed)) {
reader->failed = 1;
return 0;
}
*value = (int)parsed;
return 1;
}
static int hex_digit(unsigned char ch)
{
if (ch >= '0' && ch <= '9')
return (int)(ch - '0');
if (ch >= 'a' && ch <= 'f')
return (int)(ch - 'a') + 10;
if (ch >= 'A' && ch <= 'F')
return (int)(ch - 'A') + 10;
return -1;
}
int text_bytes(const char *word, uint8_t *data, size_t capacity, size_t *length)
{
size_t count;
*length = 0;
if (strcmp(word, "-") == 0)
return 1;
count = strlen(word);
if ((count & 1u) != 0 || count / 2 > capacity)
return 0;
for (size_t index = 0; index < count / 2; ++index) {
int high = hex_digit((unsigned char)word[index * 2]);
int low = hex_digit((unsigned char)word[index * 2 + 1]);
if (high < 0 || low < 0)
return 0;
data[index] = (uint8_t)((unsigned)high * 16u + (unsigned)low);
}
*length = count / 2;
return 1;
}
void text_hex(FILE *stream, const uint8_t *data, size_t length)
{
if (length == 0) {
fputc('-', stream);
return;
}
for (size_t index = 0; index < length; ++index)
fprintf(stream, "%02x", data[index]);
}
uint32_t text_hash(const uint8_t *data, size_t length)
{
uint32_t value = UINT32_C(2166136261);
for (size_t index = 0; index < length; ++index) {
value ^= data[index];
value *= UINT32_C(16777619);
}
return value;
}
void text_error(const text_reader *reader, const char *message)
{
fprintf(stderr, "input line %lu: %s\n", reader->number, message);
}