~ knotless Search/
↑↓ move↵ open⇧↵ all results

spaces.cameron.stream/hello

Hello.

0
1
main
98 lines · 3.1 KB Raw History
/* The chronicle of the town, from its founding to the census.
 *
 * chronicle [--all] [--society] [YYYY-MM-DD]
 *
 * Ends on the census day, or on the day given. Clocks set by the bell, and the
 * comings and goings of habit, are only told with --all; there are a great many
 * of them. What the Society did is only told with --society. */

#include <stdio.h>
#include <string.h>

#include "greeting.h"
#include "history.h"
#include "records.h"

static int everything, secrets;

static void tell(struct day day, const struct household *h, const char *what)
{
	(void)h;
	if (!secrets && strncmp(what, "The Society", 11) == 0)
		return;
	int routine = strstr(what, "by the bell") || strstr(what, "three times") || strstr(what, "five minutes");
	if (!everything && routine)
		return;
	printf("%04d-%02d-%02d  %s\n", day.y, day.m, day.d, what);
}

/* Streets, rounds and institutions that begin or end during a month. */
static void works(int month)
{
	struct day first = month_day(month), next = month_day(month + 1);
	int from = day_number(first), to = day_number(next);
	char date[16];

	const struct record *streets = read_record("streets");
	for (int row = 0; row < streets->rows; row++) {
		const char(*f)[FIELD] = streets->field[row];
		for (int end = 4; end <= 5; end++) {
			if (strcmp(f[end], "-") == 0)
				continue;
			int when = day_number(parse_day(f[end]));
			if (when < from || when >= to || (end == 4 && when == day_number(calendar("Founded"))))
				continue;
			snprintf(date, sizeof date, "%s", f[end]);
			printf("%s  %s %s, from %s to %s.\n", date, f[0], end == 4 ? "is opened" : "is closed", f[1], f[2]);
		}
	}

	const struct record *rounds = read_record("rounds");
	for (int row = 0; row < rounds->rows; row++) {
		const char(*f)[FIELD] = rounds->field[row];
		int when = day_number(parse_day(f[0]));
		if (when < from || when >= to || when == day_number(calendar("Founded")))
			continue;
		printf("%s  New timetable: %s %s round leaves at %s for %s.\n", f[0], f[2], f[3], f[4], f[5]);
	}

	const struct record *institutions = read_record("institutions");
	for (int row = 0; row < institutions->rows; row++) {
		const char(*f)[FIELD] = institutions->field[row];
		int when = day_number(parse_day(f[2]));
		if (when >= from && when < to && when != day_number(calendar("Founded")))
			printf("%s  The %s is founded, at %s.\n", f[2], f[0], f[1]);
	}
}

int main(int argc, char **argv)
{
	struct day end = census_day();
	for (int i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--all") == 0)
			everything = 1;
		else if (strcmp(argv[i], "--society") == 0)
			secrets = 1;
		else
			end = parse_day(argv[i]);
	}

	struct town town;
	found(&town);
	struct day founded = month_day(town.month);
	printf("%04d-%02d-%02d  The town is founded.\n", founded.y, founded.m, founded.d);
	chronicle = tell;
	while (town.month <= month_of(end)) {
		works(town.month);
		if (town.month == month_of(end))
			break;
		advance(&town);
	}
	chronicle = NULL;

	char text[64];
	greeting_on(end, text, sizeof text);
	printf("%04d-%02d-%02d  The dead letter office opens the post: %s\n", end.y, end.m, end.d,
	       text[0] ? text : "(nothing)");
	return 0;
}