blob: 3f48dc0df6c0eeaa55a5146ce084440ffe70a5b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#!/usr/bin/env python
#
# Compute offsets for an AIVDM message breakdown, given the bit widths.
# Takes a single argument, the line number of a table start.
# Writes the corrected table to standard output.
import sys
if __name__ == '__main__':
startline = int(sys.argv[1])
table = []
keep = False
i = 0
for line in sys.stdin:
i += 1
if i == startline:
if line.startswith("|="):
keep = True
else:
print >>sys.stderr, "Bad table start"
sys.exit(1)
elif line.startswith("|="):
keep = False
if keep:
table.append(line)
table = table[2:]
widths = map(lambda s: s.split('|')[2].strip(), table)
offsets = []
base = 0
for w in widths:
if not w:
offsets.append('')
else:
w = int(w)
offsets.append("%d-%d" % (base, base + w - 1))
base += w
print >>sys.stderr, "Total bits:", base
owidth = max(*map(len, offsets))
for (i, off) in enumerate(offsets):
offsets[i] += " " * (owidth - len(offsets[i]))
for (i, t) in enumerate(table):
print "|" + offsets[i] + t[owidth+1:].rstrip()
|