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

简介:

说明

使用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'''
  11:     help: del_comment.py <filename | dirname>
  12:     '''
  13: #--------------------------------------------------------------
  14: def deal_file(src):
  15:     # file exist or not
  16:     if not os.path.exists(src):
  17:         print 'Error: file - %s doesn\'t exist.'% src
  18:         return False
  19:     if os.path.islink(src):
  20:         print 'Error: file - %s is a link.'
  21:         return False
  22:     filetype = (os.path.splitext(src))[1]
  23:     if not filetype in ['.c','.h','.cpp','.hh','.cc']:
  24:         return False
  25:     try:
  26:         if not os.access(src, os.W_OK):
  27:             os.chmod(src, 0664)
  28:     except:
  29:         print 'Error: you can not chang %s\'s mode.'% src
  30:  
  31:     inputf = open(src, 'r')
  32:     outputfilename = (os.path.splitext(src))[0] + '_no_comment'+filetype
  33:     outputf = open(outputfilename, 'w')
  34:  
  35:     lines=inputf.read()
  36:     inputf.close()
  37:     lines=re.sub(Rule1,"",lines)
  38:     outputf.write(lines)    
  39:     outputf.close()
  40:     return True
  41:  
  42: #--------------------------------------------------------------
  43: def deal_dir(src):
  44:     #  dir exist or not
  45:     if not os.path.exists(src):
  46:         print 'Error: dir - %s is not exist.'%s (src)
  47:         return False
  48:     filelists = os.listdir(src)
  49:     for eachfile in filelists:
  50:         eachfile = src + '/' +eachfile
  51:         if os.path.isdir(eachfile):
  52:             deal_dir(eachfile)
  53:         elif os.path.isfile(eachfile):
  54:             deal_file(eachfile)
  55:     return True
  56:  
  57: #--------------------------------------------------------------
  58: def main():
  59:     if len(sys.argv) < 2:
  60:         usage()
  61:         sys.exit(1)
  62:     src = sys.argv[1]
  63:     # get absolute dir/file path
  64:     if os.path.isdir(src):
  65:         dire = os.path.abspath(src)
  66:         dirFlag = True
  67:     elif os.path.isfile(src):
  68:         fl = os.path.abspath(src)
  69:         dirFlag = False
  70:     else:
  71:         print 'File input error'
  72:  
  73:     # deal
  74:     if dirFlag:
  75:         deal_dir(dire)
  76:     else:
  77:         deal_file(fl)
  78:     print 'Successful handle file.'
  79:  
  80: #--------------------------------------------------------------
  81: if __name__ == '__main__':
  82:     main()

使用示例

 

待处理文件:

   1: #ifndef _RS232_H_
   2: #define _RS232_H_
   3:  
   4: /* the maximum number of ports we are willing to open */
   5: #define MAX_PORTS 4
   6:  
   7: /*this array hold information about each port we have opened */
   8: struct PortInfo{
   9:     int busy;
  10:     char name[32];
  11:     int handle;
  12: };
  13:  
  14: int OpenCom(int portNo,const char deviceName[],long baudRate);
  15: int CloseCom(int portNo);
  16: int ComRd(int portNo,char buf[],int maxCnt,int Timeout);
  17: int ComWrt(int portNo,const char * buf,int maxCnt);
  18:  
  19: //long GetBaudRate(long baudRate);
  20: //int OpenComConfig(int port,
  21: //                  const char deviceName[],
  22: //                  long baudRate,
  23: //                  int parity,
  24: //                  int dataBits,
  25: //                  int stopBits,
  26: //                  int iqSize,
  27: //                  int oqSize);
  28:  
  29: #endif

