htdocs: refactoring and improving web archive

This commit is contained in:
Daniel Marjamäki 2013-07-07 10:59:57 +02:00
parent 23eaedca27
commit 445be5bd45
1 changed files with 38 additions and 10 deletions

View File

@ -7,27 +7,55 @@
#define MAX_RECORDS 1000 #define MAX_RECORDS 1000
void listAll(char **data) {
puts("Content-type: text/html\r\n\r\n");
puts("<html><meta http-equiv=\"Pragma\" content=\"no-cache\"><body>\n");
puts("<table>\n");
for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
const char *name = getname(data[i]);
printf("<tr><td>%s</td><td>Delete this</td></tr>\n", name);
}
puts("<table>\n");
puts("</body></html>");
}
void listOne(char **data, const char name[]) {
int index = -1;
for (int i = 0; i < MAX_RECORDS && data[i]; i++) {
if (strcmp(getname(data[i]), name)==0) {
index = i;
break;
}
}
puts("Content-type: text/plain\r\n\r\n");
puts((index == -1) ? "Not found" : data[index]);
}
int main() { int main() {
char *data[MAX_RECORDS] = {0}; char *data[MAX_RECORDS] = {0};
// read // read
if (!readdata(data, MAX_RECORDS)) { if (!readdata(data, MAX_RECORDS)) {
printf("Internal error: failed to load data"); puts("Content-type: text/html\r\n\r\n");
puts("Internal error: failed to load data");
return 0;
} }
// sort // sort
sortdata(data,MAX_RECORDS); sortdata(data,MAX_RECORDS);
// output const char *query_string = getenv("QUERY_STRING");
printf("Content-type: text/html\r\n\r\n"); if (query_string == NULL) {
printf("<html><meta http-equiv=\"Pragma\" content=\"no-cache\"><body>\n"); listAll(data);
printf("<table>\n"); } else if (strncmp(query_string, "name=", 5) == 0 && getname(query_string) != NULL) {
for (int i = 0; i < MAX_RECORDS && data[i]; i++) { char name[32] = {0};
const char *name = getname(data[i]); strcpy(name, getname(query_string));
printf("<tr><td>%s</td><td>Delete this</td></tr>\n", name); listOne(data,name);
} else {
puts("Content-type: text/plain\r\n\r\n");
puts("Invalid query");
} }
printf("<table>\n");
printf("</body></html>");
return 0; return 0;
} }