1 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
2 
3 #if !IS_CPLUSPLUS
4 #define html_styled_ostream_representation any_ostream_representation
5 #endif
6 #line 1 "html-styled-ostream.oo.c"
7 /* Output stream for CSS styled text, producing HTML output.
8    Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc.
9    Written by Bruno Haible <bruno@clisp.org>, 2006.
10 
11    This program is free software: you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
23 
24 #include <config.h>
25 
26 /* Specification.  */
27 #include "html-styled-ostream.h"
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 #include "html-ostream.h"
35 
36 #include "binary-io.h"
37 #ifndef O_TEXT
38 # define O_TEXT 0
39 #endif
40 
41 #include "error.h"
42 #include "safe-read.h"
43 #include "xalloc.h"
44 #include "gettext.h"
45 
46 #define _(str) gettext (str)
47 
48 
49 #line 50 "html-styled-ostream.c"
50 #include "html_styled_ostream.priv.h"
51 
52 const typeinfo_t html_styled_ostream_typeinfo = { "html_styled_ostream" };
53 
54 static const typeinfo_t * const html_styled_ostream_superclasses[] =
55   { html_styled_ostream_SUPERCLASSES };
56 
57 #define super styled_ostream_vtable
58 
59 #line 53 "html-styled-ostream.oo.c"
60 
61 /* Implementation of ostream_t methods.  */
62 
63 static void
html_styled_ostream__write_mem(html_styled_ostream_t stream,const void * data,size_t len)64 html_styled_ostream__write_mem (html_styled_ostream_t stream,
65                                 const void *data, size_t len)
66 {
67   html_ostream_write_mem (stream->html_destination, data, len);
68 }
69 
70 static void
html_styled_ostream__flush(html_styled_ostream_t stream,ostream_flush_scope_t scope)71 html_styled_ostream__flush (html_styled_ostream_t stream, ostream_flush_scope_t scope)
72 {
73   html_ostream_flush (stream->html_destination, scope);
74 }
75 
76 static void
html_styled_ostream__free(html_styled_ostream_t stream)77 html_styled_ostream__free (html_styled_ostream_t stream)
78 {
79   html_ostream_free (stream->html_destination);
80   ostream_write_str (stream->destination, "</body>\n");
81   ostream_write_str (stream->destination, "</html>\n");
82   free (stream->hyperlink_id);
83   free (stream);
84 }
85 
86 /* Implementation of styled_ostream_t methods.  */
87 
88 static void
html_styled_ostream__begin_use_class(html_styled_ostream_t stream,const char * classname)89 html_styled_ostream__begin_use_class (html_styled_ostream_t stream,
90                                       const char *classname)
91 {
92   html_ostream_begin_span (stream->html_destination, classname);
93 }
94 
95 static void
html_styled_ostream__end_use_class(html_styled_ostream_t stream,const char * classname)96 html_styled_ostream__end_use_class (html_styled_ostream_t stream,
97                                     const char *classname)
98 {
99   html_ostream_end_span (stream->html_destination, classname);
100 }
101 
102 static const char *
html_styled_ostream__get_hyperlink_ref(html_styled_ostream_t stream)103 html_styled_ostream__get_hyperlink_ref (html_styled_ostream_t stream)
104 {
105   return html_ostream_get_hyperlink_ref (stream->html_destination);
106 }
107 
108 static const char *
html_styled_ostream__get_hyperlink_id(html_styled_ostream_t stream)109 html_styled_ostream__get_hyperlink_id (html_styled_ostream_t stream)
110 {
111   return stream->hyperlink_id;
112 }
113 
114 static void
html_styled_ostream__set_hyperlink(html_styled_ostream_t stream,const char * ref,const char * id)115 html_styled_ostream__set_hyperlink (html_styled_ostream_t stream,
116                                     const char *ref, const char *id)
117 {
118   char *id_copy = (id != NULL ? xstrdup (id) : NULL);
119 
120   html_ostream_set_hyperlink_ref (stream->html_destination, ref);
121   free (stream->hyperlink_id);
122   stream->hyperlink_id = id_copy;
123 }
124 
125 static void
html_styled_ostream__flush_to_current_style(html_styled_ostream_t stream)126 html_styled_ostream__flush_to_current_style (html_styled_ostream_t stream)
127 {
128   html_ostream_flush_to_current_style (stream->html_destination);
129 }
130 
131 /* Constructor.  */
132 
133 html_styled_ostream_t
html_styled_ostream_create(ostream_t destination,const char * css_filename)134 html_styled_ostream_create (ostream_t destination, const char *css_filename)
135 {
136   html_styled_ostream_t stream =
137     XMALLOC (struct html_styled_ostream_representation);
138 
139   stream->base.base.vtable = &html_styled_ostream_vtable;
140   stream->destination = destination;
141   stream->html_destination = html_ostream_create (destination);
142   stream->hyperlink_id = NULL;
143 
144   ostream_write_str (stream->destination, "<?xml version=\"1.0\"?>\n");
145   /* HTML 4.01 or XHTML 1.0?
146      Use HTML 4.01.  This is conservative.  Before switching to XHTML 1.0,
147      verify that in the output
148        - all HTML element names are in lowercase,
149        - all empty elements are denoted like <br/> or <p></p>,
150        - every attribute specification is in assignment form, like
151          <table border="1">,
152        - every <a name="..."> element also has an 'id' attribute,
153        - special characters like < > & " are escaped in the <style> and
154          <script> elements.  */
155   ostream_write_str (stream->destination,
156                      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
157   ostream_write_str (stream->destination, "<html>\n");
158   ostream_write_str (stream->destination, "<head>\n");
159   if (css_filename != NULL)
160     {
161       ostream_write_str (stream->destination, "<style type=\"text/css\">\n"
162                                               "<!--\n");
163 
164       /* Include the contents of CSS_FILENAME literally.  */
165       {
166         int fd;
167         char buf[4096];
168 
169         fd = open (css_filename, O_RDONLY | O_TEXT);
170         if (fd < 0)
171           error (EXIT_FAILURE, errno,
172                  _("error while opening \"%s\" for reading"),
173                  css_filename);
174 
175         for (;;)
176           {
177             size_t n_read = safe_read (fd, buf, sizeof (buf));
178             if (n_read == SAFE_READ_ERROR)
179               error (EXIT_FAILURE, errno, _("error reading \"%s\""),
180                      css_filename);
181             if (n_read == 0)
182               break;
183 
184             ostream_write_mem (stream->destination, buf, n_read);
185           }
186 
187         if (close (fd) < 0)
188           error (EXIT_FAILURE, errno, _("error after reading \"%s\""),
189                  css_filename);
190       }
191 
192       ostream_write_str (stream->destination, "-->\n"
193                                               "</style>\n");
194     }
195   ostream_write_str (stream->destination, "</head>\n");
196   ostream_write_str (stream->destination, "<body>\n");
197 
198   return stream;
199 }
200 
201 #line 202 "html-styled-ostream.c"
202 
203 const struct html_styled_ostream_implementation html_styled_ostream_vtable =
204 {
205   html_styled_ostream_superclasses,
206   sizeof (html_styled_ostream_superclasses) / sizeof (html_styled_ostream_superclasses[0]),
207   sizeof (struct html_styled_ostream_representation),
208   html_styled_ostream__write_mem,
209   html_styled_ostream__flush,
210   html_styled_ostream__free,
211   html_styled_ostream__begin_use_class,
212   html_styled_ostream__end_use_class,
213   html_styled_ostream__get_hyperlink_ref,
214   html_styled_ostream__get_hyperlink_id,
215   html_styled_ostream__set_hyperlink,
216   html_styled_ostream__flush_to_current_style,
217 };
218 
219 #if !HAVE_INLINE
220 
221 /* Define the functions that invoke the methods.  */
222 
223 void
html_styled_ostream_write_mem(html_styled_ostream_t first_arg,const void * data,size_t len)224 html_styled_ostream_write_mem (html_styled_ostream_t first_arg, const void *data, size_t len)
225 {
226   const struct html_styled_ostream_implementation *vtable =
227     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
228   vtable->write_mem (first_arg,data,len);
229 }
230 
231 void
html_styled_ostream_flush(html_styled_ostream_t first_arg,ostream_flush_scope_t scope)232 html_styled_ostream_flush (html_styled_ostream_t first_arg, ostream_flush_scope_t scope)
233 {
234   const struct html_styled_ostream_implementation *vtable =
235     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
236   vtable->flush (first_arg,scope);
237 }
238 
239 void
html_styled_ostream_free(html_styled_ostream_t first_arg)240 html_styled_ostream_free (html_styled_ostream_t first_arg)
241 {
242   const struct html_styled_ostream_implementation *vtable =
243     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
244   vtable->free (first_arg);
245 }
246 
247 void
html_styled_ostream_begin_use_class(html_styled_ostream_t first_arg,const char * classname)248 html_styled_ostream_begin_use_class (html_styled_ostream_t first_arg, const char *classname)
249 {
250   const struct html_styled_ostream_implementation *vtable =
251     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
252   vtable->begin_use_class (first_arg,classname);
253 }
254 
255 void
html_styled_ostream_end_use_class(html_styled_ostream_t first_arg,const char * classname)256 html_styled_ostream_end_use_class (html_styled_ostream_t first_arg, const char *classname)
257 {
258   const struct html_styled_ostream_implementation *vtable =
259     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
260   vtable->end_use_class (first_arg,classname);
261 }
262 
263 const char *
html_styled_ostream_get_hyperlink_ref(html_styled_ostream_t first_arg)264 html_styled_ostream_get_hyperlink_ref (html_styled_ostream_t first_arg)
265 {
266   const struct html_styled_ostream_implementation *vtable =
267     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
268   return vtable->get_hyperlink_ref (first_arg);
269 }
270 
271 const char *
html_styled_ostream_get_hyperlink_id(html_styled_ostream_t first_arg)272 html_styled_ostream_get_hyperlink_id (html_styled_ostream_t first_arg)
273 {
274   const struct html_styled_ostream_implementation *vtable =
275     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
276   return vtable->get_hyperlink_id (first_arg);
277 }
278 
279 void
html_styled_ostream_set_hyperlink(html_styled_ostream_t first_arg,const char * ref,const char * id)280 html_styled_ostream_set_hyperlink (html_styled_ostream_t first_arg,                               const char *ref, const char *id)
281 {
282   const struct html_styled_ostream_implementation *vtable =
283     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
284   vtable->set_hyperlink (first_arg,ref,id);
285 }
286 
287 void
html_styled_ostream_flush_to_current_style(html_styled_ostream_t first_arg)288 html_styled_ostream_flush_to_current_style (html_styled_ostream_t first_arg)
289 {
290   const struct html_styled_ostream_implementation *vtable =
291     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
292   vtable->flush_to_current_style (first_arg);
293 }
294 
295 #endif
296