From: rousskov <> Date: Thu, 5 Mar 1998 06:48:14 +0000 (+0000) Subject: - Added gb_t X-Git-Tag: SQUID_3_0_PRE1~3932 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c1dabf04c94dc6cb788983dea533e18aae97e07b;p=thirdparty%2Fsquid.git - Added gb_t --- diff --git a/src/tools.cc b/src/tools.cc index 69a4134ae9..6cdadd1cc4 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -1,6 +1,6 @@ /* - * $Id: tools.cc,v 1.149 1998/02/27 07:28:57 kostas Exp $ + * $Id: tools.cc,v 1.150 1998/03/04 23:48:14 rousskov Exp $ * * DEBUG: section 21 Misc Functions * AUTHOR: Harvest Derived @@ -820,3 +820,39 @@ kb_incr(kb_t * k, size_t v) k->kb += (k->bytes >> 10); k->bytes &= 0x3FF; } + +void +gb_flush(gb_t *g) +{ + g->gb += (g->bytes >> 30); + g->bytes &= (1<<30)-1; +} + +double +gb_to_double(const gb_t *g) +{ + return ((double)g->gb)*((double)(1<<30)) + ((double)g->bytes); +} + +const char * +gb_to_str(const gb_t *g) +{ + /* + * it is often convenient to call gb_to_str several times for _one_ printf + */ + #define max_cc_calls 5 + typedef char GbBuf[32]; + static GbBuf bufs[max_cc_calls]; + static int call_id = 0; + double value = gb_to_double(g); + char *buf = bufs[call_id++]; + /* select format */ + if (value < 1e9) + snprintf(buf, sizeof(GbBuf), "%.2f MB", value/1e6); + else + if (value < 1e12) + snprintf(buf, sizeof(GbBuf), "%.2f GB", value/1e9); + else + snprintf(buf, sizeof(GbBuf), "%.2f TB", value/1e12); + return buf; +}