设为首页 收藏本站
查看: 1014|回复: 0

[经验分享] 使用Python去除C/C++源程序中的所有注释和空行

[复制链接]

尚未签到

发表于 2015-4-21 05:36:57 | 显示全部楼层 |阅读模式
说明
  使用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    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   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的行不保存。这是目前感觉到的难点。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58947-1-1.html 上篇帖子: Eclipse Python插件 PyDev 下篇帖子: python天天进步(3)--字典排序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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