~rabbits/seconth

seconth/2th.c -rw-r--r-- 3.4 KiB
e48d7649neauoire Extended length 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#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;
}