#include #include #include #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", "

", year); fprintf(out, "%s%s ", "
", humonth[atoi(month)]); fprintf(out, "%d%s\n", atoi(day), "

"); } void padimg(FILE *out, char *p) { char site[32] = "https://kaa.neocities.org/"; fprintf(out, "\t\t\t%s%s%s", ""); fprintf(out, "\t\t\t\t%s%s%s", ""); fprintf(out, "\t\t\t\n"); fprintf(out, "\t\t\t
\n"); } void outnav(FILE *out, char *direction, char *linkfn) { if ( out == 0 ) return; if ( strcmp(linkfn, "") == 0 ) { fprintf(out, "\t\t\t%s", "
"); fprintf(out, "%s", "

"); fprintf(out, "%s%s\n", direction, "

"); } int main() { FILE* header = fopen("header/photographed.txt", "r"); FILE* in = fopen("files.json", "r"); FILE* footer = fopen("footer/photographed.txt", "r"); FILE *out = 0; char line[MAX], buffer[MAX]; int cnt, nm; char *p; char pattern[32] = "Photo/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, "photographed/"); 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; }