怎么样去除C++注释的脚本?
怎么样去除C++注释的脚本?
'
' 去除CPP注释.vbs
' 2006/11/16
' Yi Wang
'
' Ver3.0.0
' 能够区分""中的注释符号。除了多行字符串:
' "a multi-line /
' String literal signals its /
' continuation with a backslash."
'
' 用法:
' 将需要处理的文件托拽到该脚本上。
'
' 注意:
' 使用脚本之前请先备份源代码文件!!!
' 脚本长时间没有响应,到任务管理器中关闭WScript.exe进程,结束脚本执行。
' 作者不为使用脚本造成的任何损失负责。
'
' 修改记录:
' 2.0.2 (1) 修改处理"/*//"类似代码时,搜索起点的BUG。
' (2) 报告错误信息时添加文件名。
' 3.0.0 (1) 添加递归文件夹的功能
' (2) 添加识别文件后缀名的功能,能识别的后缀名:H,C,CPP,CXX,INC,JAVA
' (3) 报告结果由没处理一个文件之后,改到所有都处理完毕之后。
' (4) 报告结果写入同时生成的日志中。
'-----------------------------------------------
' Global
Const VBSNAME = "去除CPP注释"
Const SHORT_NAME = "CCC"
Dim NOWFILE
RESULT_REPORT = ""
LOGFILE_NAME = SHORT_NAME & " 处理结果记录.txt" '日志文件,文件将被放到桌面上
' Usage
usage = "该脚本用于去除文件中C++代码风格的注释。" & vbCr
usage = usage & "将需要处理的文件托拽到该脚本上。" & vbCr & vbCr
usage = usage & "* 使用脚本之前请先备份源代码文件!!!" & vbCr
usage = usage & "* 作者不为使用脚本造成的任何损失负责。"
' SHELL
Set wshshell = CreateObject( "WScript.Shell" )
LOGFILE_NAME = wshshell.specialFolders("desktop") & "/" & LOGFILE_NAME
' File System Interface
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fs = CreateObject( "Scripting.FileSystemObject" )
TMPPATH = wshshell.ExpandEnvironmentStrings( "%TMP%" )
TMPFILE = TMPPATH & "/vbstmp.tmp"
' Arguments
Set args = WScript.Arguments
If args.count <= 0 Then
MsgBox usage, vbInformation, VBSNAME
WScript.Quit
Else
' 处理参数的中的文件
For Each path In args
If fs.FolderExists( path ) Then
Set fd = fs.GetFolder( path )
CleanFolder( fd )
Else
NOWFILE = path
CleanComment NOWFILE
End If
Next
End If
' 收尾工作
If fs.FileExists( TMPFILE ) Then
fs.DeleteFile( TMPFILE )
End If
' 报告处理结果
MsgBox "处理完毕", vbInformation, VBSNAME
If Not fs.FileExists( LOGFILE_NAME ) Then
fs.createTextFile( LOGFILE_NAME )
End If
Set logfile = fs.OpenTextFile( LOGFILE_NAME, ForWriting )
logfile.write RESULT_REPORT
logfile.close
wshshell.run "notepad " & LOGFILE_NAME
'*****************************
' Sub CleanFolder
Sub CleanFolder( srcFolder )
For Each file In srcFolder.Files
NOWFILE = file.path
CleanComment NOWFILE
Next
For Each fd In srcFolder.subFolders
CleanFolder fd
Next
End Sub
'*****************************
' Sub CleanComment
Sub CleanComment ( path )
'##. 文件类型检查
If Not CheckExtName( fs.GetExtensionName( path ) ) Then
Exit Sub
End If
report = ""
'##. 检查该文件是否存在,不存在exit sub
If Not fs.FileExists(path) Then
report = path & " 文件不存在!"
MsgBox report, vbCritical, VBSNAME
Exit Sub
End If
'##. 源文件拷贝到 TMPFILE
fs.CopyFile path, TMPFILE
'##. 打开文件
Set srcfile = fs.OpenTextFile( TMPFILE, ForReading )
Set desfile = fs.OpenTextFile( path, ForWriting )
'##. 删除注释
lineCnt = 0
codeline = ""
bInBlock = False
Do While srcfile.AtEndOfStream <> True
' 读取
codeline = srcfile.ReadLine
' 以行为单位处理代码
CleanCodeLine codeline, bInBlock
' 写入
desfile.WriteLine codeline
lineCnt = lineCnt + 1
Loop
'##. 关闭文件
srcfile.close
desfile.close
'## 通报结果
report = path & vbCrLf & vbTab & "处理完毕。" & vbCrLf
report = report & vbTab & "扫描 " & lineCnt & " 行。" & vbCrLf
RESULT_REPORT = RESULT_REPORT & report
End Sub
'**********************************
' Sub CleanCodeLine
Sub CleanCodeLine( ByRef codeline, ByRef bInBlock )
' block comment pointer
pta = 0
pta2 = 0
' line comment pointer
ptb = 0
' comma pointer
ptc = 0
' buffer line
bufline = ""
If bInBlock Then
' 在注释块/**/中
pta2 = InStr( codeline, "*/" )
If pta2 <= 0 Then
codeline = ""
Else
codeline = Right( codeline, Len(codeline) - pta2 - 1 )
bInBlock = False
CleanCodeLine codeline, bInBlock
End If
Else
' 不在注释块中
pta = InStr( codeline, "/*" )
ptb = InStr( codeline, "//" )
ptc = InStr( codeline, """" )
pta2 = InStr( codeline, "*/" )
If pta2 > 0 And pta2 < pta Then
' 代码错误 /* */ 不匹配
MsgBox NOWFILE & vbCr & "代码错误 /* */ 不匹配!脚本中止!", vbCritical, VBSNAME
WScript.Quit
End If
Do While ( pta <> 0 ) Or ( ptb <> 0 )
' 存在注释
bSwCh = False
If ptc > 1 Then
If Mid(codeline, ptc-1, 1) = "/" Then
bSwCh = True
End If
End If
bCommented = False
If ptc > 0 Then
If ( pta > 0 And pta < ptc ) Or ( ptb > 0 And ptb < ptc ) Then
bCommented = True
End If
End If
If ( ptc > 0 ) And ( Not bSwCh ) And ( Not bCommented ) Then
' 存在字符串
bufline = bufline & Left( codeline, ptc )
codeline = Right( codeline, Len(codeline) - ptc )
strlen = Len( codeline )
For i = 1 To strlen
bSwCh = False
If i > 1 Then
If Mid( codeline, i-1, 1 ) = "/" Then
bSwCh = True
End If
End If
If ( Mid( codeline, i, 1 ) = """" ) And ( Not bSwCh ) Then
Exit For
End If
Next
If i > strlen Then
' 该行没找到下一个字符串界定符号",代码有误
MsgBox NOWFILE & vbCr & "字符串界定符号不匹配!脚本中止!", vbCritical, VBSNAME
WScript.Quit
Else
' 将字符串的内容放入buffer line
ptc = i
bufline = bufline & Left( codeline, ptc )
codeline = Right( codeline, Len(codeline) - ptc )
End If
pta = InStr( codeline, "/*" )
ptb = InStr( codeline, "//" )
ptc = InStr( codeline, """" )
ElseIf ( pta = 0 ) Or ( ptb <> 0 And pta > ptb ) Then
codeline = Left( codeline, ptb - 1 )
Exit Do
Else
pta2 = InStr( pta+2, codeline, "*/" )
If pta2 <= 0 Then
bInBlock = True
codeline = Left( codeline, pta - 1 )
Exit Do
Else
If pta > pta2 Then
' 代码错误 /* */ 不匹配
MsgBox NOWFILE & vbCr & "代码错误 /* */ 不匹配!脚本中止!", vbCritical, VBSNAME
WScript.Quit
End If
codeline = Left( codeline, pta - 1) & Right( codeline, Len(codeline) - pta2 - 1 )
pta = InStr( codeline, "/*" )
ptb = InStr( codeline, "//" )
ptc = InStr( codeline, """" )
End If
End If
Loop
codeline = bufline & codeline
pta2 = InStr( codeline, "*/" )
If pta2 > 0 Then
' 代码错误 /* */ 不匹配
MsgBox NOWFILE & vbCr & "代码错误 /* */ 不匹配!脚本中止!", vbCritical, VBSNAME
WScript.Quit
End If
End If
End Sub
'*******************************
' Function CleanLineComment
Function CleanLineComment( codeline )
CleanLineComment = Left( codeline, InStr( codeline, "//" ) - 1 )
End Function
'*******************************
' Function CheckExtName
' 能识别的后缀名:H,C,CPP,CXX,INC,JAVA
Function CheckExtName( extname )
extname = UCase( extname )
If extname = "H" Then
CheckExtName = True
ElseIf extname = "C" Then
CheckExtName = True
ElseIf extname = "CPP" Then
CheckExtName = True
ElseIf extname = "CXX" Then
CheckExtName = True
ElseIf extname = "INC" Then
CheckExtName = True
ElseIf extname = "JAVA" Then
CheckExtName = True
Else
CheckExtName = False
End If
End Function
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
用来测试用的cpp文件:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello, world/n");
[1]-------------------------------------------------------
block comment - a line
/*block comment - a line*/
[2]-------------------------------------------------------
block comment - multilines
/*line1
line2
line3*/
[3]-------------------------------------------------------
block comment - nest itself
drawString( g/*Graphics*/, x/*x-coordinate*/, y/*yy*/, 0 );/*ssssdijfoas
ndzxc*/
[4]-------------------------------------------------------
line comment
setences();//line comment
//line comment2
[5]-------------------------------------------------------
comment nested.
nested1;//cccccc/*--
nested2;//---*/
nested3;/*cccccccccccc // line comment symbol
ccccccccc//ccccccccccc*/
nested4./*ccccccc*/Part1;/*ccccc // line comment symbol
ccccccccc//ccccccccccc*/
nested5./*ccccccc*/Part1./*ccccc*/Part2;// line comment symbol
[6]--------------------------------------------------------
String s1 = "//not lien comment";
String s2 = "/*not block comment*/";
String s3 = "http://www.url.net";// "http://www.urlOld.net";
return 0;
}