Python 用于管理校对目录中的头文件

windows 下文件的大小写不敏感, 在移植windows的C/CPP程序到linux上常常报告头文件找不到

大多数情况是这样的:一个头文件名为HeadFile.h  在windows下 你可以这么写 #include "HeadFile.h"   #include "headfile.h"   #include "heAdfile.h"   #include "HEADFILE.H" 等等 ,他们在windows上都是OK的 ,但linux 下用ext 的文件系统的话他们则是大小写敏感的,显然就会出现导致找不到头文件的情况。

下面用python脚本实现 对目录中的所有h hpp c cpp 文件中的include 做检查,将生成一个正确的副本。

以下是代码(欢迎使用,如果发现BUG 可以写mail给我:hzdengxu@163.com): 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author <Dengxu>
# Bug Report To <e-mail:hzdengxu@163.com>
# Blogs <http://blog.csdn.net/dengxu11>
# Life's short. Python more!
##########################################

import sys
import os
import re

headfiles = {}
allheadfiles = []
pt = re.compile(r'(#\w*include\s+)("|<)([\w\.]+)("|>)')

def init(p):
    for (root, dirs, files) in os.walk(p):
        for f in files:
            if f.endswith(('.h', '.hpp')):
                allheadfiles.append(f)
                headfiles[f.lower()] = f

def doparse(fname, fpr, fpw):
    counter = 0;
    while True:
        line = fpr.readline()
        if len(line) == 0:
            break
        counter += 1
        match = pt.search(line)
        if match is not None:
            hfi = match.group(3)
            if hfi not in allheadfiles:
                lhfi = hfi.lower()
                if lhfi in headfiles:
                    good = headfiles[lhfi]
                    print 'File %s, Line %d is not correct! Transform it: [%s] => [%s]' % (fname, counter, hfi, good)
                    line = line.replace(hfi, good, 1)
        fpw.write(line)

def parse_curr(preffix):
    preffix = os.path.normpath(preffix)
    for (root, dirs, files) in os.walk('.'):
        for f in files:
            if f.endswith(('.h', '.hpp', '.cpp', '.c')):
                srcfile = os.sep.join((root, f))
                desfile = os.sep.join((preffix, root, f))
                d = os.path.dirname(desfile)
                if not os.path.exists(d):
                    os.system('mkdir -p ' + d)
                fpr = file(srcfile, 'rb')
                fpw = file(desfile, 'wb')
                doparse(srcfile, fpr, fpw)
                fpw.close()
                fpr.close()

def main():
    path = '.'
    args_count = len(sys.argv)
    if args_count == 2:
        path = sys.argv[1]
    if not os.path.exists(path):
        print "Invalid arguments!!"
        exit(1)
    abspath = os.path.abspath(path);
    if (abspath == '/'):
        print "Invalid arguments can not be toppest path!!"
        exit(1)
    filename = os.path.basename(abspath)
    os.chdir(path)
    init(path)
    parse_curr(os.sep.join(('..', 'OK', filename)))

if __name__ == '__main__':
    main()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值