Mercurial > hg > th-libs
diff th_string.c @ 616:594f197f7005
Slight improvements to the almost non-existing Doxygen documentation.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 17 Jan 2020 03:18:40 +0200 |
parents | 5ec903f366b5 |
children | d3aace9903fa |
line wrap: on
line diff
--- a/th_string.c Fri Jan 17 00:01:55 2020 +0200 +++ b/th_string.c Fri Jan 17 03:18:40 2020 +0200 @@ -12,7 +12,10 @@ #include "th_printf.c" -/* Implementation of strdup() with a NULL check +/** + * Implementation of strdup() with a @c NULL check. + * @param[in] src string to create a copy of + * @returns copy of the given string, or @c NULL if @p src was @c NULL or memory could not be allocated. */ char *th_strdup(const char *src) { @@ -31,7 +34,14 @@ } -/* Implementation of strndup() with NULL check +/** + * Implementation of strndup() with @c NULL check. Copies up to @p n + * characters from the source. A NUL terminator is always added, + * unless @p src was @c NULL or memory could not be allocated, in which + * case @c NULL pointer is returned. + * @param[in] src string pointer to be copied from + * @param[in] n maximum number of characters to copy + * @returns the resulting string or @c NULL pointer */ char *th_strndup(const char *src, const size_t n) { @@ -55,9 +65,15 @@ } -/* Like strdup, but trims whitespace from the string according to specified flags. - * See TH_TRIM_* in th_string.h. If the resulting string would be empty (length 0), - * NULL is returned. +/** + * Helper function for th_strdup_trim() and friends. Copies @p len characters from + * given string, and trims whitespace from it according to specified @p flags. + * See TH_TRIM_* in th_string.h. If @p len or the resulting trimmed string would + * be empty (length 0), no copy is allocated and a @c NULL pointer is returned. + * @param[in] src source string (does not need to be NUL terminated, as length must be specified) + * @param[in] len length of the source string, or desired number of characters to copy at maximum + * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h + * @returns the resulting string or @c NULL pointer */ static char * th_strdup_trim_do(const char *src, size_t len, const int flags) { @@ -93,6 +109,14 @@ } +/** + * Create copy of the given string, but trim the result according to specified @p flags. + * See TH_TRIM_* in th_string.h. If length the resulting trimmed string would + * be empty (0), no copy is allocated and a NULL pointer is returned. + * @param[in] src source string + * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h + * @returns the resulting string or @c NULL pointer + */ char *th_strdup_trim(const char *src, const int flags) { if (src == NULL) @@ -102,6 +126,16 @@ } +/** + * Create copy of the given string @p src, up to @p n characters (or less, if the string + * is shorter.) The result is trimmed according to specified @p flags. + * See TH_TRIM_* in th_string.h. If @p n or the resulting trimmed string would + * be empty (length 0), no copy is allocated and a NULL pointer is returned. + * @param[in] src source string + * @param[in] n maximum number of characters to copy from the source string + * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h + * @returns the resulting string or @c NULL pointer + */ char *th_strndup_trim(const char *src, const size_t n, const int flags) { size_t len; @@ -147,6 +181,14 @@ #endif +/** + * Wrapper for either libc vsnprintf() or th-libs internal + * vsnprintf() implementation, as determined by a compile-time define. + * @param[out] buf pointer to buffer to print to + * @param[in] size available space in the buffer in bytes + * @param[in] fmt format string + * @param[in] ap a va_list variable arguments list structure + */ int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) { #ifdef TH_USE_INTERNAL_SPRINTF @@ -172,6 +214,14 @@ } +/** + * Wrapper for either libc snprintf() or th-libs internal + * snprintf() implementation, as determined by a compile-time define. + * @param[out] buf pointer to buffer to print to + * @param[in] size available space in the buffer in bytes + * @param[in] fmt format string + * @param[in] ... variable arguments + */ int th_snprintf(char *buf, size_t size, const char *fmt, ...) { int n; @@ -253,6 +303,13 @@ #endif +/** + * Combination of vsprintf() and strdup. Automatically allocates memory + * for the resulting string. + * @param[in] fmt format string + * @param[in] args variable arguments list structure + * @returns the resulting string or @c NULL pointer in case of error + */ char *th_strdup_vprintf(const char *fmt, va_list args) { #ifdef TH_USE_INTERNAL_SPRINTF @@ -317,6 +374,13 @@ } +/** + * Combination of sprintf() and strdup. Automatically allocates memory + * for the resulting string. + * @param[in] fmt format string + * @param[in] ... optional printf arguments + * @returns the resulting string or @c NULL pointer in case of error + */ char *th_strdup_printf(const char *fmt, ...) { char *res; @@ -330,6 +394,14 @@ } +/** + * A helper function that is given a pointer to a pointer of string, + * which will be automatically freed (if necessary) and replaced with + * a pointer to the newly created string. + * @param[in,out] buf pointer to a char pointer/string + * @param[in] fmt format string + * @param[in] args variable arguments list structure + */ void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) { char *tmp = th_strdup_vprintf(fmt, ap); @@ -338,6 +410,14 @@ } +/** + * A helper function that is given a pointer to a pointer of string, + * which will be automatically freed (if necessary) and replaced with + * a pointer to the newly created string. + * @param[in,out] buf pointer to a char pointer/string + * @param[in] fmt format string + * @param[in] ... optional printf arguments + */ void th_pstr_printf(char **buf, const char *fmt, ...) { char *tmp;