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

spaces.cameron.stream/hello

Hello.

0
1
main
69 lines · 2.2 KB Raw History
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "census.h"
#include "town.h"

struct sender sender_of(const struct household *h, struct day day)
{
	struct sender s;
	snprintf(s.name, FIELD, "%s", h->name);
	snprintf(s.home, FIELD, "%s", h->home);
	if (h->kind == RESIDENT) {
		int wakes = weekday(day) >= 5 ? h->weekends : h->weekdays;
		s.ready = real_time(h, wakes, day) + h->writes;
		s.body[0] = north(h->home) ? h->initial : (char)tolower((unsigned char)h->initial);
		s.body[1] = '\0';
	} else {
		s.ready = real_time(h, h->weekdays, day);
		snprintf(s.body, sizeof s.body, "%s", h->sends);
	}
	return s;
}

int senders(struct day day, struct sender out[])
{
	if (day.d != 1)
		return 0;
	const struct town *town = town_on(day);
	int n = 0;
	for (int i = 0; i < town->lines; i++)
		if (active(&town->line[i], day) && !town->line[i].member)
			out[n++] = sender_of(&town->line[i], day);
	return n;
}

static void hhmm(FILE *out, int minutes)
{
	fprintf(out, "%02d:%02d", minutes / 60, minutes % 60);
}

void take_census(FILE *out, struct day day)
{
	const struct town *town = town_on(day);
	fprintf(out, "# Census of the town, taken on %04d-%02d-%02d.\n", day.y, day.m, day.d);
	fprintf(out, "# Nobody wrote this. It is what the history says the town was, that day.\n");
	fprintf(out, "# Name | lives at | wakes on weekdays | wakes at weekends | minutes writing"
		     " | clock fast by | clock last set | generation\n");
	for (int i = 0; i < town->lines; i++) {
		const struct household *h = &town->line[i];
		if (h->kind != RESIDENT)
			continue;
		fprintf(out, "%s | %s | ", h->name, h->home);
		hhmm(out, h->weekdays);
		fputs(" | ", out);
		hhmm(out, h->weekends);
		fprintf(out, " | %d | %d | %04d-%02d-%02d | %d\n", h->writes, fast(h, day), h->set.y, h->set.m,
			h->set.d, h->generation + 1);
	}
	fprintf(out, "# Institutions. Name | at | leaves to post | clock fast by | clock last set\n");
	for (int i = 0; i < town->lines; i++) {
		const struct household *h = &town->line[i];
		if (h->kind == RESIDENT || !active(h, day))
			continue;
		fprintf(out, "%s | %s | ", h->name, h->home);
		hhmm(out, h->weekdays);
		fprintf(out, " | %d | %04d-%02d-%02d\n", fast(h, day), h->set.y, h->set.m, h->set.d);
	}
}