吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1031|回复: 2
收起左侧

[Python 转载] 检测c++代码中类是否使用过.

  [复制链接]
wzqfwzy 发表于 2020-11-13 10:22
这是很久以前一个以前的同事让我帮写的,用python写的,用正则判断,很简单,各位看看就行了

[Python] 纯文本查看 复制代码
#coding=utf-8
import os,re
file_list = {}
regex = re.compile("class\s+(.*?)\s*:")  #匹配类名的正则表达式
def get_file(path = os.getcwd()):
    list = os.listdir(path)
    for l in list:
        if os.path.isdir(path + '\\' + l) and l[0] != '.':
            get_file(path + '\\' + l)
        elif l[0] != '.' and os.path.splitext(l)[-1] in ['.cpp','.hpp','.c','.h']:
            with open(path + '\\' + l) as f:
                txt = f.readlines()
                if len(txt) != 0:
                    file_list[path + '\\' + l] = ''.join(txt)
                    print 'write ' + path + '\\' + l + ' to Memory!'

def judge_use():
    for key in file_list.keys():                    #遍历文件看
        for cls in regex.findall(file_list[key]):   #查找文件中存在的类名
            is_use = 0
            for key2 in file_list.keys():           #查找此类是否出现在其它文件中
                if key2 == key:
                    continue
                if cls in file_list[key2]:          #出现过一次就不在寻找,查找其他类名
                    is_use = 1
                    break;
            if is_use == 0:                         #一次没出现旧打印出文件名+类名
                print key + ' calss ' + cls + ' : is not use in other files'

if __name__ == '__main__':
    get_file()
    judge_use()

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

川姜一男 发表于 2020-11-13 10:44
可以,原理很清晰  哈哈,看到第一个正则就想到实现方法~~
神一样啲男人 发表于 2020-11-13 11:34
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 01:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表