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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#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;
}
|