int render_html(const char *html_fn, render_cb r, void *arg) { FILE *f; char s[8192]; char fn[MAXPATHLEN]; const char *s2; if ((f = fopen(html_fn, "r")) == NULL) { printf("ERROR: fopen: %s: %s<br>\n", html_fn, strerror(errno)); return (1); } while (fgets(s, sizeof(s), f)) { char *a, *b; for (a = s; (b = strstr(a, "%%")) != NULL;) { *b = 0; printf("%s", a); a = b + 2; if ((b = strstr(a, "%%")) != NULL) { *b = 0; if (!strcmp(a, "ACTION")) { if ((s2 = get_query_param(q, "action")) == NULL) s2 = "front"; printf("%s", s2); } else if (!strcmp(a, "BASEURL")) printf("%s", conf.baseurl); else if (!strcmp(a, "BASEDIR")) printf("%s", conf.htmldir); else if (!strcmp(a, "BASECSS")) { printf("%s?action=css%s", conf.baseurl, display_type == DISPLAY_PRINT ? "&display=print" : display_type == DISPLAY_CSV ? "&display=csv" : ""); } else if (!strcmp(a, "TITLE")) printf("%s", conf.title); else if (!strcmp(a, "HEADER")) { snprintf(fn, sizeof(fn), "%s/header.html", conf.htmldir); render_html(fn, NULL, NULL); } else if (!strcmp(a, "FOOTER")) { snprintf(fn, sizeof(fn), "%s/footer.html", conf.htmldir); render_html(fn, NULL, NULL); } else if (!strcmp(a, "ROW")) { printf("row%d", rowselect); rowselect = !rowselect; } else if (!strcmp(a, "ROWRESET")) rowselect = 0; else if (!strcmp(a, "TIMESTART")) printf("%s", time_print(time_start)); else if (!strcmp(a, "TIMESTOP")) printf("%s", time_print(time_stop)); else if (!strcmp(a, "TIMENOW")) { char datetime[sizeof("9999-12-31T23:59:60+0000")]; time_t now = time(NULL); strftime(datetime, sizeof(datetime), "%FT%T%z", localtime(&now)); printf("%s", datetime); } else if (!strcmp(a, "TIMETODAY")) printf("%s", time_print(time_ext(TIME_TODAY))); else if (!strcmp(a, "TIMEYESTERDAY")) printf("%s", time_print(time_ext(TIME_YESTERDAY))); else if (!strcmp(a, "TIMETOMORROW")) printf("%s", time_print(time_ext(TIME_TOMORROW))); else if (!strcmp(a, "TIMETHISWEEK")) printf("%s", time_print(time_ext(TIME_THISWEEK))); else if (!strcmp(a, "TIMETHISMONTH")) printf("%s", time_print(time_ext(TIME_THISMONTH))); else if (!strcmp(a, "TIMETHISYEAR")) printf("%s", time_print(time_ext(TIME_THISYEAR))); else if (!strcmp(a, "TIMELASTWEEK")) printf("%s", time_print(time_ext(TIME_LASTWEEK))); else if (!strcmp(a, "TIMELASTMONTH")) printf("%s", time_print(time_ext(TIME_LASTMONTH))); else if (!strcmp(a, "TIMELASTYEAR")) printf("%s", time_print(time_ext(TIME_LASTYEAR))); else if (!strcmp(a, "QUERY")) printf("%s", q->query_string); #if 0 else if (!strcmp(a, "RCSID")) printf("%s", rcsid); #endif else if (r != NULL) (*r)(a, arg); a = b + 2; } } printf("%s", a); } fclose(f); return 0; }
int main() { struct query *q = get_query(); const char *game_id = get_query_param(q, "game_id"); FILE *f, *fm; char s[8192], p1[256], p2[256], q1[256], q2[256], id[256], r[256]; int w, h; memset(id, 0, sizeof(p1)); memset(q1, 0, sizeof(p1)); memset(q2, 0, sizeof(p1)); // printf("HTTP/1.0 200 OK\n"); printf("Content-Type: text/plain\n"); printf("Connection: close\n"); printf("\n"); /* f = fopen("441", "r"); while (fgets(s, sizeof(s), f)) printf("%s", s); return 0; */ if (strchr(game_id, '|')) { if (sscanf(game_id, "%255[^|]|%255[^|]|%255[^|]", id, q1, q2) != 3) { printf("Error: invalid game_id %s\n", game_id); goto done; } } else strlcpy(id, game_id, sizeof(id)); f = fopen(fn, "r"); while (fgets(s, sizeof(s), f)) { if (!strncmp(s, "[White \"", 8)) { memset(p1, 0, sizeof(p1)); sscanf(s + 8, "%255[^\"]", p1); } if (!strncmp(s, "[Black \"", 8)) { memset(p2, 0, sizeof(p2)); sscanf(s + 8, "%255[^\"]", p2); } if (!strncmp(s, "[Result \"", 9)) { memset(r, 0, sizeof(r)); sscanf(s + 9, "%255[^\"]", r); } if (!strncmp(s, id, strlen(id)) && (!q1[0] || !strcmp(p1, q1)) && (!q2[0] || !strcmp(p2, q2))) { /* map filename */ fgets(s, sizeof(s), f); s[strlen(s) - 1] = 0; int mapid = 0; sscanf(s, "maps/map%d.txt", &mapid); printf("game_id=%s\nwinner=0\nloser=0\nmap_id=%d\n" "draw=%s\n", id, mapid, strcmp(r, "1/2-1/2") ? "0" : "1"); printf("timestamp=%s\nplayer_one=%s\nplayer_two=%s\n", timestamp(atol(id)), p1, p2); /* game_id=4414272 winner=123465 loser=123485 map_id=741 draw=0 timestamp=2010-09-08 01:42:00 player_one=phreeza player_two=UloPee playback_string= */ if ((fm = fopen(s, "r")) == NULL) { printf("Error: fopen: %s: %s", fm, strerror(errno)); goto done; } printf("playback_string="); int planets = 0; while (fgets(s, sizeof(s), fm)) { double x, y; int owner, ships, growth; /* P 11.6742243424 11.649441005 0 56 3 P 15.5644784882 23.29888201 1 100 5 P 7.7839701966 0.0 2 100 5 */ if (s[0] != 'P' || s[1]!= ' ' || sscanf(s + 2, "%lf %lf %d %d %d", &x, &y, &owner, &ships, &growth) != 5) { printf("Error: sscanf: %s: %s", fm, s); goto done; } if (planets++) printf(":"); printf("%f,%f,%d,%d,%d", x, y, owner, ships, growth); } fclose(fm); int turns = 0; while (fgets(s, sizeof(s), f)) { s[strlen(s) - 1] = 0; if (!s[0]) break; printf("%c%s", turns++ ? ':' : '|', s); } printf("\n"); break; } } done: fclose(f); fflush(stdout); return (0); }