File: html-ostream.oo.c

package info (click to toggle)
gettext 0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 167,652 kB
  • sloc: ansic: 532,868; sh: 68,216; perl: 28,011; makefile: 9,046; lisp: 3,184; yacc: 1,055; java: 615; cs: 589; cpp: 397; objc: 343; sed: 79; tcl: 63; xml: 40; pascal: 11; awk: 7; php: 7
file content (454 lines) | stat: -rw-r--r-- 14,815 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* Output stream that produces HTML output.
   Copyright (C) 2006-2009, 2019-2020 Free Software Foundation, Inc.
   Written by Bruno Haible <bruno@clisp.org>, 2006.

   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
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include "html-ostream.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gl_xlist.h"
#include "gl_array_list.h"
#include "minmax.h"
#include "unistr.h"
#include "xalloc.h"

struct html_ostream : struct ostream
{
fields:
  /* The destination stream.  */
  ostream_t destination;
  /* The current hyperlink ref.  */
  char *hyperlink_ref;
  /* The stack of active CSS classes.  */
  gl_list_t /* <char *> */ class_stack;
  /* Current and last size of the active portion of this stack.  Always
     size(class_stack) == max(curr_class_stack_size,last_class_stack_size).  */
  size_t curr_class_stack_size;
  size_t last_class_stack_size;
  /* Last few bytes that could not yet be converted.  */
  #define BUFSIZE 6
  char buf[BUFSIZE];
  size_t buflen;
};

/* Emit an HTML attribute value.
   quote is either '"' or '\''.  */
static void
write_attribute_value (html_ostream_t stream, const char *value, char quote)
{
  /* Need to escape the '<', '>', '&', quote characters.  */
  ostream_t destination = stream->destination;
  const char *p = value;

  for (;;)
    {
      const char *q = p;

      while (*q != '\0' && *q != '<' && *q != '>' && *q != '&' && *q != quote)
        q++;
      if (p < q)
        ostream_write_mem (destination, p, q - p);
      if (*q == '\0')
        break;
      switch (*q)
        {
        case '<':
          ostream_write_str (destination, "&lt;");
          break;
        case '>':
          ostream_write_str (destination, "&gt;");
          break;
        case '&':
          ostream_write_str (destination, "&amp;");
          break;
        case '"':
          ostream_write_str (destination, "&quot;");
          break;
        case '\'':
          ostream_write_str (destination, "&apos;");
          break;
        default:
          abort ();
        }
      p = q + 1;
    }
}

/* Implementation of ostream_t methods.  */

static void
verify_invariants (html_ostream_t stream)
{
  /* Verify the invariant regarding size(class_stack).  */
  if (gl_list_size (stream->class_stack)
      != MAX (stream->curr_class_stack_size, stream->last_class_stack_size))
    abort ();
}

/* Removes the excess elements of class_stack.
   Needs to be called after max(curr_class_stack_size,last_class_stack_size)
   may have been reduced.  */
static void
shrink_class_stack (html_ostream_t stream)
{
  size_t keep =
    MAX (stream->curr_class_stack_size, stream->last_class_stack_size);
  size_t i = gl_list_size (stream->class_stack);
  while (i > keep)
    {
      i--;
      free ((char *) gl_list_get_at (stream->class_stack, i));
      gl_list_remove_at (stream->class_stack, i);
    }
}

/* Emits <span> or </span> tags, to follow the increase / decrease of the
   class_stack from last_class_stack_size to curr_class_stack_size.
   When done, sets last_class_stack_size to curr_class_stack_size.  */
static void
emit_pending_spans (html_ostream_t stream, bool shrink_stack)
{
  if (stream->curr_class_stack_size > stream->last_class_stack_size)
    {
      size_t i;

      for (i = stream->last_class_stack_size; i < stream->curr_class_stack_size; i++)
        {
          char *classname = (char *) gl_list_get_at (stream->class_stack, i);

          ostream_write_str (stream->destination, "<span class=\"");
          ostream_write_str (stream->destination, classname);
          ostream_write_str (stream->destination, "\">");
        }
      stream->last_class_stack_size = stream->curr_class_stack_size;
    }
  else if (stream->curr_class_stack_size < stream->last_class_stack_size)
    {
      size_t i;

      for (i = stream->last_class_stack_size; i > stream->curr_class_stack_size; i--)
        ostream_write_str (stream->destination, "</span>");
      stream->last_class_stack_size = stream->curr_class_stack_size;
      if (shrink_stack)
        shrink_class_stack (stream);
    }
  /* Here last_class_stack_size == curr_class_stack_size.  */
  if (shrink_stack)
    verify_invariants (stream);
}

