On Tue, 9 Nov 2004 18:14:51 +0100
Christian Biere <[EMAIL PROTECTED]> wrote:

> I'm afraid that ngettext() is no option. We'd lose the ability to
> check format strings at compile-time

I might reach your point, so I'll post it.

* As a preface

  from http://developer.gnome.org/doc/API/2.0/glib/glib-Miscellaneous-
  Macros.html#G-GNUC-PRINTF:CAPS:

    #define G_GNUC_PRINTF(format_idx, arg_idx)

    gint g_snprintf (gchar  *string,
                     gulong       n,
                     gchar const *format,
                     ...) G_GNUC_PRINTF (3, 4);

    format_idx: the index of the argument corresponding to the format
                string. (The arguments are numbered from 1).
    arg_idx:    the index of the first of the format arguments.

  in src/lib/glib-missing.h at 55-56:

    size_t gm_snprintf(gchar *str, size_t n,
        gchar const *fmt, ...) G_GNUC_PRINTF (3, 4);

  in src/ui/gtk/settings.c (modified) at 1087-1102:

    static void set_host_progress(const gchar *w, guint32 cur, guint32 max)
    {
       GtkProgressBar *pg = GTK_PROGRESS_BAR(lookup_widget(main_window, w));
       guint frac;
    
       frac = MIN(cur, max);
       if (frac != 0)
           frac = frac * 100 / max;
  
       gm_snprintf(set_tmp, sizeof(set_tmp),
             ngettext ("%u/%u host (%u%%)", "%u/%u hosts (%u%%)", max),
             cur, max, frac);
    
       gtk_progress_bar_set_text(pg, set_tmp);
       gtk_progress_bar_set_fraction(pg, frac / 100.0);
    }

  from the above mentioned, if -Wformat=2 was used it's a

    "ngettext ("%u/%u host (%u%%)", "%u/%u hosts (%u%%)", max)" and
    "cur, max, frac"

  that will be checked at compile-time, isn't it? Also, I inserted
  the cpp macro in the src/commpn.h at 294-295:

    #  define ngettext(Single, Plural, Number) \
         ((Number) == 1 ? (Single) : (Plural))

* Compilation

    $ gmake

    (snip)

    gcc -c (snip) -O2 -W -Wall -Wformat=2 -g settings.c
    settings.c: In function `set_host_progress':
    settings.c:1098: warning: format not a string literal, argument
    types not checked

  yes as you expected, warning has occurred. By the way when I disabled
  NLS, compile passes without warning.

* Results and Problem

  As an example, the return value of above mentioned in LC_ALL=C
  environment. If,

    max != 1, "0 hosts", "2 hosts"... will be appeared.
    max == 1, "1 host" will be appeared.

  Note that for English user, it's orthogonal whether NLS is enabled,
  however if you don't have both of C library with ngettext() and
  libintl with that function, it might be needed intl directory
  overhauled and modifying Configure script.

  An attached patch modifies src/common.h, src/ui/gtk/settings.c,
  even though it seems work well in my environment, there is a warning
  at compile time of course:

  warning: format not a string literal, argument types not checked

  if there is a switch that in-line develops the macro string!? and
  checks it, warning won't be emitted, I don't know what I say though...

> and we'd have to "translate" English messages for the plural form.
> gtk-gnutella should be compilable without NLS.

In my opinion, the language English has only singular and plural forms
per one noun (possibly), if those words were used in the source code at
once, there is nothing to do anymore.

Thank you.
-- 
Daichi

Attachment: ngettext.diff.gz
Description: application/gunzip

Reply via email to