how to get a list of used variables ?

Just starting out? Need help? Post your questions and find answers here.
Gadget
User
User
Posts: 39
Joined: Tue Mar 11, 2014 8:11 pm
Location: UK

Re: how to get a list of used variables ?

Post by Gadget »

Thanks for confirming that the Variable Viewer isn't working as expected Luis - I thought I was embarrassing myself again! I'll dig a little deeper shortly and report it as a bug.

EnableExplit is brilliant and helps avoid bugs - as a beginner I've learned of it's value (but then, I mkae a lot of mitkases so I hvae to :) ). Seems like some people like it and others don't ... I guess we're all different and like to do the same thing in different ways - one of the many joys of being human :D

I think a key point here is that the function does not work as described. Either the manual or (preferably) the IDE needs to be updated. A list of variables can be helpful for those who want to search through a program a retrospectively rename in a consistent manner. Again, some people will be good at being consistent up-front when naming variables and others will introduce consistency at a latter stage (and perhaps most won't bother).

Thade raises a good point about the complexity introduced by reporting variables in procedures. I think this can be a simplified by listing globals defined outside of a procedure and then grouping locals which are defined within a procedure as a line item prefixed with the procedure name. For example, running Variable Viewer on Luis' example of:

Code: Select all


Global var

Procedure a()
 Protected a, b, c
EndProcedure 

could give an output of:

var
a - a
a - b
a - c
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: how to get a list of used variables ?

Post by freak »

Gadget wrote:1. Protected variables within a procedure are not listed. According to the PB manual (below) these should be listed.
The manual is wrong. It is intended to only display items from the main program scope. The implementation changed quite a while ago.

luis wrote:Seems so gigantic I find strange nobody noticed before unless is new... or no one is using it maybe ?
The more likely explanation is that nobody reads the manual :P
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: how to get a list of used variables ?

Post by netmaestro »

Maybe "current scope" is a better term? If you issue CallDebugger from inside a procedure, all the local protected vars from that procedure are available in the Variable Viewer. This actually makes the most sense as in the main scope I'm not interested in local vars from procedures I'm not currently executing. From inside the procedure I can view both local and global vars. Perfect.
BERESHEIT
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 283
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: how to get a list of used variables ?

Post by oreopa »

Id like to add that there are TWO variable viewers :D This can be a tiny bit confusing when talking of this subject... :D

IE: Debugger/Variable Viewer and Tools/Variable Viewer.

Perhaps the one in the Tool menu should be renamed to Variable List, or something... just an observation/suggestion...
Proud supporter of PB! * Musician * C64/6502 Freak
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: how to get a list of used variables ?

Post by IdeasVacuum »

This has turned into a fascinating post :)

Wimapon, the following code will (should) produce a simple list of all allocated variables found in a source code file (pb/pbi).

Pros:
1) Lists the same var several times if it is allocated several times
2) Unlike PB's viewer, lists the line number of the var
3) Lists Global and Local vars

Cons:
1) On a large file, it's too slow
2) unallocated vars are not listed
3) Independent of the IDE
4) Crude: only been tested on my files - a different programming style might reveal gotchas

Edit 1: Added right-mouse-click menu for 'Find Var'
Edit 2: Auto-Scroll to found Var row (Windows OS only). Thanks to PB for the code snippet. ListIcon always show selection
Edit 3: An icon for this app: ListVars.zip

Code: Select all

;ListVars.pb PB5.30 x86 Unicode [15/09/2014]

UsePNGImageDecoder()

EnableExplicit

Enumeration
#Win
#FileIO
#TxtFileIn
#StrFileIn
#BtnBrowse
#RightClickPop
#List
#MenuFind
#MenuHilt
EndEnumeration

Structure VarList
sLine.s
sVar.s
EndStructure

Global   sgDefaultFolder.s = "C:\"
Global      igBtnBrwsImg.i = CatchImage(10,?BtnBrowse)
Global          igFormat.i = 0
Global         qgFileLen.q = 0
Global          *gTextBuff

Global      igGridColour.i = RGB(000,000,196)
Global      igListColour.i = RGB(196,196,196)
Global igLabelTextColour.i = RGB(000,128,255)
Global   igStrTextColour.i = RGB(016,016,016)
Global       igWinColour.i = RGB(056,056,056)
Global   igHiLightColour.i = RGB(255,255,000)

