-
Notifications
You must be signed in to change notification settings - Fork 0
/
FLATTEN.BAS
80 lines (75 loc) · 2.4 KB
/
FLATTEN.BAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
''
' 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 <[email protected]> (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$ = "_OPTION_TEST.bas"
outfile$ = "_OPTION_TEST_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