Bladeren bron

Removed useless macros and turned the code easier to debug. (related issue #13)

silvioprog 6 jaren geleden
bovenliggende
commit
2d4561a164
6 gewijzigde bestanden met toevoegingen van 37 en 37 verwijderingen
  1. 1 1
      examples/example_httpcomp.c
  2. 1 1
      include/sagui.h
  3. 6 4
      src/sg_httpres.c
  4. 4 16
      src/sg_macros.h
  5. 24 14
      src/sg_utils.c
  6. 1 1
      test/test_httpsrv_curl.c

+ 1 - 1
examples/example_httpcomp.c

@@ -37,7 +37,7 @@ static void req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_http
     struct sg_strmap **headers;
     const char *header;
     if ((headers = sg_httpreq_headers(req)) && (header = sg_strmap_get(*headers, "Accept-Encoding")) &&
-        strcasestr(header, "deflate"))
+        strstr(header, "deflate"))
         sg_httpres_zsendbinary(res, PAGE, strlen(PAGE), CONTENT_TYPE, 200);
     else
         sg_httpres_sendbinary(res, PAGE, strlen(PAGE), CONTENT_TYPE, 200);

+ 1 - 1
include/sagui.h

@@ -819,7 +819,7 @@ SG_EXTERN struct sg_strmap **sg_httpres_headers(struct sg_httpres *res);
  * \param[in] val Cookie value.
  * \retval 0 Success.
  * \retval EINVAL Invalid argument.
- * \warning It exits the application if called when no memory space is available.
+ * \retval NULL If no memory space available and sets the `errno` to `ENOMEM`.
  */
 SG_EXTERN int sg_httpres_set_cookie(struct sg_httpres *res, const char *name, const char *val);
 

+ 6 - 4
src/sg_httpres.c

