Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / src / write-xml.c
1 /* Writing XML files.
2    Copyright (C) 1995-1998, 2000-2003, 2005-2006, 2008-2009, 2014-2015
3    Free 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 <http://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 "po-charset.h"
33 #include "read-catalog.h"
34 #include "read-po.h"
35 #include "fwriteerror.h"
36 #include "xalloc.h"
37 #include "gettext.h"
38
39 #define _(str) gettext (str)
40
41 int
42 msgdomain_write_xml_bulk (msgfmt_operand_list_ty *operands,
43                           const char *template_file_name,
44                           its_rule_list_ty *its_rules,
45                           const char *file_name)
46 {
47   its_merge_context_ty *context;
48   size_t i;
49   FILE *fp;
50
51   if (strcmp (file_name, "-") == 0)
52     fp = stdout;
53   else
54     {
55       fp = fopen (file_name, "wb");
56       if (fp == NULL)
57         {
58           error (0, errno, _("cannot create output file \"%s\""),
59                  file_name);
60           return 1;
61         }
62     }
63
64   context = its_merge_context_alloc (its_rules, template_file_name);
65   for (i = 0; i < operands->nitems; i++)
66     its_merge_context_merge (context,
67                              operands->items[i].language,
68                              operands->items[i].mlp);
69   its_merge_context_write (context, fp);
70   its_merge_context_free (context);
71
72   /* Make sure nothing went wrong.  */
73   if (fwriteerror (fp))
74     {
75       error (0, errno, _("error while writing \"%s\" file"),
76              file_name);
77       return 1;
78     }
79
80   return 0;
81 }
82
83 int
84 msgdomain_write_xml (message_list_ty *mlp,
85                      const char *canon_encoding,
86                      const char *locale_name,
87                      const char *template_file_name,
88                      its_rule_list_ty *its_rules,
89                      const char *file_name)
90 {
91   msgfmt_operand_ty operand;
92   msgfmt_operand_list_ty operands;
93
94   /* Convert the messages to Unicode.  */
95   iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);
96
97   /* Create a single-element operands and run the bulk operation on it.  */
98   operand.language = (char *) locale_name;
99   operand.mlp = mlp;
100   operands.nitems = 1;
101   operands.items = &operand;
102
103   return msgdomain_write_xml_bulk (&operands,
104                                    template_file_name,
105                                    its_rules,
106                                    file_name);
107 }