2 Copyright (C) 2003-2004, 2006-2007, 2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
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 /* KDE 4 format strings are processed by method
33 KLocalizedStringPrivate::substituteSimple(string,'%',false) in
34 kde4libs-3.93.0.orig/kdecore/localization/klocalizedstring.cpp .
37 - is followed by a non-zero digit and optionally more digits. All
38 the following digits are eaten up.
39 An unterminated directive ('%' not followed by a digit or at the end) is
41 %1 denotes the first argument, %2 the second argument, etc.
42 The set of used argument numbers must be of the form {1,...,n} or
43 {1,...,n} \ {m}: one of the supplied arguments may be ignored by the
44 format string. This allows the processing of singular forms (msgstr[0]).
45 Which argument may be skipped, depends on the argument types at runtime;
46 since xgettext cannot extract this info, it is considered unknown here. */
55 unsigned int directives;
56 unsigned int numbered_arg_count;
57 unsigned int allocated;
58 struct numbered_arg *numbered;
62 numbered_arg_compare (const void *p1, const void *p2)
64 /* Subtract 1, because argument number 0 can only occur through overflow. */
65 unsigned int n1 = ((const struct numbered_arg *) p1)->number - 1;
66 unsigned int n2 = ((const struct numbered_arg *) p2)->number - 1;
68 return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
72 format_parse (const char *format, bool translated, char *fdi,
73 char **invalid_reason)
75 const char *const format_start = format;
80 spec.numbered_arg_count = 0;
84 for (; *format != '\0';)
87 const char *dir_start = format - 1;
89 if (*format > '0' && *format <= '9')
94 FDI_SET (dir_start, FMTDIR_START);
97 number = *format - '0';
98 while (format[1] >= '0' && format[1] <= '9')
100 number = 10 * number + (format[1] - '0');
104 if (spec.allocated == spec.numbered_arg_count)
106 spec.allocated = 2 * spec.allocated + 1;
107 spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, spec.allocated * sizeof (struct numbered_arg));
109 spec.numbered[spec.numbered_arg_count].number = number;
110 spec.numbered_arg_count++;
112 FDI_SET (format, FMTDIR_END);
118 /* Sort the numbered argument array, and eliminate duplicates. */
119 if (spec.numbered_arg_count > 1)
123 qsort (spec.numbered, spec.numbered_arg_count,
124 sizeof (struct numbered_arg), numbered_arg_compare);
126 /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i. */
127 for (i = j = 0; i < spec.numbered_arg_count; i++)
128 if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
133 spec.numbered[j].number = spec.numbered[i].number;
136 spec.numbered_arg_count = j;
138 /* Now spec.numbered[i] >= i + 1 for i = 0,..,spec.numbered_arg_count-1
139 (since the numbered argument counts are strictly increasing, considering
142 /* Verify that the argument numbers are of the form {1,...,n} or
144 if (spec.numbered_arg_count > 0)
149 for (; i < spec.numbered_arg_count; i++)
150 if (spec.numbered[i].number > i + 1)
152 unsigned int first_gap = i + 1;
153 for (; i < spec.numbered_arg_count; i++)
154 if (spec.numbered[i].number > i + 2)
156 unsigned int second_gap = i + 2;
158 xasprintf (_("The string refers to argument number %u but ignores the arguments %u and %u."),
159 spec.numbered[i].number, first_gap, second_gap);
166 result = XMALLOC (struct spec);
171 if (spec.numbered != NULL)
172 free (spec.numbered);
177 format_free (void *descr)
179 struct spec *spec = (struct spec *) descr;
181 if (spec->numbered != NULL)
182 free (spec->numbered);
187 format_get_number_of_directives (void *descr)
189 struct spec *spec = (struct spec *) descr;
191 return spec->directives;
195 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
196 formatstring_error_logger_t error_logger,
197 const char *pretty_msgid, const char *pretty_msgstr)
199 struct spec *spec1 = (struct spec *) msgid_descr;
200 struct spec *spec2 = (struct spec *) msgstr_descr;
203 if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
206 unsigned int n1 = spec1->numbered_arg_count;
207 unsigned int n2 = spec2->numbered_arg_count;
208 unsigned int missing = 0; /* only used if !equality */
210 /* Check the argument names are the same.
211 Both arrays are sorted. We search for the first difference. */
212 for (i = 0, j = 0; i < n1 || j < n2; )
214 int cmp = (i >= n1 ? 1 :
216 spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
217 spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
223 error_logger (_("a format specification for argument %u, as in '%s', doesn't exist in '%s'"),
224 spec2->numbered[j].number, pretty_msgstr,
234 error_logger (_("a format specification for argument %u doesn't exist in '%s'"),
235 spec1->numbered[i].number, pretty_msgstr);
242 error_logger (_("a format specification for arguments %u and %u doesn't exist in '%s', only one argument may be ignored"),
243 missing, spec1->numbered[i].number,
250 missing = spec1->numbered[i].number;
263 struct formatstring_parser formatstring_kde =
267 format_get_number_of_directives,
275 /* Test program: Print the argument list specification returned by
276 format_parse for strings read from standard input. */
281 format_print (void *descr)
283 struct spec *spec = (struct spec *) descr;
295 for (i = 0; i < spec->numbered_arg_count; i++)
297 unsigned int number = spec->numbered[i].number;
303 for (; last < number; last++)
316 size_t line_size = 0;
318 char *invalid_reason;
321 line_len = getline (&line, &line_size, stdin);
324 if (line_len > 0 && line[line_len - 1] == '\n')
325 line[--line_len] = '\0';
327 invalid_reason = NULL;
328 descr = format_parse (line, false, NULL, &invalid_reason);
330 format_print (descr);
333 printf ("%s\n", invalid_reason);
335 free (invalid_reason);
343 * For Emacs M-x compile
345 * 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-kde.c ../gnulib-lib/libgettextlib.la"