Jump to content

Proposed utility: StripComments


Celeri
 Share

Recommended Posts

#### Edited Nov. 1, 2005

#### Updated to version 1.02 thanks to comments from busysignal :o

#### Will now complain if replacing an existing file and refuse to replace the source file :graduated:

#### 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 (:D 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 B) 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 :x

; ==== 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: celeri@videotron.ca
; 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[99999], $as_Dst[99999], $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[0]
    
    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!

StripComments_102.zip

Edited by Celeri

I am endeavoring, ma'am, to construct a mnemonic circuit using stone knives and bearskins.SpockMy UDFs:Deleted - they were old and I'm lazy ... :)My utilities:Comment stripperPolicy lister 1.07AutoIT Speed Tester (new!)

Link to comment
Share on other sites

@Celeri, it is an interesting idea but here is something you can add to you script. Make a copy of the script you are about to strip the comments so it does not affect the original file. This will make this idea a bit more acceptable to people who do not want to modify the orignal source file.

What do you think?

Cheers.. B)

Link to comment
Share on other sites

@Celeri, it is an interesting idea but here is something you can add to you script. Make a copy of the script you are about to strip the comments so it does not affect the original file. This will make this idea a bit more acceptable to people who do not want to modify the orignal source file.

What do you think?

Cheers.. B)

It's a good idea but my script does not affect the original file - it makes a new file containing the stripped script.

Example:

A folder contains:

My_script.au3

After using my program (and using the default settings)

The folder now contains

My_script.au3 (the original, non-modified script)

and

My_script_[stripped].au3

What I might do however is a simple fail-safe to make sure no one ever forces my program to write the stripped script ON TOP OF the original script. Shouldn't be possible to do anyways but I've seen weirder things :o

Btw, thanks for the feedback!

I am endeavoring, ma'am, to construct a mnemonic circuit using stone knives and bearskins.SpockMy UDFs:Deleted - they were old and I'm lazy ... :)My utilities:Comment stripperPolicy lister 1.07AutoIT Speed Tester (new!)

Link to comment
Share on other sites

It's a good idea but my script does not affect the original file - it makes a new file containing the stripped script.

Example:

A folder contains:

My_script.au3

After using my program (and using the default settings)

The folder now contains

My_script.au3 (the original, non-modified script)

and

My_script_[stripped].au3

What I might do however is a simple fail-safe to make sure no one ever forces my program to write the stripped script ON TOP OF the original script. Shouldn't be possible to do anyways but I've seen weirder things :graduated:

Btw, thanks for the feedback!

Sounds good to me. It was a minor concern not to corrupt what you were trying to modify. B) I think adding that one thing will be a good safety precaution..

Cheers.. :o

Link to comment
Share on other sites

@busysignal

I'll implement the check sometime this week, as soon as I get rid of this !&*?*! spyware on my client's computer. I'm amazed they don't put people in jail for making this crap! But they're willing to put you in the slammer for years if you make an MP3... go figure.

Isn't this stuff stripped at compile time anyway?

Well in theory it is ...

Ok there's two aspects to this so let me answer as best I can (or you could wait for Valik to come in and tell you my program is utterly useless ahahahah).

When you compile your script, the comments are compressed WITH the file. So if you take away the comments, obviously, the filesize *should* be smaller. That's the main point of this program.

The other aspect is performance. I have no proof it actually make a script faster (I havent taken the time to check) but the logic is:

While running a compiled program, the interpreter has to "set aside" the comments in order not to run them. I figure there's at least a marginal speed increase if it doesn't have to deal at all with comments, tabs, empty lines, etc. etc. etc..

I mean, don't expect anything massive but you might get a few percentage points ... (hey, what do you expect for free ahahaha).

And to be completely honest I havent had the time to actually make a benchmark.

So I guess on that aspect your mileage may vary.

Edited by Celeri

I am endeavoring, ma'am, to construct a mnemonic circuit using stone knives and bearskins.SpockMy UDFs:Deleted - they were old and I'm lazy ... :)My utilities:Comment stripperPolicy lister 1.07AutoIT Speed Tester (new!)

Link to comment
Share on other sites

@busysignal

I'll implement the check sometime this week, as soon as I get rid of this !&*?*! spyware on my client's computer. I'm amazed they don't put people in jail for making this crap! But they're willing to put you in the slammer for years if you make an MP3... go figure.

We'll look forward to it.. Have fun with the **ware.. :o doing that myself at the moment..

cheers.. B)

Link to comment
Share on other sites

We'll look forward to it.. Have fun with the **ware.. :o doing that myself at the moment..

cheers.. B)

Version 1.02 out :graduated:

Should be the last one! (famous last words obviously!)

(Check the link in the first post)

Celeri

P.S.: Just be careful - some horrorware install hard to detect services.

Edited by Celeri

I am endeavoring, ma'am, to construct a mnemonic circuit using stone knives and bearskins.SpockMy UDFs:Deleted - they were old and I'm lazy ... :)My utilities:Comment stripperPolicy lister 1.07AutoIT Speed Tester (new!)

Link to comment
Share on other sites

@Celeri, I read your comments and your way of handling comments in the middle of the line is fair. People would not be putting any needed information after a comment to End-Of-Line otherwise it will be commented out via the UI or Scite.

Like the update..

Cheers.. B)

P.S. I don't P.S. alot so here it goes. In programming, a project is never really done - just always being enhanced.. :o

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...