去除cpp文件中与h/hpp文件相同的头文件

import os
def getIndexIncludeLine(file):
    '''
    description:Remove the same reference header file in file2
    param:
        -file: xxx.h/xxx.cpp file (is array)
    return:
        -res: line Index for reference header file
    '''
    res = []
    for index,line in enumerate(file):
        if(line[0:8] == "#include"):
            res.append(index)
    return res

def readFile(path):
    with open(path, "r") as f:
        content = f.readlines()
    return content


def writeFile(path,content):
    try:
        with open(path, "w") as f:
            for line in content:
                f.write(line)
        return True
    except:
        return False

def compareFileInclude(file1path,file2path):
    '''
    description:Remove the same reference header file in file2
    param:
        -file1: xxx.h file (is array)
        -file2: xxx.cpp file (is array)
    return:True or False
    '''
    print(file1path)
    print(file2path)
    file1 = readFile(file1path)
    file2 = readFile(file2path)
    file1_index = getIndexIncludeLine(file1)
    file2_index = getIndexIncludeLine(file2)

    if(len(file1_index) == 0 or len(file2_index) == 0):
        return True

    # for file2_index building a dictionary
    # file2_dict[i] = true => file2[i] is remove 
    file2_dict = {}

    # building  dictionary to compare
    file2head_dict = {}
    for i in file2_index:
        lineStr = file2[i].strip()
        file2head_dict[lineStr] = i
    
    # compare
    for i in file1_index:
        lineStr = file1[i].strip()
        if(lineStr in file2head_dict.keys()):
            # remove
            print("remove line is ",file2head_dict[lineStr])
            file2_dict[file2head_dict[lineStr]] = True

    # get return
    file = []
    for i in range(len(file2)):
        if(i not in file2_dict.keys()):
            file.append(file2[i])
    return writeFile(file2path,file)



    
def getDirPath(rootPath):
    sonFile = os.listdir(rootPath)
    if("include" in sonFile and "src" in sonFile):
        return [rootPath]
    else:
        res = []
        for folder_name in sonFile:
            sonDirPath = os.path.join(rootPath, folder_name)
            if os.path.isdir(sonDirPath):
                temp = getDirPath(sonDirPath)
                for path in temp:
                    res.append(path)
        return res


def main(rootPath):
    path_list = getDirPath(rootPath)
    print(path_list)
    for path in path_list:
        h_file = os.listdir(path + '\include')
        cpp_file = os.listdir(path + '\src')
        for file_name in h_file:
            arr = file_name.split('.')
            suffix = arr[-1]
            if(suffix == 'h' or suffix =='hpp'):
                cpp_filename = ''
                for index in range(len(arr)-1):
                    cpp_filename += arr[index]
                cpp_filename += '.cpp'
                #compare
                compareFileInclude(path+'\\include\\'+file_name,
                                   path+'\\src\\'+cpp_filename)

if __name__ == "__main__":
    rootPath = "E:\\IDE\VS_CODE\\workspace\\vscode_python\\test\\TEST"
    main(rootPath)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值