~donmcc/astr

cc2e1fd58b818e848289f37bf93a51a27b694312 — Don McCaughey 1 year, 6 months ago 88a05a8
Reimplement `astr_alloc_formatted_from_va_list()`.

Drop the need to call `vasprintf()` and change the implementation of
`astr_alloc_formatted_from_va_list()` to call
`astr_realloc_append_formatted_from_va_list()` with a `NULL` string
pointer.

Update the readme and man page and remove the check for `vasprintf()`
from the configure script.
4 files changed, 11 insertions(+), 19 deletions(-)

M README.md
M astr.3
M astr.c
M configure.ac
M README.md => README.md +7 -4
@@ 12,11 12,16 @@ string pointers, treating them similarly to a zero-length string.  Reallocating
functions make string building easier automatically resizing the string as
needed.

*astr* was inspired by the improvements that the [`asprintf()`][1] function
brings to string building.

[1]: http://man7.org/linux/man-pages/man3/asprintf.3.html

### Naming Conventions

All functions in *astr* begin with the `astr_` prefix.  Functions that return
newly allocated strings will contain `_alloc` in their names; the caller is
responsible for calling [`free()`][1] on the returned pointer.
responsible for calling [`free()`][2] on the returned pointer.
Functions with `_realloc` in their names take a pointer to a string as an 
_in-out_ parameter; these functions may reallocate the string, replacing the
pointed-to string value.


@@ 26,7 31,7 @@ given string.
Functions that end with the suffix `_from_va_list` take variable arguments as a 
`va_list` object from `stdarg.h`.

[1]: http://man7.org/linux/man-pages/man3/free.3.html
[2]: http://man7.org/linux/man-pages/man3/free.3.html

## Functions



@@ 81,10 86,8 @@ The parameter `s` may point to different address on success.


## Dependencies
`astr` requires that `<stdio.h>` defines the [`vasprintf()`][2] function.
Building from repository source requires [GNU Autotools][3].

[2]: http://man7.org/linux/man-pages/man3/asprintf.3.html
[3]: https://www.gnu.org/software/automake/faq/autotools-faq.html

## Building from Repository Source

M astr.3 => astr.3 +1 -3
@@ 197,8 197,6 @@ to
.Sh SEE ALSO
.Xr free 3 ,
.Xr printf 3 ,
.Xr stdarg 3 ,
.Xr vasprintf 3 ,
.Xr vsnprintf 3
.Xr stdarg 3
.Sh AUTHORS
.An Don McCaughey

M astr.c => astr.c +3 -11
@@ 18,17 18,9 @@ astr_alloc_formatted(char const *format, ...)
char *
astr_alloc_formatted_from_va_list(char const *format, va_list arguments)
{
    if (!format) {
        errno = EINVAL;
        return NULL;
    }

    char *s;
    va_list arguments_copy;
    va_copy(arguments_copy, arguments);
    int chars_printed = vasprintf(&s, format, arguments_copy);
    if (chars_printed < 0) return NULL;
    return s;
    char *s = NULL;
    int result = astr_realloc_append_formatted_from_va_list(&s, format, arguments);
    return (0 == result) ? s : NULL;
}



M configure.ac => configure.ac +0 -1
@@ 23,7 23,6 @@ AC_PROG_RANLIB
# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CHECK_FUNC([vasprintf],[],[AC_MSG_ERROR(`vasprintf()' not found)])

AC_CONFIG_FILES([Makefile])