+2003-09-15 Hrvoje Niksic <hniksic@xemacs.org>
+
+ * wget.h (XDIGIT_TO_XCHAR): Implement as index into a literal
+ string.
+ (XDIGIT_TO_xchar): Ditto.
+
+2003-09-15 Hrvoje Niksic <hniksic@xemacs.org>
+
+ * utils.c: Change the type of timer-related functions from long to
+ double, for better precision. On machines supporting gettimeofday
+ the timers now work with granularity of less than one millisecond.
+
2003-09-15 Hrvoje Niksic <hniksic@xemacs.org>
* cookies.c (parse_set_cookies): Fixed the parser to handle more
/* The most recent elapsed time, calculated by wtimer_elapsed().
Measured in milliseconds. */
- long elapsed_last;
+ double elapsed_last;
/* Approximately, the time elapsed between the true start of the
measurement and the time represented by START. */
- long elapsed_pre_start;
+ double elapsed_pre_start;
};
/* Allocate a timer. It is not legal to do anything with a freshly
wt->elapsed_pre_start = 0;
}
-static long
+static double
wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2)
{
#ifdef TIMER_GETTIMEOFDAY
- return ((wst1->tv_sec - wst2->tv_sec) * 1000
- + (wst1->tv_usec - wst2->tv_usec) / 1000);
+ return ((double)(wst1->tv_sec - wst2->tv_sec) * 1000
+ + (double)(wst1->tv_usec - wst2->tv_usec) / 1000);
#endif
#ifdef TIMER_TIME
#endif
#ifdef WINDOWS
- return (long)(wst1->QuadPart - wst2->QuadPart) / 10000;
+ return (double)(wst1->QuadPart - wst2->QuadPart) / 10000;
#endif
}
reset. It is allowed to call this function more than once to get
increasingly higher elapsed values. */
-long
+double
wtimer_elapsed (struct wget_timer *wt)
{
wget_sys_time now;
- long elapsed;
+ double elapsed;
wtimer_sys_set (&now);
elapsed = wt->elapsed_pre_start + wtimer_sys_diff (&now, &wt->start);
return elapsed;
}
-/* Return the assessed granularity of the timer implementation. This
- is important for certain code that tries to deal with "zero" time
- intervals. */
+/* Return the assessed granularity of the timer implementation, in
+ milliseconds. This is important for certain code that tries to
+ deal with "zero" time intervals. */
-long
+double
wtimer_granularity (void)
{
#ifdef TIMER_GETTIMEOFDAY
/* Granularity of gettimeofday is hugely architecture-dependent.
However, it appears that on modern machines it is better than
- 1ms. */
- return 1;
+ 1ms. Assume 100 usecs. */
+ return 0.1;
#endif
#ifdef TIMER_TIME
#endif
#ifdef TIMER_WINDOWS
- /* ? */
+ /* #### Fill this in! */
return 1;
#endif
}
struct wget_timer *wtimer_new PARAMS ((void));
void wtimer_delete PARAMS ((struct wget_timer *));
void wtimer_reset PARAMS ((struct wget_timer *));
-long wtimer_elapsed PARAMS ((struct wget_timer *));
-long wtimer_granularity PARAMS ((void));
+double wtimer_elapsed PARAMS ((struct wget_timer *));
+double wtimer_granularity PARAMS ((void));
char *html_quote_string PARAMS ((const char *));
/* The smaller value of the two. */
#define MINVAL(x, y) ((x) < (y) ? (x) : (y))
-/* Convert the ASCII character X to a hex-digit. X should be between
- '0' and '9', or between 'A' and 'F', or between 'a' and 'f'. The
- result is a number between 0 and 15. If X is not a hexadecimal
- digit character, the result is undefined. */
-#define XCHAR_TO_XDIGIT(x) \
- (((x) >= '0' && (x) <= '9') ? \
- ((x) - '0') : (TOUPPER(x) - 'A' + 10))
-
-/* The reverse of the above: convert a HEX digit in the [0, 15] range
- to an ASCII character representing it. The A-F characters are
- always in upper case. */
-#define XDIGIT_TO_XCHAR(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A'))
-
-/* Like XDIGIT_TO_XCHAR, but produce a lower-case char. */
-#define XDIGIT_TO_xchar(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
-
-#define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array)))
+/* Convert the ASCII character that represents a hexadecimal digit to
+ the number in range [0, 16) that corresponds to the digit. X
+ should be between '0' and '9', or between 'A' and 'F', or between
+ 'a' and 'f'. If X is not a hexadecimal digit character, the result
+ is undefined. */
+#define XCHAR_TO_XDIGIT(x) \
+ (((x) >= '0' && (x) <= '9') ? ((x) - '0') : (TOUPPER(x) - 'A' + 10))
+
+/* The reverse of the above: convert a digit number in the [0, 16)
+ range to an ASCII character. The A-F characters are in upper
+ case. */
+#define XDIGIT_TO_XCHAR(x) ("0123456789ABCDEF"[x])
+
+/* Like XDIGIT_TO_XCHAR, but generates lower-case characters. */
+#define XDIGIT_TO_xchar(x) ("0123456789abcdef"[x])
+
+/* Returns the number of elements in an array with fixed
+ initialization. For example:
+
+ static char a[] = "foo"; -- countof(a) == 4 (for terminating \0)
+
+ int a[5] = {1, 2}; -- countof(a) == 5
+
+ char *a[3] = { -- countof(a) == 3
+ "foo", "bar", "baz"
+ };
+
+ And, most importantly, it works when the compiler counts the array
+ elements for you:
+
+ char *a[] = { -- countof(a) == 4
+ "foo", "bar", "baz", "qux"
+ } */
+#define countof(array) (sizeof (array) / sizeof (*(array)))
+
+#define ARRAY_SIZE(array) countof (array)
/* Copy the data delimited with BEG and END to alloca-allocated
storage, and zero-terminate it. BEG and END are evaluated only