aboutsummaryrefslogtreecommitdiff
path: root/bitreader.cc
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2015-06-07 21:22:45 +0200
committerJavier <dev.git@javispedro.com>2015-06-07 21:22:45 +0200
commita69e97943539a8abc4d2762638c169dc19c88516 (patch)
treef3516ea29745db65971247cee4c260b49f1067b2 /bitreader.cc
downloadscribiu-a69e97943539a8abc4d2762638c169dc19c88516.tar.gz
scribiu-a69e97943539a8abc4d2762638c169dc19c88516.zip
initial import
Diffstat (limited to 'bitreader.cc')
-rw-r--r--bitreader.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/bitreader.cc b/bitreader.cc
new file mode 100644
index 0000000..dd85f74
--- /dev/null
+++ b/bitreader.cc
@@ -0,0 +1,45 @@
+#include "bitreader.h"
+
+BitReader::BitReader(QIODevice *device)
+ : child(device), buf(0), avail(0)
+{
+}
+
+BitReader::~BitReader()
+{
+}
+
+quint64 BitReader::readBits(int n)
+{
+ quint64 x = peekBits(n);
+
+ Q_ASSERT(avail >= n);
+ buf -= x << (avail - n);
+ avail -= n;
+
+ return x;
+}
+
+quint64 BitReader::peekBits(int n)
+{
+ while (n > avail) {
+ char c;
+ child->getChar(&c);
+ buf = (buf << 8) | (quint8)(c);
+ avail += 8;
+ }
+ quint64 x = buf >> (avail - n);
+
+ return x;
+}
+
+void BitReader::skipUntilNextByte()
+{
+ int skip = avail % 8;
+ readBits(skip);
+}
+
+bool BitReader::atEnd()
+{
+ return avail == 0 && child->atEnd();
+}