Jump to content

Program listings for #include, Dim Global Local Const, Func, and $ Variables helpful for debugging


ahha
 Share

Recommended Posts

I find this useful as programs get big and I can't keep the whole program and variables and globals in myhead.

It simply scans your program and spits out into separate Notepad files with line numbers for:

#include

Dim Global Local Cost

Func

$ Variables

It's *NOT* a polished program with graphs or diagrams but it helps me and I'm submitting it as my first example script.

#AutoIt3Wrapper_run_debug_mode=Y    ;use this to debug in console window <--- LOOK
;recall ;debug toggles debug - so search for and change ;debug to ;;debug
AutoItSetOption("MustDeclareVars", 1)

#include <Debug.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GUIEdit.au3>
#include <StaticConstants.au3>      ;for $SS_CENTER
#include <WindowsConstants.au3>

Global $debug = 0   ;0=off 1=on
If $debug > 0 Then Msgbox(0+262144,"Info","@AutoItVersion = " & @AutoItVersion)

Global $Author = "ahha"

;v1a - original
;...
;v1j - changed Send to copy and paste
;v2a - adding checkboxes
;v2b - cleaning up some code
;v2c - setting up a Label to show the file drop zone
;v2d - adding $h_StatusWindow status window and display message - eventually got rid of this in favor of a scrolling window
;v2e - need to strip WS in front before any searches except for $Variable
;       #include, Dim Global Local Const, Func - all need to be first after stripping leading whitespace (WS)
;       $Variable can be anywhere - even in a comment
;       this means commented out #include, Dim Global Local Const, Func will NOT be shown
;       Perhaps have a Checkbox to include/exclude commented out
;v2f - add the checkbox for commented out
;v2g - cleaned up some code, program not perfect as it catches extra stuff like comment ;for $SS_CENTER as a variable
;       also duplicates of the same line especially for Dim Global Local Const, Func
;       gets the job done and useful
;v2h - adding info about hotkeyset so user can abort processing
;v2i - getting rid of duplicate lines from Dim Global Local Const, Func processing
;v2j - getting rid of extraneous code
;v2k - combining open and paste to Notepad, pop Program Status window to top to show processing (don't keep user in the dark)
;v2ka - setting window order so user knows we're processing
;v2kb - cleaning out more extraneous code
;v2kc - cleaning out even more extraneious code
Global $version = "v2kc"    ;released to AutoIt on 2023-02-11

;possible future improvements/features
;on large files >10k lines it can be slow doing 4 iterations, may want to do a single pass of reading the array to speed it up
;program lists some stuff like #include "DateTimeConstants.au3" as a Const because we don't filter fully - but not harmful

Global $hSTBEdit, $hSTBGUI  ;for Scrolling Text Box
__scrolling_text_box_init()     ;set up box - to display text use: __scrolltext($text)

Global $fPaused = False
HotKeySet("^q", "TogglePause")  ;Crtl+q

Global $QUOTE = Chr(34) ;0x22

Global $hNP     ;handle for NotePad

Global $iColorBlack = 0x000000, $iColorRed = 0xff0000, $iColorGreen = 0x00ff00, $iColorBlue = 0x0000ff, $iColorYellow = 0xffff00, $iColorWhite = 0xffffff, $iColorReddish = 0xF57670

Global $hGUI = GUICreate("Program Listings " & $version & "   >> USE: Crtl+q to Toggle/Pause/Abort", 700, 400, Default, Default, Default, $WS_EX_ACCEPTFILES)
GUICtrlSetBkColor($hGUI, $iColorRed)    ;;debug the background for width of field - remember ;debug toggles it so use ;;debug
Global $id_Line1 = GUICtrlCreateLabel("Drop an .au3 file in the green region to automatically generate the Listings selected with line numbers.", 10, 10)
Global $id_Line2 = GUICtrlCreateLabel("Each Listing will be in a separate Notepad file.", 10, 10+20)
Global $id_Line3 = GUICtrlCreateLabel("As an example go to AutoIt3\Include and drag and drop in file Date.au3", 10, 10+20+20)
Global $id_Line4 = GUICtrlCreateLabel("Listings:", 10, 10+20+20+50)

;Global $id_LabelDrop = GUICtrlCreateInput("Drop your .au3 file here.", 700-280, 400-350, 200, 100, Default, $WS_EX_ACCEPTFILES)    ;<-- this screws up
Global $id_LabelDrop = GUICtrlCreateInput("Drop your .au3 file here.", 700-280, 400-350, 200, 100)  ;<-- this works
GUICtrlSetBkColor($id_LabelDrop, $iColorGreen)  ;;debug the background for width of field
GUICtrlSetState($id_LabelDrop, $GUI_DROPACCEPTED)

GUISetState(@SW_SHOW)

Global $id_CheckboxComment, $id_CheckboxInclude, $id_CheckboxDimGlobalLocalConst, $id_CheckboxFunc, $id_CheckboxVariables
CreateCheckboxComment()
CreateCheckboxInclude()
CreateCheckboxDimGlobalLocalConst()
CreateCheckboxFunc()
CreateCheckboxVariables()

;for use in While 1
Local $nMsg, $i = 0
Global $file, $aFile
Opt("SendKeyDelay", 0)  ;Alters the length of the brief pause in between sent keystrokes. A value of 0 removes the delay completely. Time in milliseconds to pause (default=5).

Dim $aA[1]  ;for debug
Const $xA = 0 ;for debug


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Msgbox(0+262144,"Info","Exiting program", 1)
            Exit
        Case $GUI_EVENT_DROPPED
            ;Pause("In: Case $GUI_EVENT_DROPPED")
            $file = @GUI_DragFile
            Msgbox(0+262144,"Info","Dropped filename = '" & $file & "'")
            _FileReadToArray($file, $aFile, $FRTA_COUNT)
            ;_DebugArrayDisplay($aFile, "Array of: '"& $file & "'") ;NOTA BENE - _DebugArrayDisplay STRIPS leading WS !!! for display only

            ; #include processing
            If GUICtrlRead($id_CheckboxInclude) = $GUI_CHECKED Then ;proceed to generate #include listing
                Msgbox(0+262144,"Info","Proceeding to generate #include listing", 1)
                __scrolltext(@CRLF & "Info: Proceeding to generate #include listing" & @CRLF)
                Local $sTitle = "#include list for: '" & $file & "'" & @CRLF & _
                    "Line number  #include" & @CR & _
                    "-----------  --------" & @CRLF
                _SetWindowOrder($hSTBGUI, $hGUI)    ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Local $sString = ""
                Local $sLineStr = ""    ;operational string without WS in front ;v2e
                Local $sSkip = "no"
                If GUICtrlRead($id_CheckboxComment) = $GUI_CHECKED Then $sSkip = "yes"
                ;Pause("$sSkip = '" & $sSkip & "'")

                For $i = 1 to $aFile[0]     ;[0] has count
                    __scrolltext("Reading line " & $i & "/" & $aFile[0] & " = '" & $aFile[$i] & "'" & @CRLF)
                    $sLineStr = StringStripWS($aFile[$i], $STR_STRIPLEADING)    ;v2e need to strip WS in front
                    If ( (StringLeft($sLineStr, 1) = ";") AND ($sSkip = "yes") ) Then
                        ;2f - skip line if commented out
                    Else    ;process it if line is not commented out ;v2f - include anywhere in line !! not just left justified
                        ;convert string to lower case
                        If StringInStr(StringLower($aFile[$i]), "#include") Then $sString = $sString & $i & "  " & $aFile[$i] & @CR ;just look for "#include"
                    EndIf
                Next

                $hNP = _OpenAndPasteToNotepad($sTitle & $sString)
                _SetWindowOrder($hNP, $hGUI, $hSTBGUI)  ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Beep(500, 1000)     ;to indicate done
            EndIf

            ; Dim Global Local Const processing
            If GUICtrlRead($id_CheckboxDimGlobalLocalConst) = $GUI_CHECKED Then ;proceed to generate Dim Global Local Const listing
                Msgbox(0+262144,"Info","Proceeding to generate Dim Global Local Const listing", 1)
                __scrolltext(@CRLF & "Info: Proceeding to generate Dim Global Local Const listing" & @CRLF)
                Local $sTitle = "Dim Global Local Const list for: '" & $file & "'" & @CRLF & _
                    "Line number  Dim Global Local Const" & @CR & _
                    "-----------  ----------------------" & @CRLF
                _SetWindowOrder($hSTBGUI, $hGUI)    ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Local $sString = ""
                Local $sLineStr = ""    ;operational string without WS in front ;v2e
                Local $sSkip = "no"
                If GUICtrlRead($id_CheckboxComment) = $GUI_CHECKED Then $sSkip = "yes"
                Local $flag = 0

                For $i = 1 to $aFile[0]     ;[0] has count
                    __scrolltext("Reading line " & $i & "/" & $aFile[0] & " = '" & $aFile[$i] & "'" & @CRLF)
                    $sLineStr = StringStripWS($aFile[$i], $STR_STRIPLEADING)    ;v2e need to strip WS in front
                    $flag = 0
                    If ( (StringLeft($sLineStr, 1) = ";") AND ($sSkip = "yes") ) Then
                        ;2f - skip line if commented out
                    Else    ;process it if line is not commented out ;v2f - include anywhere in line !! not just left justified
                        ;convert string to lower case
                        ;probably could do this with a Case or Switch
                        If StringInStr(StringLower($aFile[$i]), "dim") Then $flag = 1
                        If StringInStr(StringLower($aFile[$i]), "global") Then $flag = 1
                        If StringInStr(StringLower($aFile[$i]), "local") Then $flag = 1
                        If StringInStr(StringLower($aFile[$i]), "const") Then $flag = 1

                        If $flag = 1 Then $sString = $sString & $i & "  " & $aFile[$i] & @CR
                    EndIf
                Next

                $hNP = _OpenAndPasteToNotepad($sTitle & $sString)
                _SetWindowOrder($hNP, $hGUI, $hSTBGUI)  ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Beep(500, 1000)     ;to indicate done
            EndIf


            ; Func processing
            If GUICtrlRead($id_CheckboxFunc) = $GUI_CHECKED Then    ;proceed to generate Func listing
                Msgbox(0+262144,"Info","Proceeding to generate Func listing", 1)
                __scrolltext(@CRLF & "Info: Proceeding to generate Func listing" & @CRLF)
                Local $sTitle = "Func list for: '" & $file & "'" & @CRLF & _
                    "Line number  Func" & @CR & _
                    "-----------  ----" & @CRLF
                _SetWindowOrder($hSTBGUI, $hGUI)    ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Local $sString = ""
                Local $sLineStr = ""    ;operational string without WS in front ;v2e
                Local $sSkip = "no"
                If GUICtrlRead($id_CheckboxComment) = $GUI_CHECKED Then $sSkip = "yes"

                For $i = 1 to $aFile[0]     ;[0] has count
                    __scrolltext("Reading line " & $i & "/" & $aFile[0] & " = '" & $aFile[$i] & "'" & @CRLF)
                    $sLineStr = StringStripWS($aFile[$i], $STR_STRIPLEADING)    ;v2e need to strip WS in front
                    If ( (StringLeft($sLineStr, 1) = ";") AND ($sSkip = "yes") ) Then
                        ;2f - skip line if commented out
                    Else    ;process it if line is not commented out ;v2f - include anywhere in line !! not just left justified
                        If StringInStr(StringLower($aFile[$i]), "func") Then $sString = $sString & $i & "  " & $aFile[$i] & @CR ;just look for "Func"
                    EndIf
                Next

                $hNP = _OpenAndPasteToNotepad($sTitle & $sString)
                _SetWindowOrder($hNP, $hGUI, $hSTBGUI)  ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Beep(500, 1000)     ;to indicate done
            EndIf


            ; $ Variables processing
            If GUICtrlRead($id_CheckboxVariables) = $GUI_CHECKED Then   ;proceed to generate $ Variables listing
                Msgbox(0+262144,"Info","Proceeding to generate $ Variables listing", 1)
                __scrolltext(@CRLF & "Info: Proceeding to generate $ Variables listing" & @CRLF)
                Local $sTitle = "$ Variables list for: '" & $file & "'" & @CRLF & _
                    "Line number  $ Variables" & @CR & _
                    "-----------  -----------" & @CRLF
                _SetWindowOrder($hSTBGUI, $hGUI)    ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Local $sString = ""
                Local $sLineStr = ""    ;operational string without WS in front ;v2e
                Local $sSkip = "no"
                If GUICtrlRead($id_CheckboxComment) = $GUI_CHECKED Then $sSkip = "yes"

                For $i = 1 to $aFile[0]     ;[0] has count
                    __scrolltext("Reading line " & $i & "/" & $aFile[0] & " = '" & $aFile[$i] & "'" & @CRLF)
                    $sLineStr = StringStripWS($aFile[$i], $STR_STRIPLEADING)    ;v2e need to strip WS in front
                    If ( (StringLeft($sLineStr, 1) = ";") AND ($sSkip = "yes") ) Then
                        ;2f - skip line if commented out
                    Else    ;process it if line is not commented out ;v2f - include anywhere in line !! not just left justified
                        ;convert string to lower case
                        If StringInStr(StringLower($aFile[$i]), "$") Then $sString = $sString & $i & "  " & $aFile[$i] & @CR    ;just look for "$"
                    EndIf
                Next

                $hNP = _OpenAndPasteToNotepad($sTitle & $sString)
                _SetWindowOrder($hNP, $hGUI, $hSTBGUI)  ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
                Beep(500, 1000)     ;to indicate done
            EndIf

            Beep(200, 1000)     ;to indicate done processing

        Case $id_LabelDrop
            ;Pause("In: Case $id_LabelDrop")    ;comment out so don't process as checkbox activation/deactivation somethimes triggers to here

    EndSwitch

WEnd

Exit    ;should never hit this

;------------------------------ Functions -------------------------

Func  _SetWindowOrder($hWin1, $hWin2 = "", $hWin3 = "", $hWin4 = "", $hWin5 = "", $hWin6 = "", $hWin7 = "", $hWin8 = "")    ;sets windows in order leftmost top, etc.  Paints back (right) to front (left)
    ;handles at most 8 windows
    WinSetOnTop($hWin8, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin8, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin7, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin7, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin6, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin6, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin5, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin5, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin4, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin4, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin3, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin3, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin2, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin2, "", $WINDOWS_NOONTOP)
    WinSetOnTop($hWin1, "", $WINDOWS_ONTOP)
    WinSetOnTop($hWin1, "", $WINDOWS_NOONTOP)

;   Local $i
;   For $i = 8 to 1 Step - 1    ;using Assign() and Eval() commands
EndFunc     ;Func  _SetWindowOrder($hWin1, $hWin2 = "", $hWin3 = "", $hWin4 = "", $hWin5 = "", $hWin6 = "", $hWin7 = "", $hWin8 = "")


Func  _OpenAndPasteToNotepad($sString)
    Run("notepad.exe")
    $hNP = WinWaitActive("Untitled - Notepad")  ;Global above
    ClipPut($sString)       ;put to clipboard for faster pasting
    WinActivate($hNP)
    WinWaitActive($hNP)
    Send("^v")      ;Ctrl+v to paste it in
    Return($hNP)    ;so can use it if needed
EndFunc     ;Func  _OpenAndPasteToNotepad($sString)


Func  CreateCheckboxComment()
    $id_CheckboxComment = GUICtrlCreateCheckbox("Exclude if line commented out (i.e. starts with ; )", 30+100, 10+20+20+50-5, 280, 25)      ;Global above
    GUICtrlSetState($id_CheckboxComment, $GUI_CHECKED)
    GUICtrlSetTip($id_CheckboxComment, "UNCheck this if you want to have a listing that includes commented out lines in the program file.")
    ;GUICtrlSetBkColor($id_CheckboxComment, $iColorYellow)  ;;debug the background for width of field
EndFunc     ;Func  CreateCheckboxComment()

Func  CreateCheckboxInclude()
    $id_CheckboxInclude = GUICtrlCreateCheckbox("#include", 30, 100+25, 200, 25)        ;Global above
    GUICtrlSetState($id_CheckboxInclude, $GUI_CHECKED)
    GUICtrlSetTip($id_CheckboxInclude, "Check this if you want to have a listing of #includes in the program file.")
    ;GUICtrlSetBkColor($id_IncludeCheckbox, $iColorYellow)  ;;debug the background for width of field
EndFunc     ;Func  CreateCheckboxInclude()

Func  CreateCheckboxDimGlobalLocalConst()
    $id_CheckboxDimGlobalLocalConst = GUICtrlCreateCheckbox("Dim Global Local Const", 30, 100+25+25, 200, 25)       ;Global above
    GUICtrlSetState($id_CheckboxDimGlobalLocalConst, $GUI_CHECKED)
    GUICtrlSetTip($id_CheckboxDimGlobalLocalConst, "Check this if you want to have a listing of Dim Global Local and Const in the program file.")
    ;GUICtrlSetBkColor($id_CheckboxDimGlobalLocalConst, $iColorYellow)  ;;debug the background for width of field
EndFunc     ;Func  CreateCheckboxInclude()

Func  CreateCheckboxFunc()
    $id_CheckboxFunc = GUICtrlCreateCheckbox("Func", 30, 100+25+25+25, 200, 25)     ;Global above
    GUICtrlSetState($id_CheckboxFunc, $GUI_CHECKED)
    GUICtrlSetTip($id_CheckboxFunc, "Check this if you want to have a listing of Func in the program file.")
    ;GUICtrlSetBkColor($id_CheckboxFunc, $iColorYellow) ;;debug the background for width of field
EndFunc     ;Func  CreateCheckboxFunc()

Func  CreateCheckboxVariables()
    $id_CheckboxVariables = GUICtrlCreateCheckbox("$ Variables", 30, 100+25+25+25+25, 200, 25)      ;Global above
    GUICtrlSetState($id_CheckboxVariables, $GUI_CHECKED)
    GUICtrlSetTip($id_CheckboxVariables, "Check this if you want to have a listing of $ Variables in the program file.")
    ;GUICtrlSetBkColor($id_CheckboxVariables, $iColorYellow)    ;;debug the background for width of field
EndFunc     ;Func  CreateCheckboxVariables()


Func __scrolling_text_box_init()
    Local $limit, $x
    ;from http://www.autoitscript.com/forum/topic/110948-add-text-to-edit-box-and-scroll-it-down/page__p__971158__hl___guictrledit_scroll__fromsearch__1#entry971158
    ;Global $hSTBEdit, $hSTBGUI ;for Scrolling Text Box
    $hSTBGUI = GUICreate("Program Status - Program listings " & $version, 750, 500)
    $hSTBEdit = GUICtrlCreateEdit("", 10, 10, 730, 480, BitOr($GUI_SS_DEFAULT_EDIT, $ES_READONLY))
    ;$limit = 9223372036854775807   ;2^63
    $limit = 1000000000 ;works
    $x = GUICtrlSetLimit($hSTBEdit, $limit)
    ;~ If $x = 0 Then
    ;~  MsgBox(0, "ERROR", "Limit of " &  $limit & " is too large.")
    ;~ Else
    ;~  MsgBox(0, "Okay", "Limit of " &  $limit & " is okay.")
    ;~ EndIf

    ;$hButton = GUICtrlCreateButton("Add", 10, 250, 80, 30)

    GUISetState()

EndFunc     ;Func __scrolling_text_box_init()


Func __scrolltext($text)
    GUICtrlSetData($hSTBEdit, $text, 1)
EndFunc     ;Func __scrolltext($text)


Func  TogglePause()
    Local $ans = MsgBox(0 + $MB_YESNO + $MB_ICONQUESTION + $MB_TOPMOST + $MB_DEFBUTTON1, "Program paused.", "Do you want to Quit the program?") ;make Yes the default
    If $ans = $IDYES  Then
        ;MsgBox(0 + $MB_OK + $MB_ICONQUESTION + $MB_TOPMOST, "Program quitting", "Please wait as program finishes the operation and then exits.")
        Exit    ;exit entire program
    EndIf
EndFunc   ;Func  TogglePause()


Func  Pause($text="")
    ;__scrolltext("Debug: Paused " & $text & @CRLF)
    MsgBox(262144, "DEBUG", "Paused: " & $text)
EndFunc     ;Func  Pause($text="")

 

Edited by ahha
forgot to include the code :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...