Simplify LARGE_INT handling and rename the type

Hrvoje Niksic <[email protected]> Sat, 25 Jun 2005 16:39:34 +0200
Newsgroups gmane.comp.web.wget.patches
Message-ID <[email protected]>
The LARGE_INT type confuses users who look at the source code because
they think it's an LFS type for file sizes (which would be wgint).
LARGE_INT is only used for sums of downloads, such as the argument to
--quota, and as such must legitimately be larger than 32-bit even on
non-LFS platforms.

This patch renames it to SUM_SIZE_INT to better reflect its purpose,
and simplifies its use by either having it alias wgint or double.


2005-06-25  Hrvoje Niksic  <[email protected]>

	* utils.c (with_thousand_seps_sum): Now defined only if
	SUM_SIZE_INT is double.

	* wget.h (SUM_SIZE_INT): Instead of bothering with long, long
	long, __int64, and friends, simply either use wgint or double, end
	of story.  Since we know how to print either, we no longer need
	LARGE_INT_FMT.

	* sysdeps.h (LARGE_INT): Renamed to SUM_SIZE_INT to better reflect
	its intent, and moved to wget.h.

Index: src/utils.c
===================================================================
--- src/utils.c	(revision 1807)
+++ src/utils.c	(working copy)
@@ -1162,7 +1162,7 @@
 
 
 /* Add thousand separators to a number already in string form.  Used
-   by with_thousand_seps and with_thousand_seps_large.  */
+   by with_thousand_seps and with_thousand_seps_sum.  */
 
 static char *
 add_thousand_seps (const char *repr)
@@ -1213,30 +1213,19 @@
   return add_thousand_seps (inbuf);
 }
 
-/* Write a string representation of LARGE_INT NUMBER into the provided
-   buffer.
+/* 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.  */
 
-   It would be dangerous to use sprintf, because the code wouldn't
-   work on a machine with gcc-provided long long support, but without
-   libc support for "%lld".  However, such old systems platforms
-   typically lack snprintf and will end up using our version, which
-   does support "%lld" whereever long longs are available.  */
-
-static void
-large_int_to_string (char *buffer, int bufsize, LARGE_INT number)
-{
-  snprintf (buffer, bufsize, LARGE_INT_FMT, number);
-}
-
-/* The same as with_thousand_seps, but works on LARGE_INT.  */
-
+#ifndef with_thousand_seps_sum
 char *
-with_thousand_seps_large (LARGE_INT l)
+with_thousand_seps_sum (SUM_SIZE_INT l)
 {
-  char inbuf[48];
-  large_int_to_string (inbuf, sizeof (inbuf), l);
+  char inbuf[64];
+  snprintf (inbuf, sizeof (inbuf), "%.0f", l);
   return add_thousand_seps (inbuf);
 }
+#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
Index: src/options.h
===================================================================
--- src/options.h	(revision 1807)
+++ src/options.h	(working copy)
@@ -116,7 +116,7 @@
 
   wgint limit_rate;		/* Limit the download rate to this
 				   many bps. */
-  LARGE_INT quota;		/* Maximum file size to download and
+  SUM_SIZE_INT quota;		/* Maximum file size to download and
 				   store. */
 
   int numurls;			/* Number of successfully downloaded
Index: src/init.c
===================================================================
--- src/init.c	(revision 1807)
+++ src/init.c	(working copy)
@@ -63,7 +63,7 @@
 
 CMD_DECLARE (cmd_boolean);
 CMD_DECLARE (cmd_bytes);
-CMD_DECLARE (cmd_bytes_large);
+CMD_DECLARE (cmd_bytes_sum);
 #ifdef HAVE_SSL
 CMD_DECLARE (cmd_cert_type);
 #endif
@@ -200,7 +200,7 @@
   { "proxypassword",	&opt.proxy_passwd,	cmd_string },
   { "proxyuser",	&opt.proxy_user,	cmd_string },
   { "quiet",		&opt.quiet,		cmd_boolean },
-  { "quota",		&opt.quota,		cmd_bytes_large },
+  { "quota",		&opt.quota,		cmd_bytes_sum },
 #ifdef HAVE_SSL
   { "randomfile",	&opt.random_file,	cmd_file },
 #endif
@@ -862,7 +862,7 @@
   return true;
 }
 
-/* Engine for cmd_bytes and cmd_bytes_large: converts a string such as
+/* Engine for cmd_bytes and cmd_bytes_sum: converts a string such as
    "100k" or "2.5G" to a floating point number.  */
 
 static bool
@@ -948,12 +948,12 @@
 }
 
 /* Like cmd_bytes, but PLACE is interpreted as a pointer to
-   LARGE_INT.  It works by converting the string to double, therefore
+   SIZE_SUM.  It works by converting the string to double, therefore
    working with values up to 2^53-1 without loss of precision.  This
    value (8192 TB) is large enough to serve for a while.  */
 
 static bool
-cmd_bytes_large (const char *com, const char *val, void *place)
+cmd_bytes_sum (const char *com, const char *val, void *place)
 {
   double byte_value;
   if (!parse_bytes_helper (val, &byte_value))
@@ -962,7 +962,7 @@
 	       exec_name, com, val);
       return false;
     }
-  *(LARGE_INT *)place = (LARGE_INT)byte_value;
+  *(SUM_SIZE_INT *) place = (SUM_SIZE_INT) byte_value;
   return true;
 }
 
Index: src/utils.h
===================================================================
--- src/utils.h	(revision 1807)
+++ src/utils.h	(working copy)
@@ -95,7 +95,9 @@
 void free_keys_and_values (struct hash_table *);
 
 char *with_thousand_seps (wgint);
