Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / gnulib-lib / html-styled-ostream.c
1 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
2
3 #line 1 "html-styled-ostream.oo.c"
4 /* Output stream for CSS styled text, producing HTML output.
5    Copyright (C) 2006-2007, 2015 Free Software Foundation, Inc.
6    Written by Bruno Haible <bruno@clisp.org>, 2006.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #include "html-styled-ostream.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include "html-ostream.h"
32
33 #include "binary-io.h"
34 #ifndef O_TEXT
35 # define O_TEXT 0
36 #endif
37
38 #include "error.h"
39 #include "safe-read.h"
40 #include "xalloc.h"
41 #include "gettext.h"
42
43 #define _(str) gettext (str)
44
45
46 #line 47 "html-styled-ostream.c"
47 #if !IS_CPLUSPLUS
48 #define html_styled_ostream_representation any_ostream_representation
49 #endif
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 51 "html-styled-ostream.oo.c"
60
61 /* Implementation of ostream_t methods.  */
62
63 static void
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
71 html_styled_ostream__flush (html_styled_ostream_t stream)
72 {
73   html_ostream_flush (stream->html_destination);
74 }
75
76 static void
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 }
83
84 /* Implementation of styled_ostream_t methods.  */
85
86 static void
87 html_styled_ostream__begin_use_class (html_styled_ostream_t stream,
88                                       const char *classname)
89 {
90   html_ostream_begin_span (stream->html_destination, classname);
91 }
92
93 static void
94 html_styled_ostream__end_use_class (html_styled_ostream_t stream,
95                                     const char *classname)
96 {
97   html_ostream_end_span (stream->html_destination, classname);
98 }
99
100 /* Constructor.  */
101
102 html_styled_ostream_t
103 html_styled_ostream_create (ostream_t destination, const char *css_filename)
104 {
105   html_styled_ostream_t stream =
106     XMALLOC (struct html_styled_ostream_representation);
107
108   stream->base.base.vtable = &html_styled_ostream_vtable;
109   stream->destination = destination;
110   stream->html_destination = html_ostream_create (destination);
111
112   ostream_write_str (stream->destination, "<?xml version=\"1.0\"?>\n");
113   /* HTML 4.01 or XHTML 1.0?
114      Use HTML 4.01.  This is conservative.  Before switching to XHTML 1.0,
115      verify that in the output
116        - all HTML element names are in lowercase,
117        - all empty elements are denoted like <br/> or <p></p>,
118        - every attribute specification is in assignment form, like
119          <table border="1">,
120        - every <a name="..."> element also has an 'id' attribute,
121        - special characters like < > & " are escaped in the <style> and
122          <script> elements.  */
123   ostream_write_str (stream->destination,
124                      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
125   ostream_write_str (stream->destination, "<html>\n");
126   ostream_write_str (stream->destination, "<head>\n");
127   if (css_filename != NULL)
128     {
129       ostream_write_str (stream->destination, "<style type=\"text/css\">\n"
130                                               "<!--\n");
131
132       /* Include the contents of CSS_FILENAME literally.  */
133       {
134         int fd;
135         char buf[4096];
136
137         fd = open (css_filename, O_RDONLY | O_TEXT);
138         if (fd < 0)
139           error (EXIT_FAILURE, errno,
140                  _("error while opening \"%s\" for reading"),
141                  css_filename);
142
143         for (;;)
144           {
145             size_t n_read = safe_read (fd, buf, sizeof (buf));
146             if (n_read == SAFE_READ_ERROR)
147               error (EXIT_FAILURE, errno, _("error reading \"%s\""),
148                      css_filename);
149             if (n_read == 0)
150               break;
151
152             ostream_write_mem (stream->destination, buf, n_read);
153           }
154
155         if (close (fd) < 0)
156           error (EXIT_FAILURE, errno, _("error after reading \"%s\""),
157                  css_filename);
158       }
159
160       ostream_write_str (stream->destination, "-->\n"
161                                               "</style>\n");
162     }
163   ostream_write_str (stream->destination, "</head>\n");
164   ostream_write_str (stream->destination, "<body>\n");
165
166   return stream;
167 }
168
169 #line 170 "html-styled-ostream.c"
170
171 const struct html_styled_ostream_implementation html_styled_ostream_vtable =
172 {
173   html_styled_ostream_superclasses,
174   sizeof (html_styled_ostream_superclasses) / sizeof (html_styled_ostream_superclasses[0]),
175   sizeof (struct html_styled_ostream_representation),
176   html_styled_ostream__write_mem,
177   html_styled_ostream__flush,
178   html_styled_ostream__free,
179   html_styled_ostream__begin_use_class,
180   html_styled_ostream__end_use_class,
181 };
182
183 #if !HAVE_INLINE
184
185 /* Define the functions that invoke the methods.  */
186
187 void
188 html_styled_ostream_write_mem (html_styled_ostream_t first_arg, const void *data, size_t len)
189 {
190   const struct html_styled_ostream_implementation *vtable =
191     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
192   vtable->write_mem (first_arg,data,len);
193 }
194
195 void
196 html_styled_ostream_flush (html_styled_ostream_t first_arg)
197 {
198   const struct html_styled_ostream_implementation *vtable =
199     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
200   vtable->flush (first_arg);
201 }
202
203 void
204 html_styled_ostream_free (html_styled_ostream_t first_arg)
205 {
206   const struct html_styled_ostream_implementation *vtable =
207     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
208   vtable->free (first_arg);
209 }
210
211 void
212 html_styled_ostream_begin_use_class (html_styled_ostream_t first_arg, const char *classname)
213 {
214   const struct html_styled_ostream_implementation *vtable =
215     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
216   vtable->begin_use_class (first_arg,classname);
217 }
218
219 void
220 html_styled_ostream_end_use_class (html_styled_ostream_t first_arg, const char *classname)
221 {
222   const struct html_styled_ostream_implementation *vtable =
223     ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
224   vtable->end_use_class (first_arg,classname);
225 }
226
227 #endif