From c8b602d7a357dda85fadcb8da1bd015577304acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Jul 2013 09:15:35 +0200 Subject: [PATCH] htdocs: updated web archive report script so only name is listed --- htdocs/archive/report.c | 76 +++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/htdocs/archive/report.c b/htdocs/archive/report.c index 464033d6e..3782af689 100644 --- a/htdocs/archive/report.c +++ b/htdocs/archive/report.c @@ -1,20 +1,76 @@ +#include #include +#include + +#define MAX_RECORDS 1000 + +int readdata(char * * const data, int sz) +{ + FILE *f = fopen("data.txt", "rt"); + if (!f) + return 0; // failed + + char line[10000] = {0}; + int i = 0; + while (i < sz && fgets(line,sizeof(line)-2,f)) { + if (strncmp(line, "name=", 5) == 0) { + data[i] = malloc(strlen(line)); + strcpy(data[i], line); + i++; + } + } + fclose(f); + + return 1; // success +} + +const char * getname(const char *data) { + static char name[32]; + if (strncmp(data,"name=",5) != 0) + return NULL; + int i = 0; + while (i < sizeof(name) && data[i+5] && data[i+5] != '&') { + name[i] = data[i+5]; + i++; + } + if (i >= sizeof(name)) + return NULL; + while (i < sizeof(name)) + name[i++] = 0; + return name; +} int main() { - char line[4096] = {0}; - printf("Content-type: text/plain\n\n"); + char *data[MAX_RECORDS] = {0}; - FILE *f = fopen("data.txt", "rt"); - if (f) { - int c; - while (EOF != (c = fgetc(f))) - printf("%c", c); - fclose(f); - } else { - printf("failed to open data\n"); + // read + if (!readdata(data, MAX_RECORDS)) { + printf("Internal error: failed to load data"); } + // sort + for (int i = 1; i < MAX_RECORDS && data[i]; i++) { + if (strcmp(data[i-1],data[i]) > 0) { + char *p = data[i-1]; + data[i-1] = data[i]; + data[i] = p; + if (i > 1) + i--; + } + } + + // output + printf("Content-type: text/html\r\n\r\n"); + printf("\n"); + printf("\n"); + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + const char *name = getname(data[i]); + printf("\n", name); + } + printf("
%sDelete this
\n"); + printf(""); + return 0; }