XXIIVV

Seconth, or 2th, is a plain-text calendar with events.

Second is a calendar utility created to overlay events on pages of the gregorian calendar, written in ANSI C.

Event Format

The events are stored in text files in the format:

20201126 Sailing trip to Port Townsend

2th.c

#include <stdio.h>
#include <time.h>

/* 
Copyright (c) 2020 Devine Lu Linvega

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#define EVENTMAX 1024

typedef struct Event {
	int y, m, d;
	char name[256];
} Event;

typedef struct Calendar {
	int y, m, d, len;
	Event events[EVENTMAX];
} Calendar;

char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

int
slen(char *s)
{
	int i = 0;
	while(s[++i])
		;
	return i;
}

int
sint(char *s, int len)
{
	int n = 0, i = 0;
	while(i < len && s[i] && s[i] >= '0' && s[i] <= '9') {
		n = n * 10 + (s[i] - '0');
		i++;
	}
	return n;
}

char *
scpy(char *src, char *dst, int len)
{
	int i = 0;
	while(i < len - 1 && (dst[i] = src[i]))
		i++;
	return dst;
}

int
daysmonth(int y, int m)
{
	if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
		return 31;
	else if((m == 2) && ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)))
		return 29;
	else if(m == 2)
		return 28;
	else
		return 30;
	return 0;
}

int
dayweek(int y, int m, int d)
{
	int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
	y -= m < 3;
	return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}

void
setevent(Calendar *c, int y, int m, int d, char *name)
{
	Event *e = &c->events[c->len++];
	scpy(name, e->name, slen(name));
	e->y = y;
	e->m = m;
	e->d = d;
}

Event *
getevent(Calendar *c, int y, int m, int d)
{
	int i;
	for(i = 0; i < c->len; i++) {
		Event *e = &c->events[i];
		if(e->y == y && e->m == m && e->d == d)
			return e;
	}
	return NULL;
}

void
printevents(Calendar *c, int y, int m, int d)
{
	int i, l = c->m;
	for(i = 0; i < 90; i++) {
		Event *e = getevent(c, y, m, d);
		if(e) {
			if(l != m)
				puts("");
			if(i == 0)
				printf(" Today        ");
			else if(i == 1)
				printf(" Tomorrow     ");
			else
				printf(" In %02d days   ", i);
			puts(e->name);
			l = m;
		}
		d++;
		if(d > daysmonth(y, m)) {
			d = 1;
			m++;
			if(m > 12) {
				m = 1;
				y++;
			}
		}
	}
	puts("");
}

void
printcalendar(Calendar *c)
{
	int i;
	int dw = dayweek(c->y, c->m, 1);
	int dm = daysmonth(c->y, c->m);
	printf(" %s %d\n", months[c->m - 1], c->y);
	printf(" Su  Mo  Tu  We  Th  Fr  Sa ");
	for(i = 0; i <= 35; i++) {
		int d = i - dw + 1;
		if(i % 7 == 0)
			puts("");
		if(d == c->d)
			printf("<%2d>", d);
		else if(getevent(c, c->y, c->m, d))
			printf("[%2d]", d);
		else if(d > 0 && d <= dm)
			printf(" %2d ", d);
		else
			printf("    ");
	}
	puts("");
}

void
loadevents(FILE *f, Calendar *c)
{
	char line[256];
	if(!f)
		return;
	while(fgets(line, 256, f)) {
		if(line[0] == ';' || slen(line) < 12)
			continue;
		if(c->len >= EVENTMAX)
			break;
		setevent(c,
			line[0] == '*' ? c->y : sint(line, 4),
			line[5] == '*' ? c->m : sint(line + 4, 2),
			line[7] == '*' ? c->d : sint(line + 6, 2),
			line + 9);
	}
	fclose(f);
}

void
init(Calendar *cal)
{
	time_t t;
	struct tm *local;
	time(&t);
	local = localtime(&t);
	cal->y = local->tm_year + 1900;
	cal->m = local->tm_mon + 1;
	cal->d = local->tm_mday;
}

int
main(int argc, char *argv[])
{
	Calendar cal;
	init(&cal);
	loadevents(fopen(argc > 1 ? argv[1] : "calendar", "r"), &cal);
	printcalendar(&cal);
	printevents(&cal, cal.y, cal.m, cal.d);
	return 0;
}
— Submit an edit to 2th.c.txt(198 lines)

incoming(1): computer

Last update on 14X09, edited 2 times. +9/14fh -----|