Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MergeFile
#1
Code: (Select All)
$If WIN Then
    Const Slash$ = "\"
$Else
        const Slash$ = "/"
$End If

target$ = ".\source\qb64pe.bas"
outfile$ = ".\qb64onefile.bas"

If target$ = "" Then
    Print "Give me a QB64 program to unravel => ";
    Input target$
    Print "Give me a name to save the new file under => ";
    Input outfile$
End If

Open outfile$ For Output As #1
MergeFile target$

Sub MergeFile (whatfile$)
    f = FreeFile
    CurrentDir$ = _CWD$
    i = _InStrRev(whatfile$, Slash$)
    newdir$ = Left$(whatfile$, i)
    If i > 0 Then
        ChDir newdir$
        whatfile$ = Mid$(whatfile$, i + 1)
    End If
    Open whatfile$ For Binary As #f
    Do
        Line Input #f, temp$
        If Left$(UCase$(_Trim$(temp$)), 11) = "'$INCLUDE:'" Then
            temp$ = _Trim$(temp$)
            file$ = Mid$(temp$, 12)
            file$ = Left$(file$, Len(file$) - 1)
            MergeFile file$
        Else
            Print #1, temp$
        End If
    Loop Until EOF(f)
    ChDir CurrentDir$
    Close #f
End Sub


@grymmjack Was asking for a quick little program to merge $INCLUDE files into a single BAS file, so I sat down and wrote this one up in about 15 minutes.  I haven't tested it extensively as I'm kinda distracted with my niece's kids visiting today, but it appeared to work without any issues with QB64PE.BAS and merged it all into one file easily enough.

Give it a try if you're interested in this type of thing, and if you manage to break it, post me a note on how you did so an I'll update it with a fix as soon as I get a little free time later.  Wink
Reply
#2
Thanks SMcNeill - I tried to tag you, but it didn't seem to work. 

Any way... This is just what I needed!

After the discord DMs we figured out a few things. 

First, I have gone a little crazy on how scaffolded the project I needed this for is! LOL. https://github.com/grymmjack/DRAW (this is the one).

Second, for some reason the "../" relative paths were breaking the flattener process. I removed those and we got it to churn through the entire thing.

Third, as the source has evolved a little bit I'll post the one I used here:
Code: (Select All)
''
' MergeFile
'
' Takes a source target basic file and flattens all the includes to be in a
' single file. This lets you see what the compiler is compiling if you have
' multiple file includes. It works across directories, too.
'
' HOW TO USE:
' -----------
' Change target$ to your source basic file you want to flatten into one file.
' Change outfile$ to the file path of the flattened/merged file.
' Change indent to a number you prefer. This helps debug stuff in code folding.
'
' @author Steve McNeill (every thing)
' @author Rick Christy <grymmjack@gmail.com> (idea)
' @see https://qb64phoenix.com/forum/showthread.php?tid=1335&pid=12085#pid12085
'

$Debug
$If WIN Then
    Const Slash$ = "\"
$Else
    const Slash$ = "/"
$End If

' CHANGE THIS STUFF
const indent = 8
target$  = "I:\git\DRAW\DRAW.BAS"
outfile$ = "I:\git\DRAW\DRAW_FLATTENED.BAS"

' -----------------------------------------------------------------------------
' MAIN PROGRAM
' -----------------------------------------------------------------------------
If target$ = "" Then
    Print "Give me a QB64 program to unravel => ";
    Input target$
    Print "Give me a name to save the new file under => ";
    Input outfile$
End If

Open outfile$ For Output As #1
MergeFile target$
Dim SHARED stack AS INTEGER
stack% = 0

