Basically trying to create a dict of all declared variables and functions along with their types of a .c source file. Does anyone think I missed anything?
#!usr/python
import string
import re
from types import *
from pprint import pprint
varDict = {}
def find_substring(needle, haystack):
index = haystack.find(needle)
if index == -1:
return False
if index != 0 and haystack[index-1] not in string.whitespace:
return False
L = index + len(needle)
if L < len(haystack) and haystack[L] not in string.whitespace:
return False
return True
f = open('example.c')
varTypeList = []
for varType in open('typesfile.txt'):
varTypeList.append(varType[:-1])
for line in f:
for varType in varTypeList:
if find_substring(varType,line):
var = re.search('\s*[\*_a-zA-Z]+[\*_a-zA-Z0-9]?', line.split(varType)[1])
if type(var) is not NoneType:
varDict[var.group(0)] = varType
pprint(varDict)