(root)/
gettext-0.22.4/
libtextstyle/
lib/
html-styled-ostream.c
       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-2020 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 55 "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, ostream_flush_scope_t scope)
      72  {
      73    html_ostream_flush (stream->html_destination, scope);
      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    free (stream->hyperlink_id);
      83    free (stream->css_filename);
      84    free (stream);
      85  }
      86  
      87  /* Implementation of styled_ostream_t methods.  */
      88  
      89  static void
      90  html_styled_ostream__begin_use_class (html_styled_ostream_t stream,
      91                                        const char *classname)
      92  {
      93    html_ostream_begin_span (stream->html_destination, classname);
      94  }
      95  
      96  static void
      97  html_styled_ostream__end_use_class (html_styled_ostream_t stream,
      98                                      const char *classname)
      99  {
     100    html_ostream_end_span (stream->html_destination, classname);
     101  }
     102  
     103  static const char *
     104  html_styled_ostream__get_hyperlink_ref (html_styled_ostream_t stream)
     105  {
     106    return html_ostream_get_hyperlink_ref (stream->html_destination);
     107  }
     108  
     109  static const char *
     110  html_styled_ostream__get_hyperlink_id (html_styled_ostream_t stream)
     111  {
     112    return stream->hyperlink_id;
     113  }
     114  
     115  static void
     116  html_styled_ostream__set_hyperlink (html_styled_ostream_t stream,
     117                                      const char *ref, const char *id)
     118  {
     119    char *id_copy = (id != NULL ? xstrdup (id) : NULL);
     120  
     121    html_ostream_set_hyperlink_ref (stream->html_destination, ref);
     122    free (stream->hyperlink_id);
     123    stream->hyperlink_id = id_copy;
     124  }
     125  
     126  static void
     127  html_styled_ostream__flush_to_current_style (html_styled_ostream_t stream)
     128  {
     129    html_ostream_flush_to_current_style (stream->html_destination);
     130  }
     131  
     132  /* Constructor.  */
     133  
     134  html_styled_ostream_t
     135  html_styled_ostream_create (ostream_t destination, const char *css_filename)
     136  {
     137    html_styled_ostream_t stream =
     138      XMALLOC (struct html_styled_ostream_representation);
     139  
     140    stream->base.base.vtable = &html_styled_ostream_vtable;
     141    stream->destination = destination;
     142    stream->css_filename = xstrdup (css_filename);
     143    stream->html_destination = html_ostream_create (destination);
     144    stream->hyperlink_id = NULL;
     145  
     146    ostream_write_str (stream->destination, "<?xml version=\"1.0\"?>\n");
     147    /* HTML 4.01 or XHTML 1.0?
     148       Use HTML 4.01.  This is conservative.  Before switching to XHTML 1.0,
     149       verify that in the output
     150         - all HTML element names are in lowercase,
     151         - all empty elements are denoted like <br/> or <p></p>,
     152         - every attribute specification is in assignment form, like
     153           <table border="1">,
     154         - every <a name="..."> element also has an 'id' attribute,
     155         - special characters like < > & " are escaped in the <style> and
     156           <script> elements.  */
     157    ostream_write_str (stream->destination,
     158                       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
     159    ostream_write_str (stream->destination, "<html>\n");
     160    ostream_write_str (stream->destination, "<head>\n");
     161    if (css_filename != NULL)
     162      {
     163        ostream_write_str (stream->destination, "<style type=\"text/css\">\n"
     164                                                "<!--\n");
     165  
     166        /* Include the contents of CSS_FILENAME literally.  */
     167        {
     168          int fd;
     169          char buf[4096];
     170  
     171          fd = open (css_filename, O_RDONLY | O_TEXT);
     172          if (fd < 0)
     173            error (EXIT_FAILURE, errno,
     174                   _("error while opening \"%s\" for reading"),
     175                   css_filename);
     176  
     177          for (;;)
     178            {
     179              size_t n_read = safe_read (fd, buf, sizeof (buf));
     180              if (n_read == SAFE_READ_ERROR)
     181                error (EXIT_FAILURE, errno, _("error reading \"%s\""),
     182                       css_filename);
     183              if (n_read == 0)
     184                break;
     185  
     186              ostream_write_mem (stream->destination, buf, n_read);
     187            }
     188  
     189          if (close (fd) < 0)
     190            error (EXIT_FAILURE, errno, _("error after reading \"%s\""),
     191                   css_filename);
     192        }
     193  
     194        ostream_write_str (stream->destination, "-->\n"
     195                                                "</style>\n");
     196      }
     197    ostream_write_str (stream->destination, "</head>\n");
     198    ostream_write_str (stream->destination, "<body>\n");
     199  
     200    return stream;
     201  }
     202  
     203  /* Accessors.  */
     204  
     205  static ostream_t
     206  html_styled_ostream__get_destination (html_styled_ostream_t stream)
     207  {
     208    return stream->destination;
     209  }
     210  
     211  static html_ostream_t
     212  html_styled_ostream__get_html_destination (html_styled_ostream_t stream)
     213  {
     214    return stream->html_destination;
     215  }
     216  
     217  static const char *
     218  html_styled_ostream__get_css_filename (html_styled_ostream_t stream)
     219  {
     220    return stream->css_filename;
     221  }
     222  
     223  /* Instanceof test.  */
     224  
     225  bool
     226  is_instance_of_html_styled_ostream (ostream_t stream)
     227  {
     228    return IS_INSTANCE (stream, ostream, html_styled_ostream);
     229  }
     230  
     231  #line 232 "html-styled-ostream.c"
     232  
     233  const struct html_styled_ostream_implementation html_styled_ostream_vtable =
     234  {
     235    html_styled_ostream_superclasses,
     236    sizeof (html_styled_ostream_superclasses) / sizeof (html_styled_ostream_superclasses[0]),
     237    sizeof (struct html_styled_ostream_representation),
     238    html_styled_ostream__write_mem,
     239    html_styled_ostream__flush,
     240    html_styled_ostream__free,
     241    html_styled_ostream__begin_use_class,
     242    html_styled_ostream__end_use_class,
     243    html_styled_ostream__get_hyperlink_ref,
     244    html_styled_ostream__get_hyperlink_id,
     245    html_styled_ostream__set_hyperlink,
     246    html_styled_ostream__flush_to_current_style,
     247    html_styled_ostream__get_destination,
     248    html_styled_ostream__get_html_destination,
     249    html_styled_ostream__get_css_filename,
     250  };
     251  
     252  #if !HAVE_INLINE
     253  
     254  /* Define the functions that invoke the methods.  */
     255  
     256  void
     257  html_styled_ostream_write_mem (html_styled_ostream_t first_arg, const void *data, size_t len)
     258  {
     259    const struct html_styled_ostream_implementation *vtable =
     260      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     261    vtable->write_mem (first_arg,data,len);
     262  }
     263  
     264  void
     265  html_styled_ostream_flush (html_styled_ostream_t first_arg, ostream_flush_scope_t scope)
     266  {
     267    const struct html_styled_ostream_implementation *vtable =
     268      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     269    vtable->flush (first_arg,scope);
     270  }
     271  
     272  void
     273  html_styled_ostream_free (html_styled_ostream_t first_arg)
     274  {
     275    const struct html_styled_ostream_implementation *vtable =
     276      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     277    vtable->free (first_arg);
     278  }
     279  
     280  void
     281  html_styled_ostream_begin_use_class (html_styled_ostream_t first_arg, const char *classname)
     282  {
     283    const struct html_styled_ostream_implementation *vtable =
     284      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     285    vtable->begin_use_class (first_arg,classname);
     286  }
     287  
     288  void
     289  html_styled_ostream_end_use_class (html_styled_ostream_t first_arg, const char *classname)
     290  {
     291    const struct html_styled_ostream_implementation *vtable =
     292      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     293    vtable->end_use_class (first_arg,classname);
     294  }
     295  
     296  const char *
     297  html_styled_ostream_get_hyperlink_ref (html_styled_ostream_t first_arg)
     298  {
     299    const struct html_styled_ostream_implementation *vtable =
     300      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     301    return vtable->get_hyperlink_ref (first_arg);
     302  }
     303  
     304  const char *
     305  html_styled_ostream_get_hyperlink_id (html_styled_ostream_t first_arg)
     306  {
     307    const struct html_styled_ostream_implementation *vtable =
     308      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     309    return vtable->get_hyperlink_id (first_arg);
     310  }
     311  
     312  void
     313  html_styled_ostream_set_hyperlink (html_styled_ostream_t first_arg,                               const char *ref, const char *id)
     314  {
     315    const struct html_styled_ostream_implementation *vtable =
     316      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     317    vtable->set_hyperlink (first_arg,ref,id);
     318  }
     319  
     320  void
     321  html_styled_ostream_flush_to_current_style (html_styled_ostream_t first_arg)
     322  {
     323    const struct html_styled_ostream_implementation *vtable =
     324      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     325    vtable->flush_to_current_style (first_arg);
     326  }
     327  
     328  ostream_t
     329  html_styled_ostream_get_destination (html_styled_ostream_t first_arg)
     330  {
     331    const struct html_styled_ostream_implementation *vtable =
     332      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     333    return vtable->get_destination (first_arg);
     334  }
     335  
     336  html_ostream_t
     337  html_styled_ostream_get_html_destination (html_styled_ostream_t first_arg)
     338  {
     339    const struct html_styled_ostream_implementation *vtable =
     340      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     341    return vtable->get_html_destination (first_arg);
     342  }
     343  
     344  const char *
     345  html_styled_ostream_get_css_filename (html_styled_ostream_t first_arg)
     346  {
     347    const struct html_styled_ostream_implementation *vtable =
     348      ((struct html_styled_ostream_representation_header *) (struct html_styled_ostream_representation *) first_arg)->vtable;
     349    return vtable->get_css_filename (first_arg);
     350  }
     351  
     352  #endif