处理结果:

   1: #ifndef _RS232_H_
   2: #define _RS232_H_
   3: #define MAX_PORTS 4
   4: struct PortInfo{
   5:     int busy;
   6:     char name[32];
   7:     int handle;
   8: };
   9: int OpenCom(int portNo,const char deviceName[],long baudRate);
  10: int CloseCom(int portNo);
  11: int ComRd(int portNo,char buf[],int maxCnt,int Timeout);
  12: int ComWrt(int portNo,const char * buf,int maxCnt);
  13: #endif

在使用Python正则表达式处理的时候,有一个问题,就是没法删除文件中的空行。如果想删除空行,可能还得一行行读进来,把长度为0的行不保存。这是目前感觉到的难点。


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/12/09/1901349.html,如需转载请自行联系原作者


相关文章
|
2月前
|
存储 Linux iOS开发
Python入门:2.注释与变量的全面解析
在学习Python编程的过程中,注释和变量是必须掌握的两个基础概念。注释帮助我们理解代码的意图,而变量则是用于存储和操作数据的核心工具。熟练掌握这两者,不仅能提高代码的可读性和维护性,还能为后续学习复杂编程概念打下坚实的基础。
Python入门:2.注释与变量的全面解析
|
2月前
|
算法 Serverless 数据处理
从集思录可转债数据探秘:Python与C++实现的移动平均算法应用
本文探讨了如何利用移动平均算法分析集思录提供的可转债数据,帮助投资者把握价格趋势。通过Python和C++两种编程语言实现简单移动平均(SMA),展示了数据处理的具体方法。Python代码借助`pandas`库轻松计算5日SMA,而C++代码则通过高效的数据处理展示了SMA的计算过程。集思录平台提供了详尽且及时的可转债数据,助力投资者结合算法与社区讨论,做出更明智的投资决策。掌握这些工具和技术,有助于在复杂多变的金融市场中挖掘更多价值。
79 12
|
8月前
|
算法框架/工具 C++ Python
根据相机旋转矩阵求解三个轴的旋转角/欧拉角/姿态角 或 旋转矩阵与欧拉角(Euler Angles)之间的相互转换,以及python和C++代码实现
根据相机旋转矩阵求解三个轴的旋转角/欧拉角/姿态角 或 旋转矩阵与欧拉角(Euler Angles)之间的相互转换,以及python和C++代码实现
667 0
|
9月前
|
编译器 开发工具 C++
【Python】已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build
【Python】已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build
5194 0
|
6月前
|
C++ Python
探索Python与C/C++混合编程的艺术
探索Python与C/C++混合编程的艺术
147 1
|
6月前
|
Python
【python从入门到精通】-- 第二战:注释和有关量的解释
【python从入门到精通】-- 第二战:注释和有关量的解释
91 0
|
6月前
|
数据采集 前端开发 Python
Python pygame 实现游戏 彩色 五子棋 详细注释 附源码 单机版
Python pygame 实现游戏 彩色 五子棋 详细注释 附源码 单机版
153 0
WK
|
7月前
|
机器学习/深度学习 Java 程序员
为什么Python比C++慢很多?
Python相较于C++较慢主要体现在:动态类型系统导致运行时需解析类型,增加开销;作为解释型语言,逐行转换字节码的过程延长了执行时间;自动内存管理和垃圾回收机制虽简化操作但也带来了额外负担;全局解释器锁(GIL)限制了多线程性能;尽管Python库方便灵活,但在性能上往往不及C++底层库。然而,Python在某些领域如数据分析、机器学习中,凭借其高级别抽象和简洁语法仍表现出色。选语言需依据具体应用场景和需求综合考量。
WK
205 1
|
8月前
|
Unix C语言 C++
Python调用C/C++
Python调用C/C++
58 2
|
7月前
|
IDE 开发工具 Python
python3代码编程规范(命名、空格、注释、代码布局、编程建议等)
该文章详细介绍了Python3的编程规范,包括命名、空格使用、注释、代码布局等方面的最佳实践,帮助提升代码的可读性和一致性。
196 0

热门文章

最新文章

下一篇
oss创建bucket