[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] how to print size_t in LGPLv2+ program

On 08/17/2010 04:33 PM, Bruno Haible wrote:
>> What a shame that POSIX omitted an <inttypes.h> PRIu* for size_t.
> You can define it by yourself: Basically you define
>   #if @BITSIZEOF_SIZE_T@ == 32
>   # define PRIuSIZE PRIu32
>   #endif
>   #if @BITSIZEOF_SIZE_T@ == 64
>   # define PRIuSIZE PRIu64
>   #endif
> Note that this will not work inside gettext() arguments, though, because
> PRIuSIZE is not standard. For internationalized messages, you will need
> the workaround described in the second half of
> <http://www.gnu.org/software/gettext/manual/html_node/Preparing-Strings.html>.

You mean like this?

  char buf1[100];
  sprintf (buf1, "%0" MYPRId64, number);
  printf (gettext ("The amount is %s\n"), buf1);

No thanks - like inttostr() it requires a temporary buffer, but unlike
inttostr(), it ends up being a slower three-liner due to the full
overhead of a second printf() call.  If gettext can't support it, then
the shortest approach is the two-liner:

  char buf1[INT_BUFSIZE_BOUND (size_t)];
  printf (_("The amount is %s\n"), sizetomax (val, buf1));

Here's where a cross-project change to GNU Coding Standards could be
helpful - if we all agree that gnulib should add the macro and gettext
should add the support for it at the same time, then it would be much
easier for all remaining GNU projects to take advantage of a
standardized name (that is, use GCS rather than POSIX as our standard
that documents the portable use of PRIuSIZE among GNU projects).
However, it does take us back to the minimum build tool requirement
issue - any project relying on that proposed feature would necessarily
have to drag in whatever new version of gettext that added the support
for it.

>> it still requires auditing code
>> and forbidding "%zu" in favor of "%"PRIuSIZE (or whatever other name we
>> settle on).
> You could hack GCC, clang, or even xgettext to produce a warning when it
> sees a 'z' size modifier in a format string.

It sounds like xgettext might be the best place to detect %zu,
particularly if it already diagnoses existing uses of PRIuMAX in
projects that forgot to specify need-formatstring-macros in
AM_GNU_GETTEXT (and if it doesn't already diagnose translated strings
that require more features than the settings requested in configure.ac,
it probably should).  But there's also maint.mk (or cfg.mk) that has
heuristics for detecting untranslated strings, which seems like it could
be pretty easily modified to also check for translated strings
containing %zu.

Eric Blake   eblake redhat com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]