(root)/
gettext-0.22.4/
gettext-tools/
src/
write-xml.c
       1  /* Writing XML files.
       2     Copyright (C) 1995-1998, 2000-2003, 2005-2006, 2008-2009, 2014-2016 Free
       3     Software Foundation, Inc.
       4     This file was written by Daiki Ueno <ueno@gnu.org>.
       5  
       6     This program is free software: you can redistribute it and/or modify
       7     it under the terms of the GNU General Public License as published by
       8     the Free Software Foundation; either version 3 of the License, or
       9     (at your option) any later version.
      10  
      11     This program is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14     GNU General Public License for more details.
      15  
      16     You should have received a copy of the GNU General Public License
      17     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      18  
      19  #ifdef HAVE_CONFIG_H
      20  # include <config.h>
      21  #endif
      22  
      23  /* Specification.  */
      24  #include "write-xml.h"
      25  
      26  #include <assert.h>
      27  #include <errno.h>
      28  #include <stdlib.h>
      29  #include <stdio.h>
      30  #include "error.h"
      31  #include "msgl-iconv.h"
      32  #include "msgl-header.h"
      33  #include "po-charset.h"
      34  #include "read-catalog.h"
      35  #include "read-po.h"
      36  #include "fwriteerror.h"
      37  #include "xalloc.h"
      38  #include "gettext.h"
      39  
      40  #define _(str) gettext (str)
      41  
      42  int
      43  msgdomain_write_xml_bulk (msgfmt_operand_list_ty *operands,
      44                            const char *template_file_name,
      45                            its_rule_list_ty *its_rules,
      46                            const char *file_name)
      47  {
      48    its_merge_context_ty *context;
      49    size_t i;
      50    FILE *fp;
      51  
      52    if (strcmp (file_name, "-") == 0)
      53      fp = stdout;
      54    else
      55      {
      56        fp = fopen (file_name, "wb");
      57        if (fp == NULL)
      58          {
      59            error (0, errno, _("cannot create output file \"%s\""),
      60                   file_name);
      61            return 1;
      62          }
      63      }
      64  
      65    context = its_merge_context_alloc (its_rules, template_file_name);
      66    for (i = 0; i < operands->nitems; i++)
      67      its_merge_context_merge (context,
      68                               operands->items[i].language,
      69                               operands->items[i].mlp);
      70    its_merge_context_write (context, fp);
      71    its_merge_context_free (context);
      72  
      73    /* Make sure nothing went wrong.  */
      74    if (fwriteerror (fp))
      75      {
      76        error (0, errno, _("error while writing \"%s\" file"),
      77               file_name);
      78        return 1;
      79      }
      80  
      81    return 0;
      82  }
      83  
      84  int
      85  msgdomain_write_xml (message_list_ty *mlp,
      86                       const char *canon_encoding,
      87                       const char *locale_name,
      88                       const char *template_file_name,
      89                       its_rule_list_ty *its_rules,
      90                       const char *file_name)
      91  {
      92    msgfmt_operand_ty operand;
      93    msgfmt_operand_list_ty operands;
      94  
      95    /* Convert the messages to Unicode.  */
      96    iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
      97  
      98    /* Support for "reproducible builds": Delete information that may vary
      99       between builds in the same conditions.  */
     100    message_list_delete_header_field (mlp, "POT-Creation-Date:");
     101  
     102    /* Create a single-element operands and run the bulk operation on it.  */
     103    operand.language = (char *) locale_name;
     104    operand.mlp = mlp;
     105    operands.nitems = 1;
     106    operands.items = &operand;
     107  
     108    return msgdomain_write_xml_bulk (&operands,
     109                                     template_file_name,
     110                                     its_rules,
     111                                     file_name);
     112  }