]> sjero.net Git - wget/commitdiff
[svn] Rewrite with_thousand_seps to be size-agnostic. Remove printing of separators
authorhniksic <devnull@localhost>
Sat, 25 Jun 2005 23:09:29 +0000 (16:09 -0700)
committerhniksic <devnull@localhost>
Sat, 25 Jun 2005 23:09:29 +0000 (16:09 -0700)
from sizes.

src/ChangeLog
src/ftp-ls.c
src/ftp.c
src/http.c
src/main.c
src/progress.c
src/utils.c
src/utils.h

index 97773421470e62a7e707570d0209c214c1245b76..f0c7ebfe2e60448368923297c5aabbb886eb0870 100644 (file)
@@ -1,3 +1,15 @@
+2005-06-26  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * main.c (main): Print the downloaded and quota amounts with the
+       "human_readable" function.
+
+       * ftp.c (print_length): Ditto.
+
+       * http.c (gethttp): Don't display thousand separators.
+
+       * utils.c (with_thousand_seps): Rewritten to respect locale
+       settings and to be type size agnostic.
+
 2005-06-25  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * utils.c (human_readable): Divide with 1024 instead of shifting
 2005-06-25  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * utils.c (human_readable): Divide with 1024 instead of shifting
index 6fa16fa6de88c09c4d466b008fba1dba5e3965b0..7e448295e64f82d9bb5631185d2b0afe26a0c459 100644 (file)
@@ -936,7 +936,7 @@ ftp_index (const char *file, struct url *u, struct fileinfo *f)
        putc ('/', fp);
       fprintf (fp, "</a> ");
       if (f->type == FT_PLAINFILE)
        putc ('/', fp);
       fprintf (fp, "</a> ");
       if (f->type == FT_PLAINFILE)
-       fprintf (fp, _(" (%s bytes)"), with_thousand_seps (f->size));
+       fprintf (fp, _(" (%s bytes)"), number_to_static_string (f->size));
       else if (f->type == FT_SYMLINK)
        fprintf (fp, "-> %s", f->linkto ? f->linkto : "(nil)");
       putc ('\n', fp);
       else if (f->type == FT_SYMLINK)
        fprintf (fp, "-> %s", f->linkto ? f->linkto : "(nil)");
       putc ('\n', fp);
index b32d329e90dc2f7f74fa6598ef8e75c3d723c67b..de9ef06ca8d1be8b4d9cf5f171ccf43e8e720aea 100644 (file)
--- a/src/ftp.c
+++ b/src/ftp.c
@@ -214,18 +214,18 @@ ftp_do_port (int csock, int *local_sock)
 static void
 print_length (wgint size, wgint start, int authoritative)
 {
 static void
 print_length (wgint size, wgint start, int authoritative)
 {
-  logprintf (LOG_VERBOSE, _("Length: %s"), with_thousand_seps (size));
+  logprintf (LOG_VERBOSE, _("Length: %s"), number_to_static_string (size));
   if (size >= 1024)
     logprintf (LOG_VERBOSE, " (%s)", human_readable (size));
   if (start > 0)
     {
       if (start >= 1024)
        logprintf (LOG_VERBOSE, _(", %s (%s) remaining"),
   if (size >= 1024)
     logprintf (LOG_VERBOSE, " (%s)", human_readable (size));
   if (start > 0)
     {
       if (start >= 1024)
        logprintf (LOG_VERBOSE, _(", %s (%s) remaining"),
-                  with_thousand_seps (size - start),
+                  number_to_static_string (size - start),
                   human_readable (size - start));
       else
        logprintf (LOG_VERBOSE, _(", %s remaining"),
                   human_readable (size - start));
       else
        logprintf (LOG_VERBOSE, _(", %s remaining"),
-                  with_thousand_seps (size - start));
+                  number_to_static_string (size - start));
     }
   logputs (LOG_VERBOSE, !authoritative ? _(" (unauthoritative)\n") : "\n");
 }
     }
   logputs (LOG_VERBOSE, !authoritative ? _(" (unauthoritative)\n") : "\n");
 }
