一个开发中的用来格式化.c .h头文件格式的python自用脚本(python3.1欢迎试用)

这是一个名为cf.py的Python脚本,用于格式化.c和.h文件。它由tongwei于2010.2.11创建,主要用于使源代码看起来更整洁。脚本中定义了各种静态参数,如blockHead、expList等,并实现了Line类来处理代码行。脚本还包含处理不同代码块、括号对齐、注释等功能的方法。
摘要由CSDN通过智能技术生成

# 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值