From 97de4a5f7819afcf994120bcc00d8a65549b4f65 Mon Sep 17 00:00:00 2001 From: Julian Daube Date: Tue, 2 Jul 2019 14:45:29 +0200 Subject: [PATCH] reader: add unsigned integer types --- gds/reader.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/gds/reader.py b/gds/reader.py index c375b1d..594cf62 100644 --- a/gds/reader.py +++ b/gds/reader.py @@ -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 @@ -77,6 +91,7 @@ class Reader(ProgressGetter): result += -1 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)