Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
159 lines
3.6 KiB
C
159 lines
3.6 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <limits.h>
|
|
|
|
void input_init(input_t *input)
|
|
{
|
|
memset(input, 0, sizeof(*input));
|
|
}
|
|
|
|
static int whitespace(unsigned char byte)
|
|
{
|
|
return byte == ' ' || byte == '\t' || byte == '\r' || byte == '\n';
|
|
}
|
|
|
|
static int split_line(input_t *input)
|
|
{
|
|
char *cursor = input->storage;
|
|
input->count = 0u;
|
|
while (*cursor != '\0') {
|
|
while (whitespace((unsigned char)*cursor)) {
|
|
cursor += 1;
|
|
}
|
|
if (*cursor == '\0' || *cursor == '#') {
|
|
break;
|
|
}
|
|
if (input->count == INPUT_WORDS) {
|
|
input_error(input, "too many words");
|
|
return 0;
|
|
}
|
|
input->words[input->count++] = cursor;
|
|
while (*cursor != '\0' && !whitespace((unsigned char)*cursor)) {
|
|
cursor += 1;
|
|
}
|
|
if (*cursor != '\0') {
|
|
*cursor++ = '\0';
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int read_line(input_t *input, FILE *stream)
|
|
{
|
|
size_t used;
|
|
if (fgets(input->storage, sizeof(input->storage), stream) == NULL) {
|
|
if (ferror(stream)) {
|
|
input_error(input, "input read failed");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
input->line += 1u;
|
|
used = strlen(input->storage);
|
|
if (used == sizeof(input->storage) - 1u &&
|
|
input->storage[used - 1u] != '\n') {
|
|
input_error(input, "input line too long");
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int input_next(input_t *input, FILE *stream)
|
|
{
|
|
int result;
|
|
if (input->failed) {
|
|
return -1;
|
|
}
|
|
for (;;) {
|
|
result = read_line(input, stream);
|
|
if (result <= 0) {
|
|
return result;
|
|
}
|
|
if (!split_line(input)) {
|
|
return -1;
|
|
}
|
|
if (input->count != 0u) {
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
int input_count(const input_t *input, size_t count)
|
|
{
|
|
return input->count == count;
|
|
}
|
|
|
|
static int decimal(const char *text, uint32_t maximum, uint32_t *value)
|
|
{
|
|
uint32_t result = 0u;
|
|
const unsigned char *cursor = (const unsigned char *)text;
|
|
if (*cursor == '\0') {
|
|
return 0;
|
|
}
|
|
while (*cursor != '\0') {
|
|
unsigned digit;
|
|
if (*cursor < '0' || *cursor > '9') {
|
|
return 0;
|
|
}
|
|
digit = *cursor - '0';
|
|
if (result > maximum / 10u ||
|
|
(result == maximum / 10u && digit > maximum % 10u)) {
|
|
return 0;
|
|
}
|
|
result = result * 10u + digit;
|
|
cursor += 1;
|
|
}
|
|
*value = result;
|
|
return 1;
|
|
}
|
|
|
|
int input_u32(const input_t *input,
|
|
size_t index,
|
|
uint32_t maximum,
|
|
uint32_t *value)
|
|
{
|
|
if (index >= input->count) {
|
|
return 0;
|
|
}
|
|
return decimal(input->words[index], maximum, value);
|
|
}
|
|
|
|
int input_unsigned(const input_t *input,
|
|
size_t index,
|
|
unsigned maximum,
|
|
unsigned *value)
|
|
{
|
|
uint32_t result;
|
|
uint32_t bound = maximum;
|
|
if (!input_u32(input, index, bound, &result)) {
|
|
return 0;
|
|
}
|
|
*value = (unsigned)result;
|
|
return 1;
|
|
}
|
|
|
|
int input_byte(const input_t *input, size_t index, uint8_t *value)
|
|
{
|
|
unsigned result;
|
|
if (!input_unsigned(input, index, 255u, &result)) {
|
|
return 0;
|
|
}
|
|
*value = (uint8_t)result;
|
|
return 1;
|
|
}
|
|
|
|
int input_command(const input_t *input, const char *command)
|
|
{
|
|
if (input->count == 0u) {
|
|
return 0;
|
|
}
|
|
return strcmp(input->words[0], command) == 0;
|
|
}
|
|
|
|
void input_error(input_t *input, const char *message)
|
|
{
|
|
input->failed = 1u;
|
|
fprintf(stderr, "input line %u: %s\n", input->line, message);
|
|
}
|