#### Updated to version 1.02 thanks to comments from busysignal
#### Will now complain if replacing an existing file and refuse to replace the source file
#### A wee bit more comments (to strip ... whoopieee)
I'm posting here the final version of a little program I made to strip comments from .AU3 scripts.
I already posted an earlier version here: http://www.autoitscript.com/forum/index.php?showtopic=16925
Why would someone in their right minds would be interested in using such a program?
Well it so happens when you compile a script, you also compile the comments and extraneous material that comes with it (blank spaces, tabs, empty lines, etc. etc. etc).
Removing this can make a difference (although not that big) in the final compiled size.
It might also make a difference in the speed of the compiled code.
But one of the best upshots is if you don't want people to decompile your EXEs.
Even if they do get the code out, they will find it really hard to read!
Mind you this upshot is also it's greatest downfall. If you plan on ever decompiling your EXEs then I strongly suggest you do not use my program.
Running the program
This program follows you at every step of the stripping. You can stop or cancel nearly everywhere.
First up is a small disclaimer. Clicking OK brings you to a file selector. You can select the .AU3 file you wish to strip. As a test you can strip ... the comment stripper
Once selected, you are informed that the program will append [Stripped] to the filename of the destination file. If this doesn't suit you, you can click yes and select a custom filename. Default is no (keep the proposed filename)
Next string will tell you the stripping is complete.
That's all there is to it - you can now compile and enjoy!
Now be careful
My program works great but it has one problem. If you put a semicolon ( in the middle of the line, any caracter from there onto the end will be deleted. Usually a semicolon is an indicator of a comment but there might be cases where this would not be so.
My guess is that this should not be a problem anyways since the script will refuse to run or compile anyways.
Now as an example, compile the version with the comments and compile the version without the comments. There is a 2k difference (not much but a difference nevertheless). The bigger the program and the more the comments, the bigger the difference.
Happy ... stripping
; ==== StripComments 1.02 ==================================================== ; Strips away anything that's not absolutely necessary in an AutoIT3 ; script file. Deletes comments, regions delimiters, tabs, empty lines, ; and end-of-line comments. ; DO NOT USE IF YOU PLAN TO DECOMPILE YOUR .EXE! ; ; Author: email@example.com ; Thanks to busysignal! ; ==== DIRECTIVES ============================================================ #NoTrayIcon ; ==== VARIABLES ============================================================= Global $i_Msg, $s_Source, $s_Path, $s_Filename, $s_Destination Global $i_Err, $i_Return ; ==== MAIN PROGRAM ========================================================== ; Introduction message $i_Msg = MsgBox(1, "StripComments!", "Welcome to StripComments!" & @CRLF & "This program will strip anything that's" & @CRLF & "not absolutely necessary in a AutoIT3" & @CRLF & "script file." & @CRLF & @CRLF & "Do not strip a file if you wish to" & @CRLF & "edit it later! Stripping comments is " & @CRLF & "only meant as a last operation before" & @CRLF & "compilation." & @CRLF & @CRLF & "Press OK to continue") If $i_Msg = 2 Then Exit ; Source file loop While 1 $s_Source = FileOpenDialog("Select a AutoIT3 source file to strip", @ScriptDir, "AutoIT3 files (*.au3)") If Not @error Then ExitLoop $i_Msg = MsgBox(53, "File selector error", "Invalid file, no file selected," & @CRLF & "Esc or Cancel clicked." & @CRLF & @CRLF & "Do you want to Retry or Cancel?") If $i_Msg = 2 Then Exit WEnd ; Decomposing file @ path $s_Path = StringLeft($s_Source, StringInStr($s_Source, "\", 0, -1)) $s_Filename = StringTrimLeft($s_Source, StringInStr($s_Source, "\", 0, -1)) $s_Filename = StringLeft($s_Filename, StringInStr($s_Filename, ".", 0, -1) - 1) $s_Destination = $s_Path & $s_Filename & " [Stripped].au3" ; Output confirmation and output file loop $i_Msg = MsgBox(260, "Confirm output file", "StripComments will now strip all the comments from" & @CRLF & $s_Source & @CRLF & "and write the result to" & @CRLF & $s_Destination & @CRLF & @CRLF & "Would you like to rename the destination file?") If $i_Msg = 6 Then; Let's rename the destination file While 1; Infinite loop $s_Destination = FileOpenDialog("Select a AutoIT3 destination file", @ScriptDir, "AutoIT3 files (*.au3)") If Not @error Then If $s_Destination = $s_Source Then MsgBox(48,"Error","Cannot overwrite source file!" & @CRLF & "Please select another file.") ContinueLoop EndIf If FileExists($s_Destination) Then $i_Msg = MsgBox(308,"Warning!","Selected file already exists." & @CRLF & "Clicking Yes will overwrite this file." & @CRLF) If $i_Msg = 7 Then ContinueLoop EndIf ExitLoop Else $i_Msg = MsgBox(53, "File selector error", "Invalid file, no file selected," & @CRLF & "Esc or Cancel clicked." & @CRLF & @CRLF & "Do you want to Retry or Cancel?") If $i_Msg = 2 Then Exit EndIf WEnd EndIf ; Strip file $i_Return = _StripComments($s_Source, $s_Destination) $i_Err = @error ; Announce success or failure If Not $i_Err Then MsgBox(0, "Success!", "Stripping complete!" & @CRLF & "You can now compile" & @CRLF & $s_Destination) Else MsgBox(16, "Error!", "Stripping failed." & @CRLF & "Program returned error [" & $i_Err & "]") EndIf Exit ; ==== FUNCTIONS ============================================================= Func _StripComments($s_Source, $s_Destination) Local $as_Src, $as_Dst, $i_SrcLen, $s_Read, $h_Write Local $i_FindComment, $i_Counter = 1, $s_Semicolon = Chr(59), $i_CommentLock _FileReadToArray($s_Source, $as_Src) $i_SrcLen = $as_Src For $i_Loop = 1 To $i_SrcLen $s_Read = StringStripWS($as_Src[$i_Loop], 3); Clean up the string! ; Getting rid of blanks and comment lines If $s_Read = "" Or StringLeft($s_Read, 1) = $s_Semicolon Then ContinueLoop ; Getting rid of region identificators If StringLeft($s_Read, 7) = "#Region" Or StringLeft($s_Read, 10) = "#EndRegion" Then ContinueLoop ; Getting rid of region delimiters If StringLeft($s_Read, 3) = "#cs" Or StringLeft($s_Read, 15) = "#comments-start" Then $i_CommentLock = 1 ContinueLoop EndIf If StringLeft($s_Read, 3) = "#ce" Or StringLeft($s_Read, 13) = "#comments-end" Then $i_CommentLock = 0 ContinueLoop EndIf If $i_CommentLock Then ContinueLoop ; Finding a comment at the end of a line $i_FindComment = StringInStr($s_Read, $s_Semicolon, 0, -1); Find the semicolon, from the right If Not $i_FindComment Then $as_Dst[$i_Counter] = $s_Read; Not found so save the line as-is $i_Counter = $i_Counter + 1; Increment line counter Else $s_Read = StringLeft($s_Read, $i_FindComment - 1); Save whatever's before the colon $as_Dst[$i_Counter] = StringStripWS($s_Read, 2); And clean up trailing spaces (again!) $i_Counter = $i_Counter + 1; Increment line counter EndIf Next ReDim $as_Dst[$i_Counter + 1]; Resize $as_Dst (not absolutely necessary but nice $h_Write = FileOpen($s_Destination, 2); Open file for output For $i_Loop = 1 To $i_Counter - 1; Loop through all the lines FileWrite($h_Write, $as_Dst[$i_Loop] & @CRLF); and write them Next FileClose($h_Write); Close up open file handle EndFunc ;==>_StripComments ; ---------------------------------------------------------------------------- Func _FileReadToArray($sFilePath, ByRef $aArray) Local $hFile $hFile = FileOpen($sFilePath, 0) If $hFile = -1 Then SetError(1) Return 0 EndIf $aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF) FileClose($hFile) Return 1 EndFunc ;==>_FileReadToArray
Also attached is a Zip file containing the source code. Easier to download with IE. Enjoy!
Edited by Celeri, 01 November 2005 - 11:02 PM.