@@ -89,8 +89,8 @@ done:
 }
 
 static void sg__httpres_zfree_cb(void *handle) {
-    struct sg__httpres_zholder *holder;
-    if (!(holder = handle))
+    struct sg__httpres_zholder *holder = handle;
+    if (!holder)
         return;
     deflateEnd(&holder->stream);
     if (holder->free_cb)
@@ -136,7 +136,9 @@ int sg_httpres_set_cookie(struct sg_httpres *res, const char *name, const char *
     if (!res || !name || !val || !sg__is_cookie_name(name) || !sg__is_cookie_val(val))
         return EINVAL;
     len = strlen(name) + strlen("=") + strlen(val) + 1;
-    sg__alloc(str, len);
+    str = sg__malloc(len);
+    if (!str)
+        return ENOMEM;
     snprintf(str, len, "%s=%s", name, val);
     ret = sg_strmap_add(&res->headers, MHD_HTTP_HEADER_SET_COOKIE, str);
     sg__free(str);
@@ -254,7 +256,7 @@ int sg_httpres_zsendbinary(struct sg_httpres *res, void *buf, size_t size, const
         dest_size = compressBound(size);
         if (!(dest = sg__malloc(dest_size)))
             oom();
-        if (((ret = sg__compress(buf, size, dest, &dest_size)) != Z_OK) || (dest_size >= size))
+        if (((ret = sg__compress(buf, size, dest, (size_t *) &dest_size)) != Z_OK) || (dest_size >= size))
             switch (ret) {
                 case Z_STREAM_ERROR: {
                     sg__free(dest);

+ 4 - 16
src/sg_macros.h

@@ -89,27 +89,15 @@ do {
 #define sg__malloc(size) malloc((size))
 #endif
 
-#ifndef sg__alloc2
-#define sg__alloc2(ptr, size)                                                                                          \
-do {                                                                                                                   \
-    if (((ptr) = sg__malloc((size))))                                                                                  \
-        memset((ptr), 0, (size));                                                                                      \
-} while (0)
-#endif
-
-#ifndef sg__alloc
-#define sg__alloc(ptr, size)                                                                                           \
+#ifndef sg__new
+#define sg__new(ptr)                                                                                                   \
 do {                                                                                                                   \
-    if (!((ptr) = sg__malloc((size))))                                                                                 \
+    if (!((ptr) = sg__malloc(sizeof(*(ptr)))))                                                                         \
         oom();                                                                                                         \
-    memset((ptr), 0, (size));                                                                                          \
+    memset((ptr), 0, sizeof(*(ptr)));                                                                                  \
 } while (0)
 #endif
 
-#ifndef sg__new
-#define sg__new(ptr) sg__alloc((ptr), sizeof(*(ptr)))
-#endif
-
 #ifndef sg__realloc
 #define sg__realloc(ptr, size) realloc((ptr), (size))
 #endif

+ 24 - 14
src/sg_utils.c

@@ -7,7 +7,7 @@
  *
  *   –– cross-platform library which helps to develop web servers or frameworks.
  *
- * Copyright (c) 2016-2018 Silvio Clecio <[email protected]>
+ * Copyright (c) 2016-2019 Silvio Clecio <[email protected]>
  *
  * This file is part of Sagui library.
  *
@@ -53,7 +53,8 @@ char *strndup(const char *s, size_t n) {
     size_t len = strlen(s);
     if (n < len)
         len = n;
-    if (!(cpy = sg__malloc(len + 1)))
+    cpy = sg__malloc(len + 1);
+    if (!cpy)
         return NULL;
     cpy[len] = '\0';
     return memcpy(cpy, s, len);
@@ -64,8 +65,9 @@ static wchar_t *stow(const char *str) {
     wchar_t *res = NULL;
     int len;
     if (str) {
-        if ((len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, NULL, 0)) > 0) {
-            sg__alloc(res, len * sizeof(wchar_t));
+        len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, NULL, 0);
+        if (len > 0) {
+            res = sg__malloc(len * sizeof(wchar_t));
             if (res) {
                 if (MultiByteToWideChar(CP_UTF8, 0, str, -1, res, len) == 0) {
                     sg__free(res);
@@ -82,8 +84,9 @@ static char *wtos(const wchar_t *str) {
     char *res = NULL;
     int len;
     if (str) {
-        if ((len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL)) > 0) {
-            sg__alloc(res, len * sizeof(wchar_t));
+        len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
+        if (len > 0) {
+            res = sg__malloc(len * sizeof(wchar_t));
             if (res) {
                 if (WideCharToMultiByte(CP_UTF8, 0, str, -1, res, len, NULL, FALSE) == 0) {
                     sg__free(res);
@@ -98,9 +101,11 @@ static char *wtos(const wchar_t *str) {
 int sg__rename(const char *old, const char *new) {
     int ret = 0;
     wchar_t *o, *n;
-    if (!(o = stow(old)))
+    o = stow(old);
+    if (!o)
         return ENOMEM;
-    if (!(n = stow(new))) {
+    n = stow(new);
+    if (!n) {
         ret = ENOMEM;
         goto done;
     }
@@ -149,17 +154,20 @@ char *sg__strjoin(char sep, const char *a, const char *b) {
     len = strlen(a);
     if ((len == 0) || (a[len - 1] == sep)) {
         len += strlen(b) + 1;
-        if (!(str = sg__malloc(len)))
+        str = sg__malloc(len);
+        if (!str)
             return NULL;
         snprintf(str, len, "%s%s", a, b);
     } else if (strlen(b) == 0) {
         len += 1;
-        if (!(str = sg__malloc(len)))
+        str = sg__malloc(len);
+        if (!str)
             return NULL;
         memcpy(str, a, len);
     } else {
         len += 1 + strlen(b) + 1;
-        if (!(str = sg__malloc(len)))
+        str = sg__malloc(len);
+        if (!str)
             return NULL;
         snprintf(str, len, "%s%c%s", a, sep, b);
     }
@@ -197,8 +205,8 @@ int sg__compress(const void *src, size_t src_size, void *dest, size_t *dest_size
     stream.zalloc = NULL;
     stream.zfree = NULL;
     stream.opaque = NULL;
-    if ((errnum = deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL,
-                               Z_DEFAULT_STRATEGY)) != Z_OK)
+    errnum = deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
+    if (errnum != Z_OK)
         return errnum;
     stream.next_out = dest;
     stream.avail_out = 0;
@@ -236,7 +244,9 @@ const char *sg_version_str(void) {
 
 void *sg_alloc(size_t size) {
     void *ptr;
-    sg__alloc2(ptr, size);
+    ptr = sg__malloc(size);
+    if (ptr)
+        memset(ptr, 0, size);
     return ptr;
 }
 

+ 1 - 1
test/test_httpsrv_curl.c

@@ -128,7 +128,7 @@ static void srv_req_cb(__SG_UNUSED void *cls, struct sg_httpreq *req, struct sg_
         ASSERT(strcmp(sg_strmap_get(*params, "param1"), "param-value1") == 0);
         ASSERT(strcmp(sg_strmap_get(*params, "param2"), "param-value2") == 0);
         if ((headers = sg_httpreq_headers(req)) && (header = sg_strmap_get(*headers, "Accept-Encoding")) &&
-            strcasestr(header, "deflate"))
+            strstr(header, "deflate"))
             sg_httpres_zsend(res, PAGE, "text/html", 200);
         else
             sg_httpres_send(res, OK_MSG, "text/plain", 200);