2006-12-05 David Lodge <dave@cirt.net>
[dia.git] / plug-ins / cgm / cgm.py
blobe68d38cf20f766a6ecc487235fe2357f99489151
2 def read_cgm(file):
3 if type(file) == type(''):
4 file = open(file, 'rb');
5 list = []
6 head = file.read(2);
7 while head != '':
8 if len(head) != 2: break
9 el_class = ord(head[0]) >> 4
10 el_id = ((ord(head[0]) & 0x0f) << 3) | (ord(head[1]) >> 5)
11 el_len = ord(head[1]) & 0x1f
12 if el_len == 31: # long form element
13 head = file.read(2)
14 if len(head) != 2: break
15 el_len = (ord(head[0]) << 8) | ord(head[1])
16 print (el_class, el_id, el_len)
18 el_body = file.read(el_len)
19 if el_len & 1 == 1: # odd element length
20 file.read(1) # discard NULL byte
22 list.append((el_class, el_id, el_body))
24 if (el_class, el_id) == (0, 2):
25 break
27 head = file.read(2)
28 return list