1 /* YCP and Smalltalk format strings.
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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 "format-invalid.h"
33 #define _(str) gettext (str)
35 /* YCP sformat strings are described in libycp documentation YCP-builtins.html.
36 A directive starts with '%' and is followed by '%' or a nonzero digit ('1'
38 GNU Smalltalk format strings are described in the CharArray documentation,
39 methods 'bindWith:' and 'bindWithArguments:'. They have the same syntax.
44 unsigned int directives
;
45 unsigned int arg_count
;
51 format_parse (const char *format
, bool translated
, char **invalid_reason
)
59 for (; *format
!= '\0';)
67 else if (*format
>= '1' && *format
<= '9')
69 unsigned int number
= *format
- '1';
71 while (spec
.arg_count
<= number
)
72 spec
.args_used
[spec
.arg_count
++] = false;
73 spec
.args_used
[number
] = true;
81 ? INVALID_UNTERMINATED_DIRECTIVE ()
82 : (c_isprint (*format
)
83 ? xasprintf (_("In the directive number %u, the character '%c' is not a digit between 1 and 9."), spec
.directives
, *format
)
84 : xasprintf (_("The character that terminates the directive number %u is not a digit between 1 and 9."), spec
.directives
)));
89 result
= (struct spec
*) xmalloc (sizeof (struct spec
));
98 format_free (void *descr
)
100 struct spec
*spec
= (struct spec
*) descr
;
106 format_get_number_of_directives (void *descr
)
108 struct spec
*spec
= (struct spec
*) descr
;
110 return spec
->directives
;
114 format_check (void *msgid_descr
, void *msgstr_descr
, bool equality
,
115 formatstring_error_logger_t error_logger
,
116 const char *pretty_msgstr
)
118 struct spec
*spec1
= (struct spec
*) msgid_descr
;
119 struct spec
*spec2
= (struct spec
*) msgstr_descr
;
123 for (i
= 0; i
< spec1
->arg_count
|| i
< spec2
->arg_count
; i
++)
125 bool arg_used1
= (i
< spec1
->arg_count
&& spec1
->args_used
[i
]);
126 bool arg_used2
= (i
< spec2
->arg_count
&& spec2
->args_used
[i
]);
128 if (equality
? (arg_used1
!= arg_used2
) : (!arg_used1
&& arg_used2
))
131 error_logger (arg_used1
132 ? _("a format specification for argument %u doesn't exist in '%s'")
133 : _("a format specification for argument %u, as in '%s', doesn't exist in 'msgid'"),
134 i
+ 1, pretty_msgstr
);
144 struct formatstring_parser formatstring_ycp
=
148 format_get_number_of_directives
,
153 struct formatstring_parser formatstring_smalltalk
=
157 format_get_number_of_directives
,
164 /* Test program: Print the argument list specification returned by
165 format_parse for strings read from standard input. */
171 format_print (void *descr
)
173 struct spec
*spec
= (struct spec
*) descr
;
183 for (i
= 0; i
< spec
->arg_count
; i
++)
187 if (spec
->args_used
[i
])
201 size_t line_size
= 0;
203 char *invalid_reason
;
206 line_len
= getline (&line
, &line_size
, stdin
);
209 if (line_len
> 0 && line
[line_len
- 1] == '\n')
210 line
[--line_len
] = '\0';
212 invalid_reason
= NULL
;
213 descr
= format_parse (line
, false, &invalid_reason
);
215 format_print (descr
);
218 printf ("%s\n", invalid_reason
);
220 free (invalid_reason
);
228 * For Emacs M-x compile
230 * 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-ycp.c ../lib/libgettextlib.la"