2
Copyright (C) 2003-2004 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 2, or (at your option)
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, write to the Free Software Foundation,
17
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30
#include "error-progname.h"
33
#define _(str) gettext (str)
35
/* Qt format strings are processed by QString::arg and are documented in
36
qt-3.0.5/doc/html/qstring.html.
37
A directive starts with '%' and is followed by a digit ('0' to '9').
38
Each %n must occur only once in the given string.
39
The first .arg() invocation replaces the %n with the lowest numbered n,
40
the next .arg() invocation then replaces the %n with the second-lowest
41
numbered n, and so on.
42
(This is inherently buggy because a '%' in the first replacement confuses
43
the second .arg() invocation.)
44
Although %0 is supported, usually %1 denotes the first argument, %2 the
45
second argument etc. */
49
unsigned int directives;
50
unsigned int arg_count;
56
format_parse (const char *format, bool translated, char **invalid_reason)
64
for (; *format != '\0';)
66
if (*format >= '0' && *format <= '9')
73
number = *format - '0';
75
while (spec.arg_count <= number)
76
spec.args_used[spec.arg_count++] = false;
77
if (spec.args_used[number])
80
xasprintf (_("Multiple references to %%%c."), *format);
83
spec.args_used[number] = true;
88
result = (struct spec *) xmalloc (sizeof (struct spec));
97
format_free (void *descr)
99
struct spec *spec = (struct spec *) descr;
105
format_get_number_of_directives (void *descr)
107
struct spec *spec = (struct spec *) descr;
109
return spec->directives;
113
format_check (const lex_pos_ty *pos, void *msgid_descr, void *msgstr_descr,
114
bool equality, bool noisy, const char *pretty_msgstr)
116
struct spec *spec1 = (struct spec *) msgid_descr;
117
struct spec *spec2 = (struct spec *) msgstr_descr;
121
for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
123
bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
124
bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
126
/* The translator cannot omit a %n from the msgstr because that would
127
yield a "Argument missing" warning at runtime. */
128
if (arg_used1 != arg_used2)
132
error_with_progname = false;
133
error_at_line (0, 0, pos->file_name, pos->line_number,
135
? _("a format specification for argument %u doesn't exist in '%s'")
136
: _("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
138
error_with_progname = true;
149
struct formatstring_parser formatstring_qt =
153
format_get_number_of_directives,
160
/* Test program: Print the argument list specification returned by
161
format_parse for strings read from standard input. */
167
format_print (void *descr)
169
struct spec *spec = (struct spec *) descr;
179
for (i = 0; i < spec->arg_count; i++)
183
if (spec->args_used[i])
197
size_t line_size = 0;
199
char *invalid_reason;
202
line_len = getline (&line, &line_size, stdin);
205
if (line_len > 0 && line[line_len - 1] == '\n')
206
line[--line_len] = '\0';
208
invalid_reason = NULL;
209
descr = format_parse (line, false, &invalid_reason);
211
format_print (descr);
214
printf ("%s\n", invalid_reason);
216
free (invalid_reason);
224
* For Emacs M-x compile
226
* compile-command: "/bin/sh ../libtool --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../lib -I../intl -DHAVE_CONFIG_H -DTEST format-qt.c ../lib/libgettextlib.la"