-char *with_thousand_seps_large (LARGE_INT);
+#ifndef with_thousand_seps_sum
+char *with_thousand_seps_sum (SUM_SIZE_INT);
+#endif
 char *human_readable (wgint);
 int numdigit (wgint);
 char *number_to_string (char *, wgint);
Index: src/sysdep.h
===================================================================
--- src/sysdep.h	(revision 1807)
+++ src/sysdep.h	(working copy)
@@ -96,35 +96,6 @@
 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
 #endif
 
-/* Define a large integral type useful for storing large sizes that
-   exceed sizes of one download, such as when printing the sum of all
-   downloads.  Note that this has nothing to do with large file
-   support, which determines the wgint type.  This should be as large
-   as possible even on systems where when wgint is 32-bit; also,
-   unlike wgint, this can be a floating point type.
-
-   We use a 64-bit integral type where available, `double' otherwise.
-   It's hard to print LARGE_INT's portably, but fortunately it's
-   rarely needed.  */
-
-#if SIZEOF_LONG >= 8
-/* Long is large enough: use it.  */
-typedef long LARGE_INT;
-# define LARGE_INT_FMT "%ld"
-#elif SIZEOF_LONG_LONG >= 8
-/* Long long is large enough: use it.  */
-typedef long long LARGE_INT;
-# define LARGE_INT_FMT "%lld"
-#elif WINDOWS
-/* Use __int64 under Windows. */
-typedef __int64 LARGE_INT;
-# define LARGE_INT_FMT "%I64"
-#else
-/* Large integer type unavailable; fake it with `double'.  */
-typedef double LARGE_INT;
-# define LARGE_INT_FMT "%.0f"
-#endif
-
 /* These are needed so we can #define struct_stat to struct _stati64
    under Windows. */
 #ifndef struct_stat
Index: src/http.c
===================================================================
--- src/http.c	(revision 1807)
+++ src/http.c	(working copy)
@@ -59,7 +59,7 @@
 #include "convert.h"
 
 extern char *version_string;
-extern LARGE_INT total_downloaded_bytes;
+extern SUM_SIZE_INT total_downloaded_bytes;
 
 extern FILE *output_stream;
 extern bool output_stream_regular;
Index: src/ftp.c
===================================================================
--- src/ftp.c	(revision 1807)
+++ src/ftp.c	(working copy)
@@ -50,7 +50,7 @@
 #include "convert.h"		/* for downloaded_file */
 #include "recur.h"		/* for INFINITE_RECURSION */
 
-extern LARGE_INT total_downloaded_bytes;
+extern SUM_SIZE_INT total_downloaded_bytes;
 
 /* File where the "ls -al" listing will be saved.  */
 #define LIST_FILENAME ".listing"
Index: src/wget.h
===================================================================
--- src/wget.h	(revision 1807)
+++ src/wget.h	(working copy)
@@ -129,6 +129,25 @@
 # endif
 #endif
 
+/* Now define a large integral type useful for storing sizes of *sums*
+   of downloads, such as the value of the --quota option.  This should
+   be a type able to hold 2G+ values even on systems without large
+   file support.  (It is useful to limit Wget's download quota to say
+   10G even if a single file cannot be that large.)
+
+   To make sure we get the largest size possible, we use `double' on
+   systems without a 64-bit integral type.  (Since it is used in very
+   few places in Wget, this is acceptable.)  */
+
+#if SIZEOF_WGINT >= 8
+/* just use wgint, which we already know how to print */
+typedef wgint SUM_SIZE_INT;
+# define with_thousand_seps_sum with_thousand_seps
+#else
+/* On systems without LFS, use double, which buys us integers up to 2^53. */
+typedef double SUM_SIZE_INT;
+#endif
+
 #include "options.h"
 
 /* Everything uses this, so include them here directly.  */
Index: src/retr.c
===================================================================
--- src/retr.c	(revision 1807)
+++ src/retr.c	(working copy)
@@ -52,7 +52,7 @@
 #include "ptimer.h"
 
 /* Total size of downloaded files.  Used to enforce quota.  */
-LARGE_INT total_downloaded_bytes;
+SUM_SIZE_INT total_downloaded_bytes;
 
 /* If non-NULL, the stream to which output should be written.  This
    stream is initialized when `-O' is used.  */
Index: src/recur.c
===================================================================
--- src/recur.c	(revision 1807)
+++ src/recur.c	(working copy)
@@ -50,7 +50,7 @@
 #include "convert.h"
 
 extern char *version_string;
-extern LARGE_INT total_downloaded_bytes;
+extern SUM_SIZE_INT total_downloaded_bytes;
 
 extern struct hash_table *dl_url_file_map;
 extern struct hash_table *downloaded_html_set;
Index: src/main.c
===================================================================
--- src/main.c	(revision 1807)
+++ src/main.c	(working copy)
@@ -61,7 +61,7 @@
 
 struct options opt;
 
-extern LARGE_INT total_downloaded_bytes;
+extern SUM_SIZE_INT total_downloaded_bytes;
 extern char *version_string;
 
 extern struct cookie_jar *wget_cookie_jar;
@@ -961,13 +961,14 @@
     {
       logprintf (LOG_NOTQUIET,
 		 _("\nFINISHED --%s--\nDownloaded: %s bytes in %d files\n"),
-		 time_str (NULL), with_thousand_seps_large (total_downloaded_bytes),
+		 time_str (NULL),
+		 with_thousand_seps_sum (total_downloaded_bytes),
 		 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_large (opt.quota));
+		   with_thousand_seps_sum (opt.quota));
     }
 
   if (opt.cookies_output)