index 295475d4c94285ae78b11aa6c4fcf82335d9c3aa..30207e6c374a1176b8d51453143df3d186fab10b 100644 (file)
@@ -1838,7 +1838,7 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
          logputs (LOG_VERBOSE, _("Length: "));
          if (contlen != -1)
            {
          logputs (LOG_VERBOSE, _("Length: "));
          if (contlen != -1)
            {
-             logputs (LOG_VERBOSE, with_thousand_seps (contlen + contrange));
+             logputs (LOG_VERBOSE, number_to_static_string (contlen + contrange));
              if (contlen + contrange >= 1024)
                logprintf (LOG_VERBOSE, " (%s)",
                           human_readable (contlen + contrange));
              if (contlen + contrange >= 1024)
                logprintf (LOG_VERBOSE, " (%s)",
                           human_readable (contlen + contrange));
@@ -1846,11 +1846,11 @@ gethttp (struct url *u, struct http_stat *hs, int *dt, struct url *proxy)
                {
                  if (contlen >= 1024)
                    logprintf (LOG_VERBOSE, _(", %s (%s) remaining"),
                {
                  if (contlen >= 1024)
                    logprintf (LOG_VERBOSE, _(", %s (%s) remaining"),
-                              with_thousand_seps (contlen),
+                              number_to_static_string (contlen),
                               human_readable (contlen));
                  else
                    logprintf (LOG_VERBOSE, _(", %s remaining"),
                               human_readable (contlen));
                  else
                    logprintf (LOG_VERBOSE, _(", %s remaining"),
-                              with_thousand_seps (contlen));
+                              number_to_static_string (contlen));
                }
            }
          else
                }
            }
          else
index a70812333e2be7881bec7a505c36bc0e5c8df29a..797472cd3008096d48f9f9018aee4f407fcdaefb 100644 (file)
@@ -960,15 +960,15 @@ Can't timestamp and not clobber old files at the same time.\n"));
       || (opt.input_filename && total_downloaded_bytes != 0))
     {
       logprintf (LOG_NOTQUIET,
       || (opt.input_filename && total_downloaded_bytes != 0))
     {
       logprintf (LOG_NOTQUIET,
-                _("\nFINISHED --%s--\nDownloaded: %s bytes in %d files\n"),
+                _("\nFINISHED --%s--\nDownloaded: %s in %d files\n"),
                 time_str (NULL),
                 time_str (NULL),
-                with_thousand_seps_sum (total_downloaded_bytes),
+                human_readable (total_downloaded_bytes),
                 opt.numurls);
       /* Print quota warning, if exceeded.  */
       if (opt.quota && total_downloaded_bytes > opt.quota)
        logprintf (LOG_NOTQUIET,
                 opt.numurls);
       /* Print quota warning, if exceeded.  */
       if (opt.quota && total_downloaded_bytes > opt.quota)
        logprintf (LOG_NOTQUIET,
-                  _("Download quota (%s bytes) EXCEEDED!\n"),
-                  with_thousand_seps_sum (opt.quota));
+                  _("Download quota of %s EXCEEDED!\n"),
+                  human_readable (opt.quota));
     }
 
   if (opt.cookies_output)
     }
 
   if (opt.cookies_output)
index 137794d6cb4c3047dc6933f210390b5f517cd154..524aaf4e3bcc1410f7ca3831cb4bdbed7d1e66ce 100644 (file)
@@ -717,8 +717,8 @@ create_image (struct bar_progress *bp, double dl_total_time)
   char *p = bp->buffer;
   wgint size = bp->initial_length + bp->count;
 
   char *p = bp->buffer;
   wgint size = bp->initial_length + bp->count;
 
-  char *size_legible = with_thousand_seps (size);
-  int size_legible_len = strlen (size_legible);
+  const char *size_grouped = with_thousand_seps (size);
+  int size_grouped_len = strlen (size_grouped);
 
   struct bar_progress_hist *hist = &bp->hist;
 
 
   struct bar_progress_hist *hist = &bp->hist;
 
