From 40889e9f8e116789b65fc8fd9d3147bc9123e78c Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Mon, 19 Dec 2016 11:30:11 +0100 Subject: [PATCH] httplib_strdup() now global --- doc/APIReference.md | 1 + doc/api/httplib_strcasecmp.md | 2 ++ doc/api/httplib_strdup.md | 30 ++++++++++++++++++++++++++++++ doc/api/httplib_strncasecmp.md | 2 ++ doc/api/httplib_strndup.md | 1 + src/httplib_strdup.c | 21 ++++++++++++++++++--- 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 doc/api/httplib_strdup.md diff --git a/doc/APIReference.md b/doc/APIReference.md index 50fd4096..4dfe9635 100644 --- a/doc/APIReference.md +++ b/doc/APIReference.md @@ -79,6 +79,7 @@ LibHTTP is often used as HTTP and HTTPS library inside a larger application. A * [`httplib_base64_encode( src, src_len, dst, dst_len );`](api/httplib_base64_encode.md) * [`httplib_md5( buf, ... );`](api/httplib_md5.md) * [`httplib_strcasecmp( s1, s2 );`](api/httplib_strcasecmp.md) +* [`httplib_strdup( str );`](api/httplib_strdup.md) * [`httplib_strlcpy( dst, src, len );`](api/httplib_strlcpy.md) * [`httplib_strncasecmp( s1, s2, len );`](api/httplib_strncasecmp.md) * [`httplib_strndup( str, len );`](api/httplib_strndup.md) diff --git a/doc/api/httplib_strcasecmp.md b/doc/api/httplib_strcasecmp.md index aa9a9114..86eab6ed 100644 --- a/doc/api/httplib_strcasecmp.md +++ b/doc/api/httplib_strcasecmp.md @@ -21,4 +21,6 @@ The function `httplib_strcasecmp()` is a helper function to compare two strings. ### See Also +* [`httplib_strdup();`](httplib_strdup.md) * [`httplib_strncasecmp();`](httplib_strncasecmp.md) +* [`httplib_strndup();`](httplib_strndup.md) diff --git a/doc/api/httplib_strdup.md b/doc/api/httplib_strdup.md new file mode 100644 index 00000000..bb901309 --- /dev/null +++ b/doc/api/httplib_strdup.md @@ -0,0 +1,30 @@ +# LibHTTP API Reference + +### `httplib_strdup( str );` + +### Parameters + +| Parameter | Type | Description | +| :--- | :--- | :--- | +|**`str`**|`const char *`|Pointer to the source string which must be duplicated| + +### Return Value + +| Type | Description | +| :--- | :--- | +|`char *`|Pointer to the duplicate, or NULL if an error occured| + +### Description + +The function `httplib_strdup()` duplicates a NUL terminated string to a new string. The duplicate is stored in a newly allocated block of memory. The function is equivalent to the Posix `strdup()` function with the difference that the LibHTTP memory allocation functions are used which allow for tracking of allocation requests and memory leaks through a monitor hook. + +If the duplicate of the string is no longer used, the allocated memory should be returned to the heap with a call to [`httplib_free()`](httplib_free.md). + +If the function fails the value `NULL` is returned, otherwise a pointer to the duplicate. + +### See Also + +* [`httplib_free();`](httplib_free.md) +* [`httplib_strcasecmp();`](httplib_strcasecmp.md) +* [`httplib_strncasecmp();`](httplib_strncasecmp.md) +* [`httplib_strndup();`](httplib_strndup.md) diff --git a/doc/api/httplib_strncasecmp.md b/doc/api/httplib_strncasecmp.md index a1361ffb..b1793303 100644 --- a/doc/api/httplib_strncasecmp.md +++ b/doc/api/httplib_strncasecmp.md @@ -23,3 +23,5 @@ The function `httplib_strncasecmp()` is a helper function to compare two strings ### See Also * [`httplib_strcasecmp();`](httplib_strcasecmp.md) +* [`httplib_strdup();`](httplib_strdup.md) +* [`httplib_strndup();`](httplib_strndup.md) diff --git a/doc/api/httplib_strndup.md b/doc/api/httplib_strndup.md index 16e75d96..c6fe9e97 100644 --- a/doc/api/httplib_strndup.md +++ b/doc/api/httplib_strndup.md @@ -27,4 +27,5 @@ If the function fails the value `NULL` is returned, otherwise a pointer to the d * [`httplib_free();`](httplib_free.md) * [`httplib_strcasecmp();`](httplib_strcasecmp.md) +* [`httplib_strdup();`](httplib_strdup.md) * [`httplib_strncasecmp();`](httplib_strncasecmp.md) diff --git a/src/httplib_strdup.c b/src/httplib_strdup.c index 2a9f42d3..2722709e 100644 --- a/src/httplib_strdup.c +++ b/src/httplib_strdup.c @@ -22,14 +22,29 @@ * THE SOFTWARE. * * ============ - * Release: 1.8 + * Release: 2.0 */ #include "httplib_main.h" #include "httplib_string.h" -char * XX_httplib_strdup( const char *str ) { +/* + * char *httplib_strdup( const char *str ); + * + * The function httplib_strdup() duplicates a NUL terminated string. This + * function is equivalent to the Posix strdup() function, with the difference + * that the own LibHTTP memory allocation functions are used. The memory used + * by the duplicated string should therefore after use be returned to the heap + * with a call to httplib_free(). + * + * If the function succeeds, a pointer to the duplicate is returned. Otherwise + * the returned value is NULL. + */ + +char *httplib_strdup( const char *str ) { + + if ( str == NULL ) return NULL; return httplib_strndup(str, strlen( str ) ); -} /* XX_httplib_strdup */ +} /* httplib_strdup */