]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
- Added gb_t
authorrousskov <>
Thu, 5 Mar 1998 06:48:14 +0000 (06:48 +0000)
committerrousskov <>
Thu, 5 Mar 1998 06:48:14 +0000 (06:48 +0000)
src/tools.cc

index 69a4134ae9cd41e978cefc43beab4c3ce87d90db..6cdadd1cc4f9127328423031522520dc1d69d4ae 100644 (file)
@@ -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;
+}