From 6ffde080eb369acd4f0147feb27fa690455b7689 Mon Sep 17 00:00:00 2001 From: nishi Date: Mon, 29 Jan 2024 00:07:39 +0000 Subject: [PATCH] updating w3b git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@125 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/w3b/w3b.c | 55 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/Example/w3b/w3b.c b/Example/w3b/w3b.c index 79b16ac..d01f120 100644 --- a/Example/w3b/w3b.c +++ b/Example/w3b/w3b.c @@ -17,12 +17,41 @@ #include #include +#ifdef __MINGW32__ +#include +HANDLE winstdout; +#else +#include +#endif + +void clear_console(){ +#ifdef __MINGW32__ + DWORD written = 0; + const char* seq = "\x1b[2J\x1b[1;1H"; + WriteConsole(winstdout, seq, strlen(seq), &written, NULL); +#else + printf("\x1b[2J\x1b[1;1H"); + fflush(stdout); +#endif +} + +void get_terminal_size(int* width, int* height){ +#ifdef __MINGW32__ + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(winstdout, &csbi); + *width = csbi.dwSize.X; + *height = csbi.dwSize.Y; +#else + struct winsize ws; + ioctl(0, TIOCGWINSZ, &ws); + *width = ws.ws_col; + *height = ws.ws_row; +#endif +} + char* databuf; int datalen; -void status_handler(struct W3* w3, int status) { printf("Response code: %d\n", status); } -void header_handler(struct W3* w3, char* key, char* value) { printf("Header: %s: %s\n", key, value); } - void data_handler(struct W3* w3, char* data, size_t size) { if(databuf == NULL) { databuf = malloc(size); @@ -51,8 +80,6 @@ void access_site(const char* url) { W3_Set_Method(w3, "GET"); W3_Set_Path(w3, u->path); W3_HTTP_Enable_Redirect(w3); - W3_On(w3, "status", (void*)status_handler); - W3_On(w3, "header", (void*)header_handler); W3_On(w3, "data", (void*)data_handler); W3_Send_Request(w3); W3_Free(w3); @@ -95,10 +122,18 @@ int main(int argc, char** argv) { access_site(url); acc = true; } +#ifdef __MINGW32__ + winstdout = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD mode = 0; + GetConsoleMode(winstdout, &mode); + const DWORD origmode = mode; + mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + SetConsoleMode(winstdout, mode); +#endif + clear_console(); while(true) { /* Loop */ if(c != '\n' && c != '\r') { printf("(O)pen, (Q)uit"); - if(acc) printf(", (P)rint all"); printf("? "); fflush(stdout); } @@ -114,11 +149,6 @@ int main(int argc, char** argv) { scanf("%s", url); acc = false; break; - case 'p': - if(acc) { - write(1, databuf, datalen); - } - break; case '\n': case '\r': break; @@ -133,4 +163,7 @@ int main(int argc, char** argv) { } printf("\n"); exitnow:; +#ifdef __MINGW32__ + SetConsoleMode(winstdout, origmode); +#endif } -- 2.43.0