#include #include #include #include #include inline char *get_filename(const char c) { char *qout; switch(c) { default: qout = "queries/mranks.sql"; break; case 'm': qout = "queries/mleaderboard-ojoin.sql"; break; case 'p': qout = "queries/rplayers.sql"; break; case 't': qout = "queries/test.txt"; break; } return qout; } inline void print_tblheader(const char *c) { char *out; switch (*c) { default: out = "\ \ \ \ \ \ \ "; break; case 'm': out = "


Map List

NameRecordsBest TimeHeld By
\ \ \ \ \ \ "; break; case 'p': out = "


Leaderboard

RankNameTime
\ \ \ \ \ \ "; break; } printf("%s", out); } //display and format the results of the query. void qresult(sqlite3_stmt *sp, const char *c) { int e; unsigned int i; unsigned int cc = sqlite3_column_count(sp); print_tblheader(c); while ((e = sqlite3_step(sp)) == SQLITE_ROW) { printf(""); for (i = 0; i < cc; i++) { printf("", sqlite3_column_text(sp, i)); } printf(""); } printf("


Ranks

NameMapRank
%s
"); } bool executequery(const char *sql, const char *str) { const char *p = strchr(str, '=') + 1; const char qc = str[0]; sqlite3 *db; char *error; int con = sqlite3_open("db/cts.db", &db); if (con != SQLITE_OK) { fprintf(stderr, "Can not open database: %s\n", sqlite3_errmsg(db)); return false; } sqlite3_stmt *s; // prepare sql to s, reading until null character. con = sqlite3_prepare_v2(db, sql, -1, &s, NULL); if (con != SQLITE_OK) { fprintf(stderr, "Could not prepare SQL statement: %s\n", sqlite3_errmsg(db)); return false; } // bind parameter with a string length that is the # of bytes to null char. con = sqlite3_bind_text(s, 1, p, -1, NULL); qresult(s, &qc); sqlite3_finalize(s); sqlite3_close(db); return true; } // both allocates and frees memory used up by the string.. // containing the data from query files. void getquery(const char *qs) { char *qf, *qp; char *fname = get_filename(qs[0]); if (fname != NULL) { FILE *f = fopen(fname, "r"); if (f != NULL && qf != NULL) { // change from fseek/SEEK_END when sql queries are >2 GB in size. fseek(f, 0, SEEK_END); // go to the end of file unsigned int size = ftell(f); qf = calloc(size, sizeof(char)); if (qf != NULL) { fseek(f, 0, SEEK_SET); // go to the start of file fread(qf, sizeof(char), size, f); } } fclose(f); } qp = strchr(qs, '=') + 1; executequery(qf, qs); free(qf); } void html(void) { const char *html_top = "Content-Type: text/html\n\n\ \ \ /v/ - Xonotic\ "; const char *html_bot = "

Pages under construction.
\ Service may sporadically become unavailable.
\ In-game database is not directly synced with this web server.\

\ "; printf("%s", html_top); // getquery(getenv("QUERY_STRING")); getquery("map=pornstar-kaine"); printf("%s", html_bot); } int main(void) { html(); // getquery("player=duHTyaSGpdTk7oebwPFoo899xPoTwP9bja4DUjCjTLo="); }