现在的位置: 首页 > 综合 > 正文

使用Python去除C/C++源程序中的所有注释和空行

2012年03月13日 ⁄ 综合 ⁄ 共 2815字 ⁄ 字号 评论关闭

说明

使用Python的字符串处理和正则表达式处理实现了一个删除C/C++源程序中所有注释和空行的小脚本。

使用字符串处理

   1: # delete all the comments and empty line of a C/C++ source file

   2: import os, sys,string

   3:  

   4: #-------------------------------------------------------------

   5: def usage():

   6:     print u'''

   7:     help: del_comment.py <filename | dirname>

   8:     '''

   9: #--------------------------------------------------------------

  10: def deal_file(src):

  11:     # file exist or not

  12:     if not os.path.exists(src):

  13:         print 'Error: file - %s doesn\'t exist.'% src

  14:         return False

  15:     if os.path.islink(src):

  16:         print 'Error: file - %s is a link.'

  17:         return False

  18:     filetype = (os.path.splitext(src))[1]

  19:     if not filetype in ['.c','.h','.cpp','.hh','.cc']:

  20:         return False

  21:     try:

  22:         if not os.access(src, os.W_OK):

  23:             os.chmod(src, 0664)

  24:     except:

  25:         print 'Error: you can not chang %s\'s mode.'% src

  26:  

  27:     inputf = open(src, 'r')

  28:     outputfilename = (os.path.splitext(src))[0] + '_no_comment'+filetype

  29:     outputf = open(outputfilename, 'w')    

  30:  

  31:     try:

  32:         #-----find /*.....*/

  33:         rFlag=0

  34:         line=inputf.readline()

  35:         while(line):

  36:             fm=string.find(line,'/*')

  37:             if fm!=-1:  # find a /*

  38:                 if fm>1:# not empty line

  39:                     outputf.write(line[:fm]+'\n')

  40:                 rFlag=1

  41:                 # find */

  42:                 fm=string.find(line,'*/')

  43:                 if fm!=-1:

  44:                     rFlag=0

  45:                 else:

  46:                     line=inputf.readline()

  47:                     while line:

  48:                         fm=string.find(line,'*/')

  49:                         if fm!=-1:

  50:                             rFlag=0

  51:                             break

  52:                         line=inputf.readline()

  53:                     if not line:

  54:                         print 'Match /*...*/ error'

  55:             else: # deal with //

  56:                 fm=string.find(line,'//')

  57:                 if fm==-1:

  58:                     if len(line)>1: # not empty line

  59:                         outputf.write(line)

  60:                 elif fm!=-1 and (not rFlag):

  61:                     if fm>1: # not empty line

  62:                         outputf.write(line[:fm]+'\n')

  63:             #read nextline

  64:             line=inputf.readline()

  65:     except:

  66:         print 'Error: unexcept error.'

  67:         inputf.close()

  68:         outputf.close()

  69:     return True

  70:  

  71: #--------------------------------------------------------------

  72: def deal_dir(src):

  73:     #  dir exist or not

  74:     if not os.path.exists(src):

  75:         print 'Error: dir - %s is not exist.'%s (src)

  76:         return False

  77:     filelists = os.listdir(src)

  78:     for eachfile in filelists:

  79:         eachfile = src + '/' +eachfile

  80:         if os.path.isdir(eachfile):

  81:             deal_dir(eachfile)

  82:         elif os.path.isfile(eachfile):

  83:             deal_file(eachfile)

  84:     return True

  85:  

  86: #--------------------------------------------------------------

  87: def main():

  88:     if len(sys.argv) < 2:

  89:         usage()

  90:         sys.exit(1)

  91:     src = sys.argv[1]

  92:     # get absolute dir/file path

  93:     if os.path.isdir(src):

  94:         dire = os.path.abspath(src)

  95:         dirFlag = True

  96:     elif os.path.isfile(src):

  97:         fl = os.path.abspath(src)

  98:         dirFlag = False

  99:     else:

 100:         print 'File input error'

 101:  

 102:     # deal

 103:     if dirFlag:

 104:         deal_dir(dire)

 105:     else:

 106:         deal_file(fl)

 107:     print 'Successful handle file.'

 108:  

 109: #--------------------------------------------------------------

 110: if __name__ == '__main__':

 111:     main()

使用正则表达式

   1: # delete all the comments and empty line of a C/C++ source file

   2: import os, sys,string,re,glob

   3:  

   4: # /*..*/  //...

   5: Rule1 = "(\/\*(\s|.)*?\*\/)|(\/\/.*)"

   6: c1=re.compile(Rule1)

   7:  

   8: #-------------------------------------------------------------

   9: def usage():

  10:     print u'''

【上篇】
【下篇】

抱歉!评论已关闭.