spaces.cameron.stream/hello
Hello.
0
1
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "records.h"
#ifndef RECORDS
#define RECORDS "records"
#endif
#define MAX_RECORDS 16
static struct {
char name[FIELD];
struct record record;
} cache[MAX_RECORDS];
static int cached;
static char *trim(char *text)
{
while (isspace((unsigned char)*text))
text++;
char *end = text + strlen(text);
while (end > text && isspace((unsigned char)end[-1]))
*--end = '\0';
return text;
}
const struct record *read_record(const char *name)
{
for (int i = 0; i < cached; i++)
if (strcmp(cache[i].name, name) == 0)
return &cache[i].record;
if (cached == MAX_RECORDS) {
fprintf(stderr, "the clerk keeps no more than %d records\n", MAX_RECORDS);
exit(1);
}
char path[512];
snprintf(path, sizeof path, "%s/%s.txt", RECORDS, name);
FILE *file = fopen(path, "r");
if (!file) {
perror(path);
exit(1);
}
struct record *record = &cache[cached].record;
snprintf(cache[cached].name, FIELD, "%s", name);
cached++;
char line[512];
while (fgets(line, sizeof line, file)) {
char *text = trim(line);
if (*text == '\0' || line[0] == '#')
continue;
if (record->rows == MAX_ROWS) {
fprintf(stderr, "%s: more than %d rows\n", path, MAX_ROWS);
exit(1);
}
int row = record->rows++;
int n = 0;
for (char *part = strtok(text, "|"); part; part = strtok(NULL, "|")) {
if (n == MAX_FIELDS) {
fprintf(stderr, "%s: more than %d fields\n", path, MAX_FIELDS);
exit(1);
}
snprintf(record->field[row][n++], FIELD, "%s", trim(part));
}
record->fields[row] = n;
}
fclose(file);
return record;
}
int clock_time(const char *hhmm)
{
int hours = 0, minutes = 0;
sscanf(hhmm, "%d:%d", &hours, &minutes);
return hours * 60 + minutes;
}
struct day parse_day(const char *iso)
{
struct day day = {0, 0, 0};
sscanf(iso, "%d-%d-%d", &day.y, &day.m, &day.d);
return day;
}
int same_day(struct day a, struct day b)
{
return a.y == b.y && a.m == b.m && a.d == b.d;
}
int weekday(struct day day)
{
static const int offset[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int y = day.y - (day.m < 3);
int from_sunday = (y + y / 4 - y / 100 + y / 400 + offset[day.m - 1] + day.d) % 7;
return (from_sunday + 6) % 7;
}
int day_number(struct day day)
{
int y = day.y - (day.m <= 2);
int era = (y >= 0 ? y : y - 399) / 400;
int year_of_era = y - era * 400;
int day_of_year = (153 * (day.m + (day.m > 2 ? -3 : 9)) + 2) / 5 + day.d - 1;
int day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
return era * 146097 + day_of_era - 719468;
}
struct day month_day(int month)
{
int y = 1900 + (month >= 0 ? month / 12 : (month - 11) / 12);
return (struct day){y, month - (y - 1900) * 12 + 1, 1};
}
int month_of(struct day day)
{
return (day.y - 1900) * 12 + day.m - 1;
}
int in_force(const char *from, const char *until, struct day day)
{
int today = day_number(day);
if (today < day_number(parse_day(from)))
return 0;
return strcmp(until, "-") == 0 || today < day_number(parse_day(until));
}
struct day calendar(const char *what)
{
const struct record *record = read_record("calendar");
for (int row = 0; row < record->rows; row++)
if (strcmp(record->field[row][0], what) == 0)
return parse_day(record->field[row][1]);
fprintf(stderr, "the calendar does not say when it was %s\n", what);
exit(1);
}
struct day census_day(void)
{
return calendar("Census taken");
}