Sub MergeFile (whatfile$)
    f = FreeFile
    CurrentDir$ = _CWD$
    i = _InStrRev(whatfile$, Slash$)
    newdir$ = Left$(whatfile$, i)
    If i > 0 Then
        ChDir newdir$
        whatfile$ = Mid$(whatfile$, i + 1)
    End If
    Print whatfile$
    Open whatfile$ For Binary As #f
    If LOF(f) Then
        Do
            Line Input #f, temp$
            If Left$(UCase$(_Trim$(temp$)), 11) = "'$INCLUDE:'" Then
                temp$ = _Trim$(temp$)
                file$ = Mid$(temp$, 12)
                file$ = Left$(file$, Len(file$) - 1)
                stack% = stack% +1
                MergeFile file$
            Else
                Print #1, STRING$(stack% * indent, " ") + temp$
            End If
        Loop Until EOF(f)
    End If
    ChDir CurrentDir$
    ' See below comment
    IF LOF(f) = 0 THEN killempty = 1 ELSE killempty = 0
    Close #f
    ' In severe cases of nesting (the catalyst for this MergeFile program LOL)
    ' the program can get confused and output 0 byte files in the same dir it
    ' ran from. This is a kludge to get rid of those after it runs.
    if killempty = 1 THEN KILL whatfile$
    stack% = stack% - 1
End Sub

Last here is a video showing how the stack counter and indention helps to see what includes are including other includes (lol):
https://app.screencast.com/v89y1GUNVSQG4...0FLXws7IsI
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#3
Also, if you use PowerShell:

Code: (Select All)
Shell Hide "PowerShell -NoProfile Get-Content .\*.bas ^| Out-File .\Merged.bas"
'or
Shell Hide "PowerShell -NoProfile Get-Content file1.bas, file2.bas ^| Out-File .\Merged.bas"

You may or may not need the caret. I've needed it before to escape the pipe in SHELL.
Tread on those who tread on you

Reply
#4
Thanks Spriggsy on PS tip. The main thing I was after was a 100% replica of what is included in the final output evaluating everything as the compiler would. Since the PS would be a big concatenation and have no idea where includes happened and stuff it wouldn't be identical. I appreciate the suggestion and will remember the tip. I used to do this in old days to take many files and make them into one for binary. I think it was copy /b in DOS?
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply
#5
There was also a utility made on the old forum for merging bas files. I used to use it all the time for my InForm programs because it was a hassle trying to distribute those. Lots of dependencies for the source and whatnot. You just open the main bas file and all the files it needs gets dumped to a new file with no blank spaces. I had made a GUI for it as well. I'd have to find it, though.
Tread on those who tread on you

Reply
#6
(01-10-2023, 03:38 PM)Spriggsy Wrote: There was also a utility made on the old forum for merging bas files. I used to use it all the time for my InForm programs because it was a hassle trying to distribute those. Lots of dependencies for the source and whatnot. You just open the main bas file and all the files it needs gets dumped to a new file with no blank spaces. I had made a GUI for it as well. I'd have to find it, though.

This one? QB64-Museum/Bin2Include at main · a740g/QB64-Museum (github.com)

Although, I am not sure if this is the latest.
Reply
#7
(01-10-2023, 05:16 PM)a740g Wrote:
(01-10-2023, 03:38 PM)Spriggsy Wrote: There was also a utility made on the old forum for merging bas files. I used to use it all the time for my InForm programs because it was a hassle trying to distribute those. Lots of dependencies for the source and whatnot. You just open the main bas file and all the files it needs gets dumped to a new file with no blank spaces. I had made a GUI for it as well. I'd have to find it, though.

This one? QB64-Museum/Bin2Include at main · a740g/QB64-Museum (github.com)

Although, I am not sure if this is the latest.

@a740g Nah, that one is for including binaries. There was another one a while back just for merging source files. I'll try finding it.
Tread on those who tread on you

Reply
#8
Ah! Well, I didn't find my GUI thing I made for it but I did find (I think) the original post for the source merger utility.
OpenInclude, soon to be made a SMART code merger. (alephc.xyz)
Tread on those who tread on you

Reply
#9
(01-10-2023, 05:20 PM)Balderdash Wrote: Ah! Well, I didn't find my GUI thing I made for it but I did find (I think) the original post for the source merger utility.
OpenInclude, soon to be made a SMART code merger. (alephc.xyz)

Thank you!
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply




Users browsing this thread: 1 Guest(s)