Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / src / format-qt.c
1 /* Qt format strings.
2    Copyright (C) 2003-2004, 2006-2007, 2009, 2015 Free Software
3    Foundation, Inc.
4    Written by Bruno Haible <bruno@clisp.org>, 2003.
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 #include <stdbool.h>
24 #include <stdlib.h>
25
26 #include "format.h"
27 #include "xalloc.h"
28 #include "xvasprintf.h"
29 #include "gettext.h"
30
31 #define _(str) gettext (str)
32
33 /* Qt format strings are processed by QString::arg and are documented in
34    qt-4.3.0/doc/html/qstring.html.
35    A directive
36      - starts with '%',
37      - is optionally followed by 'L' (indicates locale-dependent processing),
38      - is followed by one or two digits ('0' to '9'). %0n is equivalent to %n.
39    An unterminated directive ('%' or '%L' not followed by a digit or at the
40    end) is not an error.
41    The first .arg() invocation replaces the %n with the lowest numbered n,
42    the next .arg() invocation then replaces the %n with the second-lowest
43    numbered n, and so on.
44    This is inherently buggy because a '%' in the first replacement confuses
45    the second .arg() invocation.
46    To reduce this problem and introduce another one, there are also .arg()
47    methods that take up to 9 strings and perform the replacements in one swoop.
48    But this method works only on strings that contain no 'L' flags and only
49    single-digit argument designators.
50    Although %0 is supported, usually %1 denotes the first argument, %2 the
51    second argument etc.  */
52
53 struct spec
54 {
55   /* Number of format directives.  */
56   unsigned int directives;
57
58   /* True if the string supports the multi-argument .arg() methods, i.e. if it
59      contains no 'L' flags and only single-digit argument designators.  */
60   bool simple;
61
62   /* Booleans telling which %nn was seen.  */
63   unsigned int arg_count;
64   bool args_used[100];
65 };
66
67
68 static void *
69 format_parse (const char *format, bool translated, char *fdi,
70               char **invalid_reason)
71 {
72   const char *const format_start = format;
73   struct spec spec;
74   struct spec *result;
75
76   spec.directives = 0;
77   spec.simple = true;
78   spec.arg_count = 0;
79
80   for (; *format != '\0';)
81     if (*format++ == '%')
82       {
83         const char *dir_start = format - 1;
84         bool locale_flag = false;
85
86         if (*format == 'L')
87           {
88             locale_flag = true;
89             format++;
90           }
91         if (*format >= '0' && *format <= '9')
92           {
93             /* A directive.  */
94             unsigned int number;
95
96             FDI_SET (dir_start, FMTDIR_START);
97             spec.directives++;
98             if (locale_flag)
99               spec.simple = false;
100
101             number = *format - '0';
102             if (format[1] >= '0' && format[1] <= '9')
103               {
104                 number = 10 * number + (format[1] - '0');
105                 spec.simple = false;
106                 format++;
107               }
108
109             while (spec.arg_count <= number)
110               spec.args_used[spec.arg_count++] = false;
111             spec.args_used[number] = true;
112
113             FDI_SET (format, FMTDIR_END);
114
115             format++;
116           }
117       }
118
119   result = XMALLOC (struct spec);
120   *result = spec;
121   return result;
122 }
123
124 static void
125 format_free (void *descr)
126 {
127   struct spec *spec = (struct spec *) descr;
128
129   free (spec);
130 }
131
132 static int
133 format_get_number_of_directives (void *descr)
134 {
135   struct spec *spec = (struct spec *) descr;
136
137   return spec->directives;
138 }
139
140 static bool
141 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
142               formatstring_error_logger_t error_logger,
143               const char *pretty_msgid, const char *pretty_msgstr)
144 {
145   struct spec *spec1 = (struct spec *) msgid_descr;
146   struct spec *spec2 = (struct spec *) msgstr_descr;
147   bool err = false;
148   unsigned int i;
149
150   if (spec1->simple && !spec2->simple)
151     {
152       if (error_logger)
153         error_logger (_("'%s' is a simple format string, but '%s' is not: it contains an 'L' flag or a double-digit argument number"),
154                       pretty_msgid, pretty_msgstr);
155       err = true;
156     }
157
158   if (!err)
159     for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
160       {
161         bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
162         bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
163
164         /* The translator cannot omit a %n from the msgstr because that would
165            yield a "Argument missing" warning at runtime.  */
166         if (arg_used1 != arg_used2)
167           {
168             if (error_logger)
169               {
170                 if (arg_used1)
171                   error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
172                                 i, pretty_msgstr);
173                 else
174                   error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
175                                 i, pretty_msgstr, pretty_msgid);
176               }
177             err = true;
178             break;
179           }
180       }
181
182   return err;
183 }
184
185
186 struct formatstring_parser formatstring_qt =
187 {
188   format_parse,
189   format_free,
190   format_get_number_of_directives,
191   NULL,
192   format_check
193 };
194
195
196 #ifdef TEST
197
198 /* Test program: Print the argument list specification returned by
199    format_parse for strings read from standard input.  */
200
201 #include <stdio.h>
202
203 static void
204 format_print (void *descr)
205 {
206   struct spec *spec = (struct spec *) descr;
207   unsigned int i;
208
209   if (spec == NULL)
210     {
211       printf ("INVALID");
212       return;
213     }
214
215   printf ("(");
216   for (i = 0; i < spec->arg_count; i++)
217     {
218       if (i > 0)
219         printf (" ");
220       if (spec->args_used[i])
221         printf ("*");
222       else
223         printf ("_");
224     }
225   printf (")");
226 }
227
228 int
229 main ()
230 {
231   for (;;)
232     {
233       char *line = NULL;
234       size_t line_size = 0;
235       int line_len;
236       char *invalid_reason;
237       void *descr;
238
239       line_len = getline (&line, &line_size, stdin);
240       if (line_len < 0)
241         break;
242       if (line_len > 0 && line[line_len - 1] == '\n')
243         line[--line_len] = '\0';
244
245       invalid_reason = NULL;
246       descr = format_parse (line, false, NULL, &invalid_reason);
247
248       format_print (descr);
249       printf ("\n");
250       if (descr == NULL)
251         printf ("%s\n", invalid_reason);
252
253       free (invalid_reason);
254       free (line);
255     }
256
257   return 0;
258 }
259
260 /*
261  * For Emacs M-x compile
262  * Local Variables:
263  * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../intl -DHAVE_CONFIG_H -DTEST format-qt.c ../gnulib-lib/libgettextlib.la"
264  * End:
265  */
266
267 #endif /* TEST */