reader: add unsigned integer types

This commit is contained in:
Julian Daube 2019-07-02 14:45:29 +02:00
parent 4eb3dc8407
commit 97de4a5f78

View File

@ -1,7 +1,7 @@
from datetime import datetime from datetime import datetime
from .record import * from .record import *
import abc import abc
import ctypes
class ProgressGetter(object, metaclass=abc.ABCMeta): class ProgressGetter(object, metaclass=abc.ABCMeta):
@abc.abstractproperty @abc.abstractproperty
@ -40,20 +40,36 @@ class Reader(ProgressGetter):
def skip(self, n): def skip(self, n):
self.stream.read(n) self.stream.read(n)
def read_int(self): def read_uint(self):
temp = self.stream.read(4) temp = self.stream.read(4)
if len(temp) != 4: if len(temp) != 4:
return None return None
return int(temp[3]) | int(temp[2]) << 8 | int(temp[1]) << 16 | int(temp[0]) << 24 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) temp = self.stream.read(2)
if len(temp) != 2: if len(temp) != 2:
return None return None
return int(temp[1]) | int(temp[0]) << 8 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): def read_double(self):
temp = self.stream.read(8) temp = self.stream.read(8)
if len(temp) != 8: if len(temp) != 8:
@ -64,10 +80,8 @@ class Reader(ProgressGetter):
for i in temp: for i in temp:
if int(i) != 0: if int(i) != 0:
# read double # read double
result = 1
for j in range(1,8): 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 = int(temp[0]) & 0x7F
exp -= 64 exp -= 64
@ -78,6 +92,7 @@ class Reader(ProgressGetter):
return result return result
# double is Zero # double is Zero
return 0 return 0
@ -87,8 +102,8 @@ class Reader(ProgressGetter):
def read_record(self): def read_record(self):
result = Record() result = Record()
try: try:
result.len = self.read_short() result.len = self.read_ushort()
result.ident = Records(self.read_short()) result.ident = Records(self.read_ushort())
except ValueError: except ValueError:
return None return None
@ -98,14 +113,14 @@ class Reader(ProgressGetter):
def read_date(self): def read_date(self):
# date # date
year = self.read_short() year = self.read_ushort()
month = self.read_short() month = self.read_ushort()
day = self.read_short() day = self.read_ushort()
# time # time
hour = self.read_short() hour = self.read_ushort()
minute = self.read_short() minute = self.read_ushort()
second = self.read_short() second = self.read_ushort()
return datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second) return datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)