blender-gdsimporter/gds/reader.py

97 lines
2.2 KiB
Python
Raw Normal View History

from datetime import datetime
from .record import *
class Reader(object):
def __init__(self, file):
self.stream = file
def skip(self, n):
self.stream.read(n)
def read_int(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):
temp = self.stream.read(2)
if len(temp) != 2:
return None
return int(temp[1]) | int(temp[0]) << 8
def read_double(self):
temp = self.stream.read(8)
if len(temp) != 8:
return None
result = 0
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))
exp = int(temp[0]) & 0x7F
exp -= 64
result *= 16**exp
if int(temp[0]) & 0x80:
result += -1
return result
# double is Zero
return 0
def read_ascii(self, len):
return self.stream.read(len).decode("ASCII").strip()
def read_record(self):
result = Record()
try:
result.len = self.read_short()
result.ident = Records(self.read_short())
except ValueError:
return None
result.len -= 4 # remove record header len
return result
def read_date(self):
# date
year = self.read_short()
month = self.read_short()
day = self.read_short()
# time
hour = self.read_short()
minute = self.read_short()
second = self.read_short()
return datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
def read_coord(self):
X = self.read_int()
Y = self.read_int()
return (X,Y)
def read_coords(self, len):
len /= 8
result = []
while len > 0:
point = self.read_coord()
if not point:
return None
result.append(point)
len -= 1
return result