import json
import re

ret = []
with open('visualize.out', 'r') as f:
    for line in f:
        line = line.strip()
        print 'Calc:', line.strip()
        m = re.search('VISUALIZE: ([A-Z]+)', line)
        t = m.group(1)
        if t == 'CALLOC':
            m = re.search('VISUALIZE: CALLOC for size (\d+) returned (0x[0-9a-f]+); Heap: (0x[0-9a-f]+), (0x[0-9a-f]+)', line)
            print 'size', m.group(1)
            print 'addr', m.group(2)
            ret.append(['CALLOC', m.group(2), m.group(1), m.group(3), m.group(4)])
        elif t == 'REALLOC':
            m = re.search('VISUALIZE: REALLOC for size (\d+) of (0x[0-9a-f]+) returned (0x[0-9a-f]+); Heap: (0x[0-9a-f]+), (0x[0-9a-f]+)', line)
            print 'size', m.group(1)
            print 'addr', m.group(3)
            ret.append(['REALLOC', m.group(3), m.group(1), m.group(4), m.group(5), m.group(2)])
        elif t == 'FREE':
            m = re.search('VISUALIZE: FREE for (0x[0-9a-f]+)', line)
            print 'addr', m.group(1)
            ret.append(['FREE', m.group(1)])
        else:
            raise Exception('None of the types we wanted')

with open('visualize.json', 'w') as f:
    json.dump(ret, f)

