#include #include #include #include #define MAX_POST_SIZE 512 #define HEX_ESCAPE '%' #define FROMASCII(c) (c) int sudoku[10][10]; char HTAsciiHexToChar(char c) { return c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F'? c - 'A' + 10 : c - 'a' + 10; } char* HTUnEscape(char* str) { char * p = str; char * q = str; if (!str) { return NULL; } while(*p) { if (*p == HEX_ESCAPE) { p++; if (*p) *q = HTAsciiHexToChar(*p++) * 16; if (*p) *q = FROMASCII(*q + HTAsciiHexToChar(*p)); p++, q++; } else { *q++ = *p++; } } *q++ = 0; return str; } void plusses_to_spaces(char* s) { if (!s) return; for(; *s; s++) if (*s == '+') *s=' '; } void fill_sudoku(char* query_string) { char* cur; int i = 0; int cnt = 0; if (query_string) { cur = query_string; while (cur) { char *end1 = strchr(cur, '='); char *end2 = (end1? strchr(end1, '&') : NULL); if (end1) { *(end1++) = 0; if (end2) { *(end2++) = 0; } HTUnEscape(cur); plusses_to_spaces(end1); HTUnEscape(end1); //printf("found %s=%s\n", cur, end1); if (strncmp(cur, "cxx", 1) == 0 && strlen(cur) == 3) { // get the row and column int i = (int)(cur[1] - '0'); int j = (int)(cur[2] - '0'); int v = atoi(end1); if (i >= 1 && i <= 9 && j >= 1 && j <= 9 && v >= 1 && v <= 9) { // TODO: fill SUDOKU HERE sudoku[i][j] = v; //printf("setting sudoku[%d][%d]=%d\n", i, j, v); } } cnt++; } cur = end2; } } } void print_sudoku() { int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { printf("%3d", sudoku[i][j]); } printf("\n"); } } void print_start_html_page() { printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("SUDOKU\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("
\n"); printf("

SUDOKU

\n"); printf("
\n"); } void print_end_html_page() { printf("

Try again!

"); printf("
\n"); printf("
\n"); printf("\n"); printf("\n"); } void print_error_html_message() { printf("

We are very sorry!

\n"); printf("

Some error in the aplication has ocurred and your sudoku cannot be accomplished. Please check all the numbers are well copied.

\n"); } void print_sudoku_html_page() { int i, j; for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { printf("\n", i, j, (i%3 == 1)? "-top" : (i%3 == 0)? "-bottom" : "", (j%3 == 1)? "-left" : (j%3 == 0)? "-right" : "", sudoku[i][j]); } printf("
\n"); } } void print_error_html_page() { print_start_html_page(); print_error_html_message(); print_end_html_page(); } void print_ok_html_page() { print_start_html_page(); print_sudoku_html_page(); print_end_html_page(); } int main(int argc, char** args) { char input[MAX_POST_SIZE]; char* request_method = getenv("REQUEST_METHOD"); char* query_string = getenv("QUERY_STRING"); memset(&sudoku, 0, sizeof(sudoku)); if (request_method != NULL && strcmp(request_method, "POST") == 0) { // read from stdin char* lenstr = getenv("CONTENT_LENGTH"); if (lenstr == NULL) { print_error_html_page(); return; } long len = 0; if (sscanf(lenstr,"%ld",&len) != 1) { print_error_html_page(); return; } if (len == 0 || len >= MAX_POST_SIZE) { print_error_html_page(); return; } if (fgets(input, len+1, stdin) == NULL) { print_error_html_page(); return; } fill_sudoku(input); } else { // read from query_string fill_sudoku(query_string); } // // TODO: code HERE // print_ok_html_page(); }