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.
* 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
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/.
term-styled-ostream
filename
isatty
+ vasprintf-posix
xalloc
xconcat-filename
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
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
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
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
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);
};
/* 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
/* Specification. */
#include "ostream.h"
+#include <stdio.h>
+
struct ostream
{
fields:
}
#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;
+}
#ifndef _OSTREAM_H
#define _OSTREAM_H
+#include <stdarg.h>
#include <stddef.h>
#include <string.h>
/* 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
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);
};
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);
};
#ifndef _TEXTSTYLE_H
#define _TEXTSTYLE_H
+#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <textstyle/stdbool.h>
/* 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
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
}
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
}
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
}