From: Bruno Haible Date: Mon, 28 Oct 2019 00:08:41 +0000 (+0100) Subject: libtextstyle: Add ostream_printf, ostream_vprintf functions. X-Git-Tag: v0.21~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=332548d266dca59fe80cfb6c1287489b09be4445;p=thirdparty%2Fgettext.git libtextstyle: Add ostream_printf, ostream_vprintf functions. Suggested by José Marchesi. * libtextstyle/autogen.sh (GNULIB_MODULES): Add 'vasprintf-posix'. * libtextstyle/gnulib-local/lib/ostream.oo.h: Include . (ostream_printf, ostream_vprintf): New declarations. * libtextstyle/gnulib-local/lib/ostream.oo.c: Include . (ostream_printf, ostream_vprintf): New functions. * libtextstyle/gnulib-local/lib/styled-ostream.oo.h: Update comments. * libtextstyle/gnulib-local/lib/term-ostream.oo.h: Likewise. * libtextstyle/gnulib-local/lib/html-ostream.oo.h: Likewise. * libtextstyle/lib/textstyle.h: Include . (ostream_printf, ostream_vprintf): New functions. (styled_ostream_flush_to_current_style, term_ostream_flush_to_current_style, html_ostream_flush_to_current_style): Update comments. * libtextstyle/doc/libtextstyle.texi (The ostream class): Document ostream_printf, ostream_vprintf. (The styled_ostream class, The term_ostream class, The html_ostream class): Update comments. * libtextstyle/NEWS: Mention it. * NEWS: Likewise. --- diff --git a/NEWS b/NEWS index 31377d208..67a97a58d 100644 --- a/NEWS +++ b/NEWS @@ -28,6 +28,7 @@ Version 0.21 - September 2019 * Libtextstyle: - Added support for emitting hyperlinks. + - New API for doing formatted output. - The example programs support the NO_COLOR environment variable, Version 0.20.1 - May 2019 diff --git a/libtextstyle/NEWS b/libtextstyle/NEWS index 67375b507..03d6f2680 100644 --- a/libtextstyle/NEWS +++ b/libtextstyle/NEWS @@ -10,6 +10,8 @@ New in 0.21: term_ostream_t term_ostream_get_hyperlink_id html_ostream_t html_ostream_set_hyperlink_ref html_ostream_t html_ostream_get_hyperlink_ref +* New API for doing formatted output: + functions ostream_printf, ostream_vprintf. * The example programs support the NO_COLOR environment variable, as specified in https://no-color.org/. diff --git a/libtextstyle/autogen.sh b/libtextstyle/autogen.sh index 45708ba61..9edce49ca 100755 --- a/libtextstyle/autogen.sh +++ b/libtextstyle/autogen.sh @@ -87,6 +87,7 @@ if test $skip_gnulib = false; then term-styled-ostream filename isatty + vasprintf-posix xalloc xconcat-filename diff --git a/libtextstyle/doc/libtextstyle.texi b/libtextstyle/doc/libtextstyle.texi index f65a0f928..1dd344152 100644 --- a/libtextstyle/doc/libtextstyle.texi +++ b/libtextstyle/doc/libtextstyle.texi @@ -853,6 +853,14 @@ Writes a sequence of bytes to a stream. Writes a string's contents to a stream. @end deftypefn +@deftypefn Function ptrdiff_t ostream_printf (ostream_t@tie{}@var{stream}, const@tie{}char@tie{}*@var{format}, ...) +@deftypefnx Function ptrdiff_t ostream_vprintf (ostream_t@tie{}@var{stream}, const@tie{}char@tie{}*@var{format}, va_list args) +Writes formatted output to a stream. + +These functions return the size of formatted output, or a negative value +in case of an error. +@end deftypefn + @deftypefn Function void ostream_flush (ostream_t@tie{}@var{stream}, ostream_flush_scope_t@tie{}@var{scope}) Brings buffered data to its destination. @end deftypefn @@ -918,7 +926,7 @@ instead of with the default text style. After calling this function, you can output strings without newlines(!) to the underlying stream, and they will be rendered like strings passed to -@code{ostream_write_mem} or @code{ostream_write_str}. +@code{ostream_write_mem}, @code{ostream_write_str}, or @code{ostream_printf}. @end deftypefn @node ostream subclasses without styling @@ -1062,7 +1070,7 @@ instead of with the default text attributes. After calling this function, you can output strings without newlines(!) to the underlying file descriptor, and they will be rendered like strings passed to -@code{ostream_write_mem} or @code{ostream_write_str}. +@code{ostream_write_mem}, @code{ostream_write_str}, or @code{ostream_printf}. @end deftypefn @node The html_ostream class @@ -1129,7 +1137,7 @@ instead of with the default text style. After calling this function, you can output strings without newlines(!) to the underlying stream, and they will be rendered like strings passed to -@code{ostream_write_mem} or @code{ostream_write_str}. +@code{ostream_write_mem}, @code{ostream_write_str}, or @code{ostream_printf}. @end deftypefn @node The memory_ostream class diff --git a/libtextstyle/gnulib-local/lib/html-ostream.oo.h b/libtextstyle/gnulib-local/lib/html-ostream.oo.h index 05649c128..93456d476 100644 --- a/libtextstyle/gnulib-local/lib/html-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/html-ostream.oo.h @@ -43,7 +43,7 @@ methods: of with the default text style. After calling this function, you can output strings without newlines(!) to the underlying stream, and they will be rendered like strings passed - to 'ostream_write_mem' or 'ostream_write_str'. */ + to 'ostream_write_mem', 'ostream_write_str', or 'ostream_write_printf'. */ void flush_to_current_style (html_ostream_t stream); }; diff --git a/libtextstyle/gnulib-local/lib/ostream.oo.c b/libtextstyle/gnulib-local/lib/ostream.oo.c index 5fbac10fc..a6690c39f 100644 --- a/libtextstyle/gnulib-local/lib/ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/ostream.oo.c @@ -1,5 +1,5 @@ /* Abstract output stream data type. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2019 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -20,6 +20,8 @@ /* Specification. */ #include "ostream.h" +#include + struct ostream { fields: @@ -34,3 +36,36 @@ ostream_write_str (ostream_t stream, const char *string) } #endif + +ptrdiff_t +ostream_printf (ostream_t stream, const char *format, ...) +{ + va_list args; + char *temp_string; + ptrdiff_t ret; + + va_start (args, format); + ret = vasprintf (&temp_string, format, args); + va_end (args); + if (ret >= 0) + { + if (ret > 0) + ostream_write_str (stream, temp_string); + free (temp_string); + } + return ret; +} + +ptrdiff_t +ostream_vprintf (ostream_t stream, const char *format, va_list args) +{ + char *temp_string; + ptrdiff_t ret = vasprintf (&temp_string, format, args); + if (ret >= 0) + { + if (ret > 0) + ostream_write_str (stream, temp_string); + free (temp_string); + } + return ret; +} diff --git a/libtextstyle/gnulib-local/lib/ostream.oo.h b/libtextstyle/gnulib-local/lib/ostream.oo.h index c1c914338..dc78e7132 100644 --- a/libtextstyle/gnulib-local/lib/ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/ostream.oo.h @@ -18,6 +18,7 @@ #ifndef _OSTREAM_H #define _OSTREAM_H +#include #include #include @@ -65,6 +66,21 @@ extern "C" { /* Write a string's contents to a stream. */ extern void ostream_write_str (ostream_t stream, const char *string); +/* Writes formatted output to a stream. + Returns the size of formatted output, or a negative value in case of an + error. */ +extern ptrdiff_t ostream_printf (ostream_t stream, const char *format, ...) +#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ > 3 + __attribute__ ((__format__ (__printf__, 2, 3))) +#endif + ; +extern ptrdiff_t ostream_vprintf (ostream_t stream, + const char *format, va_list args) +#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ > 3 + __attribute__ ((__format__ (__printf__, 2, 0))) +#endif + ; + #if HAVE_INLINE #define ostream_write_str ostream_write_str_inline diff --git a/libtextstyle/gnulib-local/lib/styled-ostream.oo.h b/libtextstyle/gnulib-local/lib/styled-ostream.oo.h index eed57f468..cee0e64ea 100644 --- a/libtextstyle/gnulib-local/lib/styled-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/styled-ostream.oo.h @@ -50,7 +50,7 @@ methods: of with the default text style. After calling this function, you can output strings without newlines(!) to the underlying stream, and they will be rendered like strings passed - to 'ostream_write_mem' or 'ostream_write_str'. */ + to 'ostream_write_mem', 'ostream_write_str', or 'ostream_write_printf'. */ void flush_to_current_style (styled_ostream_t stream); }; diff --git a/libtextstyle/gnulib-local/lib/term-ostream.oo.h b/libtextstyle/gnulib-local/lib/term-ostream.oo.h index 21527e0d6..8e8309787 100644 --- a/libtextstyle/gnulib-local/lib/term-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/term-ostream.oo.h @@ -99,7 +99,8 @@ methods: with the default text attributes. After calling this function, you can output strings without newlines(!) to the underlying file descriptor, and they will be rendered like strings - passed to 'ostream_write_mem' or 'ostream_write_str'. */ + passed to 'ostream_write_mem', 'ostream_write_str', or + 'ostream_write_printf'. */ void flush_to_current_style (term_ostream_t stream); }; diff --git a/libtextstyle/lib/textstyle.h b/libtextstyle/lib/textstyle.h index a80987a61..c1b381db7 100644 --- a/libtextstyle/lib/textstyle.h +++ b/libtextstyle/lib/textstyle.h @@ -19,6 +19,7 @@ #ifndef _TEXTSTYLE_H #define _TEXTSTYLE_H +#include #include #include #include @@ -70,6 +71,21 @@ extern "C" { /* Write a string's contents to a stream. */ extern void ostream_write_str (ostream_t stream, const char *string); +/* Writes formatted output to a stream. + Returns the size of formatted output, or a negative value in case of an + error. */ +extern ptrdiff_t ostream_printf (ostream_t stream, const char *format, ...) +#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ > 3 + __attribute__ ((__format__ (__printf__, 2, 3))) +#endif + ; +extern ptrdiff_t ostream_vprintf (ostream_t stream, + const char *format, va_list args) +#if (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ > 3 + __attribute__ ((__format__ (__printf__, 2, 0))) +#endif + ; + #ifdef __cplusplus } #endif @@ -101,7 +117,7 @@ extern void styled_ostream_set_hyperlink (styled_ostream_t first_arg, const char of with the default text style. After calling this function, you can output strings without newlines(!) to the underlying stream, and they will be rendered like strings passed - to 'ostream_write_mem' or 'ostream_write_str'. */ + to 'ostream_write_mem', 'ostream_write_str', or 'ostream_write_printf'. */ extern void styled_ostream_flush_to_current_style (styled_ostream_t stream); #ifdef __cplusplus } @@ -237,7 +253,8 @@ extern void term_ostream_set_hyperlink (term_ostream_t first_arg, const char *re with the default text attributes. After calling this function, you can output strings without newlines(!) to the underlying file descriptor, and they will be rendered like strings - passed to 'ostream_write_mem' or 'ostream_write_str'. */ + passed to 'ostream_write_mem', 'ostream_write_str', or + 'ostream_write_printf'. */ extern void term_ostream_flush_to_current_style (term_ostream_t first_arg); #ifdef __cplusplus } @@ -368,7 +385,7 @@ extern void html_ostream_set_hyperlink_ref (html_ostream_t first_arg, const char of with the default text style. After calling this function, you can output strings without newlines(!) to the underlying stream, and they will be rendered like strings passed - to 'ostream_write_mem' or 'ostream_write_str'. */ + to 'ostream_write_mem', 'ostream_write_str', or 'ostream_write_printf'. */ extern void html_ostream_flush_to_current_style (html_ostream_t stream); #ifdef __cplusplus }