Mercurial > touhou
annotate pytouhou/utils/bitstream.py @ 110:3ac41b966fed
Add writing support to pytouhou.formats.std!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Tue, 06 Sep 2011 16:40:32 +0200 |
parents | ac2e5e1c2c3c |
children |
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 | |
97 | 22 def __enter__(self): |
23 return self | |
24 | |
25 | |
26 def __exit__(self, type, value, traceback): | |
27 return self.io.__exit__(type, value, traceback) | |
28 | |
29 | |
0 | 30 def seek(self, offset, whence=0): |
31 self.io.seek(offset, whence) | |
32 self.byte = 0 | |
33 self.bits = 0 | |
34 | |
35 | |
36 def tell(self): | |
37 return self.io.tell() | |
38 | |
39 | |
40 def tell2(self): | |
41 return self.io.tell(), self.bits | |
42 | |
43 | |
44 def read_bit(self): | |
45 if not self.bits: | |
46 self.byte = ord(self.io.read(1)) | |
47 self.bits = 8 | |
48 self.bits -= 1 | |
49 return (self.byte >> self.bits) & 0x01 | |
50 | |
51 | |
52 def read(self, nb_bits): | |
53 value = 0 | |
54 for i in range(nb_bits - 1, -1, -1): | |
55 value |= self.read_bit() << i | |
56 return value | |
57 | |
58 | |
59 def write_bit(self, bit): | |
60 if self.bits == 8: | |
61 self.io.write(chr(self.byte)) | |
62 self.bits = 0 | |
63 self.byte = 0 | |
64 self.byte &= ~(1 << (7 - self.bits)) | |
65 self.byte |= bit << (7 - self.bits) | |
66 self.bits += 1 | |
67 | |
68 | |
69 def write(self, bits, nb_bits): | |
70 for i in range(nb_bits): | |
71 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | |
72 | |
73 | |
74 def flush(self): | |
75 self.io.write(chr(self.byte)) | |
76 self.bits = 0 | |
77 self.byte = 0 | |
78 self.io.flush() | |
79 |