Posts: 2,177
Threads: 222
Joined: Apr 2022
Reputation:
104
I updated mine and included a test file.
I'm old school, that's for sure. I do love the new QB64 features that cut out a lot of code but I'm just set in may ways about coding the routines from scratch. It's all I can do to stop myself from just writing the routine in machine code! j/k. Only did that headache once.
Steve, if you run my test code with your routine, you will see you need to add a few things...
Test file results after running Steve's routine...
---------------------------------------------------------------------------
Rem $Dynamic
Print " ' this should not be removed. Rem 123" rem this should be removed.
a$ = "apple":
b$ = "orange": Rem orange
c$ = "pear"
d$ = "banana" Rem banana
Rem line 3
Data don
Data "don't"
Data "don't":
Data "don't": Rem remark.
Data don
Data don
---------------------------------------------------------------------------
Test file results after running Pete's routine...
---------------------------------------------------------------------------
' $Dynamic
Rem $Dynamic
Print " ' this should not be removed. Rem 123"
a$ = "apple"
b$ = "orange"
c$ = "pear"
d$ = "banana"
Data don't
Data "don't"
Data "don't"
Data "don't"
Data don't ' remark is part of data and should stay.
Data don't rem remark is part of data and should stay, too.
----------------------------------------------------------------------------
The test file is posted in post #1, but I'll repost it here so it's easy to see
Test file --------------------------
' $Dynamic
Rem $Dynamic
' Simple rem line.
' "***": for i = 1 to 5
Print " ' this should not be removed. Rem 123" rem this should be removed.
a$ = "apple": ' apple
b$ = "orange": Rem orange
c$ = "pear" ' pear
d$ = "banana" Rem banana
' line 1
' line 2
Rem line 3
Data don't
Data "don't"
Data "don't": ' remark.
Data "don't": Rem remark.
Data don't ' remark is part of data and should stay.
Data don't rem remark is part of data and should stay, too.
---------------------------------------------------------------------------
The random indentations were made to make sure the routines can handle indenting.
If you guys find any errors in mine, let me know. There's a good chance I may have missed something. Actually Steve got ' $ for meta commands, and I added Rem $, which was also used back in the day. I love it that forums provide more hands on deck to make improving a project easier. Thanks guys,
Pete
Posts: 2,697
Threads: 327
Joined: Apr 2022
Reputation:
217
I didn't code mine to look for REM or DATA at all. I figure those are generally edge cases and not something which I tend to code into my own stuff very often.
If DATA has colons in it, then they should be inside quotes, INHO. If not, then you're ust asking for troubles.
It'd be easy enough to expand upon, if one needed to, but I think it does everything I need for my personal use.
Personally, I think before I tried to remove remarks from code like you're testing, I'd first do a simple Find-and-Replace for " REM " and replace that with " ' ".
Posts: 2,177
Threads: 222
Joined: Apr 2022
Reputation:
104
10-26-2024, 11:03 PM
(This post was last modified: 10-27-2024, 01:28 AM by Pete.)
Basically that replace Rem with ' is what mine is essentially doing.
Now back in the day, we were a pretty tough bunch. Find one little oopsie in a routine and 10 members were eating your lunch! Ah, the good ol' days. No participation trophies.
Okay, I'll comprise, for our kids sake: @Dimster, here is a combo of Pete and Steve's remark remover. It uses Steve's new age QB64 cool way to look up your file, with my remove remarks code. It's EXCELLENT!
Code: (Select All)
_Title "Pete and Steve's Excellent Adventure"
Do
BasIn$ = _OpenFileDialog$("File to Remove Comments From", "", "*.bas|*.bi|*.bm", "QB64PE files", 0)
If BasIn$ <> "" Then
period = _InStrRev(BasIn$, ".")
BasOut$ = Left$(BasIn$, period - 1) + "_nc" + Mid$(BasIn$, period)
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
i = q + lineout% - 1
new$ = RTrim$(new$)
If Right$(new$, 1) = ":" Then
new$ = Mid$(new$, 1, Len(new$) - 1) ' Remove trailing colon.
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$
Shell _DontWait _Hide "QB64pe.exe " + BasOut$
Rem Shell _DontWait _Hide "notepad " + BasOut$
Rem _OpenFileDialog$("Here's your RemLess file!", BasOut$)
Close #2: Exit Do
Else
Exit Do
End If
Loop
System
Well, it beats the hell out of anything Bill (Sheldon) and Ted (Clippy) could ever put together.
Pete
For Phil. Rem/Unrem any of the last few lines for how you would like to gain access to the new file.
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
10-26-2024, 11:26 PM
(This post was last modified: 10-26-2024, 11:27 PM by PhilOfPerth.)
(10-26-2024, 11:03 PM)Pete Wrote: Basically that replace Rem with ' is what mine is essentially doing.
Now back in the day, we were a pretty tough bunch. Find one little oopsie in a routine and 10 members were eating your lunch! Ah, the good ol' days. No participation trophies.
Okay, I'll comprise, for our kids sake: @Dimster, here is a combo of Pete and Steve's remark remover. It uses Steve's new age QB64 cool way to look up your file, with my remove remarks code. It's EXCELLENT!
Code: (Select All)
_Title "Pete and Steve's Excellent Adventure"
Do
BasIn$ = _OpenFileDialog$("File to Remove Comments From", "", "*.bas|*.bi|*.bm", "QB64PE files", 0)
If BasIn$ <> "" Then
period = _InStrRev(BasIn$, ".")
BasOut$ = Left$(BasIn$, period - 1) + "_nc" + Mid$(BasIn$, period)
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
i = q + lineout% - 1
new$ = RTrim$(new$)
If Right$(new$, 1) = ":" Then
new$ = Mid$(new$, 1, Len(new$) - 1) ' Remove trailing colon.
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: Exit Do
Else
Exit Do
End If
Loop
System
Well, it beats the hell out of anything Bill (Sheldon) and Ted (Clippy) could ever put together.
Pete
Very nice! But it would be nicer if it told you it was successful, and the name of the new file. I was looking at the one created prevously (by Steve's version), not realizing it had a different name, and could see no improvements..
Posts: 2,177
Threads: 222
Joined: Apr 2022
Reputation:
104
@ PhilOfPerth
Well, there's at least one of that ******* tough crowd from days past I mentioned. Okay, okay, okay... Now it opens the new code in the QB64 IDE.
Pete
Shoot first and shoot people who ask questions, later.
Posts: 383
Threads: 56
Joined: Apr 2022
Reputation:
13
Steve - I make the change to line 26 as you suggested but now getting an invalid name error on that line. I believe the old line 26 simply read "Print #2, o$". Is that correct?
Pete - not sure what I'm doing wrong but seems I'm constantly getting "File Not Found". Something seems to be off with path?
Posts: 2,697
Threads: 327
Joined: Apr 2022
Reputation:
217
10-27-2024, 01:04 PM
(This post was last modified: 10-27-2024, 01:08 PM by SMcNeill.)
(10-26-2024, 09:42 PM)SMcNeill Wrote: Change line 26 to:
If _TRME$(o$) <> "" _OrElse temp$ = "" Then Print #1, o$
That should get rid of those blank lines for you, while preserving blank lines which existed originally in your code between SUBS or whatever.
(10-27-2024, 12:49 PM)Dimster Wrote: Steve - I make the change to line 26 as you suggested but now getting an invalid name error on that line. I believe the old line 26 simply read "Print #2, o$". Is that correct?
Pete - not sure what I'm doing wrong but seems I'm constantly getting "File Not Found". Something seems to be off with path?
Invalid name is correct, as I can't type on the iPad worth a hoot at 2am.
Try this:
If _TRIM$(o$) <> "" _OrElse temp$ = "" Then Print #2, o$
Whole version with the change to completely eliminate those extra lines for you:
Code: (Select All)
Do
BasIn$ = _OpenFileDialog$("File to Remove Comments From", "", "*.bas|*.bi|*.bm", "QB64PE files", 0)
If BasIn$ <> "" Then
period = _InStrRev(BasIn$, ".")
BasOut$ = Left$(BasIn$, period - 1) + " (No Rem)" + Mid$(BasIn$, period)
Open BasIn$ For Binary As #1
Open BasOut$ For Output As #2
Do Until EOF(1)
Line Input #1, temp$
in_quote = 0
For p = 1 To Len(temp$)
m$ = Mid$(temp$, p, 1)
Select Case m$
Case Chr$(34) 'quote
in_quote = Not in_quote
Case "'" 'Remark character
If Not in_quote Then 'remark found
If p = 1 _Andalso Mid$(temp$, 2, 1) = "$" Then p = Len(temp$) + 1 'it's a metacommand
Exit For
End If
Case "$"
If p = 1 Then p = Len(temp$) + 1 'it's a metacommand
End Select
Next
o$ = Left$(temp$, p - 1)
If _Trim$(o$) <> "" _Orelse temp$ = "" Then Print #2, o$
Loop
Else System
End If
Loop
Posts: 383
Threads: 56
Joined: Apr 2022
Reputation:
13
Like a charm Steve, like a charm. Cleans a programs' comments up so nicely.
|