reader: add unsigned integer types
This commit is contained in:
parent
4eb3dc8407
commit
97de4a5f78
@ -1,7 +1,7 @@
|
||||
from datetime import datetime
|
||||
from .record import *
|
||||
import abc
|
||||
|
||||
import ctypes
|
||||
|
||||
class ProgressGetter(object, metaclass=abc.ABCMeta):
|
||||
@abc.abstractproperty
|
||||
@ -40,20 +40,36 @@ class Reader(ProgressGetter):
|
||||
def skip(self, n):
|
||||
self.stream.read(n)
|
||||
|
||||
def read_int(self):
|
||||
def read_uint(self):
|
||||
temp = self.stream.read(4)
|
||||
if len(temp) != 4:
|
||||
return None
|
||||
|
||||
return int(temp[3]) | int(temp[2]) << 8 | int(temp[1]) << 16 | int(temp[0]) << 24
|
||||
|
||||
def read_short(self):
|
||||
def read_ushort(self):
|
||||
temp = self.stream.read(2)
|
||||
if len(temp) != 2:
|
||||
return None
|
||||
|
||||
return int(temp[1]) | int(temp[0]) << 8
|
||||
|
||||
def read_short(self):
|
||||
temp = self.read_ushort()
|
||||
|
||||
if temp == None:
|
||||
return None
|
||||
|
||||
return ctypes.c_short(temp).value
|
||||
|
||||
def read_int(self):
|
||||
temp = self.read_uint()
|
||||
|
||||
if temp == None:
|
||||
return None
|
||||
|
||||
return ctypes.c_int(temp).value
|
||||
|
||||
def read_double(self):
|
||||
temp = self.stream.read(8)
|
||||
if len(temp) != 8:
|
||||
@ -64,10 +80,8 @@ class Reader(ProgressGetter):
|
||||
for i in temp:
|
||||
if int(i) != 0:
|
||||
# read double
|
||||
result = 1
|
||||
|
||||
for j in range(1,8):
|
||||
result += float(temp[8-j])/(2**(j*8))
|
||||
result += float(temp[j])/(2.0**(j*8))
|
||||
|
||||
exp = int(temp[0]) & 0x7F
|
||||
exp -= 64
|
||||
@ -78,6 +92,7 @@ class Reader(ProgressGetter):
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# double is Zero
|
||||
return 0
|
||||
|
||||
@ -87,8 +102,8 @@ class Reader(ProgressGetter):
|
||||
def read_record(self):
|
||||
result = Record()
|
||||
try:
|
||||
result.len = self.read_short()
|
||||
result.ident = Records(self.read_short())
|
||||
result.len = self.read_ushort()
|
||||
result.ident = Records(self.read_ushort())
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
@ -98,14 +113,14 @@ class Reader(ProgressGetter):
|
||||
|
||||
def read_date(self):
|
||||
# date
|
||||
year = self.read_short()
|
||||
month = self.read_short()
|
||||
day = self.read_short()
|
||||
year = self.read_ushort()
|
||||
month = self.read_ushort()
|
||||
day = self.read_ushort()
|
||||
|
||||
# time
|
||||
hour = self.read_short()
|
||||
minute = self.read_short()
|
||||
second = self.read_short()
|
||||
hour = self.read_ushort()
|
||||
minute = self.read_ushort()
|
||||
second = self.read_ushort()
|
||||
|
||||
return datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user