1*946379e7Schristos /* Qt format strings.
2*946379e7Schristos Copyright (C) 2003-2004, 2006 Free Software Foundation, Inc.
3*946379e7Schristos Written by Bruno Haible <bruno@clisp.org>, 2003.
4*946379e7Schristos
5*946379e7Schristos This program is free software; you can redistribute it and/or modify
6*946379e7Schristos it under the terms of the GNU General Public License as published by
7*946379e7Schristos the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos any later version.
9*946379e7Schristos
10*946379e7Schristos This program is distributed in the hope that it will be useful,
11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*946379e7Schristos GNU General Public License for more details.
14*946379e7Schristos
15*946379e7Schristos You should have received a copy of the GNU General Public License
16*946379e7Schristos along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*946379e7Schristos
19*946379e7Schristos #ifdef HAVE_CONFIG_H
20*946379e7Schristos # include <config.h>
21*946379e7Schristos #endif
22*946379e7Schristos
23*946379e7Schristos #include <stdbool.h>
24*946379e7Schristos #include <stdlib.h>
25*946379e7Schristos
26*946379e7Schristos #include "format.h"
27*946379e7Schristos #include "xalloc.h"
28*946379e7Schristos #include "xvasprintf.h"
29*946379e7Schristos #include "gettext.h"
30*946379e7Schristos
31*946379e7Schristos #define _(str) gettext (str)
32*946379e7Schristos
33*946379e7Schristos /* Qt format strings are processed by QString::arg and are documented in
34*946379e7Schristos qt-3.0.5/doc/html/qstring.html.
35*946379e7Schristos A directive starts with '%' and is followed by a digit ('0' to '9').
36*946379e7Schristos Each %n must occur only once in the given string.
37*946379e7Schristos The first .arg() invocation replaces the %n with the lowest numbered n,
38*946379e7Schristos the next .arg() invocation then replaces the %n with the second-lowest
39*946379e7Schristos numbered n, and so on.
40*946379e7Schristos (This is inherently buggy because a '%' in the first replacement confuses
41*946379e7Schristos the second .arg() invocation.)
42*946379e7Schristos Although %0 is supported, usually %1 denotes the first argument, %2 the
43*946379e7Schristos second argument etc. */
44*946379e7Schristos
45*946379e7Schristos struct spec
46*946379e7Schristos {
47*946379e7Schristos unsigned int directives;
48*946379e7Schristos unsigned int arg_count;
49*946379e7Schristos bool args_used[10];
50*946379e7Schristos };
51*946379e7Schristos
52*946379e7Schristos
53*946379e7Schristos static void *
format_parse(const char * format,bool translated,char ** invalid_reason)54*946379e7Schristos format_parse (const char *format, bool translated, char **invalid_reason)
55*946379e7Schristos {
56*946379e7Schristos struct spec spec;
57*946379e7Schristos struct spec *result;
58*946379e7Schristos
59*946379e7Schristos spec.directives = 0;
60*946379e7Schristos spec.arg_count = 0;
61*946379e7Schristos
62*946379e7Schristos for (; *format != '\0';)
63*946379e7Schristos if (*format++ == '%')
64*946379e7Schristos if (*format >= '0' && *format <= '9')
65*946379e7Schristos {
66*946379e7Schristos /* A directive. */
67*946379e7Schristos unsigned int number;
68*946379e7Schristos
69*946379e7Schristos spec.directives++;
70*946379e7Schristos
71*946379e7Schristos number = *format - '0';
72*946379e7Schristos
73*946379e7Schristos while (spec.arg_count <= number)
74*946379e7Schristos spec.args_used[spec.arg_count++] = false;
75*946379e7Schristos if (spec.args_used[number])
76*946379e7Schristos {
77*946379e7Schristos *invalid_reason =
78*946379e7Schristos xasprintf (_("Multiple references to %%%c."), *format);
79*946379e7Schristos goto bad_format;
80*946379e7Schristos }
81*946379e7Schristos spec.args_used[number] = true;
82*946379e7Schristos
83*946379e7Schristos format++;
84*946379e7Schristos }
85*946379e7Schristos
86*946379e7Schristos result = (struct spec *) xmalloc (sizeof (struct spec));
87*946379e7Schristos *result = spec;
88*946379e7Schristos return result;
89*946379e7Schristos
90*946379e7Schristos bad_format:
91*946379e7Schristos return NULL;
92*946379e7Schristos }
93*946379e7Schristos
94*946379e7Schristos static void
format_free(void * descr)95*946379e7Schristos format_free (void *descr)
96*946379e7Schristos {
97*946379e7Schristos struct spec *spec = (struct spec *) descr;
98*946379e7Schristos
99*946379e7Schristos free (spec);
100*946379e7Schristos }
101*946379e7Schristos
102*946379e7Schristos static int
format_get_number_of_directives(void * descr)103*946379e7Schristos format_get_number_of_directives (void *descr)
104*946379e7Schristos {
105*946379e7Schristos struct spec *spec = (struct spec *) descr;
106*946379e7Schristos
107*946379e7Schristos return spec->directives;
108*946379e7Schristos }
109*946379e7Schristos
110*946379e7Schristos static bool
format_check(void * msgid_descr,void * msgstr_descr,bool equality,formatstring_error_logger_t error_logger,const char * pretty_msgstr)111*946379e7Schristos format_check (void *msgid_descr, void *msgstr_descr, bool equality,
112*946379e7Schristos formatstring_error_logger_t error_logger,
113*946379e7Schristos const char *pretty_msgstr)
114*946379e7Schristos {
115*946379e7Schristos struct spec *spec1 = (struct spec *) msgid_descr;
116*946379e7Schristos struct spec *spec2 = (struct spec *) msgstr_descr;
117*946379e7Schristos bool err = false;
118*946379e7Schristos unsigned int i;
119*946379e7Schristos
120*946379e7Schristos for (i = 0; i < spec1->arg_count || i < spec2->arg_count; i++)
121*946379e7Schristos {
122*946379e7Schristos bool arg_used1 = (i < spec1->arg_count && spec1->args_used[i]);
123*946379e7Schristos bool arg_used2 = (i < spec2->arg_count && spec2->args_used[i]);
124*946379e7Schristos
125*946379e7Schristos /* The translator cannot omit a %n from the msgstr because that would
126*946379e7Schristos yield a "Argument missing" warning at runtime. */
127*946379e7Schristos if (arg_used1 != arg_used2)
128*946379e7Schristos {
129*946379e7Schristos if (error_logger)
130*946379e7Schristos error_logger (arg_used1
131*946379e7Schristos ? _("a format specification for argument %u doesn't exist in '%s'")
132*946379e7Schristos : _("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
133*946379e7Schristos i, pretty_msgstr);
134*946379e7Schristos err = true;
135*946379e7Schristos break;
136*946379e7Schristos }
137*946379e7Schristos }
138*946379e7Schristos
139*946379e7Schristos return err;
140*946379e7Schristos }
141*946379e7Schristos
142*946379e7Schristos
143*946379e7Schristos struct formatstring_parser formatstring_qt =
144*946379e7Schristos {
145*946379e7Schristos format_parse,
146*946379e7Schristos format_free,
147*946379e7Schristos format_get_number_of_directives,
148*946379e7Schristos NULL,
149*946379e7Schristos format_check
150*946379e7Schristos };
151*946379e7Schristos
152*946379e7Schristos
153*946379e7Schristos #ifdef TEST
154*946379e7Schristos
155*946379e7Schristos /* Test program: Print the argument list specification returned by
156*946379e7Schristos format_parse for strings read from standard input. */
157*946379e7Schristos
158*946379e7Schristos #include <stdio.h>
159*946379e7Schristos #include "getline.h"
160*946379e7Schristos
161*946379e7Schristos static void
format_print(void * descr)162*946379e7Schristos format_print (void *descr)
163*946379e7Schristos {
164*946379e7Schristos struct spec *spec = (struct spec *) descr;
165*946379e7Schristos unsigned int i;
166*946379e7Schristos
167*946379e7Schristos if (spec == NULL)
168*946379e7Schristos {
169*946379e7Schristos printf ("INVALID");
170*946379e7Schristos return;
171*946379e7Schristos }
172*946379e7Schristos
173*946379e7Schristos printf ("(");
174*946379e7Schristos for (i = 0; i < spec->arg_count; i++)
175*946379e7Schristos {
176*946379e7Schristos if (i > 0)
177*946379e7Schristos printf (" ");
178*946379e7Schristos if (spec->args_used[i])
179*946379e7Schristos printf ("*");
180*946379e7Schristos else
181*946379e7Schristos printf ("_");
182*946379e7Schristos }
183*946379e7Schristos printf (")");
184*946379e7Schristos }
185*946379e7Schristos
186*946379e7Schristos int
main()187*946379e7Schristos main ()
188*946379e7Schristos {
189*946379e7Schristos for (;;)
190*946379e7Schristos {
191*946379e7Schristos char *line = NULL;
192*946379e7Schristos size_t line_size = 0;
193*946379e7Schristos int line_len;
194*946379e7Schristos char *invalid_reason;
195*946379e7Schristos void *descr;
196*946379e7Schristos
197*946379e7Schristos line_len = getline (&line, &line_size, stdin);
198*946379e7Schristos if (line_len < 0)
199*946379e7Schristos break;
200*946379e7Schristos if (line_len > 0 && line[line_len - 1] == '\n')
201*946379e7Schristos line[--line_len] = '\0';
202*946379e7Schristos
203*946379e7Schristos invalid_reason = NULL;
204*946379e7Schristos descr = format_parse (line, false, &invalid_reason);
205*946379e7Schristos
206*946379e7Schristos format_print (descr);
207*946379e7Schristos printf ("\n");
208*946379e7Schristos if (descr == NULL)
209*946379e7Schristos printf ("%s\n", invalid_reason);
210*946379e7Schristos
211*946379e7Schristos free (invalid_reason);
212*946379e7Schristos free (line);
213*946379e7Schristos }
214*946379e7Schristos
215*946379e7Schristos return 0;
216*946379e7Schristos }
217*946379e7Schristos
218*946379e7Schristos /*
219*946379e7Schristos * For Emacs M-x compile
220*946379e7Schristos * Local Variables:
221*946379e7Schristos * 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"
222*946379e7Schristos * End:
223*946379e7Schristos */
224*946379e7Schristos
225*946379e7Schristos #endif /* TEST */
226