View Full Version : SubFcnList.tbasic - Adds Sub&Function summary to your scripts
dcromley
15-11-2011, 06:44
Hello,
This isn't rocket science, but I wrote it, use it, and like it.
It scans a program for "Sub"s and "Functions"s and adds those lines
as a summary at the top of the program (removing the old summary).
You need to change:
1) the Const BkupFile to a place where you want to have a backup file.
2) the Const FileName to the program you want processed. A good addition
would be to have a file dialog instead of a Const.
Deleted -- I'm thinking it needs some work. I'll re-post in a day or two. dCromley
dcromley
15-11-2011, 18:37
OK, now I'm proud of this program. I got to thinking that in the
first version, it seemed possible to mess up both the program and
the backup. Now the program is copied to the backup and that is
not touched. From my first post:
Hello,
This isn't rocket science, but I wrote it, use it, and like it.
It scans a program for "Sub"s and "Functions"s and adds those lines
as a summary at the top of the program (removing the old summary).
You need to change:
1) the Const BkupFile to a place where you want to have a backup file.
2) the Const FileName to the program you want processed. A good addition
would be to have a file dialog instead of a Const.
' [re-posted on 16-11-2011 with suggestions from thread]
' ---- sub/function summary
' Sub TBMain()
' Function CkIterate(txt As String) As Long
' Function LFill(s1 As String, pl As Long) As String
' Sub ErrStop(txt As String)
' ---- end of sub/function summary
Uses "Console", "file", "UI" ' by dCromley 11/2011
Const sHeader As String = "' ---- sub/function summary"
Const sFooter As String = "' ---- end of sub/function summary"
Const BkupFile As String = APP_ScriptName + ".BACKUP"
Sub TBMain()
Local fhin, fhout As Long
Local i1 As Long, s1, s1u, s2, sRec As String
Local FileName As String
FileName = Dialog_OpenFile(0, "Select a file", DIR_GetCurrent, _
"TB Files (*.tBasic*)|*.tBasic*|All Files (*.*)|*.*", "", 0)
If FileName = "" Then Exit Sub
' -- copy the file to the backup
If Not FILE_Exists(FileName) Then ErrStop "<" & FileName & "> doesn't exist"
fhout = FILE_Open(BkupFile, "output") ' check the output file
If fhout = 0 Then ErrStop "Open Output " & BkupFile
FILE_Close(fhout)
FILE_Copy(FileName, BkupFile)
PrintL FileName & " copied to " & bkupfile & $CRLF
' -- now output to the original file
fhout = FILE_Open(FileName, "output")
If fhout = 0 Then ErrStop "Open Output " & FileName
' -- pass 1, delete old summary and make new summary
fhin = FILE_Open(BkupFile, "input")
If fhin = 0 Then ErrStop "Open Input " & BkupFile
FILE_LinePrint(fhout, sHeader) ' header
PrintL sHeader
Do While Not FILE_EOF(fhin)
sRec = FILE_LineInput(fhin)
If CkIterate(srec) Then Iterate Do ' bypass old list
s1 = Parse$(srec, " ", 1) ' get "Sub" or "Function" or ..
s1u = Ucase$(s1) ' ensure no case difference
If s1u = "SUB" Or s1u = "FUNCTION" Or s1u = "CALLBACK" Then
i1 = InStr(1, srec, s1) ' get rest of line
s2 = "' " & LFill(s1, 9) & Mid$(srec, Len(s1)+2)
PrintL s2 ' re-constructed line
FILE_LinePrint(fhout, s2)
End If
Loop
FILE_Close(fhin)
FILE_LinePrint(fhout, sFooter) ' footer
PrintL sFooter & $CRLF
FILE_Close(fhin)
' -- pass 2, the rest of the file
fhin = FILE_Open(BkupFile, "input")
If fhin = 0 Then ErrStop "Open Input " & BkupFile
Do While Not FILE_EOF(fhin)
srec = FILE_LineInput(fhin)
If CkIterate(srec) Then Iterate Do ' bypass old list again
FILE_LinePrint(fhout, srec)
Loop
FILE_Close(fhin)
' -- end of pass 2
FILE_Close(fhout)
PrintL "End - reveiw " & FileName
WaitKey
End Sub
Function CkIterate(txt As String) As Long
Static swIterate As Long
' set bypass if header
If txt = sHeader Then swIterate = 1
Function = swIterate
' reset bypass if footer
If txt = sFooter Then swIterate = 0
If LEFT$(txt, 1) <> "'" Then ' safety ck
swIterate = 0
Function = 0
End If
End Function
Function LFill(s1 As String, pl As Long) As String
' Left$, but fill with " "s
Function = LEFT$(s1 & String$(pl, " "), pl)
End Function
Sub ErrStop(txt As String)
MsgBox 0, "Err: " & txt: Stop
End Sub
Petr Schreiber
15-11-2011, 18:48
Thanks for the example,
to make it work "out of the box", you might want to change two lines in the code to:
Const BkupFile As String = APP_ScriptName + ".BACKUP"
Const FileName As String = APP_ScriptName
... this will analyze the code itself :)
Petr
Michael Clease
16-11-2011, 00:03
I like this very useful, here is something to make sure that it catches scripts that are not formatted to first char upper case and have callbacks.
If Ucase$(s1) = "SUB" Or Ucase$(s1) = "FUNCTION" Or Ucase$(s1) = "CALLBACK" Then
thanks
Mike C.
dcromley
16-11-2011, 00:05
Michael,
Very good! Thanks
Petr,
> Const BkupFile As String = APP_ScriptName + ".BACKUP"
It took awhile for me to process this, but I like it.
Just taking the "z:\" out of "z:\zbkup.tbasic" would have allowed
it to work "out of the box", which I like. So I have your
Const BkupFile As String = APP_ScriptName + ".BACKUP"
Thank you for the idea.
> Const FileName As String = APP_ScriptName
"FileName" needs to selected by the user. So instead of
"Const FileName ..", I want the Dialog_OpenFile(), which I haven't
used before. But you made me look into it :) The following
(from samplescripts) works GREAT:
' (have to add "UI")
uses "Console", "File", "UI"
' (early in TBMain)
Local FileName As String
FileName = Dialog_OpenFile(0, "Select a file", DIR_GetCurrent, _
"TB Files (*.tBasic*)|*.tBasic*|All Files (*.*)|*.*", "", 0)
If FileName = "" Then Exit Sub
> Thanks for the example
I appreciate your comments very much!
dcromley
16-11-2011, 18:50
I want to make it easier for somebody who wants the updated script.
I could have posted it here, or replaced the version in the top post.
I replaced the version in the top post.
Is this unethically changing history? :rolleyes:
for me i prefer to see the history of the examples, and anyway in a time travel movie they said that it is prohibited to change the history.this is a useful example thanks
ErosOlmi
16-11-2011, 21:19
Dave,
I saw you developed LFill function.
thinBasic has LSET$ (http://www.thinbasic.com/public/products/thinBasic/help/html/lset$.htm), RSET$ (http://www.thinbasic.com/public/products/thinBasic/help/html/rset$.htm) and CSET$ (http://www.thinbasic.com/public/products/thinBasic/help/html/cset$.htm) functions that can be used for string alignment.
Ciao
Eros
dcromley
16-11-2011, 23:29
< .. LSet ..
Yup, that's what I wanted. Thanks for pointing that out, Eros.
Good point, Zak.
ErosOlmi
17-11-2011, 08:08
My idea is that if there are general purpose functions enough general and interesting to also be used by other programmers, I add them as native in thinBasic.
Like Lerp (http://www.thinbasic.com/community/project.php?issueid=331) function you suggested and that will be present in next thinBasic release.