Mercurial > touhou
annotate pytouhou/utils/bitstream.py @ 52:ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 22 Aug 2011 22:37:14 +0200 |
parents | 6b2c7af2384c |
children | ac2e5e1c2c3c |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
14 |
0 | 15 class BitStream(object): |
16 def __init__(self, io): | |
17 self.io = io | |
18 self.bits = 0 | |
19 self.byte = 0 | |
20 | |
21 | |
22 def seek(self, offset, whence=0): | |
23 self.io.seek(offset, whence) | |
24 self.byte = 0 | |
25 self.bits = 0 | |
26 | |
27 | |
28 def tell(self): | |
29 return self.io.tell() | |
30 | |
31 | |
32 def tell2(self): | |
33 return self.io.tell(), self.bits | |
34 | |
35 | |
36 def read_bit(self): | |
37 if not self.bits: | |
38 self.byte = ord(self.io.read(1)) | |
39 self.bits = 8 | |
40 self.bits -= 1 | |
41 return (self.byte >> self.bits) & 0x01 | |
42 | |
43 | |
44 def read(self, nb_bits): | |
45 value = 0 | |
46 for i in range(nb_bits - 1, -1, -1): | |
47 value |= self.read_bit() << i | |
48 return value | |
49 | |
50 | |
51 def write_bit(self, bit): | |
52 if self.bits == 8: | |
53 self.io.write(chr(self.byte)) | |
54 self.bits = 0 | |
55 self.byte = 0 | |
56 self.byte &= ~(1 << (7 - self.bits)) | |
57 self.byte |= bit << (7 - self.bits) | |
58 self.bits += 1 | |
59 | |
60 | |
61 def write(self, bits, nb_bits): | |
62 for i in range(nb_bits): | |
63 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | |
64 | |
65 | |
66 def flush(self): | |
67 self.io.write(chr(self.byte)) | |
68 self.bits = 0 | |
69 self.byte = 0 | |
70 self.io.flush() | |
71 |