]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
libtextstyle: Add ostream_printf, ostream_vprintf functions.
authorBruno Haible <bruno@clisp.org>
Mon, 28 Oct 2019 00:08:41 +0000 (01:08 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 28 Oct 2019 00:08:41 +0000 (01:08 +0100)
Suggested by José Marchesi.

* libtextstyle/autogen.sh (GNULIB_MODULES): Add 'vasprintf-posix'.
* libtextstyle/gnulib-local/lib/ostream.oo.h: Include <stdarg.h>.
(ostream_printf, ostream_vprintf): New declarations.
* libtextstyle/gnulib-local/lib/ostream.oo.c: Include <stdio.h>.
(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 <stdio.h>.
(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.

NEWS
libtextstyle/NEWS
libtextstyle/autogen.sh
libtextstyle/doc/libtextstyle.texi
libtextstyle/gnulib-local/lib/html-ostream.oo.h
libtextstyle/gnulib-local/lib/ostream.oo.c
libtextstyle/gnulib-local/lib/ostream.oo.h
libtextstyle/gnulib-local/lib/styled-ostream.oo.h
libtextstyle/gnulib-local/lib/term-ostream.oo.h
libtextstyle/lib/textstyle.h

diff --git a/NEWS b/NEWS
index 31377d208e575cf8d298dc5e6fb7b264d95f168b..67a97a58dd32ae83fc31c35a9d6e799c7d53f111 100644 (file)
--- 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
index 67375b507afe89cacfd991e0c7cbbec1713b9d57..03d6f2680077f8066d0d049720c112f3e5eee267 100644 (file)
@@ -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/.
 
index 45708ba61161e3e63057db6030774341170813c9..9edce49cade056cebec2347628a9ac254750f90a 100755 (executable)
@@ -87,6 +87,7 @@ if test $skip_gnulib = false; then
       term-styled-ostream
     filename
     isatty
+    vasprintf-posix
     xalloc
     xconcat-filename
 
index f65a0f928e18858c8e214e75ea2463f6e8d33cf7..1dd344152bca3fe37970c1f802614e72dfe9faf1 100644 (file)
@@ -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
index 05649c1283458b20aab634ae045f0b27169f5dbc..93456d4769a2b27928172f6ba7de3e2d18fd89c6 100644 (file)
@@ -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);
 };
 
index 5fbac10fc6f2f051ff2434e3b3bd26b72abc87a5..a6690c39f73094702a8ad5ebea5dcc351eaf73c7 100644 (file)
@@ -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 <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -20,6 +20,8 @@
 /* Specification.  */
 #include "ostream.h"
 
+#include <stdio.h>
+
 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;
+}
index c1c9143388494d004e656610d891f56ba992d953..dc78e7132aca2bb632c7ce1f0451fdfda471ee89 100644 (file)
@@ -18,6 +18,7 @@
 #ifndef _OSTREAM_H
 #define _OSTREAM_H
 
+#include <stdarg.h>
 #include <stddef.h>
 #include <string.h>
 
@@ -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
index eed57f468965fd1b2308f98e05714f7431c49a1c..cee0e64ea0c4f837f11fed411fe683c34f47fa60 100644 (file)
@@ -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);
 };
 
index 21527e0d672e2352c92033998af9b56f65cfce42..8e830978776a8bf71c69324011344b3a72393da2 100644 (file)
@@ -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);
 };
 
index a80987a619a7a41543d662ce6b16345d07c6069c..c1b381db7f544ddaddf6d42bbe534296a41de2d9 100644 (file)
@@ -19,6 +19,7 @@
 #ifndef _TEXTSTYLE_H
 #define _TEXTSTYLE_H
 
+#include <stdarg.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <textstyle/stdbool.h>
@@ -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
 }