2 Copyright (C) 2003-2004, 2006-2007, 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "xvasprintf.h"
30 #define _(str) gettext (str)
32 /* Qt format strings are processed by QString::arg and are documented in
33 qt-4.3.0/doc/html/qstring.html.
36 - is optionally followed by 'L' (indicates locale-dependent processing),
37 - is followed by one or two digits ('0' to '9'). %0n is equivalent to %n.
38 An unterminated directive ('%' or '%L' not followed by a digit or at the
40 The first .arg() invocation replaces the %n with the lowest numbered n,
41 the next .arg() invocation then replaces the %n with the second-lowest
42 numbered n, and so on.
43 This is inherently buggy because a '%' in the first replacement confuses
44 the second .arg() invocation.
45 To reduce this problem and introduce another one, there are also .arg()
46 methods that take up to 9 strings and perform the replacements in one swoop.
47 But this method works only on strings that contain no 'L' flags and only
48 single-digit argument designators.
49 Although %0 is supported, usually %1 denotes the first argument, %2 the
50 second argument etc. */
54 /* Number of format directives. */
55 unsigned int directives;
57 /* True if the string supports the multi-argument .arg() methods, i.e. if it
58 contains no 'L' flags and only single-digit argument designators. */
61 /* Booleans telling which %nn was seen. */
62 unsigned int arg_count;
68 format_parse (const char *format, bool translated, char *fdi,
69 char **invalid_reason)
71 const char *const format_start = format;
79 for (; *format != '\0';)
82 const char *dir_start = format - 1;
83 bool locale_flag = false;
90 if (*format >= '0' && *format <= '9')
95 FDI_SET (dir_start, FMTDIR_START);
100 number = *format - '0';
101 if (format[1] >= '0' && format[1] <= '9')
103 number = 10 * number + (format[1] - '0');
108 while (spec.arg_count <= number)
109 spec.args_used[spec.arg_count++] = false;
110 spec.args_used[number] = true;
112 FDI_SET (format, FMTDIR_END);
118 result = XMALLOC (struct spec);
124 format_free (void *descr)
126 struct spec *spec = (struct spec *) descr;
132 format_get_number_of_directives (void *descr)
134 struct spec *spec = (struct spec *) descr;
136 return spec->directives;
140 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
141 formatstring_error_logger_t error_logger,
142 const char *pretty_msgid, const char *pretty_msgstr)
144 struct spec *spec1 = (struct spec *) msgid_descr;
145 struct spec *spec2 = (struct spec *) msgstr_descr;
149 if (spec1->simple && !spec2->simple)
152 error_logger (_("'%s' is a simple format string, but '%s' is not: it contains an 'L' flag or a double-digit argument number"),
153 pretty_msgid, pretty_msgstr);
158 for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
160 bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
161 bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
163 /* The translator cannot omit a %n from the msgstr because that would
164 yield a "Argument missing" warning at runtime. */
165 if (arg_used1 != arg_used2)
170 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
173 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
174 i, pretty_msgstr, pretty_msgid);
185 struct formatstring_parser formatstring_qt =
189 format_get_number_of_directives,
197 /* Test program: Print the argument list specification returned by
198 format_parse for strings read from standard input. */
203 format_print (void *descr)
205 struct spec *spec = (struct spec *) descr;
215 for (i = 0; i < spec->arg_count; i++)
219 if (spec->args_used[i])
233 size_t line_size = 0;
235 char *invalid_reason;
238 line_len = getline (&line, &line_size, stdin);
241 if (line_len > 0 && line[line_len - 1] == '\n')
242 line[--line_len] = '\0';
244 invalid_reason = NULL;
245 descr = format_parse (line, false, NULL, &invalid_reason);
247 format_print (descr);
250 printf ("%s\n", invalid_reason);
252 free (invalid_reason);
260 * For Emacs M-x compile
262 * 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"