Remark Remover (WIP) - Pete - 12-18-2024
This works as posted, but I intend to slowly add more to it.
Currently it will make a new file with a _nc addition. _nc for no comments.
Example: Pete.bas gets a new uncommented file made called: Pete_nc.bas
It also makes a _remarks file, with all the remarks listed.
So we get our 1 untouched original file and 2 new files in our example...
Pete.bas
Pete_nc
Pete_remarks.bas
It has an overwrite alert, so if by the smallest possibility you already have a file with same name and a _nc or _remarks, it will alert you. The same is true if you are running it multiple times selecting the same file again and again.
Code: (Select All)
' Creates a new file with added _nc suffix void of all remark lines and a _remarks file with all the remarks.
_Title "Remark Remover"
Do
Print "Open file dialog..."
BasIn$ = _OpenFileDialog$("Remove Comments From File:", "", "*.bas|*.bi|*.bm", "QB64PE files", 0)
Cls
If BasIn$ <> "" Then
period = _InStrRev(BasIn$, ".")
BasOut$ = Left$(BasIn$, period - 1) + "_nc" + Mid$(BasIn$, period)
dir$ = Mid$(BasOut$, 1, _InStrRev(BasOut$, "\"))
file$ = Mid$(BasOut$, _InStrRev(BasOut$, "\") + 1)
If _FileExists(BasOut$) Then
j = _MessageBox(" Alert!", "File " + file$ + " already exists." + Chr$(13) + Chr$(10) + Chr$(13) + Chr$(10) + "Overwrite?", "yesnocancel")
Select Case j
Case 2: Run
Case 0: System
End Select
End If
Open BasIn$ For Binary As #1
a$ = Space$(LOF(1))
Get #1, , a$
Close #1
Open BasOut$ For Output As #2
Do
i = i + 1
x$ = Mid$(a$, i, 1)
If Asc(x$) > 32 Then col = col + 1
x2$ = LCase$(Mid$(a$, i, 4))
If Mid$(x2$, 1, 1) = "'" Then
If LTrim$(Mid$(a$, i + 2, 1)) = "$" Then x2$ = ""
ElseIf x2$ = "rem " Then
If Mid$(a$, i + 4, 1) <> "$" Then x2$ = "'" Else x2$ = ""
End If
If LCase$(Mid$(a$, i, 5)) = "data " Then dta = -1
Select Case Mid$(x2$, 1, 1)
Case "'"
If quote = 0 And dta = 0 Then
q = InStr(i, a$ + Chr$(13), Chr$(13))
If col = 1 Then lineout% = 2 Else lineout% = 0
temp$ = Mid$(a$, i, q + lineout% - i)
For k = 1 To Len(temp$)
If LCase$(Mid$(temp$, k, 1)) >= "a" And LCase$(Mid$(temp$, k, 1)) <= "z" Then flag = 1: Exit For
Next
If flag Then ' Only keep remarks with letters a-z.
cb$ = cb$ + Mid$(a$, i, q + lineout% - i) + Chr$(13) + Chr$(10): flag = 0
End If
i = q + lineout% - 1
new$ = RTrim$(new$)
If Right$(new$, 1) = ":" Then
temp$ = Mid$(new$, _InStrRev(Chr$(10) + new$, Chr$(10)))
If InStr(temp$, " ") Then ' Only remove if it is not a label. (solid characters ending in a colon).
new$ = Mid$(new$, 1, Len(new$) - 1) ' Remove trailing colon.
End If
End If
col = 0
_Continue
End If
Case Chr$(34)
If dta = 0 Then quote = 1 - quote
Case ":"
If dta Then dta = 0
Case Chr$(13), Chr$(10)
quote = 0: col = 0: dta = 0
End Select
new$ = new$ + x$
Loop Until i >= Len(a$)
Print #2, new$
Close #2
If _FileExists(Left$(BasIn$, period - 1) + "_remarks" + Mid$(BasIn$, period)) Then
If _MessageBox(" Alert!", "File " + Left$(BasIn$, period - 1) + "_remarks" + Mid$(BasIn$, period) + " already exists." + Chr$(13) + Chr$(10) + Chr$(13) + Chr$(10) + "Overwrite to update contents?", "yesno") = 1 Then
Open Left$(BasIn$, period - 1) + "_remarks" + Mid$(BasIn$, period) For Output As #1: Print #1, cb$: Close #1
End If
End If
Open Left$(BasIn$, period - 1) + "_remarks" + Mid$(BasIn$, period) For Output As #1: Print #1, cb$: Close #1
a$ = "File: " + BasOut$
Locate 5, _Width \ 2 - Len(a$) \ 2: Print a$
Locate 7
Locate , _Width \ 2 - 4
Print "Options:": Print
j = _Width \ 2 - 16
Locate , j: Print "Press 1 to Open in IDE"
Locate , j: Print "Press 2 to Open in Notepad"
Locate , j: Print "Press 3 to View Remarks Removed"
Locate , j: Print "Press 4 to Show in File Explorer"
Locate , j: Print "Press 5 to Rerun"
Locate , j: Print "Press Esc to Quit"
Do
_Limit 30
b$ = InKey$
If Len(b$) Then
Select Case b$
Case "1": Shell _DontWait _Hide "QB64pe.exe " + Chr$(34) + BasOut$ + Chr$(34)
Case "2": Shell _DontWait "notepad " + dir$ + file$
Case "3": Shell _DontWait "notepad tmp.tmp"
Case "4": nul$ = _OpenFileDialog$("Here's your Unremarkable file!", BasOut$)
Case "5": Cls: Run
Case Chr$(27): System
End Select
End If
Loop
Exit Do
Else
Exit Do
End If
Loop
System
Pete
|