Global       igFontGui10.i = LoadFont(0, "Microsoft Sans Serif", 10, #PB_Font_HighQuality + 2)
Global      igFontStrs12.i = LoadFont(1, "Microsoft Sans Serif", 12, #PB_Font_HighQuality + 2)

Global NewList Vars.VarList()

Procedure PfPopUp()
;------------------

              DisplayPopupMenu(#RightClickPop, WindowID(#Win))
EndProcedure

Procedure UnhighlightVar()
;-------------------------
Protected iRow.i = 0, iCol1.i = 1
Protected iTotalRows.i = CountGadgetItems(#List)

               For iRow = 0 To (iTotalRows - 1)

                       SetGadgetItemColor(#List, iRow, #PB_Gadget_BackColor, igListColour, iCol1)
               Next
EndProcedure

Procedure FindVar()
;------------------
Protected sVarToFind.s = InputRequester("Find Var", "Input Var Name to Find", "")
Protected iCol1.i = 1, iFoundCnt.i = 0, iRow.i = 0, iLastFoundRow.i = 0
Protected sMsg.s

               ForEach Vars()

                        If(Vars()\sVar = sVarToFind)

                                     iFoundCnt = iFoundCnt + 1
                                 iLastFoundRow = iRow
                                 SetGadgetItemColor(#List, iRow, #PB_Gadget_BackColor, igHiLightColour, iCol1)
                        EndIf

                        iRow = iRow + 1
               Next

               If(iFoundCnt > 0)

                           SetGadgetState(#List, -1)
                       SetGadgetItemState(#List, iLastFoundRow, #PB_ListIcon_Selected)
                             SendMessage_(GadgetID(#List), #LVM_ENSUREVISIBLE, iLastFoundRow, 0) ;scroll to.
                       sMsg = "Found: " + Str(iFoundCnt) + " times"
                       MessageRequester("Var Found", sMsg, #PB_MessageRequester_Ok | #MB_ICONINFORMATION)
               Else
                       sMsg = "Var: " + sVarToFind + " Not Found"
                       MessageRequester("Var Not Found", sMsg, #PB_MessageRequester_Ok | #MB_ICONINFORMATION)
               EndIf
EndProcedure

Procedure ListVars()
;-------------------
Protected qStart.q, qLen.q, qLineLen.q
Protected iPosn.i, iEquals.i, iLine.i, iVarCnt.i
Protected iIgnore.i, iIgnore1.i, iIgnore2.i, iIgnore3.i, iIgnore4.i
Protected sLine.s, sCodeLine.s, sVar1.s,  sVar2.s, sVarRev.s

              Repeat
                           iPosn = FindString(PeekS(*gTextBuff, qgFileLen, igFormat), #CRLF$, qStart, #PB_String_NoCase)
                        If(iPosn = 0) : Break : EndIf
                           sLine = Mid(PeekS(*gTextBuff, qgFileLen, igFormat), qStart, (iPosn - qStart))
                       sCodeLine = Trim(sLine)
                        qLineLen = StringByteLength(sLine, igFormat)
                          qStart = iPosn + Len(#CRLF$)
                            qLen = qLen + qLineLen
                           iLine = iLine + 1

                         iEquals = CountString(sCodeLine, "=")
                      If(iEquals = 1)

                                    sVar1 = StringField(sCodeLine, 1, "=")
                                    sVar2 = Trim(sVar1)
                                  sVarRev = ReverseString(sVar2)
                                    sVar1 = StringField(sVarRev, 1, " ")
                                    sVar2 = ReverseString(sVar1)
                                 iIgnore1 = CountString(sVar2, ";")
                                 iIgnore2 = CountString(sVar2, "If")
                                 iIgnore3 = CountString(sVar2, "(")
                                 iIgnore4 = CountString(sVar2, ")")
                                  iIgnore = iIgnore1 + iIgnore2 + iIgnore3 + iIgnore4
                               If(iIgnore < 1)

                                          AddElement(Vars())
                                                     Vars()\sLine = RSet(Str(iLine), 6, "0")
                                                     Vars()\sVar = sVar2

                                          iVarCnt = iVarCnt + 1
                                          SetWindowTitle(#Win, "List Vars " + "[" + RSet(Str(iVarCnt), 6, "0") + "]")
                               EndIf
                      EndIf

              Until((qLen = qgFileLen) Or (qLen > qgFileLen))

              ;Populate ListIcon
              SortStructuredList(Vars(), #PB_Sort_Ascending, OffsetOf(VarList\sVar), TypeOf(VarList\sVar))

              SendMessage_(GadgetID(#List),#WM_SETREDRAW,0,0) ;Faster update

              ForEach Vars()

                      AddGadgetItem(#List, -1, Vars()\sLine + Chr(10) + Vars()\sVar)
              Next

              SendMessage_(GadgetID(#List),#WM_SETREDRAW,1,0)
                   HideGadget(#List, #False)
EndProcedure

Procedure PfReadFile(sFullPath.s)
;--------------------------------

               If ReadFile(#FileIO, sFullPath)

                        igFormat = ReadStringFormat(#FileIO)
                       qgFileLen = Lof(#FileIO)
                      *gTextBuff = AllocateMemory(qgFileLen)

                       ReadData(#FileIO, *gTextBuff, qgFileLen)
                      CloseFile(#FileIO)
               Else
                      MessageRequester("List Vars", "Problem: Could not open file", #MB_ICONERROR)
               EndIf
EndProcedure

Procedure BrowseForFile()
;------------------------
Protected      sPat.s = "PB file (*.pb *.pbi)|*.pb;*.pbi|All files (*.*)|*.*"
Protected sFullPath.s = OpenFileRequester("List Vars: " + "Browse to and Select File", sgDefaultFolder, sPat, 0)
Protected qFileSize.q = FileSize(sFullPath) ;Tests if path is valid

              If(qFileSize > -1)

                        SetWindowTitle(#Win, "List Vars")
                         SetGadgetText(#StrFileIn, sFullPath)
                      ClearGadgetItems(#List)
                             ClearList(Vars())
                            HideGadget(#List, #True)

                      While WindowEvent() : Wend

                      sgDefaultFolder = GetPathPart(sFullPath)
                      PfReadFile(sFullPath)
                        ListVars()
              EndIf
EndProcedure

Procedure Win()
;--------------
Protected iExit = #False, iCol1 = 1

              If OpenWindow(#Win, 0, 0, 300, 265, "List Vars", #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered)

                            SetWindowColor(#Win,igWinColour)

                             If CreatePopupMenu(#RightClickPop)

                                       MenuItem(#MenuFind,  "Find Var")
                                       MenuItem(#MenuHilt,  "Un-highlight Vars")
                             EndIf

                                TextGadget(#TxtFileIn,    5,   6, 260,  16, "File")
                              StringGadget(#StrFileIn,    5,  24, 260,  26, "", #PB_String_BorderLess | #PB_String_ReadOnly)
                         ButtonImageGadget(#BtnBrowse,  270,  24,  26,  26, igBtnBrwsImg)

                            ListIconGadget(#List,         0,  60, 300, 205, "Line", 85, #PB_ListIcon_GridLines | #PB_ListIcon_AlwaysShowSelection)
                           AddGadgetColumn(#List, iCol1, "Var", 200)

                             SetGadgetFont(#TxtFileIn, igFontGui10)
                             SetGadgetFont(#StrFileIn, igFontStrs12)
                             SetGadgetFont(#List, igFontStrs12)

                            SetGadgetColor(#TxtFileIn, #PB_Gadget_FrontColor, igLabelTextColour)
                            SetGadgetColor(#TxtFileIn, #PB_Gadget_BackColor, igWinColour)

                            SetGadgetColor(#List, #PB_Gadget_FrontColor, igStrTextColour)
                            SetGadgetColor(#List, #PB_Gadget_BackColor, igListColour)
                            SetGadgetColor(#List, #PB_Gadget_LineColor, igGridColour)

                           BindGadgetEvent(#List, @PfPopUp(), #PB_EventType_RightClick)

                     Repeat

                           Select WaitWindowEvent(1)

                                      Case #PB_Event_CloseWindow: iExit = #True

                                      Case #PB_Event_RestoreWindow, #PB_Event_SizeWindow

                                                                    ;X                      ;Y          ;W                      ;H
                                           ResizeGadget(#List,      #PB_Ignore,             #PB_Ignore, WindowWidth(#Win),      WindowHeight(#Win) - 60)
                                           ResizeGadget(#StrFileIn, #PB_Ignore,             #PB_Ignore, WindowWidth(#Win) - 40, #PB_Ignore)
                                           ResizeGadget(#BtnBrowse, WindowWidth(#Win) - 30, #PB_Ignore, #PB_Ignore,             #PB_Ignore)

                                           SetGadgetItemAttribute(#List, #Null, #PB_ListIcon_ColumnWidth, WindowWidth(#Win) - 115, iCol1)

                                      Case #PB_Event_Gadget

                                                 Select EventGadget()

                                                            Case #BtnBrowse: BrowseForFile()
                                                 EndSelect

                                     Case #PB_Event_Menu

                                                 Select EventMenu()

                                                          Case  #MenuFind: FindVar()
                                                          Case  #MenuHilt: UnhighlightVar()
                                                 EndSelect

                           EndSelect

                     Until iExit = #True
              EndIf
EndProcedure

Win()

End

DataSection
  BtnBrowse:
  Data.q $0A1A0A0D474E5089,$524448490D000000,$1800000018000000,$3D77E00000000608,$59487009000000F8
  Data.q $0E0000C40E000073,$00001B0E2B9501C4,$DE07454D49740700,$24B9FB01330F0201,$584574070000005F
  Data.q $00726F6874754174,$0C00000048CCAEA9,$6373654474584574,$006E6F6974706972,$0A00000023210913
  Data.q $79706F4374584574,$0FAC007468676972,$45740E0000003ACC,$6974616572437458,$00656D6974206E6F
  Data.q $09000000090FF735,$74666F5374584574,$FF705D0065726177,$5845740B0000003A,$69616C6373694474
  Data.q $8FB4C0B70072656D,$7458457408000000,$00676E696E726157,$0700000087E61BC0,$72756F5374584574
  Data.q $00EB83FFF5006563,$4374584574080000,$F600746E656D6D6F,$7406000000BF96CC,$656C746954745845
  Data.q $01000027D2EEA800,$ED894854414449C9,$9F871840D36F3F95,$40A085DAA34ECFB3,$A58803A95A81235B
  Data.q $037C40622A03282C,$581958D826502DFA,$6C426262421502F9,$DC6A0924F9425448,$32F3FE7C4969A23F
  Data.q $82C8512B1A88B6B8,$3BBF3F5EEFB267D4,$D3BD76EDDAA77D9F,$AC8885E8A28DBCE9,$ED5AB507529400B2
  Data.q $FA69A76D7AD5AB53,$BF2116F012F318CE,$5DDBB751E4924BC0,$2C68AA4EEC9249D6,$68E04CFBCA5B7509
  Data.q $B24924A7B3671164,$C6CA7E3B96BD27AB,$71793B5B0A52824A,$7264B9E8FC50C86C,$377FD359111135C9
  Data.q $53A7865EEB19CB04,$DF655A4FA78D6082,$E3C6FD7CBE2FC9C9,$5480324BEA986220,$6CAB73BD16679F84
  Data.q $4C7AEB615C572B2B,$37364D5BD58379B3,$E1E35C68DC3D8EEE,$7EE23953234603F6,$065D88331F4F10FF
  Data.q $E7EF3E4E78078D46,$556DEFF7052A76CF,$91DF19F8F87C15F6,$4EE3B4028B9CA032,$558159AC5F434C15
  Data.q $C3A2A602AE30B751,$2B6A729869600430,$FB8F4F093FC1EFCC,$2201E76678421D1C,$FE3397E42229E7F9
  Data.q $AFFFFBC29E1415A6,$6C409901ECC055C1,$8A234E5BBD54510C,$082A179D8F1C710C,$0476E7612D700102
  Data.q $1F26C5E9B7CEE380,$A032A9930D6EF7DC,$4A19EB372117AC07,$29568708110337CD,$98015E03D81A7A07
  Data.q $664FB3E7B73DD14C,$4D80701F7F7DF6CF,$DE07BC07B8905FC0,$DDB042AFD70A01FC,$454900000000060C
  Data.q $0000826042AE444E
EndDataSection
[/size]
Last edited by IdeasVacuum on Wed Nov 26, 2014 4:42 pm, edited 3 times in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Re: how to get a list of used variables ?

Post by SniffTheGlove »

wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

Re: how to get a list of used variables ?

Post by wimapon »

Hi Ideasvacuum,
Sorry, i did not look for a long time at this threat.

Now i found your program, and i like it very much.

But i can not compile it... there is a syntax error in it.

Here is it:
iPosn = FindString(PeekS(*gTextBuff, qgFileLen, igFormat), #CRLF$, qStart, #PB_String_NoCase)

It has an incorrect number of parameters...

( i know i do use an old version of PB... V 4.51 , but i think that is not the cause of
the error)

Again, sorry for my late reaction...

Wim Apon
infratec
Always Here
Always Here
Posts: 7584
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: how to get a list of used variables ?

Post by infratec »

Hi Wim,

look at the help of FindString(), I think the last parameter was nao available in 4.51.

Bernd
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: how to get a list of used variables ?

Post by IdeasVacuum »

Hi Wim, infratec is correct, PB4.51 does not have flags for case sensitivity.

So, iPosn = FindString(PeekS(*gTextBuff, qgFileLen, igFormat), #CRLF$, qStart) should work.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
wimapon
Enthusiast
Enthusiast
Posts: 290
Joined: Thu Dec 16, 2010 2:05 pm
Location: Delfzijl ( The Netherlands )
Contact:

Re: how to get a list of used variables ?

Post by wimapon »

Hi Infratec and Ideasvacuum,
Always very nice to get your help!
i just changed the program a bit, and now it is working.
thank you very much!

But i like to avoid this version problem for the future....

So now a very important question for me:
If i download the newest version of Pure Basic.....
will all my old programs work without changes????

Point is: i do not exact understand most of my old programs
anymore.
I just use them as black boxes.
So if i have to change things, i can not use them anymore.
And i don't dare to change them.

(Because, i started a new radio-telescope measuring program.
now with other apparatus and an other frequency.
so changing the old programs will be a disaster....)

Wim
Post Reply