static void
html_ostream::write_mem (html_ostream_t stream, const void *data, size_t len)
{
  if (len > 0)
    {
      #define BUFFERSIZE 2048
      char inbuffer[BUFFERSIZE];
      size_t inbufcount;

      inbufcount = stream->buflen;
      if (inbufcount > 0)
        memcpy (inbuffer, stream->buf, inbufcount);
      for (;;)
        {
          /* At this point, inbuffer[0..inbufcount-1] is filled.  */
          {
            /* Combine the previous rest with a chunk of new input.  */
            size_t n =
              (len <= BUFFERSIZE - inbufcount ? len : BUFFERSIZE - inbufcount);

            if (n > 0)
              {
                memcpy (inbuffer + inbufcount, data, n);
                data = (const char *) data + n;
                inbufcount += n;
                len -= n;
              }
          }
          {
            /* Handle complete UTF-8 characters.  */
            const char *inptr = inbuffer;
            size_t insize = inbufcount;

            while (insize > 0)
              {
                unsigned char c0;
                ucs4_t uc;
                int nbytes;

                c0 = ((const unsigned char *) inptr)[0];
                if (insize < (c0 < 0xc0 ? 1 : c0 < 0xe0 ? 2 : c0 < 0xf0 ? 3 :
                              c0 < 0xf8 ? 4 : c0 < 0xfc ? 5 : 6))
                  break;

                nbytes = u8_mbtouc (&uc, (const unsigned char *) inptr, insize);

                if (uc == '\n')
                  {
                    verify_invariants (stream);
                    /* Emit </span> tags to follow the decrease of the class stack
                       from last_class_stack_size to 0.  Then emit the newline.
                       Then prepare for emitting <span> tags to go back from 0
                       to curr_class_stack_size.  */
                    size_t prev_class_stack_size = stream->curr_class_stack_size;
                    stream->curr_class_stack_size = 0;
                    emit_pending_spans (stream, false);
                    stream->curr_class_stack_size = prev_class_stack_size;
                    ostream_write_str (stream->destination, "<br/>");
                    shrink_class_stack (stream);
                    verify_invariants (stream);
                  }
                else
                  {
                    emit_pending_spans (stream, true);

                    switch (uc)
                      {
                      case '"':
                        ostream_write_str (stream->destination, "&quot;");
                        break;
                      case '&':
                        ostream_write_str (stream->destination, "&amp;");
                        break;
                      case '<':
                        ostream_write_str (stream->destination, "&lt;");
                        break;
                      case '>':
                        /* Needed to avoid "]]>" in the output.  */
                        ostream_write_str (stream->destination, "&gt;");
                        break;
                      case ' ':
                        /* Needed because HTML viewers merge adjacent spaces
                           and drop spaces adjacent to <br> and similar.  */
                        ostream_write_str (stream->destination, "&nbsp;");
                        break;
                      default:
                        if (uc >= 0x20 && uc < 0x7F)
                          {
                            /* Output ASCII characters as such.  */
                            char bytes[1];
                            bytes[0] = uc;
                            ostream_write_mem (stream->destination, bytes, 1);
                          }
                        else
                          {
                            /* Output non-ASCII characters in #&nnn;
                               notation.  */
                            char bytes[32];
                            sprintf (bytes, "&#%d;", (int) uc);
                            ostream_write_str (stream->destination, bytes);
                          }
                        break;
                      }
                  }

                inptr += nbytes;
                insize -= nbytes;
              }
            /* Put back the unconverted part.  */
            if (insize > BUFSIZE)
              abort ();
            if (len == 0)
              {
                if (insize > 0)
                  memcpy (stream->buf, inptr, insize);
                stream->buflen = insize;
                break;
              }
            if (insize > 0)
              memmove (inbuffer, inptr, insize);
            inbufcount = insize;
          }
        }
      #undef BUFFERSIZE
    }
}

static void
html_ostream::flush (html_ostream_t stream, ostream_flush_scope_t scope)
{
  verify_invariants (stream);
  /* stream->buf[] contains only a few bytes that don't correspond to a
     character.  Can't flush it.  */
  /* Close the open <span> tags, and prepare for reopening the same <span>
     tags.  */
  size_t prev_class_stack_size = stream->curr_class_stack_size;
  stream->curr_class_stack_size = 0;
  emit_pending_spans (stream, false);
  stream->curr_class_stack_size = prev_class_stack_size;
  shrink_class_stack (stream);
  verify_invariants (stream);

  if (scope != FLUSH_THIS_STREAM)
    ostream_flush (stream->destination, scope);
}

