QB64 Phoenix Edition
Remove Remarks for Programs - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Remove Remarks for Programs (/showthread.php?tid=3169)

Pages: 1 2


Remove Remarks for Programs - Pete - 10-26-2024

For @Dimster.  (Beta Version).

Note: Formerly incorrectly titled as a Remline clone. Thanks ahenry for the heads up!

This one lets you use the clipboard to add the path and file name, but if you prefer to just open a dialog box and select the file, I combined my remover with Steve's open dialog in post #14, here: https://qb64phoenix.com/forum/showthread.php?tid=3169&pid=29513#pid29513

Code: (Select All)
_Title "Pete's Remark-Disable"
Width 80, 25
_Font 16
_ScreenMove 20, 0
Dim i As _Integer64
Dim col As Integer
_Clipboard$ = "C:\qb64pe\steve-junk.bas"
If Len(_Clipboard$) > 100 Then
    Print "Warning: This application will erase your current clipboard contents."
    Print: Print "Press Enter to continue or Esc to quit..."
    Do
        b$ = InKey$
        If Len(b$) Then
            If b$ = Chr$(27) Then System
            If b$ = Chr$(13) Then Exit Do
        End If
        _Limit 60
    Loop
    _Clipboard$ = Chr$(0)
Else
    hold$ = _Clipboard$
    _Clipboard$ = Chr$(0)
End If
Cls
Do
    redo1:
    While -1
        Locate 1, 1: View Print CsrLin To _Height - 1: Cls 2: View Print
        Locate 1, 1: Print "Paste directory name: ": Locate 3, 1, 1, 7, 30
        Do
            _Limit 60
            b$ = InKey$
            If Len(b$) Then
                _Limit 10
                Select Case b$
                    Case Chr$(27): System
                    Case Chr$(13): dir1$ = _CWD$: Exit Do
                    Case Chr$(32): GoSub manual: If x$ = "" Then redo = 1: b$ = Chr$(8): Exit While
                End Select
            End If
            If Len(_Clipboard$) Then GoSub clip: dir1$ = x$: Exit Do
        Loop
        _Clipboard$ = Chr$(0)
        If Right$(dir1$, 1) <> "\" Then dir1$ = dir1$ + "\"
        Locate 3, 1: Print dir1$
        If _DirExists(dir1$) Then Exit While Else Print "Directory not found: "; dir1$; " Any key to redo...";: Sleep
    Wend
    If b$ = Chr$(8) Then Exit Do
    redo2:
    While -1
        Locate 5, 1: View Print CsrLin To _Height - 1: Cls 2: View Print
        Locate 5, 1: Print "Paste file name: ": Locate 7, 1
        If Len(filehold$) Then
            file1$ = filehold$: filehold$ = ""
        Else
            Do
                _Limit 60
                b$ = InKey$
                If Len(b$) Then
                    Select Case b$
                        Case Chr$(27): System
                        Case Chr$(8): redo = 1: Exit While
                        Case Chr$(32): GoSub manual: If x$ = "" Then redo = 2: b$ = Chr$(8): Exit While
                    End Select
                End If
                If Len(_Clipboard$) Then GoSub clip: file1$ = x$: Exit Do
            Loop
        End If
        _Clipboard$ = Chr$(0)
        If InStr(file1$, ".") = 0 Then file1$ = file1$ + ".bas"
        Print file1$
        If _FileExists(dir1$ + file1$) Then Exit Do Else Print "File not found: "; dir1$ + file1$; " Any key to redo...";: Sleep
    Wend
    If b$ = Chr$(8) Then Exit Do
Loop
If b$ = Chr$(8) Then GoTo redo1
If _FileExists(dir1$ + file1$) Then Else Print: Print "Error. File not found: "; dir1$ + file1$: End
Open dir1$ + file1$ For Binary As #1
a$ = Space$(LOF(1))
Get #1, , a$
Close #1
quote = 0: col = 0: i = 0: new$ = ""
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$)
_Clipboard$ = a$
GoSub makefile
_KeyClear
Sleep 5
System

