summary refs log blame commit diff stats
path: root/drawn/monthly.c
blob: 1734cea9c78e6ff627953f8d51005f0402cc8043 (plain) (tree)






































































































































































































































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

#define MAX  200
#define MIN 8

int
readbot(FILE *in, char *line)
{
	int ch, cnt;

	if ( fseek(in, -2, SEEK_CUR) != -1 ) {
		ch = fgetc(in);
		for ( cnt = 0; ch != '\n' && ftell(in) > 1; ++cnt ) {
			fseek(in, -2, SEEK_CUR);
			line[cnt] = ch;
			ch = fgetc(in);
		}
	}
	else
		return -1;

	line[cnt] = ch;

	return cnt;
}

void
revline(char *line, char *buffer, int cnt)
{
	int len;

	for (len = cnt; cnt >= 0; --cnt)
		buffer[len - cnt] = line[cnt];

	buffer[len] = '\0';
}

void
stripdate(char *out, char *in)
{
	int i;

	for ( i = 6; i < 16; ++i )
		out[i-6] = in[i];
	out[10] = '\0';	
}

char *
findyear(char *date)
{
	static char year[16];
	int i;

	for ( i = 0; i < 4; ++i )
		year[i] = date[i];
	year[i] = '\0';

	return year;
}

char *
findmonth(char *date)
{
	static char month[8];
	int i;

	for ( i = 5; i < 7; ++i )
		month[i-5] = date[i];
	month[i-5] = '\0';

	return month;
}

char *
findday(char *date)
{
	static char day[8];
	int i;

	for ( i = 8; i < 10; ++i )
		day[i-8] = date[i];
	day[i-8] = '\0';

	return day;
}

void
outall(FILE *out, FILE *in)
{
	char line[MAX];

	if ( out == 0 )
		return;

	while (fgets(line, MAX, in) != NULL)
		fprintf(out, "%s", line);
}

void
padh(char *year, char *month, char *day, FILE *out)
{
	char *humonth[] = {
		"mystery",
		"january",
		"february",
		"march",
		"april",
		"may",
		"june",
		"july",
		"august",
		"september",
		"october",
		"november",
		"december"
	};

	
	fprintf(out, "\t\t\t%s%s", "<h2>", year);
	fprintf(out, "%s%s ", "<br>", humonth[atoi(month)]);
	fprintf(out, "%d%s\n", atoi(day), "</h2>");
}

void
padimg(FILE *out, char *p)
{
	char site[32] = "https://kaa.neocities.org/";

	fprintf(out, "\t\t\t%s%s%s", "<a href=\"", site, p);
	fprintf(out, "%s\n", "\" target=\"_blank\">");
	fprintf(out, "\t\t\t\t%s%s%s", "<img src=\"", site, p);
	fprintf(out, "%s\n", "\" loading=\"lazy\">");
	fprintf(out, "\t\t\t</a>\n");
	fprintf(out, "\t\t\t<br>\n");
}

void
outnav(FILE *out, char *direction, char *linkfn)
{
	if ( out == 0 )
		return;

	if ( strcmp(linkfn, "") == 0 ) {
		fprintf(out, "\t\t\t%s", "<br>");
		fprintf(out, "%s", "<a style=\"color: #ffffec\"");
		fprintf(out, "%s", "href=\"");
	 } else {
		fprintf(out, "\t\t\t%s", "<br><a href=\"");
		fprintf(out, "%s%s", "/", linkfn);
	}

	fprintf(out, "%s", "\"><h2>");
	fprintf(out, "%s%s\n", direction, "</h2></a>");
}

int
main()
{
	FILE* header = fopen("header/drawn.txt", "r");
	FILE* in = fopen("files.json", "r");
	FILE* footer = fopen("footer/drawn.txt", "r");
	FILE *out = 0;
	char line[MAX], buffer[MAX];
	int cnt, nm;
	char *p;
	char pattern[16] = "Image/20";
	char date[16] = "0000-00-00";
	char pyear[16] = "0000", pmonth[8] = "00", pday[8] = "00";
	char outfn[32] = "", poutfn[32];
	char *year, *month, *day;

	if ( header == NULL || in == NULL || footer == NULL ) {
		puts("Ow!");
		return 1;
	}

	fseek(in, 0, SEEK_END);
	while ( (cnt = readbot(in, line)) != -1 ) {
		if (cnt < MIN)
			continue;
		revline(line, buffer, cnt);
		if ( ( p = strstr(buffer, pattern) ) == NULL )
			continue;
		p[strlen(p) - 1] = '\0';

		if (nm == 1) {
			padh(pyear, pmonth, pday, out);
			nm = 0;
		}

		stripdate(date, p);
		year = findyear(date);
		month = findmonth(date);
		day = findday(date);
		if ( strcmp(pyear, year) != 0 ||
		strcmp(pmonth, month) != 0 ) {
			outnav(out, "next", poutfn);
			strcpy(poutfn, outfn);
			strcpy(outfn, "drawn/");
			strcat(outfn, year);
			strcat(outfn, "-");
			strcat(outfn, month);
			strcat(outfn, ".html");
			outnav(out, "previous", outfn);
			outall(out, footer);
			fseek(footer, 0, SEEK_SET);
			out = fopen(outfn, "w");
			outall(out, header);
			fseek(header, 0, SEEK_SET);
		}

		if ( strcmp(pday, day) != 0 )
			nm = 1;

		padimg(out, p);

		strcpy(pyear, year);
		strcpy(pmonth, month);
		strcpy(pday, day);
	}
	outnav(out, "next", poutfn);
	outall(out, footer);

	fclose(header);
	fclose(in);
	fclose(footer);

	return 0;
}