|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <time.h> |
| 5 | + |
| 6 | +#include "webarchive.h" |
| 7 | + |
| 8 | +#define MAX_RECORDS 1000 |
| 9 | + |
| 10 | +void listAll(char **data) |
| 11 | +{ |
| 12 | + puts("Content-type: text/html\r\n\r\n"); |
| 13 | + puts("<html>"); |
| 14 | + puts("<head>"); |
| 15 | + puts("<meta http-equiv=\"Pragma\" content=\"no-cache\">"); |
| 16 | + puts("<script>"); |
| 17 | + puts("function addfile() {"); |
| 18 | + puts(" var name = prompt(\"Name of library/platform/etc\", \"\");"); |
| 19 | + puts(" if (name != null)"); |
| 20 | + puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/addfile.cgi?name=\" + name + \"&version=\" + version;"); |
| 21 | + puts("}"); |
| 22 | + puts("function editfile(name,version) {\n"); |
| 23 | + puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/edit.cgi?name=\" + name + \"&version=\" + version;\n"); |
| 24 | + puts("}"); |
| 25 | + puts("function renamefile(name1,version) {\n"); |
| 26 | + puts(" var name2 = prompt(\"Name\", name1);\n"); |
| 27 | + puts(" if (name2 != null)\n"); |
| 28 | + puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/renamefile.cgi?name1=\" + name1 + \"&name2=\" + name2;\n"); |
| 29 | + puts("}\n"); |
| 30 | + puts("function deletefile(name,version) {\n"); |
| 31 | + puts(" window.location = \"http://cppcheck.sf.net/cgi-bin/deletefile.cgi?name=\" + name + \"&version=\" + version;\n"); |
| 32 | + puts("}"); |
| 33 | + puts("</script>"); |
| 34 | + puts("</head><body>"); |
| 35 | + puts("<input type=\"button\" onclick=\"addfile()\" value=\"Add file\">"); |
| 36 | + puts("<table border=\"1\"><tr><td><table>"); |
| 37 | + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { |
| 38 | + const char *name = getname(data[i]); |
| 39 | + int version = getversion(data[i]); |
| 40 | + if (version < 0) |
| 41 | + version = time(0); |
| 42 | + if (i > 0) |
| 43 | + printf("<tr height=\"1\"><td colspan=\"2\" bgcolor=\"gray\"></td></tr>"); |
| 44 | + printf("<tr><td width=\"200\">%s</td>", name); |
| 45 | + printf("<td><input type=\"button\" onclick=\"editfile(\'%s\','%i')\" value=\"Edit\">", name, version); |
| 46 | + printf("<input type=\"button\" onclick=\"renamefile(\'%s\','%i')\" value=\"Rename\">", name, version); |
| 47 | + printf("<input type=\"button\" onclick=\"deletefile(\'%s\','%i')\" value=\"Delete\"> </td>", name, version); |
| 48 | + printf("</tr>\n"); |
| 49 | + } |
| 50 | + puts("</table></td></tr></table>"); |
| 51 | + puts("</body></html>"); |
| 52 | +} |
| 53 | + |
| 54 | +void listOne(char **data, const char name[]) |
| 55 | +{ |
| 56 | + int index = -1; |
| 57 | + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { |
| 58 | + if (strcmp(getname(data[i]), name)==0) { |
| 59 | + index = i; |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + puts("Content-type: text/plain\r\n\r\n"); |
| 65 | + puts((index == -1) ? "Not found" : data[index]); |
| 66 | +} |
| 67 | + |
| 68 | +int main() |
| 69 | +{ |
| 70 | + char *data[MAX_RECORDS] = {0}; |
| 71 | + |
| 72 | + // read |
| 73 | + if (!readdata(data, MAX_RECORDS)) { |
| 74 | + puts("Content-type: text/html\r\n\r\n"); |
| 75 | + puts("Internal error: failed to load data"); |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + // sort |
| 80 | + sortdata(data,MAX_RECORDS); |
| 81 | + |
| 82 | + const char *query_string = getenv("QUERY_STRING"); |
| 83 | + if (query_string == NULL || *query_string == '\0') { |
| 84 | + listAll(data); |
| 85 | + } else if (strncmp(query_string, "name=", 5) == 0 && getname(query_string) != NULL) { |
| 86 | + char name[32] = {0}; |
| 87 | + strcpy(name, getname(query_string)); |
| 88 | + listOne(data,name); |
| 89 | + } else { |
| 90 | + puts("Content-type: text/plain\r\n\r\n"); |
| 91 | + puts("Invalid query"); |
| 92 | + } |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
0 commit comments