1 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 #line 1 "html-ostream.oo.c"
4 /* Output stream that produces HTML output.
5 Copyright (C) 2006-2009 Free Software Foundation, Inc.
6 Written by Bruno Haible <bruno@clisp.org>, 2006.
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.
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.
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/>. */
24 #include "html-ostream.h"
31 #include "gl_array_list.h"
35 #line 36 "html-ostream.c"
37 #define html_ostream_representation any_ostream_representation
39 #include "html_ostream.priv.h"
41 const typeinfo_t html_ostream_typeinfo = { "html_ostream" };
43 static const typeinfo_t * const html_ostream_superclasses[] =
44 { html_ostream_SUPERCLASSES };
46 #define super ostream_vtable
48 #line 48 "html-ostream.oo.c"
50 /* Implementation of ostream_t methods. */
53 emit_pending_spans (html_ostream_t stream, bool shrink_stack)
55 if (stream->curr_class_stack_size > stream->last_class_stack_size)
59 for (i = stream->last_class_stack_size; i < stream->curr_class_stack_size; i++)
61 char *classname = (char *) gl_list_get_at (stream->class_stack, i);
63 ostream_write_str (stream->destination, "<span class=\"");
64 ostream_write_str (stream->destination, classname);
65 ostream_write_str (stream->destination, "\">");
67 stream->last_class_stack_size = stream->curr_class_stack_size;
69 else if (stream->curr_class_stack_size < stream->last_class_stack_size)
71 size_t i = stream->last_class_stack_size;
73 while (i > stream->curr_class_stack_size)
78 classname = (char *) gl_list_get_at (stream->class_stack, i);
79 ostream_write_str (stream->destination, "</span>");
82 gl_list_remove_at (stream->class_stack, i);
86 stream->last_class_stack_size = stream->curr_class_stack_size;
91 html_ostream__write_mem (html_ostream_t stream, const void *data, size_t len)
95 #define BUFFERSIZE 2048
96 char inbuffer[BUFFERSIZE];
99 inbufcount = stream->buflen;
101 memcpy (inbuffer, stream->buf, inbufcount);
104 /* At this point, inbuffer[0..inbufcount-1] is filled. */
106 /* Combine the previous rest with a chunk of new input. */
108 (len <= BUFFERSIZE - inbufcount ? len : BUFFERSIZE - inbufcount);
112 memcpy (inbuffer + inbufcount, data, n);
113 data = (char *) data + n;
119 /* Handle complete UTF-8 characters. */
120 const char *inptr = inbuffer;
121 size_t insize = inbufcount;
129 c0 = ((const unsigned char *) inptr)[0];
130 if (insize < (c0 < 0xc0 ? 1 : c0 < 0xe0 ? 2 : c0 < 0xf0 ? 3 :
131 c0 < 0xf8 ? 4 : c0 < 0xfc ? 5 : 6))
134 nbytes = u8_mbtouc (&uc, (const unsigned char *) inptr, insize);
138 size_t prev_class_stack_size = stream->curr_class_stack_size;
139 stream->curr_class_stack_size = 0;
140 emit_pending_spans (stream, false);
141 ostream_write_str (stream->destination, "<br/>");
142 stream->curr_class_stack_size = prev_class_stack_size;
146 emit_pending_spans (stream, true);
151 ostream_write_str (stream->destination, """);
154 ostream_write_str (stream->destination, "&");
157 ostream_write_str (stream->destination, "<");
160 /* Needed to avoid "]]>" in the output. */
161 ostream_write_str (stream->destination, ">");
164 /* Needed because HTML viewers merge adjacent spaces
165 and drop spaces adjacent to <br> and similar. */
166 ostream_write_str (stream->destination, " ");
169 if (uc >= 0x20 && uc < 0x7F)
171 /* Output ASCII characters as such. */
174 ostream_write_mem (stream->destination, bytes, 1);
178 /* Output non-ASCII characters in #&nnn;
181 sprintf (bytes, "&#%d;", (int) uc);
182 ostream_write_str (stream->destination, bytes);
191 /* Put back the unconverted part. */
192 if (insize > BUFSIZE)
197 memcpy (stream->buf, inptr, insize);
198 stream->buflen = insize;
202 memmove (inbuffer, inptr, insize);
211 html_ostream__flush (html_ostream_t stream)
213 /* There's nothing to do here, since stream->buf[] contains only a few
214 bytes that don't correspond to a character, and it's not worth closing
219 html_ostream__free (html_ostream_t stream)
221 stream->curr_class_stack_size = 0;
222 emit_pending_spans (stream, true);
223 gl_list_free (stream->class_stack);
227 /* Implementation of html_ostream_t methods. */
230 html_ostream__begin_span (html_ostream_t stream, const char *classname)
232 if (stream->last_class_stack_size > stream->curr_class_stack_size
233 && strcmp ((char *) gl_list_get_at (stream->class_stack,
234 stream->curr_class_stack_size),
236 emit_pending_spans (stream, true);
238 last_class_stack_size <= curr_class_stack_size
239 - in this case we have to append the given CLASSNAME -
241 last_class_stack_size > curr_class_stack_size
242 && class_stack[curr_class_stack_size] == CLASSNAME
243 - in this case we only need to increment curr_class_stack_size. */
244 if (stream->last_class_stack_size <= stream->curr_class_stack_size)
245 gl_list_add_at (stream->class_stack, stream->curr_class_stack_size,
246 xstrdup (classname));
247 stream->curr_class_stack_size++;
251 html_ostream__end_span (html_ostream_t stream, const char *classname)
253 if (!(stream->curr_class_stack_size > 0
254 && strcmp ((char *) gl_list_get_at (stream->class_stack,
255 stream->curr_class_stack_size - 1),
257 /* Improperly nested begin_span/end_span calls. */
259 stream->curr_class_stack_size--;
265 html_ostream_create (ostream_t destination)
267 html_ostream_t stream = XMALLOC (struct html_ostream_representation);
269 stream->base.vtable = &html_ostream_vtable;
270 stream->destination = destination;
271 stream->class_stack =
272 gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
273 stream->curr_class_stack_size = 0;
274 stream->last_class_stack_size = 0;
280 #line 281 "html-ostream.c"
282 const struct html_ostream_implementation html_ostream_vtable =
284 html_ostream_superclasses,
285 sizeof (html_ostream_superclasses) / sizeof (html_ostream_superclasses[0]),
286 sizeof (struct html_ostream_representation),
287 html_ostream__write_mem,
290 html_ostream__begin_span,
291 html_ostream__end_span,
296 /* Define the functions that invoke the methods. */
299 html_ostream_write_mem (html_ostream_t first_arg, const void *data, size_t len)
301 const struct html_ostream_implementation *vtable =
302 ((struct html_ostream_representation_header *) (struct html_ostream_representation *) first_arg)->vtable;
303 vtable->write_mem (first_arg,data,len);
307 html_ostream_flush (html_ostream_t first_arg)
309 const struct html_ostream_implementation *vtable =
310 ((struct html_ostream_representation_header *) (struct html_ostream_representation *) first_arg)->vtable;
311 vtable->flush (first_arg);
315 html_ostream_free (html_ostream_t first_arg)
317 const struct html_ostream_implementation *vtable =
318 ((struct html_ostream_representation_header *) (struct html_ostream_representation *) first_arg)->vtable;
319 vtable->free (first_arg);
323 html_ostream_begin_span (html_ostream_t first_arg, const char *classname)
325 const struct html_ostream_implementation *vtable =
326 ((struct html_ostream_representation_header *) (struct html_ostream_representation *) first_arg)->vtable;
327 vtable->begin_span (first_arg,classname);
331 html_ostream_end_span (html_ostream_t first_arg, const char *classname)
333 const struct html_ostream_implementation *vtable =
334 ((struct html_ostream_representation_header *) (struct html_ostream_representation *) first_arg)->vtable;
335 vtable->end_span (first_arg,classname);