clip:
x$ = _Trim$(_Clipboard$)
If InStr(x$, Chr$(13)) Then x$ = Mid$(x$, 1, InStr(x$, Chr$(13)) - 1)
If InStr(x$, ".") And InStr(x$, "\") <> 0 Then
    If dir1$ = "" Then
        filehold$ = Mid$(x$, _InStrRev(x$, "\") + 1)
        x$ = Mid$(x$, 1, _InStrRev(x$, "\"))
    ElseIf dir2$ = "" Then
        filehold$ = Mid$(x$, _InStrRev(x$, "\") + 1)
        x$ = Mid$(x$, 1, _InStrRev(x$, "\"))
    End If
End If
Return

manual:
y = CsrLin: x = 12
Line Input "Type name: ", x$: Locate y, x
If Len(x$) Then _Clipboard$ = x$ Else _Clipboard$ = Chr$(0)
Return

makefile:
file1_nc$ = Mid$(file1$, 1, InStr(file1$, ".") - 1) + "_nc" + Mid$(file1$, InStr(file1$, "."))
If _FileExists(dir1$ + file1_nc$) Then
    x$ = "File: " + file1_nc$ + " already exists. Overwrite? Y/N: "
    Print: Print x$;
    Do
        _Limit 30
        b$ = InKey$
        If Len(b$) Then
            Select Case UCase$(b$)
                Case "Y": Print "Y": Exit Do
                Case "N", Chr$(27): Print: Print "File creation aborted by user...": End
            End Select
        End If
    Loop
End If
Open dir1$ + file1_nc$ For Output As #3
Print #3, new$
Close #3
Print: Print "Loading "; file1_nc$
Shell _DontWait _Hide "QB64pe.exe " + file1_nc$
Rem Shell _DontWait _Hide "notepad " + file1_nc$
Return

Utility to remove remarks and make a new file using your old file name + "_nc" added for "no comments."
It will not overwrite your original file and warns if you are going to overwrite a "_nc" file. (Like if you run it more than once).

1) Run program.

2) Go to File Explorer and copy the path to the clipboard. It will be included on the screen. You can also just press Enter if you are working out of the QB64 folder, or you can press the spacebar and it will let you type the path, manually.

3) Do the same for the file name. For .bas files you do not ave to add the extension.

4) Press any key to begin.

It will make the new 'remarkless' version and open it in QB64. If you don't want that 'feature' just REMARK out the SHELL statement.

This is a beta version, so let me know if you find any bugs or a remark situation it can't handle. Oh, it does remove trailing colons, and lines that are fully remarked, and it also skips removal for any ' or Rem keywords inside quotes. What I didn't add is the remote instances where QB64 programs have that trailing _ symbol to line wrap code that is very long.

test file: Name as: "junkme.bas"

Code: (Select All)
  ' $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.
Pete


RE: Remline clone for QB64. - ahenry3068 - 10-26-2024

We wouldn't be programmers if we didn't reinvent the wheel once in a while.   However, I had need last year of a REMLINE program and I found that the original REMLINE from the Qbasic in MS-Dos 5.0 compiles just fine and with no necessary changes in QB64PE.     I actually did change exactly 1 line of code.    CONST MAXLINES=400 I changed to CONST MAXLINES = 40000 .


RE: Remline clone for QB64. - ahenry3068 - 10-26-2024

I guess Pete is not using this the same way I thought.   His is remove Remarks instead of Remove Lines.

Anyway.  If anyone does need to remove some Line numbers from BASIC code the original REMLINE works fine for that.


RE: Remline clone for QB64. - Pete - 10-26-2024

I'll pony up a + 1 for that info. Damn conflating. It's been so long I got the REM in REMLINE confused with remarks instead of Remove line numbers. 20 some years back, I made one of those in QBasic. So I'm off to change the title so others don't catch my aging issues. Thanks.

Pete

- Imagine my embarrassed face. Angry  (Well, it's the closest thing we have. I mean, hey, it's red!)


RE: Remline clone for QB64. - ahenry3068 - 10-26-2024

(10-26-2024, 04:02 AM)Pete Wrote: I'll pony up a + 1 for that info. Damn conflating. It's been so long I got the REM in REMLINE confused with remarks instead of Remove line numbers. 20 some years back, I made one of those in QBasic. So I'm off to change the title so others don't catch my aging issues. Thanks.

Pete

- Imagine my embarrassed face. Angry  (Well, it's the closest thing we have. I mean, hey, it's red!)
   Pete,

        I feel your pain.   To make matters worse if you were doing work in QBasic only 20 years ago you were already way out of date.    MS-Dos 5.0 (and QBasic with it) was released in June of 1991`over 30 years ago.   QuickBasic that it was based on was released 6 years prior to that.   I'm allowed to poke a little fun.  I'm guessing we are probably no further apart in age than about 10 years at very most.    The BASIC programming language is 14 month's younger than I am.


RE: Remove Remarks for Programs - SMcNeill - 10-26-2024

That seems like an awful lot of code to do something really simple @Pete 

How's this work 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)
Print #2, o$
Loop
Else System
End If
Loop



RE: Remove Remarks for Programs - PhilOfPerth - 10-26-2024

(10-26-2024, 08:16 AM)SMcNeill Wrote: That seems like an awful lot of code to do something really simple @Pete 

How's this work 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)
            Print #2, o$
        Loop
    Else System
    End If
Loop

Well, that worked (surprise, surprise)! at least, on a 1-liner with code plus '
Edit: and on a 400+ line prog with copious rems


RE: Remove Remarks for Programs - Dimster - 10-26-2024

So Pete and Steve, I'm having trouble getting your programs  to work for me, and by the way, thanks very much for them.

The bulk of my programs are on my D drive, So I'm assuming, when your program asks for the identity of the "File" it's looking in the C drive?
Also, Pete's program is asking to "Paste" the name of the file, I've tried a number of ways to this but it keeps displaying the C drive, user,user, qb64pe etc and never gets to any actual file names I do have on the C drive. Is there a limit to the path characters? 

I have tried to follow your code but not sure how to alter it to recognize my D drive.


RE: Remove Remarks for Programs - Dimster - 10-26-2024

Update - Steve's worked perfectly this time and I have no idea what I did to cause it to fail the first time I ran it. 

File this next comment under "There is no pleasing some folks". The new "no rem" file however has gapping spaces where the old remarks use to be which has me highlighting the gaps to delete them. So kind of back to the same task that "The Lazy Programmer" was trying to avoid.

On the other hand it's a really clean look and with all those remarks gone I see new potential in better labels or better placed labels for code sections which cuts the search for these sections (ie the search doesn't hit on every odd match)

It's crazy I know but I think I may re-add some Remarks.

Thanks again


RE: Remove Remarks for Programs - SMcNeill - 10-26-2024

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.