zoukankan      html  css  js  c++  java
  • 批量删除C和C++注释

    使用Python语言 ,实现批量删除C/C++类型注释
    1.目前支持去掉.h .hpp .c .cpp .java 这些以//或/**/为注释符的源文件

    2.支持递归遍历目录

    3.当前版本为Python2.7版本,故只有安装了Python2.7(或Python3.x以下版本的才可以直接使用,测试没问题后将编出exe直接使用)

    4.使用方法:
    (1)建立源目录,如d:src(路径中最好不要有中文);
    (2)建立目标目录,如d:dst(路径中最好不要有中文);
    (3)将RemoveComment.py放到某处(路径中最好不要有中文),如d:RemoveComment.py
    (4)打开cmd,输入切换到(3)中RemoveComment.py所在的目录(这里是d:),输入python RemoveComment.py d:src d:dst,回车
    (5)去掉了注释的源代码将放在d:dst目录中

    http://download.csdn.net/download/zp373860147/4361780

    #coding:utf-8
    
    import os
    import sys
    
    def DelComment(src, dst): 
    
        fSrc = open(src, 'rb')
        fDst = open(dst, 'wb')
          
        out = []
    
        STATE_NORMAL            = 0
        STATE_BEGIN             = 1
        STATE_LINE_COMMENT      = 2
        STATE_BLOCK_COMMENT     = 3
        STATE_END               = 4
    
        State = STATE_NORMAL
        
        while 1:
            ReadInChar = fSrc.read(1)
    
            if ReadInChar == '':
                break;
            if State == STATE_NORMAL:
                if ReadInChar == '/':
                    State = STATE_BEGIN
                else:
                    out.append(ReadInChar)
            elif State == STATE_BEGIN:
                if ReadInChar == '/':
                    State = STATE_LINE_COMMENT
                elif ReadInChar == '*':
                    State = STATE_BLOCK_COMMENT
                else:
                    State = STATE_NORMAL
                    out.append('/'+ReadInChar)
            elif State == STATE_LINE_COMMENT:
                if ReadInChar == '
    ':
                    State = STATE_NORMAL
            elif State == STATE_BLOCK_COMMENT:
                if ReadInChar == '*':
                    State = STATE_END
            elif State == STATE_END:
                if ReadInChar == '/':
                    State = STATE_NORMAL
                    ReadInChar = fSrc.read(1)
                    while ReadInChar == '
    ' or ReadInChar == '
    ':
                        ReadInChar = fSrc.read(1)
                    fSrc.seek(-1, 1)
                else:
                    State = STATE_BLOCK_COMMENT
                    fSrc.seek(-1, 1)
        
        fDst.writelines(out)
        fDst.flush()
        fDst.close()
        fSrc.close()
    
    def scanDir(srcpath, dstpath):
        if os.path.isdir(srcpath):
            for files in os.listdir(srcpath):
                fSrc = os.path.join(srcpath, files)
                if os.path.isfile(fSrc):
                    scanDir(fSrc, dstpath)
                else:
                    fDst = os.path.join(dstpath, files)
                    if not os.path.exists(fDst):
                        os.mkdir(fDst)
                    scanDir(fSrc, fDst)
        else:
            if srcpath.endswith(('.h','.c','.cpp','.hpp','.jave'):
                DelComment(srcpath, os.path.join(dstpath, os.path.basename(srcpath)))
    
    if __name__ == '__main__':
        paramlen = len(sys.argv)
        if paramlen!=3:
            print '输入参数错误'
            sys.exit(1)
       
        srcpath = sys.argv[1].rstrip('\').rstrip('/')
        print 'src_path: ' + srcpath
    
        dstpath = sys.argv[2].rstrip('\').rstrip('/')
        print 'dst_path: ' + dstpath
        
        print 'convert......'
        scanDir(srcpath, dstpath)
        print 'done!'
  • 相关阅读:
    如何在xml中存储图片
    软件开发平台化催生软件产业链新层级(1)
    在sql server 中如何移动tempdb到新的位置
    Base64在XML中存储图片的解决方案
    东软金算盘VP*台:拆分标准与个性
    浪潮“楼上”开发平台简介
    python模块整理3random模块
    python模块整理1os模块
    python学习笔记18重点和忘记知识点总结
    python模块整理8glob(类似grep)和fnmatch(匹配文件名)
  • 原文地址:https://www.cnblogs.com/findumars/p/7379568.html
Copyright © 2011-2022 走看看