This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_ext.py
71 lines (51 loc) · 1.69 KB
/
open_ext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import io
import struct
class open_ext:
def __init__ (self, *args):
self.file = open(*args)
def __getattr__(self, attr):
return getattr(self.file, attr)
def __enter__ (self):
return self
def __exit__ (self, exc_type, exc_value, traceback):
self.file.close()
def eof(self):
if not self.file.read(1):
return True
self.file.seek(-1, io.SEEK_CUR)
return False
def skip(self, bytes):
self.file.seek(bytes, io.SEEK_CUR)
def read_until_zero(self):
out = b''
byte = self.file.read(1)
while byte != b"\x00":
out += byte
byte = self.file.read(1)
return out
def read_until_end(self):
return self.file.read()
def read_i8(self):
return struct.unpack('b', self.file.read(1))[0]
def read_i16_le(self):
return struct.unpack('<h', self.file.read(2))[0]
def read_i32_le(self):
return struct.unpack('<i', self.file.read(4))[0]
def read_u8(self):
return struct.unpack('B', self.file.read(1))[0]
def read_u16_le(self):
return struct.unpack('<H', self.file.read(2))[0]
def read_u32_le(self):
return struct.unpack('<I', self.file.read(4))[0]
def write_i8(self, x):
self.file.write(struct.pack('b', x))
def write_i16_le(self, x):
self.file.write(struct.pack('<h', x))
def write_i32_le(self, x):
self.file.write(struct.pack('<i', x))
def write_u8(self, x):
self.file.write(struct.pack('B', x))
def write_u16_le(self, x):
self.file.write(struct.pack('<H', x))
def write_u32_le(self, x):
self.file.write(struct.pack('<I', x))