1 /* Output stream with no-op styling.
2 Copyright (C) 2006, 2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2019.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include "noop-styled-ostream.h"
22
23 #include "xalloc.h"
24
25
26 struct noop_styled_ostream : struct styled_ostream
27 {
28 fields:
29 /* The destination stream. */
30 ostream_t destination;
31 bool own_destination;
32 /* The current hyperlink ref and id. */
33 char *hyperlink_ref;
34 char *hyperlink_id;
35 };
36
37 /* Implementation of ostream_t methods. */
38
39 static void
write_mem(noop_styled_ostream_t stream,const void * data,size_t len)40 noop_styled_ostream::write_mem (noop_styled_ostream_t stream,
41 const void *data, size_t len)
42 {
43 ostream_write_mem (stream->destination, data, len);
44 }
45
46 static void
flush(noop_styled_ostream_t stream,ostream_flush_scope_t scope)47 noop_styled_ostream::flush (noop_styled_ostream_t stream,
48 ostream_flush_scope_t scope)
49 {
50 ostream_flush (stream->destination, scope);
51 }
52
53 static void
free(noop_styled_ostream_t stream)54 noop_styled_ostream::free (noop_styled_ostream_t stream)
55 {
56 if (stream->own_destination)
57 ostream_free (stream->destination);
58 free (stream->hyperlink_ref);
59 free (stream->hyperlink_id);
60 free (stream);
61 }
62
63 /* Implementation of styled_ostream_t methods. */
64
65 static void
begin_use_class(noop_styled_ostream_t stream,const char * classname)66 noop_styled_ostream::begin_use_class (noop_styled_ostream_t stream,
67 const char *classname)
68 {
69 }
70
71 static void
end_use_class(noop_styled_ostream_t stream,const char * classname)72 noop_styled_ostream::end_use_class (noop_styled_ostream_t stream,
73 const char *classname)
74 {
75 }
76
77 static const char *
get_hyperlink_ref(noop_styled_ostream_t stream)78 noop_styled_ostream::get_hyperlink_ref (noop_styled_ostream_t stream)
79 {
80 return stream->hyperlink_ref;
81 }
82
83 static const char *
get_hyperlink_id(noop_styled_ostream_t stream)84 noop_styled_ostream::get_hyperlink_id (noop_styled_ostream_t stream)
85 {
86 return stream->hyperlink_id;
87 }
88
89 static void
set_hyperlink(noop_styled_ostream_t stream,const char * ref,const char * id)90 noop_styled_ostream::set_hyperlink (noop_styled_ostream_t stream,
91 const char *ref, const char *id)
92 {
93 char *ref_copy = (ref != NULL ? xstrdup (ref) : NULL);
94 char *id_copy = (id != NULL ? xstrdup (id) : NULL);
95
96 free (stream->hyperlink_ref);
97 stream->hyperlink_ref = ref_copy;
98 free (stream->hyperlink_id);
99 stream->hyperlink_id = id_copy;
100 }
101
102 static void
flush_to_current_style(noop_styled_ostream_t stream)103 noop_styled_ostream::flush_to_current_style (noop_styled_ostream_t stream)
104 {
105 ostream_flush (stream->destination, FLUSH_THIS_STREAM);
106 }
107
108 /* Constructor. */
109
110 noop_styled_ostream_t
noop_styled_ostream_create(ostream_t destination,bool pass_ownership)111 noop_styled_ostream_create (ostream_t destination, bool pass_ownership)
112 {
113 noop_styled_ostream_t stream =
114 XMALLOC (struct noop_styled_ostream_representation);
115
116 stream->base.base.vtable = &noop_styled_ostream_vtable;
117 stream->destination = destination;
118 stream->own_destination = pass_ownership;
119 stream->hyperlink_ref = NULL;
120 stream->hyperlink_id = NULL;
121
122 return stream;
123 }
124