/*
- * $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
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;
+}