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