static void
html_ostream::free (html_ostream_t stream)
{
  stream->curr_class_stack_size = 0;
  emit_pending_spans (stream, true);
  if (stream->hyperlink_ref != NULL)
    {
      /* Close the current <a> element.  */
      ostream_write_str (stream->destination, "</a>");
      free (stream->hyperlink_ref);
    }
  verify_invariants (stream);
  gl_list_free (stream->class_stack);
  free (stream);
}

/* Implementation of html_ostream_t methods.  */

static void
html_ostream::begin_span (html_ostream_t stream, const char *classname)
{
  verify_invariants (stream);
  if (stream->last_class_stack_size > stream->curr_class_stack_size
      && strcmp ((char *) gl_list_get_at (stream->class_stack,
                                          stream->curr_class_stack_size),
                 classname) != 0)
    emit_pending_spans (stream, true);
  /* Now either
       last_class_stack_size <= curr_class_stack_size
       - in this case we have to append the given CLASSNAME -
     or
       last_class_stack_size > curr_class_stack_size
       && class_stack[curr_class_stack_size] == CLASSNAME
       - in this case we only need to increment curr_class_stack_size.  */
  if (stream->last_class_stack_size <= stream->curr_class_stack_size)
    gl_list_add_at (stream->class_stack, stream->curr_class_stack_size,
                    xstrdup (classname));
  stream->curr_class_stack_size++;
  verify_invariants (stream);
}

static void
html_ostream::end_span (html_ostream_t stream, const char *classname)
{
  verify_invariants (stream);
  if (stream->curr_class_stack_size > 0)
    {
      char *innermost_active_span =
        (char *) gl_list_get_at (stream->class_stack,
                                 stream->curr_class_stack_size - 1);
      if (strcmp (innermost_active_span, classname) == 0)
        {
          stream->curr_class_stack_size--;
          shrink_class_stack (stream);
          verify_invariants (stream);
          return;
        }
    }
  /* Improperly nested begin_span/end_span calls.  */
  abort ();
}

static const char *
html_ostream::get_hyperlink_ref (html_ostream_t stream)
{
  return stream->hyperlink_ref;
}

static void
html_ostream::set_hyperlink_ref (html_ostream_t stream, const char *ref)
{
  char *ref_copy = (ref != NULL ? xstrdup (ref) : NULL);

  verify_invariants (stream);
  if (stream->hyperlink_ref != NULL)
    {
      /* Close the open <span> tags, and prepare for reopening the same <span>
         tags.  */
      size_t prev_class_stack_size = stream->curr_class_stack_size;
      stream->curr_class_stack_size = 0;
      emit_pending_spans (stream, false);
      stream->curr_class_stack_size = prev_class_stack_size;
      /* Close the current <a> element.  */
      ostream_write_str (stream->destination, "</a>");
      shrink_class_stack (stream);

      free (stream->hyperlink_ref);
    }
  stream->hyperlink_ref = ref_copy;
  if (stream->hyperlink_ref != NULL)
    {
      /* Close the open <span> tags, and prepare for reopening the same <span>
         tags.  */
      size_t prev_class_stack_size = stream->curr_class_stack_size;
      stream->curr_class_stack_size = 0;
      emit_pending_spans (stream, false);
      stream->curr_class_stack_size = prev_class_stack_size;
      /* Open an <a> element.  */
      ostream_write_str (stream->destination, "<a href=\"");
      write_attribute_value (stream, stream->hyperlink_ref, '"');
      ostream_write_str (stream->destination, "\">");
      shrink_class_stack (stream);
    }
  verify_invariants (stream);
}

static void
html_ostream::flush_to_current_style (html_ostream_t stream)
{
  verify_invariants (stream);
  /* stream->buf[] contains only a few bytes that don't correspond to a
     character.  Can't flush it.  */
  /* Open all requested <span> tags.  */
  emit_pending_spans (stream, true);
  verify_invariants (stream);
}

/* Constructor.  */

html_ostream_t
html_ostream_create (ostream_t destination)
{
  html_ostream_t stream = XMALLOC (struct html_ostream_representation);

  stream->base.vtable = &html_ostream_vtable;
  stream->destination = destination;
  stream->hyperlink_ref = NULL;
  stream->class_stack =
    gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
  stream->curr_class_stack_size = 0;
  stream->last_class_stack_size = 0;
  stream->buflen = 0;

  return stream;
}

/* Accessors.  */

static ostream_t
html_ostream::get_destination (html_ostream_t stream)
{
  return stream->destination;
}

/* Instanceof test.  */

bool
is_instance_of_html_ostream (ostream_t stream)
{
  return IS_INSTANCE (stream, ostream, html_ostream);
}