1*63eb84d1Schristos /* C# 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 "c-ctype.h"
28*63eb84d1Schristos #include "xalloc.h"
29*63eb84d1Schristos #include "xvasprintf.h"
30*63eb84d1Schristos #include "gettext.h"
31*63eb84d1Schristos 
32*63eb84d1Schristos #define _(str) gettext (str)
33*63eb84d1Schristos 
34*63eb84d1Schristos /* C# format strings are described in the description of the .NET System.String
35*63eb84d1Schristos    class and implemented in
36*63eb84d1Schristos      pnetlib-0.5.6/runtime/System/String.cs
37*63eb84d1Schristos    and
38*63eb84d1Schristos      mcs-0.28/class/corlib/System/String.cs
39*63eb84d1Schristos    A format string consists of literal text (that is output verbatim), doubled
40*63eb84d1Schristos    braces ('{{' and '}}', that lead to a single brace when output), and
41*63eb84d1Schristos    directives.
42*63eb84d1Schristos    A directive
43*63eb84d1Schristos    - starts with '{',
44*63eb84d1Schristos    - is followed by a nonnegative integer m,
45*63eb84d1Schristos    - is optionally followed by ',' and an integer denoting a width,
46*63eb84d1Schristos    - is optionally followed by ':' and a sequence of format specifiers.
47*63eb84d1Schristos      (But the interpretation of the format specifiers is up to the IFormattable
48*63eb84d1Schristos      implementation, depending on the argument's runtime value. New classes
49*63eb84d1Schristos      implementing IFormattable can be defined by the user.)
50*63eb84d1Schristos    - is finished with '}'.
51*63eb84d1Schristos  */
52*63eb84d1Schristos 
53*63eb84d1Schristos struct spec
54*63eb84d1Schristos {
55*63eb84d1Schristos   unsigned int directives;
56*63eb84d1Schristos   unsigned int numbered_arg_count;
57*63eb84d1Schristos };
58*63eb84d1Schristos 
59*63eb84d1Schristos static void *
format_parse(const char * format,bool translated,char ** invalid_reason)60*63eb84d1Schristos format_parse (const char *format, bool translated, char **invalid_reason)
61*63eb84d1Schristos {
62*63eb84d1Schristos   struct spec spec;
63*63eb84d1Schristos   struct spec *result;
64*63eb84d1Schristos 
65*63eb84d1Schristos   spec.directives = 0;
66*63eb84d1Schristos   spec.numbered_arg_count = 0;
67*63eb84d1Schristos 
68*63eb84d1Schristos   for (; *format != '\0';)
69*63eb84d1Schristos     {
70*63eb84d1Schristos       char c = *format++;
71*63eb84d1Schristos 
72*63eb84d1Schristos       if (c == '{')
73*63eb84d1Schristos 	{
74*63eb84d1Schristos 	  if (*format == '{')
75*63eb84d1Schristos 	    format++;
76*63eb84d1Schristos 	  else
77*63eb84d1Schristos 	    {
78*63eb84d1Schristos 	      /* A directive.  */
79*63eb84d1Schristos 	      unsigned int number;
80*63eb84d1Schristos 
81*63eb84d1Schristos 	      spec.directives++;
82*63eb84d1Schristos 
83*63eb84d1Schristos 	      if (!c_isdigit (*format))
84*63eb84d1Schristos 		{
85*63eb84d1Schristos 		  *invalid_reason =
86*63eb84d1Schristos 		    xasprintf (_("In the directive number %u, '{' is not followed by an argument number."), spec.directives);
87*63eb84d1Schristos 		  return NULL;
88*63eb84d1Schristos 		}
89*63eb84d1Schristos 	      number = 0;
90*63eb84d1Schristos 	      do
91*63eb84d1Schristos 		{
92*63eb84d1Schristos 		  number = 10 * number + (*format - '0');
93*63eb84d1Schristos 		  format++;
94*63eb84d1Schristos 		}
95*63eb84d1Schristos 	      while (c_isdigit (*format));
96*63eb84d1Schristos 
97*63eb84d1Schristos 	      if (*format == ',')
98*63eb84d1Schristos 		{
99*63eb84d1Schristos 		  /* Parse width.  */
100*63eb84d1Schristos 		  format++;
101*63eb84d1Schristos 		  if (*format == '-')
102*63eb84d1Schristos 		    format++;
103*63eb84d1Schristos 		  if (!c_isdigit (*format))
104*63eb84d1Schristos 		    {
105*63eb84d1Schristos 		      *invalid_reason =
106*63eb84d1Schristos 			xasprintf (_("In the directive number %u, ',' is not followed by a number."), spec.directives);
107*63eb84d1Schristos 		      return NULL;
108*63eb84d1Schristos 		    }
109*63eb84d1Schristos 		  do
110*63eb84d1Schristos 		    format++;
111*63eb84d1Schristos 		  while (c_isdigit (*format));
112*63eb84d1Schristos 		}
113*63eb84d1Schristos 
114*63eb84d1Schristos 	      if (*format == ':')
115*63eb84d1Schristos 		{
116*63eb84d1Schristos 		  /* Parse format specifiers.  */
117*63eb84d1Schristos 		  do
118*63eb84d1Schristos 		    format++;
119*63eb84d1Schristos 		  while (*format != '\0' && *format != '}');
120*63eb84d1Schristos 		}
121*63eb84d1Schristos 
122*63eb84d1Schristos 	      if (*format == '\0')
123*63eb84d1Schristos 		{
124*63eb84d1Schristos 		  *invalid_reason =
125*63eb84d1Schristos 		    xstrdup (_("The string ends in the middle of a directive: found '{' without matching '}'."));
126*63eb84d1Schristos 		  return NULL;
127*63eb84d1Schristos 		}
128*63eb84d1Schristos 
129*63eb84d1Schristos 	      if (*format != '}')
130*63eb84d1Schristos 		{
131*63eb84d1Schristos 		  *invalid_reason =
132*63eb84d1Schristos 		    (c_isprint (*format)
133*63eb84d1Schristos 		     ? xasprintf (_("The directive number %u ends with an invalid character '%c' instead of '}'."), spec.directives, *format)
134*63eb84d1Schristos 		     : xasprintf (_("The directive number %u ends with an invalid character instead of '}'."), spec.directives));
135*63eb84d1Schristos 		  return NULL;
136*63eb84d1Schristos 		}
137*63eb84d1Schristos 
138*63eb84d1Schristos 	      format++;
139*63eb84d1Schristos 
140*63eb84d1Schristos 	      if (spec.numbered_arg_count <= number)
141*63eb84d1Schristos 		spec.numbered_arg_count = number + 1;
142*63eb84d1Schristos 	    }
143*63eb84d1Schristos 	}
144*63eb84d1Schristos       else if (c == '}')
145*63eb84d1Schristos 	{
146*63eb84d1Schristos 	  if (*format == '}')
147*63eb84d1Schristos 	    format++;
148*63eb84d1Schristos 	  else
149*63eb84d1Schristos 	    {
150*63eb84d1Schristos 	      *invalid_reason =
151*63eb84d1Schristos 		(spec.directives == 0
152*63eb84d1Schristos 		 ? xstrdup (_("The string starts in the middle of a directive: found '}' without matching '{'."))
153*63eb84d1Schristos 		 : xasprintf (_("The string contains a lone '}' after directive number %u."), spec.directives));
154*63eb84d1Schristos 	      return NULL;
155*63eb84d1Schristos 	    }
156*63eb84d1Schristos 	}
157*63eb84d1Schristos     }
158*63eb84d1Schristos 
159*63eb84d1Schristos   result = (struct spec *) xmalloc (sizeof (struct spec));
160*63eb84d1Schristos   *result = spec;
161*63eb84d1Schristos   return result;
162*63eb84d1Schristos }
163*63eb84d1Schristos 
164*63eb84d1Schristos static void
format_free(void * descr)165*63eb84d1Schristos format_free (void *descr)
166*63eb84d1Schristos {
167*63eb84d1Schristos   struct spec *spec = (struct spec *) descr;
168*63eb84d1Schristos 
169*63eb84d1Schristos   free (spec);
170*63eb84d1Schristos }
171*63eb84d1Schristos 
172*63eb84d1Schristos static int
format_get_number_of_directives(void * descr)173*63eb84d1Schristos format_get_number_of_directives (void *descr)
174*63eb84d1Schristos {
175*63eb84d1Schristos   struct spec *spec = (struct spec *) descr;
176*63eb84d1Schristos 
177*63eb84d1Schristos   return spec->directives;
178*63eb84d1Schristos }
179*63eb84d1Schristos 
180*63eb84d1Schristos static bool
format_check(void * msgid_descr,void * msgstr_descr,bool equality,formatstring_error_logger_t error_logger,const char * pretty_msgstr)181*63eb84d1Schristos format_check (void *msgid_descr, void *msgstr_descr, bool equality,
182*63eb84d1Schristos 	      formatstring_error_logger_t error_logger,
183*63eb84d1Schristos 	      const char *pretty_msgstr)
184*63eb84d1Schristos {
185*63eb84d1Schristos   struct spec *spec1 = (struct spec *) msgid_descr;
186*63eb84d1Schristos   struct spec *spec2 = (struct spec *) msgstr_descr;
187*63eb84d1Schristos   bool err = false;
188*63eb84d1Schristos 
189*63eb84d1Schristos   /* Check that the argument counts are the same.  */
190*63eb84d1Schristos   if (equality
191*63eb84d1Schristos       ? spec1->numbered_arg_count != spec2->numbered_arg_count
192*63eb84d1Schristos       : spec1->numbered_arg_count < spec2->numbered_arg_count)
193*63eb84d1Schristos     {
194*63eb84d1Schristos       if (error_logger)
195*63eb84d1Schristos 	error_logger (_("number of format specifications in 'msgid' and '%s' does not match"),
196*63eb84d1Schristos 		      pretty_msgstr);
197*63eb84d1Schristos       err = true;
198*63eb84d1Schristos     }
199*63eb84d1Schristos 
200*63eb84d1Schristos   return err;
201*63eb84d1Schristos }
202*63eb84d1Schristos 
203*63eb84d1Schristos 
204*63eb84d1Schristos struct formatstring_parser formatstring_csharp =
205*63eb84d1Schristos {
206*63eb84d1Schristos   format_parse,
207*63eb84d1Schristos   format_free,
208*63eb84d1Schristos   format_get_number_of_directives,
209*63eb84d1Schristos   NULL,
210*63eb84d1Schristos   format_check
211*63eb84d1Schristos };
212*63eb84d1Schristos 
213*63eb84d1Schristos 
214*63eb84d1Schristos #ifdef TEST
215*63eb84d1Schristos 
216*63eb84d1Schristos /* Test program: Print the argument list specification returned by
217*63eb84d1Schristos    format_parse for strings read from standard input.  */
218*63eb84d1Schristos 
219*63eb84d1Schristos #include <stdio.h>
220*63eb84d1Schristos #include "getline.h"
221*63eb84d1Schristos 
222*63eb84d1Schristos static void
format_print(void * descr)223*63eb84d1Schristos format_print (void *descr)
224*63eb84d1Schristos {
225*63eb84d1Schristos   struct spec *spec = (struct spec *) descr;
226*63eb84d1Schristos   unsigned int i;
227*63eb84d1Schristos 
228*63eb84d1Schristos   if (spec == NULL)
229*63eb84d1Schristos     {
230*63eb84d1Schristos       printf ("INVALID");
231*63eb84d1Schristos       return;
232*63eb84d1Schristos     }
233*63eb84d1Schristos 
234*63eb84d1Schristos   printf ("(");
235*63eb84d1Schristos   for (i = 0; i < spec->numbered_arg_count; i++)
236*63eb84d1Schristos     {
237*63eb84d1Schristos       if (i > 0)
238*63eb84d1Schristos 	printf (" ");
239*63eb84d1Schristos       printf ("*");
240*63eb84d1Schristos     }
241*63eb84d1Schristos   printf (")");
242*63eb84d1Schristos }
243*63eb84d1Schristos 
244*63eb84d1Schristos int
main()245*63eb84d1Schristos main ()
246*63eb84d1Schristos {
247*63eb84d1Schristos   for (;;)
248*63eb84d1Schristos     {
249*63eb84d1Schristos       char *line = NULL;
250*63eb84d1Schristos       size_t line_size = 0;
251*63eb84d1Schristos       int line_len;
252*63eb84d1Schristos       char *invalid_reason;
253*63eb84d1Schristos       void *descr;
254*63eb84d1Schristos 
255*63eb84d1Schristos       line_len = getline (&line, &line_size, stdin);
256*63eb84d1Schristos       if (line_len < 0)
257*63eb84d1Schristos 	break;
258*63eb84d1Schristos       if (line_len > 0 && line[line_len - 1] == '\n')
259*63eb84d1Schristos 	line[--line_len] = '\0';
260*63eb84d1Schristos 
261*63eb84d1Schristos       invalid_reason = NULL;
262*63eb84d1Schristos       descr = format_parse (line, false, &invalid_reason);
263*63eb84d1Schristos 
264*63eb84d1Schristos       format_print (descr);
265*63eb84d1Schristos       printf ("\n");
266*63eb84d1Schristos       if (descr == NULL)
267*63eb84d1Schristos 	printf ("%s\n", invalid_reason);
268*63eb84d1Schristos 
269*63eb84d1Schristos       free (invalid_reason);
270*63eb84d1Schristos       free (line);
271*63eb84d1Schristos     }
272*63eb84d1Schristos 
273*63eb84d1Schristos   return 0;
274*63eb84d1Schristos }
275*63eb84d1Schristos 
276*63eb84d1Schristos /*
277*63eb84d1Schristos  * For Emacs M-x compile
278*63eb84d1Schristos  * Local Variables:
279*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-csharp.c ../lib/libgettextlib.la"
280*63eb84d1Schristos  * End:
281*63eb84d1Schristos  */
282*63eb84d1Schristos 
283*63eb84d1Schristos #endif /* TEST */
284