From c33aa2819ddc5d660e2ebed9d5b672ed39917a45 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 24 Jan 2024 11:42:23 +0000 Subject: [PATCH] format git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@57 d27a3e52-49c5-7645-884c-6793ebffc270 --- .clang-format | 11 +++++++ Example/fetch.c | 24 ++++++-------- Library/Core.c | 81 +++++++++++++++++++++++------------------------- Library/DNS.c | 29 +++++++++-------- Library/File.c | 24 +++++++------- Library/HTTP.c | 70 ++++++++++++++++++++--------------------- Library/Util.c | 40 ++++++++++++------------ Library/W3Core.h | 50 +++++++++++++++--------------- Library/W3DNS.h | 7 ++--- Library/W3File.h | 4 +-- Makefile | 7 +++-- 11 files changed, 176 insertions(+), 171 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..4fbff0c --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +--- +# $Id$ +Language: Cpp +UseTab: Always +TabWidth: 8 +IndentWidth: 8 +PointerAlignment: Left +ColumnLimit: 1024 +AllowShortIfStatementsOnASingleLine: Always +AllowShortLoopsOnASingleLine: True +SpaceBeforeParens: Never diff --git a/Example/fetch.c b/Example/fetch.c index f8a60a3..393ff29 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -12,34 +12,30 @@ FILE* f; -void fetch_data(struct W3* w3, char* data, size_t size){ - fwrite(data, size, 1, f); -} +void fetch_data(struct W3* w3, char* data, size_t size) { fwrite(data, size, 1, f); } -void status(struct W3* w3, int status){ - fprintf(stderr, "Response code is %d\n", status); -} +void status(struct W3* w3, int status) { fprintf(stderr, "Response code is %d\n", status); } -void header(struct W3* w3, char* key, char* value){ +void header(struct W3* w3, char* key, char* value) { fprintf(stderr, "Header: %s is `%s'\n", key, value); - if(strcasecmp(key, "Server") == 0){ + if(strcasecmp(key, "Server") == 0) { /* For the example - We get the server software */ fprintf(stderr, "Server is using `%s'\n", value); } } -int main(int argc, char** argv){ - if(argv[1] != NULL && strcmp(argv[1], "--version") == 0){ +int main(int argc, char** argv) { + if(argv[1] != NULL && strcmp(argv[1], "--version") == 0) { printf("LibW3 %s\n", LIBW3_VERSION); return 0; } - if(argc < 3){ + if(argc < 3) { fprintf(stderr, "Usage: %s URL Path [method [data]]\n", argv[0]); return 1; } W3_Library_Init(); struct W3* w3 = W3_Create("http", argv[1], 80); - if(w3 != NULL){ + if(w3 != NULL) { W3_Set_Read_Size(w3, 1024); W3_Set_Method(w3, argv[3] == NULL ? "GET" : argv[3]); W3_Set_Path(w3, argv[2]); @@ -47,13 +43,13 @@ int main(int argc, char** argv){ W3_On(w3, "data", (void*)fetch_data); W3_On(w3, "header", (void*)header); f = fopen("example.bin", "wb"); - if(argv[3] != NULL && strcmp(argv[3], "POST") == 0 && argv[4] != NULL){ + if(argv[3] != NULL && strcmp(argv[3], "POST") == 0 && argv[4] != NULL) { W3_Set_Data(w3, argv[4], strlen(argv[4])); } W3_Send_Request(w3); fclose(f); W3_Free(w3); - }else{ + } else { fprintf(stderr, "Failed to fetch\n"); return 1; } diff --git a/Library/Core.c b/Library/Core.c index 1f34525..527ac62 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -4,14 +4,14 @@ #include "W3DNS.h" #include "W3Util.h" -#include "W3HTTP.h" #include "W3File.h" +#include "W3HTTP.h" -#include -#include +#include #include +#include #include -#include +#include #ifdef __MINGW32__ #include @@ -22,12 +22,12 @@ #include #endif -int W3_Library_Init(void){ +int W3_Library_Init(void) { __W3_Debug("LibW3", "Initializing"); #ifdef SSL_SUPPORT - if(SSL_library_init() >= 0){ + if(SSL_library_init() >= 0) { __W3_Debug("LibW3-SSL", "Initialized"); - }else{ + } else { return 1; } #endif @@ -43,13 +43,11 @@ int W3_Library_Init(void){ return 0; } -struct W3* W3_Create(const char* protocol, const char* hostname, int port){ +struct W3* W3_Create(const char* protocol, const char* hostname, int port) { __W3_Debug("Create", "Creating a struct"); struct W3* w3 = malloc(sizeof(*w3)); bool ssl = false; - if( - strcmp(protocol, "https") == 0 - ){ + if(strcmp(protocol, "https") == 0) { ssl = true; } w3->method = NULL; @@ -64,17 +62,16 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ w3->ssl = NULL; w3->ssl_ctx = NULL; #endif - if(strcmp(protocol, "file") != 0){ + if(strcmp(protocol, "file") != 0) { w3->hostname = __W3_Strdup(hostname); if(ssl) __W3_Debug("Protocol", "Enabled SSL"); w3->sock = __W3_DNS_Connect(hostname, ssl, port #ifdef SSL_SUPPORT - , - &w3->ssl, - &w3->ssl_ctx + , + &w3->ssl, &w3->ssl_ctx #endif ); - if(w3->sock == -1){ + if(w3->sock == -1) { W3_Free(w3); w3 = NULL; } @@ -82,108 +79,108 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port){ return w3; } -void W3_Set_Read_Size(struct W3* w3, size_t size){ - w3->readsize = size; -} +void W3_Set_Read_Size(struct W3* w3, size_t size) { w3->readsize = size; } -void W3_Set_Method(struct W3* w3, const char* method){ +void W3_Set_Method(struct W3* w3, const char* method) { if(w3->method != NULL) free(w3->method); w3->method = __W3_Strdup(method); } -void W3_Set_Path(struct W3* w3, const char* path){ +void W3_Set_Path(struct W3* w3, const char* path) { if(w3->path != NULL) free(w3->path); w3->path = __W3_Strdup(path); } -void W3_Send_Request(struct W3* w3){ - if(strcmp(w3->protocol, "http") == 0 || strcmp(w3->protocol, "https") == 0){ +void W3_Send_Request(struct W3* w3) { + if(strcmp(w3->protocol, "http") == 0 || strcmp(w3->protocol, "https") == 0) { __W3_HTTP_Request(w3); - }else if(strcmp(w3->protocol, "file") == 0){ + } else if(strcmp(w3->protocol, "file") == 0) { __W3_File_Request(w3); } } -void W3_Set_Data(struct W3* w3, char* data, size_t size){ +void W3_Set_Data(struct W3* w3, char* data, size_t size) { w3->data = data; w3->size = size; } -void W3_Set_Header(struct W3* w3, const char* key, const char* value){ +void W3_Set_Header(struct W3* w3, const char* key, const char* value) { char* tmp = __W3_Concat3("Setting the header `", key, "` to `"); char* str = __W3_Concat3(tmp, value, "`"); free(tmp); __W3_Debug("Header", str); free(str); int len = 0; - if(w3->headers == NULL){ + if(w3->headers == NULL) { w3->headers = malloc(sizeof(*w3->headers) * (len + 3)); w3->headers[len] = __W3_Strdup(key); w3->headers[len + 1] = __W3_Strdup(value); int i; - for(i = 0; w3->headers[len][i] != 0; i++){ + for(i = 0; w3->headers[len][i] != 0; i++) { w3->headers[len][i] = tolower(w3->headers[len][i]); } w3->headers[len + 2] = NULL; - }else{ - for(len = 0; w3->headers[len] != NULL; len++); + } else { + for(len = 0; w3->headers[len] != NULL; len++) + ; char** headers = w3->headers; w3->headers = malloc(sizeof(*w3->headers) * (len + 3)); - for(len = 0; headers[len] != NULL; len++){ + for(len = 0; headers[len] != NULL; len++) { w3->headers[len] = headers[len]; } w3->headers[len] = __W3_Strdup(key); w3->headers[len + 1] = __W3_Strdup(value); w3->headers[len + 2] = NULL; int i; - for(i = 0; w3->headers[len][i] != 0; i++){ + for(i = 0; w3->headers[len][i] != 0; i++) { w3->headers[len][i] = tolower(w3->headers[len][i]); } free(headers); } } -void W3_On(struct W3* w3, const char* eventname, void* func){ +void W3_On(struct W3* w3, const char* eventname, void* func) { int len = 0; - if(w3->events == NULL){ + if(w3->events == NULL) { w3->events = malloc(sizeof(*w3->events) * (len + 3)); w3->events[len] = __W3_Strdup(eventname); w3->events[len + 1] = func; int i; - for(i = 0; ((char*)w3->events[len])[i] != 0; i++){ + for(i = 0; ((char*)w3->events[len])[i] != 0; i++) { ((char*)w3->events[len])[i] = tolower(((char*)w3->events[len])[i]); } w3->events[len + 2] = NULL; - }else{ - for(len = 0; w3->events[len] != NULL; len++); + } else { + for(len = 0; w3->events[len] != NULL; len++) + ; void** events = w3->events; w3->events = malloc(sizeof(*w3->events) * (len + 3)); - for(len = 0; events[len] != NULL; len++){ + for(len = 0; events[len] != NULL; len++) { w3->events[len] = events[len]; } w3->events[len] = __W3_Strdup(eventname); w3->events[len + 1] = func; w3->events[len + 2] = NULL; int i; - for(i = 0; ((char*)w3->events[len])[i] != 0; i += 2){ + for(i = 0; ((char*)w3->events[len])[i] != 0; i += 2) { ((char*)w3->events[len])[i] = tolower(((char*)w3->events[len])[i]); } free(events); } } -void W3_Free(struct W3* w3){ +void W3_Free(struct W3* w3) { __W3_Debug("LibW3", "Freeing"); if(w3->method != NULL) free(w3->method); if(w3->path != NULL) free(w3->path); if(w3->protocol != NULL) free(w3->protocol); if(w3->hostname != NULL) free(w3->hostname); - if(w3->headers != NULL){ + if(w3->headers != NULL) { int i; for(i = 0; w3->headers[i] != 0; i++) free(w3->headers[i]); free(w3->headers); } - if(w3->events != NULL){ + if(w3->events != NULL) { int i; for(i = 0; w3->events[i] != 0; i += 2) free(w3->events[i]); free(w3->events); diff --git a/Library/DNS.c b/Library/DNS.c index ed29f21..4efabfb 100644 --- a/Library/DNS.c +++ b/Library/DNS.c @@ -7,17 +7,17 @@ #include #ifdef __MINGW32__ +#include #include #include -#include #else -#include #include +#include #endif +#include #include #include -#include #include #ifdef SSL_SUPPORT @@ -30,11 +30,10 @@ int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port #ifdef SSL_SUPPORT - , - void** o_ssl, - void** o_ctx + , + void** o_ssl, void** o_ctx #endif -){ +) { __W3_Debug("DNS-Connect", "Resolving"); ADDRINFO hints; ADDRINFO* result; @@ -50,13 +49,13 @@ int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port memset(strport, 0, 6); sprintf(strport, "%d", port); s = getaddrinfo(hostname, strport, &hints, &result); - if(s != 0){ + if(s != 0) { free(strport); __W3_Debug("Resolve", "Failed"); - return -1; /* Failed to resolve */ + return -1; /* Failed to resolve */ } int sock; - for(rp = result; rp != NULL; rp = rp->ai_next){ + for(rp = result; rp != NULL; rp = rp->ai_next) { sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if(sock == -1) continue; int nzero = 0; @@ -66,26 +65,26 @@ int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port } freeaddrinfo(result); free(strport); - if(rp == NULL){ + if(rp == NULL) { __W3_Debug("Connect", "Failed to connect"); - return -1; /* Failed to connect */ + return -1; /* Failed to connect */ } __W3_Debug("Connect", "Conencted"); #ifdef SSL_SUPPORT - if(ssl){ + if(ssl) { __W3_Debug("SSL", "Initializing"); const SSL_METHOD* method = TLSv1_2_client_method(); *o_ctx = SSL_CTX_new(method); *o_ssl = SSL_new(*o_ctx); SSL_set_fd(*o_ssl, sock); - if(SSL_connect(*o_ssl) != 1){ + if(SSL_connect(*o_ssl) != 1) { SSL_CTX_free(*o_ctx); SSL_free(*o_ssl); *o_ctx = NULL; *o_ssl = NULL; close(sock); sock = -1; - }else{ + } else { __W3_Debug("SSL", "Connected"); } } diff --git a/Library/File.c b/Library/File.c index be0e9f6..98a2ce9 100644 --- a/Library/File.c +++ b/Library/File.c @@ -4,32 +4,32 @@ #include "W3Core.h" #include "W3Util.h" -#include #include -#include #include +#include +#include -void __W3_File_Request(struct W3* w3){ +void __W3_File_Request(struct W3* w3) { __W3_Debug("LibW3-File", "Sending the request"); FILE* f = fopen(w3->path, "r"); - if(f == NULL){ + if(f == NULL) { void* funcptr = __W3_Get_Event(w3, "status"); - if(funcptr != NULL){ - void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, int) = (void (*)(struct W3*, int))funcptr; func(w3, LIBW3_FILE_NOT_FOUND); } - }else{ + } else { void* funcptr = __W3_Get_Event(w3, "status"); - if(funcptr != NULL){ - void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, int) = (void (*)(struct W3*, int))funcptr; func(w3, LIBW3_FILE_FOUND); } char* buf = malloc(w3->readsize); - while(true){ + while(true) { int len = fread(buf, 1, w3->readsize, f); void* funcptr = __W3_Get_Event(w3, "data"); - if(funcptr != NULL){ - void(*func)(struct W3*, char*, size_t) = (void(*)(struct W3*, char*, size_t))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, char*, size_t) = (void (*)(struct W3*, char*, size_t))funcptr; char* buffer = malloc(len); memcpy(buffer, buf, len); func(w3, buffer, len); diff --git a/Library/HTTP.c b/Library/HTTP.c index 3d722b5..75e0b19 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -4,12 +4,12 @@ #include "W3Core.h" #include "W3Util.h" -#include -#include -#include #include +#include +#include +#include -void __W3_HTTP_Request(struct W3* w3){ +void __W3_HTTP_Request(struct W3* w3) { __W3_Debug("LibW3-HTTP", "Sending the request"); __W3_Auto_Write(w3, w3->method, strlen(w3->method)); __W3_Auto_Write(w3, " ", 1); @@ -20,7 +20,7 @@ void __W3_HTTP_Request(struct W3* w3){ __W3_Auto_Write(w3, "Host: ", 6); __W3_Auto_Write(w3, w3->hostname, strlen(w3->hostname)); __W3_Auto_Write(w3, "\r\n", 2); - if(!__W3_Have_Header(w3, "user-agent")){ + if(!__W3_Have_Header(w3, "user-agent")) { __W3_Auto_Write(w3, "User-Agent: ", 12); __W3_Auto_Write(w3, "LibW3/", 6); __W3_Auto_Write(w3, LIBW3_VERSION, strlen(LIBW3_VERSION)); @@ -34,9 +34,9 @@ void __W3_HTTP_Request(struct W3* w3){ __W3_Auto_Write(w3, "Connection: ", 12); __W3_Auto_Write(w3, "closed", 6); __W3_Auto_Write(w3, "\r\n", 2); - if(w3->headers != NULL){ + if(w3->headers != NULL) { int i; - for(i = 0; w3->headers[i] != NULL; i += 2){ + for(i = 0; w3->headers[i] != NULL; i += 2) { if(strcmp(w3->headers[i], "connection") == 0) continue; if(strcmp(w3->headers[i], "content-length") == 0) continue; __W3_Auto_Write(w3, w3->headers[i], strlen(w3->headers[i])); @@ -45,7 +45,7 @@ void __W3_HTTP_Request(struct W3* w3){ __W3_Auto_Write(w3, "\r\n", 2); } } - if(w3->data != NULL){ + if(w3->data != NULL) { char* len = malloc(129); memset(len, 0, 129); sprintf(len, "%d", w3->size); @@ -55,7 +55,7 @@ void __W3_HTTP_Request(struct W3* w3){ free(len); } __W3_Auto_Write(w3, "\r\n", 2); - if(w3->data != NULL){ + if(w3->data != NULL) { __W3_Auto_Write(w3, w3->data, w3->size); } char* buf = malloc(w3->readsize); @@ -64,31 +64,31 @@ void __W3_HTTP_Request(struct W3* w3){ char* headerbuf = malloc(1); headerbuf[0] = 0; int phase = 0; - while(true){ + while(true) { int l = __W3_Auto_Read(w3, buf, w3->readsize); if(l <= 0) break; int i; - for(i = 0; i < l; i++){ - if(phase == 0){ - if(buf[i] == '\r'){ + for(i = 0; i < l; i++) { + if(phase == 0) { + if(buf[i] == '\r') { int phase2 = 0; int j = 0; int start_status = 0; - for(j = 0; statusbuf[j] != 0; j++){ - if(phase2 == 0){ - if(statusbuf[j] == ' '){ + for(j = 0; statusbuf[j] != 0; j++) { + if(phase2 == 0) { + if(statusbuf[j] == ' ') { phase2++; start_status = j + 1; } - }else if(phase2 == 1){ - if(statusbuf[j] == ' '){ + } else if(phase2 == 1) { + if(statusbuf[j] == ' ') { char* code = malloc(j - start_status + 1); code[j - start_status] = 0; memcpy(code, statusbuf + start_status, j - start_status); w3->status = atoi(code); void* funcptr = __W3_Get_Event(w3, "status"); - if(funcptr != NULL){ - void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, int) = (void (*)(struct W3*, int))funcptr; func(w3, w3->status); } free(code); @@ -97,7 +97,7 @@ void __W3_HTTP_Request(struct W3* w3){ } } phase++; - }else{ + } else { char* oldbuf = statusbuf; statusbuf = malloc(strlen(oldbuf) + 2); strcpy(statusbuf, oldbuf); @@ -105,7 +105,7 @@ void __W3_HTTP_Request(struct W3* w3){ statusbuf[strlen(oldbuf) + 1] = 0; free(oldbuf); } - }else if(phase == 1){ + } else if(phase == 1) { char* oldbuf = headerbuf; headerbuf = malloc(strlen(oldbuf) + 2); strcpy(headerbuf, oldbuf); @@ -113,29 +113,29 @@ void __W3_HTTP_Request(struct W3* w3){ headerbuf[strlen(oldbuf) + 1] = 0; free(oldbuf); int len = strlen(headerbuf); - if(len >= 4){ - if(headerbuf[len - 1] == '\n' && headerbuf[len - 2] == '\r' && headerbuf[len - 3] == '\n' && headerbuf[len - 4] == '\r'){ + if(len >= 4) { + if(headerbuf[len - 1] == '\n' && headerbuf[len - 2] == '\r' && headerbuf[len - 3] == '\n' && headerbuf[len - 4] == '\r') { headerbuf[len - 4] = 0; char* headers = malloc(strlen(headerbuf) + 1); int j; int incr = 0; int start = 0; - for(j = 1; headerbuf[j] != 0; j++){ + for(j = 1; headerbuf[j] != 0; j++) { char c = headerbuf[j]; - if(c == '\r'){ + if(c == '\r') { headers[incr] = 0; headers[incr + 1] = 0; char* data = __W3_Strdup(headers + start); int k; - for(k = 0; data[k] != 0; k++){ - if(data[k] == ':'){ + for(k = 0; data[k] != 0; k++) { + if(data[k] == ':') { data[k] = 0; k++; for(; data[k] != 0 && data[k] != ' ' && data[k] != '\t'; k++) data[k] = 0; - if(data[k] == ' ' || data[k] == '\t'){ + if(data[k] == ' ' || data[k] == '\t') { void* funcptr = __W3_Get_Event(w3, "header"); - if(funcptr != NULL){ - void(*func)(struct W3*, char*, char*) = (void(*)(struct W3*, char*, char*))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, char*, char*) = (void (*)(struct W3*, char*, char*))funcptr; func(w3, data, data + k + 1); } } @@ -146,7 +146,7 @@ void __W3_HTTP_Request(struct W3* w3){ start = incr + 1; incr++; j++; - }else{ + } else { headers[incr] = c; headers[incr + 1] = 0; incr++; @@ -156,10 +156,10 @@ void __W3_HTTP_Request(struct W3* w3){ phase++; } } - }else if(phase == 2){ + } else if(phase == 2) { void* funcptr = __W3_Get_Event(w3, "data"); - if(funcptr != NULL){ - void(*func)(struct W3* w3, char*, size_t) = (void(*)(struct W3* w3, char*, size_t))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, char*, size_t) = (void (*)(struct W3*, char*, size_t))funcptr; char* buffer = malloc(l - i); memcpy(buffer, buf + i, l - i); func(w3, buffer, l - i); diff --git a/Library/Util.c b/Library/Util.c index 0486987..4d0e92f 100644 --- a/Library/Util.c +++ b/Library/Util.c @@ -3,18 +3,18 @@ #include "W3Core.h" +#include #include -#include #include -#include +#include #ifdef __MINGW32__ #include #include #else #include -#include #include +#include #include #endif @@ -24,7 +24,7 @@ #define __DEBUG_LEN 12 -void __W3_Debug(const char* title, const char* message){ +void __W3_Debug(const char* title, const char* message) { #ifdef __DEBUG__ char* periods = malloc(__DEBUG_LEN - strlen(title) + 1); periods[__DEBUG_LEN - strlen(title)] = 0; @@ -33,7 +33,7 @@ void __W3_Debug(const char* title, const char* message){ #endif } -char* __W3_Concat(const char* str1, const char* str2){ +char* __W3_Concat(const char* str1, const char* str2) { char* str = malloc(strlen(str1) + strlen(str2) + 1); strcpy(str, str1); strcpy(str + strlen(str1), str2); @@ -41,24 +41,24 @@ char* __W3_Concat(const char* str1, const char* str2){ return str; } -char* __W3_Concat3(const char* str1, const char* str2, const char* str3){ +char* __W3_Concat3(const char* str1, const char* str2, const char* str3) { char* tmp = __W3_Concat(str1, str2); char* str = __W3_Concat(tmp, str3); free(tmp); return str; } -char* __W3_Strdup(const char* str){ +char* __W3_Strdup(const char* str) { char* result = malloc(strlen(str) + 1); memcpy(result, str, strlen(str) + 1); return result; } -unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length){ +unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length) { #ifdef SSL_SUPPORT - if(w3->ssl != NULL){ + if(w3->ssl != NULL) { return SSL_write(w3->ssl, data, length); - }else{ + } else { return send(w3->sock, data, length, 0); } #else @@ -66,11 +66,11 @@ unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length){ #endif } -unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length){ +unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length) { #ifdef SSL_SUPPORT - if(w3->ssl != NULL){ + if(w3->ssl != NULL) { return SSL_read(w3->ssl, data, length); - }else{ + } else { return recv(w3->sock, data, length, 0); } #else @@ -78,29 +78,29 @@ unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length){ #endif } -void* __W3_Get_Event(struct W3* w3, const char* eventname){ +void* __W3_Get_Event(struct W3* w3, const char* eventname) { if(w3->events == NULL) return NULL; int i; - for(i = 0; w3->events[i] != NULL; i += 2){ - if(strcmp(w3->events[i], eventname) == 0){ + for(i = 0; w3->events[i] != NULL; i += 2) { + if(strcmp(w3->events[i], eventname) == 0) { return w3->events[i + 1]; } } return NULL; } -bool __W3_Have_Header(struct W3* w3, const char* name){ +bool __W3_Have_Header(struct W3* w3, const char* name) { if(w3->headers == NULL) return false; int i; - for(i = 0; w3->headers[i] != NULL; i += 2){ - if(strcmp(w3->headers[i], name) == 0){ + for(i = 0; w3->headers[i] != NULL; i += 2) { + if(strcmp(w3->headers[i], name) == 0) { return true; } } return false; } -char* __W3_Get_Platform(void){ +char* __W3_Get_Platform(void) { #ifdef __MINGW32__ return __W3_Strdup("Windows"); #else diff --git a/Library/W3Core.h b/Library/W3Core.h index 305fb6c..338fc7d 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -6,40 +6,40 @@ extern "C" { #endif -#include #include +#include #include "W3Version.h" struct W3 { - int sock; /* Socket */ - char* protocol; /* As you can read from its name */ - char* method; /* Used in HTTP */ - char* path; /* As you can read from its name */ - char* hostname; /* As you can read from its name */ - char** headers; /* As you can read from its name */ - void** events; /* As you can read from its name */ - int status; /* As you can read from its name */ - char* data; /* As you can read from its name */ - size_t size; /* Size of the data */ - size_t readsize; /* Read buffer size, default is 512 */ + int sock; /* Socket */ + char* protocol; /* As you can read from its name */ + char* method; /* Used in HTTP */ + char* path; /* As you can read from its name */ + char* hostname; /* As you can read from its name */ + char** headers; /* As you can read from its name */ + void** events; /* As you can read from its name */ + int status; /* As you can read from its name */ + char* data; /* As you can read from its name */ + size_t size; /* Size of the data */ + size_t readsize; /* Read buffer size, default is 512 */ #ifdef SSL_SUPPORT - void* ssl; /* Actually SSL*, NULL if no SSL */ - void* ssl_ctx; /* Actually SSL_CTX* */ + void* ssl; /* Actually SSL*, NULL if no SSL */ + void* ssl_ctx; /* Actually SSL_CTX* */ #endif }; -int W3_Library_Init(void); /* Initialize the Library */ -struct W3* W3_Create(const char* protocol, const char* hostname, int port); /* Create the struct */ - -void W3_Set_Method(struct W3* w3, const char* method); /* Set the method */ -void W3_Set_Path(struct W3* w3, const char* path); /* Set the path */ -void W3_Send_Request(struct W3* w3); /* Send the request */ -void W3_Set_Header(struct W3* w3, const char* key, const char* value); /* Set the header */ -void W3_Free(struct W3* w3); /* Free the struct */ -void W3_On(struct W3* w3, const char* eventname, void* func); /* Set Handlers */ -void W3_Set_Data(struct W3* w3, char* data, size_t size); /* Send the data - LibW3 won't free the data */ -void W3_Set_Read_Size(struct W3* w3, size_t size); /* Change the read buffer size */ +int W3_Library_Init(void); /* Initialize the Library */ +struct W3* W3_Create(const char* protocol, const char* hostname, int port); /* Create the struct */ + +void W3_Set_Method(struct W3* w3, const char* method); /* Set the method */ +void W3_Set_Path(struct W3* w3, const char* path); /* Set the path */ +void W3_Send_Request(struct W3* w3); /* Send the request */ +void W3_Set_Header(struct W3* w3, const char* key, const char* value); /* Set the header */ +void W3_Free(struct W3* w3); /* Free the struct */ +void W3_On(struct W3* w3, const char* eventname, void* func); /* Set Handlers */ +void W3_Set_Data(struct W3* w3, char* data, size_t size); /* Send the data - LibW3 won't free the data */ +void W3_Set_Read_Size(struct W3* w3, size_t size); /* Change the read buffer size */ #ifdef __cplusplus } diff --git a/Library/W3DNS.h b/Library/W3DNS.h index e53e2ee..dbc304f 100644 --- a/Library/W3DNS.h +++ b/Library/W3DNS.h @@ -6,14 +6,13 @@ extern "C" { #endif -#include #include +#include int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port #ifdef SSL_SUPPORT - , - void** o_ssl, - void** o_ctx + , + void** o_ssl, void** o_ctx #endif ); diff --git a/Library/W3File.h b/Library/W3File.h index 370745e..41e1658 100644 --- a/Library/W3File.h +++ b/Library/W3File.h @@ -9,8 +9,8 @@ extern "C" { #include "W3Core.h" void __W3_File_Request(struct W3* w3); -#define LIBW3_FILE_FOUND 0 -#define LIBW3_FILE_NOT_FOUND 1 +#define LIBW3_FILE_FOUND 0 +#define LIBW3_FILE_NOT_FOUND 1 #ifdef __cplusplus } diff --git a/Makefile b/Makefile index 6233a50..c10ce5c 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ CFLAGS += -g -D__DEBUG__ endif ifeq ($(WINDOWS),YES) -.PHONY: all clean ./Library/w3.dll ./Example/fetch +.PHONY: all clean ./Library/w3.dll ./Example/fetch format ALL := ./Library/w3.dll ./Example/fetch.exe @@ -52,7 +52,7 @@ all: ./w3.pc ./Library/W3Version.h $(ALL) else -.PHONY: all clean ./Library/libw3.so ./Library/libw3.a ./Example/fetch +.PHONY: all clean ./Library/libw3.so ./Library/libw3.a ./Example/fetch format ALL := ./Library/libw3.so ./Library/libw3.a ./Example/fetch @@ -113,3 +113,6 @@ endif zip -rv w3-$(VERSION).zip w3-$(VERSION) -/usr/lha/bin/lha a w3-$(VERSION).lzh w3-$(VERSION) rm -rf w3-$(VERSION) + +format: + clang-format -i `find Library Example -name "*.h" -or -name "*.c"` -- 2.43.0