python 计算结构数量

import os
import re
import sys

def extract(pattern1, str0):
    result1 = pattern1.findall(str0)
    str = ""
    if (result1):
        str = result1[0]
    return str

def ExtractVariablesFromFile (sourceFileStr, resultFileStr):
    sourceFile = open(sourceFileStr, mode='r', encoding='gbk')
    resultFile = open(resultFileStr, "w")
    pattern1 = re.compile(r'}[ ]*(\w+)')
    string = ""
    for line in sourceFile:
        string = extract(pattern1, line)
        if(string == ""):
            continue
        num = count(sourceFileStr, string)
        resultFile.write(string + " ")
        resultFile.write(str(num))
        resultFile.write("\n")
    sourceFile.close()
    resultFile.close()
def count(sourceFileStr, str):
    sourceFile = open(sourceFileStr, mode='r', encoding='gbk')
    position = sourceFile.tell()
    sourceFile.seek(0, 0)
    total = 0
    reStr=str
    pattern1 = re.compile(reStr)
    result1 = pattern1.findall(str)
    str = ""
    if (result1):
        str = result1[0]
    for line in sourceFile:
        result1 = pattern1.findall(line.strip())
        if(result1):
            total += 1
        # if str in line:
        #     total += 1
    sourceFile.seek(position, 0)
    return total
def count2(sourceFile, str):
    sourceFile.seek(0, 0)
    total = 0
    reStr=' '+str+ '( |;|[)'
    pattern1 = re.compile(reStr)
    result1 = pattern1.findall(str)
    str = ""
    if (result1):
        str = result1[0]
    for line in sourceFile:
        result1 = pattern1.findall(line.strip())
        if(result1):
            total += 1
            # if str in line:
            # total += 1
    return total

def VariablesFromFile(sourceFileStr, valuesFileStr, resultFileStr):
    sourceFile = open(sourceFileStr, mode='r', encoding='UTF-8')
    valuesFile = open(valuesFileStr)
    resultFile = open(resultFileStr, "w")
    for line in valuesFile:
        if(line.strip() != ""):
            line=line.strip()
            num = count(sourceFile,line )
            resultFile.write(line+" ")
            resultFile.write(str(num))
            resultFile.write("\n")
    sourceFile.close()
    valuesFile.close()
    resultFile.close()
a="LinkQueue.h"
b="LinkQueueStruct.txt"
c=""
d='a.txt'
e='aResult.txt'

# sourceFile = sys.argv[1]
# valuesFile = sys.argv[2]
sourceFile = a
valuesFile = b
ExtractVariablesFromFile (sourceFile, valuesFile)

你可能感兴趣的:(python)