2 Copyright (C) 2003-2004, 2006-2007, 2009, 2015 Free Software
4 Written by Bruno Haible <bruno@clisp.org>, 2003.
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.
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.
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/>. */
28 #include "xvasprintf.h"
31 #define _(str) gettext (str)
33 /* Qt format strings are processed by QString::arg and are documented in
34 qt-4.3.0/doc/html/qstring.html.
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
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. */
55 /* Number of format directives. */
56 unsigned int directives;
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. */
62 /* Booleans telling which %nn was seen. */
63 unsigned int arg_count;
69 format_parse (const char *format, bool translated, char *fdi,
70 char **invalid_reason)
72 const char *const format_start = format;
80 for (; *format != '\0';)
83 const char *dir_start = format - 1;
84 bool locale_flag = false;
91 if (*format >= '0' && *format <= '9')
96 FDI_SET (dir_start, FMTDIR_START);
101 number = *format - '0';
102 if (format[1] >= '0' && format[1] <= '9')
104 number = 10 * number + (format[1] - '0');
109 while (spec.arg_count <= number)
110 spec.args_used[spec.arg_count++] = false;
111 spec.args_used[number] = true;
113 FDI_SET (format, FMTDIR_END);
119 result = XMALLOC (struct spec);
125 format_free (void *descr)
127 struct spec *spec = (struct spec *) descr;
133 format_get_number_of_directives (void *descr)
135 struct spec *spec = (struct spec *) descr;
137 return spec->directives;
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)
145 struct spec *spec1 = (struct spec *) msgid_descr;
146 struct spec *spec2 = (struct spec *) msgstr_descr;
150 if (spec1->simple && !spec2->simple)
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);
159 for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
161 bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
162 bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
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)
171 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
174 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
175 i, pretty_msgstr, pretty_msgid);
186 struct formatstring_parser formatstring_qt =
190 format_get_number_of_directives,
198 /* Test program: Print the argument list specification returned by
199 format_parse for strings read from standard input. */
204 format_print (void *descr)
206 struct spec *spec = (struct spec *) descr;
216 for (i = 0; i < spec->arg_count; i++)
220 if (spec->args_used[i])
234 size_t line_size = 0;
236 char *invalid_reason;
239 line_len = getline (&line, &line_size, stdin);
242 if (line_len > 0 && line[line_len - 1] == '\n')
243 line[--line_len] = '\0';
245 invalid_reason = NULL;
246 descr = format_parse (line, false, NULL, &invalid_reason);
248 format_print (descr);
251 printf ("%s\n", invalid_reason);
253 free (invalid_reason);
261 * For Emacs M-x compile
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"