summaryrefslogtreecommitdiff
path: root/journal.c
blob: 4e316ddc78442871f1e97121e3c2caca8c0aac30 (plain) (blame)
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
#define _XOPEN_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define TITLE u8"david’s journal"
#define ENTRY_DIR "entries"
#define MAX_ENTRIES 1024

static char *entries[MAX_ENTRIES];
static int num_entries;

static void load_entries(char *dir, size_t *max_fn_size)
{
	DIR *d = opendir(dir);
	if (!d) exit(1);

	struct dirent *de;
	while ((de = readdir(d))) {
		if (de->d_name[0] == '.') continue;
		if (num_entries == MAX_ENTRIES) exit(1);

		size_t fn_size = strlen(de->d_name) + 1;
		if (fn_size > *max_fn_size)
			*max_fn_size = fn_size;
		char *entry = malloc(fn_size);
		memcpy(entry, de->d_name, fn_size);
		entries[num_entries++] = entry;
	}

	closedir(d);
}

static void print_entry(char *path, char *ts)
{
	FILE *f = fopen(path, "r");
	if (!f) return;

	struct tm tm;
	strptime(ts, "%s", &tm);
	printf(
		"<div class='entry'><p><strong>%d/%02d/%02d</strong>&emsp;",
		tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);

	for (int c = getc(f), nl = 0; c != EOF; c = getc(f)) {
		if (c == '\n') {
			++nl;
			continue;
		}

		if (nl > 0) {
			printf(nl == 1 ? "<br>" : "</p><p>");
			nl = 0;
		}

		putchar(c);
	}

	fclose(f);
	puts("</p></div>");
}

static int entry_cmp(const void *a, const void *b)
{
	return atoi(*(const char **)b) - atoi(*(const char **)a);
}

int main()
{
	size_t max_fn_size = 0;
	load_entries(ENTRY_DIR, &max_fn_size);
	qsort(entries, num_entries, sizeof(entries[0]), entry_cmp);

	puts(
		"<!DOCTYPE html>\n"
		"<html lang='en'>\n"
		"<head>\n"
		"<meta charset='utf-8'>\n"
		"<meta name='viewport' content='width=device-width, initial-scale=1'>\n"
		"<link href='style.css' rel='stylesheet'>\n"
		"<title>"TITLE"</title>\n"
		"</head>\n"
		"<body>\n"
		"<h1>"TITLE"</h1>");

	char *path = malloc(sizeof(ENTRY_DIR) + max_fn_size);
	memcpy(path, ENTRY_DIR"/", sizeof(ENTRY_DIR));
	for (int i = 0; i < num_entries; ++i) {
		strcpy(path + sizeof(ENTRY_DIR), entries[i]);
		print_entry(path, entries[i]);
		free(entries[i]);
	}

	free(path);
	puts(
		"</body>\n"
		"</html>");

	return 0;
}