@@ -740,7 +740,7 @@ create_image (struct bar_progress *bp, double dl_total_time)
 
      "=====>..."       - progress bar             - the rest
   */
 
      "=====>..."       - progress bar             - the rest
   */
-  int dlbytes_size = 1 + MAX (size_legible_len, 11);
+  int dlbytes_size = 1 + MAX (size_grouped_len, 11);
   int progress_size = bp->width - (4 + 2 + dlbytes_size + 11 + 13);
 
   if (progress_size < 5)
   int progress_size = bp->width - (4 + 2 + dlbytes_size + 11 + 13);
 
   if (progress_size < 5)
@@ -823,7 +823,7 @@ create_image (struct bar_progress *bp, double dl_total_time)
     }
 
   /* " 234,567,890" */
     }
 
   /* " 234,567,890" */
-  sprintf (p, " %-11s", with_thousand_seps (size));
+  sprintf (p, " %-11s", size_grouped);
   p += strlen (p);
 
   /* " 1012.45K/s" */
   p += strlen (p);
 
   /* " 1012.45K/s" */
index 9eea3a4e1d66429d5c13f99b7c70fc5ddfc5e694..31598348103685b4de50f4b70c5924cb2f8bdbcf 100644 (file)
@@ -58,6 +58,9 @@ so, delete this exception statement from your version.  */
 #include <fcntl.h>
 #include <assert.h>
 #include <stdarg.h>
 #include <fcntl.h>
 #include <assert.h>
 #include <stdarg.h>
+#ifdef HAVE_LOCALE_H
+# include <locale.h>
+#endif
 
 /* For TIOCGWINSZ and friends: */
 #ifdef HAVE_SYS_IOCTL_H
 
 /* For TIOCGWINSZ and friends: */
 #ifdef HAVE_SYS_IOCTL_H
@@ -1161,71 +1164,67 @@ free_keys_and_values (struct hash_table *ht)
 }
 
 \f
 }
 
 \f
-/* Add thousand separators to a number already in string form.  Used
-   by with_thousand_seps and with_thousand_seps_sum.  */
+/* Return a printed representation of N with thousand separators.
+   This should respect locale settings, with the exception of the "C"
+   locale which mandates no separator, but we use one anyway.
 
 
-static char *
-add_thousand_seps (const char *repr)
+   Unfortunately, we cannot use %'d (in fact it would be %'j) to get
+   the separators because it's too non-portable, and it's hard to test
+   for this feature at configure time.  Besides, it wouldn't work in
+   the "C" locale, which many Unix users still work in.  */
+
+const char *
+with_thousand_seps (wgint n)
 {
   static char outbuf[48];
 {
   static char outbuf[48];
-  int i, i1, mod;
-  char *outptr;
-  const char *inptr;
 
 
-  /* Reset the pointers.  */
-  outptr = outbuf;
-  inptr = repr;
+  static char loc_sepchar;
+  static const char *loc_grouping;
 
 
-  /* Ignore the sign for the purpose of adding thousand
-     separators.  */
-  if (*inptr == '-')
-    {
-      *outptr++ = '-';
-      ++inptr;
-    }
-  /* How many digits before the first separator?  */
-  mod = strlen (inptr) % 3;
-  /* Insert them.  */
-  for (i = 0; i < mod; i++)
-    *outptr++ = inptr[i];
-  /* Now insert the rest of them, putting separator before every
-     third digit.  */
-  for (i1 = i, i = 0; inptr[i1]; i++, i1++)
+  int i = 0, groupsize;
+  char *p;
+  const char *atgroup;
+
+  if (!loc_sepchar)
     {
     {
-      if (i % 3 == 0 && i1 != 0)
-       *outptr++ = ',';
-      *outptr++ = inptr[i1];
+#ifdef LC_NUMERIC
+      /* Get the grouping character from the locale. */
+      struct lconv *lconv;
+      const char *oldlocale = setlocale (LC_NUMERIC, "");
+      lconv = localeconv ();
+      loc_sepchar = *lconv->thousands_sep;
+      loc_grouping = xstrdup (lconv->grouping);
+      /* Restore the C locale semantics of printing and reading numbers */
+      setlocale (LC_NUMERIC, oldlocale);
+      if (!loc_sepchar)
+#endif
+       /* defaults for C locale or no locale */
+       loc_sepchar = ',', loc_grouping = "\x03";
     }
     }
-  /* Zero-terminate the string.  */
-  *outptr = '\0';
-  return outbuf;
-}
-
-/* Return a static pointer to the number printed with thousand
-   separators inserted at the right places.  */
-
-char *
-with_thousand_seps (wgint l)
-{
-  char inbuf[24];
-  /* Print the number into the buffer.  */
-  number_to_string (inbuf, l);
-  return add_thousand_seps (inbuf);
-}
+  atgroup = loc_grouping;
 
 
-/* When SUM_SIZE_INT is wgint, with_thousand_seps_large is #defined to
-   with_thousand_seps.  The function below is used on non-LFS systems
-   where SUM_SIZE_INT typedeffed to double.  */
+  p = outbuf + sizeof outbuf;
+  *--p = '\0';
+  groupsize = *atgroup++;
 
 
-#ifndef with_thousand_seps_sum
-char *
-with_thousand_seps_sum (SUM_SIZE_INT l)
-{
-  char inbuf[32];
-  snprintf (inbuf, sizeof (inbuf), "%.0f", l);
-  return add_thousand_seps (inbuf);
+  while (1)
+    {
+      *--p = n % 10 + '0';
+      n /= 10;
+      if (n == 0)
+       break;
+      /* Insert the separator on every groupsize'd digit, and get the
+        new groupsize.  */
+      if (++i == groupsize)
+       {
+         *--p = loc_sepchar;
+         i = 0;
+         if (*atgroup)
+           groupsize = *atgroup++;
+       }
+    }
+  return p;
 }
 }
