'
' 去除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;
}