# Filename: cf.py
''' Format tools for .h and .c files python 3.1
Made by tongwei v1 2010.2.11 ver 0.9(DO NOT MOVE statement Pls)
Used to make your source code look better'''
#static params defines
#static define
blockHead = ['typedef','function','none','static array','functionHead']
#expressionHead
expList = ['if','while','case','switch']
eleTag = 4
#every line there is 2 params
paramNumSet = 2
secondBlockAlign = 16
commentTag = '/*'
functionHeadModify =['static','extern']
lineHead = ['{','}','typedef']
lineEnd = [';',',','*/','{']
#var params defines
fileName = 'plat.h'
class Line:
#a tab is 4 space
#eleTag=4;
#the max elements in one lines is 8
elementsPos=[5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#which block in charge of the line
blockFlag = 'none'
handle = 'none'
#the length of fucHead ,fe. void abc();, funcHeadLen is the len('void')
funcHeadLen=0
#record the bracketPos,it only record the { pos.
#for example
#{
# {
#bracketPosStack=[1,5]
#at most 8 loops embeded
bracketPosStack=[]
#ind the stacks in the func body
functionStack=0
fctLineResult = ''
def __init__(self,lineParts):
self.lineParts=lineParts
#print('befor format',Line.elementsPos)
curElePos = 0
prtLine=''
for i in range(0,len(lineParts)):
if i==0:
#get first tab
spaceNum=Line.elementsPos[0]-1
prtLine=prtLine+' '*spaceNum
prtLine=prtLine+lineParts[0]
curElePos=Line.elementsPos[0]+len(lineParts[i])
else:
#calc next pos
nextPos=curElePos+eleTag
#print(i)
if nextPos>Line.elementsPos[i]:
Line.elementsPos[i]=nextPos
#calc space num
spaceNum=Line.elementsPos[i]-curElePos
prtLine=prtLine+' '*spaceNum
prtLine=prtLine+lineParts[i]
curElePos=Line.elementsPos[i]+len(lineParts[i])
Line.resultLine=prtLine
#print('after format',Line.elementsPos)
def getStructDefLine(self):
return 'get struct line'
def lprint(self):
'''lprint print agv[] as a wanted format
'''
if len(lineParts)==0:
return
#tan need to be handle
Line.handle=='none'
lnHead = self.lineParts[0]
#judege which part now block is:
#if lnHead in blockHead:
# Line.blockFlag = lnHead
# #block,#means macro or comment
if lnHead.startswith('#'):
Line.resultLine = Line.resultLine.strip()
#static array = block
if (self.lineParts[0].lower() == 'static') and Line.resultLine.strip().endswith('='):
Line.blockFlag = 'static array'
if Line.blockFlag == 'static array':
Line.handle = 'static array'
Line.resultLine =Line.resultLine.strip()
#it is static arry first line
if self.lineParts[0].lower() == 'static':
Line.resultLine = ''
for parts in self.lineParts:
Line.resultLine = Line.resultLine + parts +eleTag*' '
Line.resultLine = cutLineEquaBlank(Line.resultLine)
else:
# { line in static array
if Line.resultLine.startswith('{'):
Line.resultLine = len(Line.bracketPosStack)*eleTag*' '+Line.resultLine
Line.bracketPosStack.append('1')
# } line in static array
elif Line.resultLine.startswith('}'):
#here must think about the line as '}}};'
bResultLine = ''
founded = False
resultAfterB = Line.resultLine
for wd in range(0,len(Line.resultLine)):
#print(Line.resultLine[wd])
if (Line.resultLine[wd] == '}') and (not founded):
#if now char is not the last char, and the is another '}' after now char,insert a /n
if (len(Line.bracketPosStack)>0) and (wd<len(Line.resultLine)-1):
if Line.resultLine[wd+1] == '}':
resultAfterB=Line.resultLine[wd+1:]
#print('??/',resultAfterB)
bResultLine = bResultLine+(len(Line.bracketPosStack)-1)*eleTag*' '+'}/n'
del Line.bracketPosStack[len(Line.bracketPosStack)-1]
else:
#print('else resulftAfterB is ',resultAfterB)
bResultLine = bResultLine+(len(Line.bracketPosStack)-1)*eleTag*' '+resultAfterB
founded = True
del Line.bracketPosSt