-#endif /* not with_thousand_seps_sum */
 
 /* N, a byte quantity, is converted to a human-readable abberviated
    form a la sizes printed by `ls -lh'.  The result is written to a
 
 /* N, a byte quantity, is converted to a human-readable abberviated
    form a la sizes printed by `ls -lh'.  The result is written to a
@@ -1245,7 +1244,7 @@ with_thousand_seps_sum (SUM_SIZE_INT l)
    discusses this in some detail.  */
 
 char *
    discusses this in some detail.  */
 
 char *
-human_readable (wgint n)
+human_readable (HR_NUMTYPE n)
 {
   /* These suffixes are compatible with those of GNU `ls -lh'. */
   static char powers[] =
 {
   /* These suffixes are compatible with those of GNU `ls -lh'. */
   static char powers[] =
index 043924d4464251fab4a7f24452fcc3f4ad45e853..007c05f11b2d86d60cf4ceb7b9086fc6dddfb321 100644 (file)
@@ -94,11 +94,19 @@ void string_set_to_array (struct hash_table *, char **);
 void string_set_free (struct hash_table *);
 void free_keys_and_values (struct hash_table *);
 
 void string_set_free (struct hash_table *);
 void free_keys_and_values (struct hash_table *);
 
-char *with_thousand_seps (wgint);
-#ifndef with_thousand_seps_sum
-char *with_thousand_seps_sum (SUM_SIZE_INT);
+const char *with_thousand_seps (wgint);
+
+/* human_readable must be able to accept wgint and SUM_SIZE_INT
+   arguments.  On machines where wgint is 32-bit, declare it to accept
+   double.  */
+#if SIZEOF_WGINT >= 8
+# define HR_NUMTYPE wgint
+#else
+# define HR_NUMTYPE double
 #endif
 #endif
-char *human_readable (wgint);
+char *human_readable (HR_NUMTYPE);
+
+
 int numdigit (wgint);
 char *number_to_string (char *, wgint);
 char *number_to_static_string (wgint);
 int numdigit (wgint);
 char *number_to_string (char *, wgint);
 char *number_to_static_string (wgint);