From: Bruno Haible Date: Mon, 14 Dec 2020 22:31:41 +0000 (+0100) Subject: libtextstyle: Add is_instance_of_* predicates. X-Git-Tag: v0.22~285 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bc8b756841727f1887dcf17fc628af18943dd30c;p=thirdparty%2Fgettext.git libtextstyle: Add is_instance_of_* predicates. * libtextstyle/gnulib-local/lib/fd-ostream.oo.h (is_instance_of_fd_ostream): New declaration. * libtextstyle/gnulib-local/lib/fd-ostream.oo.c (is_instance_of_fd_ostream): New function. * libtextstyle/gnulib-local/lib/file-ostream.oo.h (is_instance_of_file_ostream): New declaration. * libtextstyle/gnulib-local/lib/file-ostream.oo.c (is_instance_of_file_ostream): New function. * libtextstyle/gnulib-local/lib/html-ostream.oo.h (is_instance_of_html_ostream): New declaration. * libtextstyle/gnulib-local/lib/html-ostream.oo.c (is_instance_of_html_ostream): New function. * libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h (is_instance_of_html_styled_ostream): New declaration. * libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c (is_instance_of_html_styled_ostream): New function. * libtextstyle/gnulib-local/lib/iconv-ostream.oo.h (is_instance_of_iconv_ostream): New declaration. * libtextstyle/gnulib-local/lib/iconv-ostream.oo.c (is_instance_of_iconv_ostream): New function. * libtextstyle/gnulib-local/lib/memory-ostream.oo.h (is_instance_of_memory_ostream): New declaration. * libtextstyle/gnulib-local/lib/memory-ostream.oo.c (is_instance_of_memory_ostream): New function. * libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h (is_instance_of_noop_styled_ostream): New declaration. * libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c (is_instance_of_noop_styled_ostream): New function. * libtextstyle/gnulib-local/lib/styled-ostream.oo.h (is_instance_of_styled_ostream): New declaration. * libtextstyle/gnulib-local/lib/styled-ostream.oo.c (is_instance_of_styled_ostream): New function. * libtextstyle/gnulib-local/lib/term-ostream.oo.h (is_instance_of_term_ostream): New declaration. * libtextstyle/gnulib-local/lib/term-ostream.oo.c (is_instance_of_term_ostream): New function. * libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h (is_instance_of_term_styled_ostream): New declaration. * libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c (is_instance_of_term_styled_ostream): New function. * libtextstyle/lib/textstyle.h (is_instance_of_styled_ostream, is_instance_of_file_ostream, is_instance_of_fd_ostream, is_instance_of_term_ostream, is_instance_of_memory_ostream, is_instance_of_iconv_ostream, is_instance_of_html_ostream, is_instance_of_term_styled_ostream, is_instance_of_html_styled_ostream, is_instance_of_noop_styled_ostream): New declarations. * libtextstyle/tests/test-instanceof.c: New file. * libtextstyle/tests/Makefile.am: Arrange to compile it. * libtextstyle/doc/libtextstyle.texi (The output stream hierarchy): Mention the is_instance_of_* predicates. * libtextstyle/NEWS: Mention the new functions. --- diff --git a/libtextstyle/.gitignore b/libtextstyle/.gitignore index a2e030de9..9666781ba 100644 --- a/libtextstyle/.gitignore +++ b/libtextstyle/.gitignore @@ -239,6 +239,7 @@ /lib/xvasprintf.h /tests/ !/tests/Makefile.am +!/tests/test-instanceof.c # Files brought in by "automake --add-missing --copy": /build-aux/compile diff --git a/libtextstyle/NEWS b/libtextstyle/NEWS index 125a1307a..953c7ab54 100644 --- a/libtextstyle/NEWS +++ b/libtextstyle/NEWS @@ -1,3 +1,17 @@ +New in 0.21.1: +* Added type test functions: + TYPE FUNCTION + styled_ostream_t is_instance_of_styled_ostream + file_ostream_t is_instance_of_file_ostream + fd_ostream_t is_instance_of_fd_ostream + term_ostream_t is_instance_of_term_ostream + memory_ostream_t is_instance_of_memory_ostream + iconv_ostream_t is_instance_of_iconv_ostream + html_ostream_t is_instance_of_html_ostream + term_styled_ostream_t is_instance_of_term_styled_ostream + html_styled_ostream_t is_instance_of_html_styled_ostream + noop_styled_ostream_t is_instance_of_noop_styled_ostream + New in 0.21: * Added support for emitting hyperlinks. New functions: diff --git a/libtextstyle/doc/libtextstyle.texi b/libtextstyle/doc/libtextstyle.texi index 1dd344152..82860aa29 100644 --- a/libtextstyle/doc/libtextstyle.texi +++ b/libtextstyle/doc/libtextstyle.texi @@ -36,7 +36,7 @@ This manual provides documentation for the GNU @code{libtextstyle} library. @copying -Copyright (C) 2018-2019 Free Software Foundation, Inc. +Copyright (C) 2018-2020 Free Software Foundation, Inc. This manual is free documentation. It is dually licensed under the GNU FDL and the GNU GPL. This means that you can redistribute this @@ -68,7 +68,7 @@ A copy of the license is at @url{https://www.gnu.org/licenses/gpl.html}. @page @vskip 0pt plus 1filll @c @insertcopying -Copyright (C) 2018-2019 Free Software Foundation, Inc. +Copyright (C) 2018-2020 Free Software Foundation, Inc. This manual is free documentation. It is dually licensed under the GNU FDL and the GNU GPL. This means that you can redistribute this @@ -824,7 +824,9 @@ Every @samp{some_ostream_t} object can be converted to an @item The opposite conversion, from @samp{ostream_t} to @samp{some_ostream_t}, can also be performed, provided that the object is actually an instance -of @samp{some_ostream_t}. +of @samp{some_ostream_t}. You can test whether an object is an instance +of @samp{some_ostream_t} by invoking the method +@samp{bool is_instance_of_some_ostream (ostream_t stream)}. @item Every method @samp{ostream_@var{foobar}} exists also as a method @samp{some_ostream_@var{foobar}} with compatible argument types and a diff --git a/libtextstyle/gnulib-local/lib/fd-ostream.oo.c b/libtextstyle/gnulib-local/lib/fd-ostream.oo.c index cabe2a07c..f5aa13495 100644 --- a/libtextstyle/gnulib-local/lib/fd-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/fd-ostream.oo.c @@ -199,3 +199,11 @@ fd_ostream_create (int fd, const char *filename, bool buffered) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_fd_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, fd_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/fd-ostream.oo.h b/libtextstyle/gnulib-local/lib/fd-ostream.oo.h index 57e0f8230..71b82100e 100644 --- a/libtextstyle/gnulib-local/lib/fd-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/fd-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream referring to a file descriptor. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -41,6 +41,10 @@ extern fd_ostream_t fd_ostream_create (int fd, const char *filename, bool buffered); +/* Test whether a given output stream is a fd_ostream. */ +extern bool is_instance_of_fd_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/file-ostream.oo.c b/libtextstyle/gnulib-local/lib/file-ostream.oo.c index 12565b04c..0937191b6 100644 --- a/libtextstyle/gnulib-local/lib/file-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/file-ostream.oo.c @@ -1,5 +1,5 @@ /* Output stream referring to an stdio FILE. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -106,3 +106,11 @@ file_ostream_create (FILE *fp) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_file_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, file_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/file-ostream.oo.h b/libtextstyle/gnulib-local/lib/file-ostream.oo.h index 98bd29cd9..cbc7da611 100644 --- a/libtextstyle/gnulib-local/lib/file-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/file-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream referring to an stdio FILE. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,7 @@ #ifndef _FILE_OSTREAM_H #define _FILE_OSTREAM_H +#include #include #include "ostream.h" @@ -39,6 +40,10 @@ extern "C" { extern file_ostream_t file_ostream_create (FILE *fp); +/* Test whether a given output stream is a file_ostream. */ +extern bool is_instance_of_file_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/html-ostream.oo.c b/libtextstyle/gnulib-local/lib/html-ostream.oo.c index 3c6bfbbc9..9a613e33c 100644 --- a/libtextstyle/gnulib-local/lib/html-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/html-ostream.oo.c @@ -436,3 +436,11 @@ html_ostream_create (ostream_t destination) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_html_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, html_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/html-ostream.oo.h b/libtextstyle/gnulib-local/lib/html-ostream.oo.h index 93456d476..987fbb271 100644 --- a/libtextstyle/gnulib-local/lib/html-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/html-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream that produces HTML output. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,8 @@ #ifndef _HTML_OSTREAM_H #define _HTML_OSTREAM_H +#include + #include "ostream.h" @@ -63,6 +65,10 @@ extern "C" { extern html_ostream_t html_ostream_create (ostream_t destination); +/* Test whether a given output stream is a html_ostream. */ +extern bool is_instance_of_html_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c index 945396499..a78740ab6 100644 --- a/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c @@ -1,5 +1,5 @@ /* Output stream for CSS styled text, producing HTML output. - Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -190,3 +190,11 @@ html_styled_ostream_create (ostream_t destination, const char *css_filename) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_html_styled_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, html_styled_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h b/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h index 14a36f2c0..74fe376ad 100644 --- a/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream for CSS styled text, producing HTML output. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,8 @@ #ifndef _HTML_STYLED_OSTREAM_H #define _HTML_STYLED_OSTREAM_H +#include + #include "styled-ostream.h" @@ -41,6 +43,10 @@ extern html_styled_ostream_t const char *css_filename); +/* Test whether a given output stream is a html_styled_ostream. */ +extern bool is_instance_of_html_styled_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/iconv-ostream.oo.c b/libtextstyle/gnulib-local/lib/iconv-ostream.oo.c index 1df6b18cc..3e9f7a59e 100644 --- a/libtextstyle/gnulib-local/lib/iconv-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/iconv-ostream.oo.c @@ -222,6 +222,14 @@ iconv_ostream_create (const char *from_encoding, const char *to_encoding, return stream; } +/* Instanceof test. */ + +bool +is_instance_of_iconv_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, iconv_ostream); +} + #else static void @@ -242,4 +250,12 @@ iconv_ostream::free (iconv_ostream_t stream) abort (); } +/* Instanceof test. */ + +bool +is_instance_of_iconv_ostream (ostream_t stream) +{ + return false; +} + #endif /* HAVE_ICONV */ diff --git a/libtextstyle/gnulib-local/lib/iconv-ostream.oo.h b/libtextstyle/gnulib-local/lib/iconv-ostream.oo.h index 7a9baa546..a2a870573 100644 --- a/libtextstyle/gnulib-local/lib/iconv-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/iconv-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream that converts the output to another encoding. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -21,6 +21,8 @@ /* Note that this stream does not provide accurate error messages with line and column number when the conversion fails. */ +#include + #include "ostream.h" @@ -46,6 +48,10 @@ extern iconv_ostream_t iconv_ostream_create (const char *from_encoding, #endif /* HAVE_ICONV */ +/* Test whether a given output stream is an iconv_ostream. */ +extern bool is_instance_of_iconv_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/memory-ostream.oo.c b/libtextstyle/gnulib-local/lib/memory-ostream.oo.c index 4c12b655e..a5f73126b 100644 --- a/libtextstyle/gnulib-local/lib/memory-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/memory-ostream.oo.c @@ -1,5 +1,5 @@ /* Output stream that accumulates the output in memory. - Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -99,3 +99,11 @@ memory_ostream_create (void) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_memory_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, memory_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/memory-ostream.oo.h b/libtextstyle/gnulib-local/lib/memory-ostream.oo.h index 625557d64..384e0e823 100644 --- a/libtextstyle/gnulib-local/lib/memory-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/memory-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream that accumulates the output in memory. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,7 @@ #ifndef _MEMORY_OSTREAM_H #define _MEMORY_OSTREAM_H +#include #include #include "ostream.h" @@ -41,6 +42,10 @@ extern "C" { extern memory_ostream_t memory_ostream_create (void); +/* Test whether a given output stream is a memory_ostream. */ +extern bool is_instance_of_memory_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c index fb824b905..7ef0f5d64 100644 --- a/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c @@ -1,5 +1,5 @@ /* Output stream with no-op styling. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2019. This program is free software: you can redistribute it and/or modify @@ -121,3 +121,11 @@ noop_styled_ostream_create (ostream_t destination, bool pass_ownership) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_noop_styled_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, noop_styled_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h b/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h index fa64e1c87..4a57e5462 100644 --- a/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream with no-op styling. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2019. This program is free software: you can redistribute it and/or modify @@ -44,6 +44,10 @@ extern noop_styled_ostream_t noop_styled_ostream_create (ostream_t destination, bool pass_ownership); +/* Test whether a given output stream is a noop_styled_ostream. */ +extern bool is_instance_of_noop_styled_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/styled-ostream.oo.c index e87eea9ee..b06553d05 100644 --- a/libtextstyle/gnulib-local/lib/styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/styled-ostream.oo.c @@ -1,5 +1,5 @@ /* Abstract output stream for CSS styled text. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -25,3 +25,11 @@ struct styled_ostream : struct ostream { fields: }; + +/* Instanceof test. */ + +bool +is_instance_of_styled_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, styled_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/styled-ostream.oo.h b/libtextstyle/gnulib-local/lib/styled-ostream.oo.h index cee0e64ea..f90f8bfba 100644 --- a/libtextstyle/gnulib-local/lib/styled-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/styled-ostream.oo.h @@ -1,5 +1,5 @@ /* Abstract output stream for CSS styled text. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,8 @@ #ifndef _STYLED_OSTREAM_H #define _STYLED_OSTREAM_H +#include + #include "ostream.h" @@ -54,5 +56,17 @@ methods: void flush_to_current_style (styled_ostream_t stream); }; +#ifdef __cplusplus +extern "C" { +#endif + + +/* Test whether a given output stream is a styled_ostream. */ +extern bool is_instance_of_styled_ostream (ostream_t stream); + + +#ifdef __cplusplus +} +#endif #endif /* _STYLED_OSTREAM_H */ diff --git a/libtextstyle/gnulib-local/lib/term-ostream.oo.c b/libtextstyle/gnulib-local/lib/term-ostream.oo.c index 36d84d31c..02e873eac 100644 --- a/libtextstyle/gnulib-local/lib/term-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/term-ostream.oo.c @@ -2740,3 +2740,11 @@ term_ostream_create (int fd, const char *filename, ttyctl_t tty_control) return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_term_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, term_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/term-ostream.oo.h b/libtextstyle/gnulib-local/lib/term-ostream.oo.h index 8e8309787..949649f04 100644 --- a/libtextstyle/gnulib-local/lib/term-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/term-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream for attributed text, producing ANSI escape sequences. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,8 @@ #ifndef _TERM_OSTREAM_H #define _TERM_OSTREAM_H +#include + #include "ostream.h" @@ -123,6 +125,10 @@ extern term_ostream_t term_ostream_create (int fd, const char *filename, ttyctl_t tty_control); +/* Test whether a given output stream is a term_ostream. */ +extern bool is_instance_of_term_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c index 2ff978f66..3ed00d1d6 100644 --- a/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c @@ -667,3 +667,11 @@ term_styled_ostream_create (int fd, const char *filename, ttyctl_t tty_control, return stream; } + +/* Instanceof test. */ + +bool +is_instance_of_term_styled_ostream (ostream_t stream) +{ + return IS_INSTANCE (stream, ostream, term_styled_ostream); +} diff --git a/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h b/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h index 5918f2870..59f441ec0 100644 --- a/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream for CSS styled text, producing ANSI escape sequences. - Copyright (C) 2006, 2019 Free Software Foundation, Inc. + Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -18,6 +18,8 @@ #ifndef _TERM_STYLED_OSTREAM_H #define _TERM_STYLED_OSTREAM_H +#include + #include "styled-ostream.h" #include "term-ostream.h" @@ -45,6 +47,10 @@ extern term_styled_ostream_t const char *css_filename); +/* Test whether a given output stream is a term_styled_ostream. */ +extern bool is_instance_of_term_styled_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/lib/textstyle.h b/libtextstyle/lib/textstyle.h index c1b381db7..2cf56b1c5 100644 --- a/libtextstyle/lib/textstyle.h +++ b/libtextstyle/lib/textstyle.h @@ -1,5 +1,5 @@ /* Public API of the libtextstyle library. - Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -123,6 +123,19 @@ extern void styled_ostream_flush_to_current_style (styled_ostream_t stream); } #endif +#ifdef __cplusplus +extern "C" { +#endif + + +/* Test whether a given output stream is a styled_ostream. */ +extern bool is_instance_of_styled_ostream (ostream_t stream); + + +#ifdef __cplusplus +} +#endif + /* -------------------------- From file-ostream.h -------------------------- */ /* file_ostream_t is a subtype of ostream_t. */ @@ -149,6 +162,10 @@ extern "C" { extern file_ostream_t file_ostream_create (FILE *fp); +/* Test whether a given output stream is a file_ostream. */ +extern bool is_instance_of_file_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -181,6 +198,10 @@ extern fd_ostream_t fd_ostream_create (int fd, const char *filename, bool buffered); +/* Test whether a given output stream is a fd_ostream. */ +extern bool is_instance_of_fd_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -294,6 +315,10 @@ extern term_ostream_t term_ostream_create (int fd, const char *filename, ttyctl_t tty_control); +/* Test whether a given output stream is a term_ostream. */ +extern bool is_instance_of_term_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -324,6 +349,10 @@ extern "C" { extern memory_ostream_t memory_ostream_create (void); +/* Test whether a given output stream is a memory_ostream. */ +extern bool is_instance_of_memory_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -358,6 +387,10 @@ extern iconv_ostream_t iconv_ostream_create (const char *from_encoding, ostream_t destination); +/* Test whether a given output stream is an iconv_ostream. */ +extern bool is_instance_of_iconv_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -406,6 +439,10 @@ extern "C" { extern html_ostream_t html_ostream_create (ostream_t destination); +/* Test whether a given output stream is a html_ostream. */ +extern bool is_instance_of_html_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -449,6 +486,10 @@ extern term_styled_ostream_t const char *css_filename); +/* Test whether a given output stream is a term_styled_ostream. */ +extern bool is_instance_of_term_styled_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -489,6 +530,10 @@ extern html_styled_ostream_t const char *css_filename); +/* Test whether a given output stream is a html_styled_ostream. */ +extern bool is_instance_of_html_styled_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif @@ -530,6 +575,10 @@ extern noop_styled_ostream_t noop_styled_ostream_create (ostream_t destination, bool pass_ownership); +/* Test whether a given output stream is a noop_styled_ostream. */ +extern bool is_instance_of_noop_styled_ostream (ostream_t stream); + + #ifdef __cplusplus } #endif diff --git a/libtextstyle/tests/Makefile.am b/libtextstyle/tests/Makefile.am index 852d0b6f2..dd3ad6f90 100644 --- a/libtextstyle/tests/Makefile.am +++ b/libtextstyle/tests/Makefile.am @@ -1,5 +1,5 @@ ## Makefile for the tests subdirectory of GNU libtextstyle. -## Copyright (C) 2009-2019 Free Software Foundation, Inc. +## Copyright (C) 2009-2020 Free Software Foundation, Inc. ## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -24,6 +24,11 @@ AUTOMAKE_OPTIONS += 1.13 subdir-objects color-tests # Ensure that ../lib/config.h is seen before ../config.h. DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)/lib -I$(top_builddir) +TESTS += test-instanceof +check_PROGRAMS += test-instanceof +EXTRA_DIST += test-instanceof.c +AM_CPPFLAGS += -DSRCDIR=\"$(srcdir)/\" + # For debugging memory leaks and memory allocation bugs. # You should build with --disable-shared when using valgrind. diff --git a/libtextstyle/tests/test-instanceof.c b/libtextstyle/tests/test-instanceof.c new file mode 100644 index 000000000..3d8a11751 --- /dev/null +++ b/libtextstyle/tests/test-instanceof.c @@ -0,0 +1,141 @@ +/* Test for the is_instance_of_* functions. */ + +#include + +#include "textstyle.h" + +#include +#include + +#define ASSERT(x) if (!(x)) abort () + +int +main () +{ + ostream_t stream1 = file_ostream_create (stdout); + ostream_t stream2 = fd_ostream_create (1, "(stdout)", false); + ostream_t stream3 = term_ostream_create (1, "(stdout)", TTYCTL_AUTO); + ostream_t stream4 = memory_ostream_create (); + ostream_t stream5 = html_ostream_create (stream1); + ostream_t sstream1 = + term_styled_ostream_create (1, "(stdout)", TTYCTL_AUTO, + SRCDIR "../adhoc-tests/hello-default.css"); + ostream_t sstream2 = + html_styled_ostream_create (stream5, + SRCDIR "../adhoc-tests/hello-default.css"); + ostream_t sstream3 = noop_styled_ostream_create (stream1, false); + + if (stream1 != NULL) + { + ASSERT (! is_instance_of_styled_ostream (stream1)); + ASSERT (is_instance_of_file_ostream (stream1)); + ASSERT (! is_instance_of_fd_ostream (stream1)); + ASSERT (! is_instance_of_term_ostream (stream1)); + ASSERT (! is_instance_of_memory_ostream (stream1)); + ASSERT (! is_instance_of_iconv_ostream (stream1)); + ASSERT (! is_instance_of_html_ostream (stream1)); + ASSERT (! is_instance_of_term_styled_ostream (stream1)); + ASSERT (! is_instance_of_html_styled_ostream (stream1)); + ASSERT (! is_instance_of_noop_styled_ostream (stream1)); + } + + if (stream2 != NULL) + { + ASSERT (! is_instance_of_styled_ostream (stream2)); + ASSERT (! is_instance_of_file_ostream (stream2)); + ASSERT (is_instance_of_fd_ostream (stream2)); + ASSERT (! is_instance_of_term_ostream (stream2)); + ASSERT (! is_instance_of_memory_ostream (stream2)); + ASSERT (! is_instance_of_iconv_ostream (stream2)); + ASSERT (! is_instance_of_html_ostream (stream2)); + ASSERT (! is_instance_of_term_styled_ostream (stream2)); + ASSERT (! is_instance_of_html_styled_ostream (stream2)); + ASSERT (! is_instance_of_noop_styled_ostream (stream2)); + } + + if (stream3 != NULL) + { + ASSERT (! is_instance_of_styled_ostream (stream3)); + ASSERT (! is_instance_of_file_ostream (stream3)); + ASSERT (! is_instance_of_fd_ostream (stream3)); + ASSERT (is_instance_of_term_ostream (stream3)); + ASSERT (! is_instance_of_memory_ostream (stream3)); + ASSERT (! is_instance_of_iconv_ostream (stream3)); + ASSERT (! is_instance_of_html_ostream (stream3)); + ASSERT (! is_instance_of_term_styled_ostream (stream3)); + ASSERT (! is_instance_of_html_styled_ostream (stream3)); + ASSERT (! is_instance_of_noop_styled_ostream (stream3)); + } + + if (stream4 != NULL) + { + ASSERT (! is_instance_of_styled_ostream (stream4)); + ASSERT (! is_instance_of_file_ostream (stream4)); + ASSERT (! is_instance_of_fd_ostream (stream4)); + ASSERT (! is_instance_of_term_ostream (stream4)); + ASSERT (is_instance_of_memory_ostream (stream4)); + ASSERT (! is_instance_of_iconv_ostream (stream4)); + ASSERT (! is_instance_of_html_ostream (stream4)); + ASSERT (! is_instance_of_term_styled_ostream (stream4)); + ASSERT (! is_instance_of_html_styled_ostream (stream4)); + ASSERT (! is_instance_of_noop_styled_ostream (stream4)); + } + + if (stream5 != NULL) + { + ASSERT (! is_instance_of_styled_ostream (stream5)); + ASSERT (! is_instance_of_file_ostream (stream5)); + ASSERT (! is_instance_of_fd_ostream (stream5)); + ASSERT (! is_instance_of_term_ostream (stream5)); + ASSERT (! is_instance_of_memory_ostream (stream5)); + ASSERT (! is_instance_of_iconv_ostream (stream5)); + ASSERT (is_instance_of_html_ostream (stream5)); + ASSERT (! is_instance_of_term_styled_ostream (stream5)); + ASSERT (! is_instance_of_html_styled_ostream (stream5)); + ASSERT (! is_instance_of_noop_styled_ostream (stream5)); + } + + if (sstream1 != NULL) + { + ASSERT (is_instance_of_styled_ostream (sstream1)); + ASSERT (! is_instance_of_file_ostream (sstream1)); + ASSERT (! is_instance_of_fd_ostream (sstream1)); + ASSERT (! is_instance_of_term_ostream (sstream1)); + ASSERT (! is_instance_of_memory_ostream (sstream1)); + ASSERT (! is_instance_of_iconv_ostream (sstream1)); + ASSERT (! is_instance_of_html_ostream (sstream1)); + ASSERT (is_instance_of_term_styled_ostream (sstream1)); + ASSERT (! is_instance_of_html_styled_ostream (sstream1)); + ASSERT (! is_instance_of_noop_styled_ostream (sstream1)); + } + + if (sstream2 != NULL) + { + ASSERT (is_instance_of_styled_ostream (sstream2)); + ASSERT (! is_instance_of_file_ostream (sstream2)); + ASSERT (! is_instance_of_fd_ostream (sstream2)); + ASSERT (! is_instance_of_term_ostream (sstream2)); + ASSERT (! is_instance_of_memory_ostream (sstream2)); + ASSERT (! is_instance_of_iconv_ostream (sstream2)); + ASSERT (! is_instance_of_html_ostream (sstream2)); + ASSERT (! is_instance_of_term_styled_ostream (sstream2)); + ASSERT (is_instance_of_html_styled_ostream (sstream2)); + ASSERT (! is_instance_of_noop_styled_ostream (sstream2)); + } + + if (sstream3 != NULL) + { + ASSERT (is_instance_of_styled_ostream (sstream3)); + ASSERT (! is_instance_of_file_ostream (sstream3)); + ASSERT (! is_instance_of_fd_ostream (sstream3)); + ASSERT (! is_instance_of_term_ostream (sstream3)); + ASSERT (! is_instance_of_memory_ostream (sstream3)); + ASSERT (! is_instance_of_iconv_ostream (sstream3)); + ASSERT (! is_instance_of_html_ostream (sstream3)); + ASSERT (! is_instance_of_term_styled_ostream (sstream3)); + ASSERT (! is_instance_of_html_styled_ostream (sstream3)); + ASSERT (is_instance_of_noop_styled_ostream (sstream3)); + } + + return 0; +}