Tool Procedure sorter & dep finder 4 x platform porting incs

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Tool Procedure sorter & dep finder 4 x platform porting incs

Post by Zebuddi123 »

Hi to all I needed a tool to look through source code (ex: Droopy.pb) sort through the procedures and seperate the cross platform (no api) and non cross platform procedures ( with api ) also to show any dependencies that the cross platform section has in the non cross platform section.

I thought this would give me a head start on porting any large includes (for me ) from windows to linux its wip but may be useful to some

Not tested on windows but shoud be cross platform.

Zebuddi. :)

Code: Select all

Structure plist
	procname$
	List p.s()
EndStructure

Structure dep
	cpp$
	List ncppd.s()
EndStructure


Global Window_0
Global Tree_0, Editor_cp, Editor_ncp, Splitter_0, Button_File, dep_lable, nbrdep.i, file_lnbs.i, String_file
Global boundbox$= "******************************************************************************************"
; Global regex.i = CreateRegularExpression(#PB_Any, "^Procedure.*_\(")
; Global regex_procs.i = CreateRegularExpression(#PB_Any, "(?<=(=|\*|\+|\-|/| ))[a-zA-z0-9_]+_\(")

Global NewList proc.plist()
Global NewList cp.plist()
Global NewList ncp.plist()
Global NewList dep.dep()

Procedure.s get_proc_name(string.s)
	sp=FindString(string," ")
	se=FindString(string,("("))
	ProcedureReturn Trim(Mid(string,sp+1,(se-sp)))	
EndProcedure

Procedure.s beautifynumber(a)
	p=4
	x.s=Str(a)
	x=ReverseString(x)
	If Mod(Len(x),3)=0
		n=((Len(x)/3)-1)
	Else
		n=Len(x)/3
	EndIf
	For i=1 To n
		x=InsertString(x,",",p)
		p+4
	Next
	ProcedureReturn ReverseString(x)
EndProcedure

Procedure.i procs_To_list( file.s , List proc.plist())
	Protected l.i
	Protected NewList strings.s()
	With proc()
		If ReadFile(0,file)
			
			While Not Eof(0)
				a$=ReadString(0,ReadStringFormat(0)) : l+1
				If Trim(a$)>"" And Left(Trim(a$),1)<>Chr(59)
					AddElement(strings())
					strings()=a$
				EndIf
			Wend
			CloseFile(0)  : a$=""
			If ListSize(strings())>2
				ForEach strings()
					If strings()>""
						If  Left(strings(),1)<>Chr(59)   ; not a comment string
							If Left(strings(),9)="Procedure"
								aaa$=strings()						
								AddElement(proc())
								proc()\procname$=strings()
								AddElement(\p())
								\p()=strings()
								
								While Trim(strings())<>"EndProcedure" 
									NextElement(strings()): l+1
									aaa$=strings()
									If strings()>"" And Left(Trim(strings()),1)<>Chr(59) Or Left(strings(),1)<>Chr(35)
										AddElement(\p())
										\p()=strings()
									EndIf
								Wend
								
							EndIf
						EndIf
					EndIf
				Next
			EndIf
		EndWith
		ProcedureReturn l
	EndIf
EndProcedure

Procedure.i proc_sort_to_list(List from.plist(), List to_cp.plist(), List to_ncp.plist())
	Protected match_api.b
	ForEach from()
		
		ForEach from()\p()
			aaa$=from()\p()
			
			If Trim(Left(from()\p(), 1))<>Chr(59) ; not a comment string
				If FindString(Trim(from()\p()),"_(") Or FindString(Trim(from()\p()),"CallCFuntion(") Or  FindString(Trim(from()\p()),"CallCFuntionFast(")  Or FindString(Trim(from()\p()),"CallFuntion(") Or FindString(Trim(from()\p()),"CallFuntionFast(") 
					match_api+1
					Break
				EndIf	
			EndIf
		Next
		
		If match_api=0 ; cross platform
			AddElement(to_cp())
			FirstElement(from()\p())
			to_cp()\procname$=get_proc_name(from()\p())
			ForEach from()\p() ; loop through procedure code and and add to linked lust
				aaa$=from()\p()
				AddElement(to_cp()\p())
				to_cp()\p() = from()\p()
			Next
			
		ElseIf match_api=1 ; non cross platform
			AddElement(to_ncp())
			FirstElement(from()\p())
			to_ncp()\procname$=get_proc_name(from()\p())
			ForEach from()\p(); loop through procedure code and and add to linked lust
				aaa$=from()\p()
				AddElement(to_ncp()\p())
				to_ncp()\p() = from()\p()
			Next
			
			match_api=0
		EndIf
	Next
	
	ProcedureReturn ListSize(to_cp())
EndProcedure

; add cross platform procs to editor gadget
Procedure show_cross_platform(List cp.plist())
	Protected line_counter.i
	SetGadgetColor(Editor_cp,#PB_Gadget_FrontColor,$1DBF3F)
	ForEach cp()
		AddGadgetItem(Editor_cp,-1,Boundbox$)
		ForEach cp()\p()
			line_counter+1
			AddGadgetItem(Editor_cp,-1,Str(line_counter)+".	"+cp()\p())
		Next
		line_counter=0
		AddGadgetItem(Editor_cp,-1,Boundbox$)
		AddGadgetItem(Editor_cp,-1,Chr(10)+Chr(10))
	Next
EndProcedure

; add non cross platform procs to editor gadget
Procedure show_non_cross_platform(List ncp.plist())
	Protected line_counter.i
	ForEach ncp()
		AddGadgetItem(Editor_ncp,-1,Boundbox$)
		ForEach ncp()\p()
			line_counter+1
			If FindString(ncp()\p(),"_(")
				SetGadgetColor(Editor_ncp,#PB_Gadget_FrontColor,$2D0CFF)
				AddGadgetItem(Editor_ncp,-1,Str(line_counter)+".	"+ncp()\p()+" ;<<<<<<<<<<<<<<<<")  ; add identifier for the line containg the API with <<<<<<<<<<<<<<<
			Else
				AddGadgetItem(Editor_ncp,-1,Str(line_counter)+".	"+ncp()\p())
			EndIf
		Next
		line_counter=0
		AddGadgetItem(Editor_ncp,-1,Boundbox$)
		AddGadgetItem(Editor_ncp,-1,Chr(10)+Chr(10))
	Next
EndProcedure

Procedure dependencies(List to_cp.plist(), List to_ncp.plist(), List to_dep.dep())
	opn.s=""
	ForEach to_cp() ; cross platform
		AddElement(to_dep()) ; dependencies
		to_dep()\cpp$=RemoveString(to_cp()\procname$,"(") ; get the procname  in \ccp
		ForEach to_cp()\p()					  ; loop through tyhe code lines in the cross plpatform procs to search for a match in  non cross platform proc names
			acp_cp.s=to_cp()\p()				  ;debug
			ForEach to_ncp()					  ; non cross platform
				acp_ncp.s= to_ncp()\procname$
				If FindString(to_cp()\p(), to_ncp()\procname$) And opn<> to_ncp()\procname$
					AddElement(to_dep()\ncppd()) : gg+1 : nbrdep+1; add to the list of required dependencies
					to_dep()\ncppd()=Str(gg)+". "+Trim(to_cp()\p()) ; get the needed procname from the non cross platform proc
				EndIf
				opn=to_ncp()\procname$
			Next
		Next
		gg=0
	Next
EndProcedure

Procedure show_dependencies(List dep.dep())
	ForEach  dep()
		If ListSize(dep()\ncppd())>0
			AddGadgetItem(Tree_0, -1, dep()\cpp$+"   ----  "+Str(ListSize(dep()\ncppd()))+" Dependencies",0,0)
			ForEach  dep()\ncppd()
				AddGadgetItem(Tree_0,-1, dep()\ncppd(),0,1)
			Next
		EndIf
	Next
EndProcedure

Procedure refresh()
	file_lnbs=0
	StatusBarText(0,1,"")
	StatusBarText(0,2,"")
	StatusBarText(0,3,"")
	StatusBarText(0,4,"")
	ClearList(dep())
	ClearList(cp())
	ClearList(ncp())
	ClearList(proc())
	ClearGadgetItems(Editor_cp)
	ClearGadgetItems(Editor_ncp)
	ClearGadgetItems(Tree_0)
	SetGadgetText(String_file,"")
EndProcedure

Procedure cleanup()
	FreeList( dep())
	FreeList( cp())
	FreeList( ncp())
	FreeList( proc())
EndProcedure

Procedure process_file()
	Protected f$
	refresh()
	f$=OpenFileRequester("Select A .pb Source File","*.*","Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*",1)
	If f$
		SetGadgetText(String_file, f$)
		If OpenFile(0,f$)
			While Not Eof(0)
				ReadString(0,ReadStringFormat(0)) : file_lnbs+1
			Wend
			CloseFile(0)
		EndIf
		
		procs_To_list(f$, proc())    ;  process file to a linked list  only code lines
		cpp = proc_sort_to_list(proc(), cp(), ncp());  sort list to non cross platform and and cross platform linked lists
		
		;update statusbar text`s
		StatusBarText(0,1,beautifynumber(file_lnbs))
		StatusBarText(0,2,"Cross Platform Proc`s: "+beautifynumber(ListSize(cp())))
		StatusBarText(0,3,("Non Cross Platform Proc`s: "+beautifynumber(ListSize(ncp()))))
		dependencies(cp(), ncp(), dep())
		StatusBarText(0,4,("Nbr Dependencies: "+beautifynumber(nbrdep)))
		
		SortStructuredList(cp(), #PB_Sort_Ascending, OffsetOf(plist\procname$) ,TypeOf(plist\procname$))
		show_cross_platform(cp())
		
		SortStructuredList(ncp(), #PB_Sort_Ascending, OffsetOf(plist\procname$) ,TypeOf(plist\procname$))
		show_non_cross_platform(ncp())		
		
		SortStructuredList(dep(), #PB_Sort_Ascending, OffsetOf(dep\cpp$) ,TypeOf(dep\cpp$))
		show_dependencies(dep())
		
	EndIf
EndProcedure

Procedure OpenWindow_0(x = 0, y = 0, width = 1240, height = 730)
	Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Dependency Finder", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget)
	CreateStatusBar(0, WindowID(Window_0))
	AddStatusBarField(100)
	AddStatusBarField(100)
	AddStatusBarField(350)
	AddStatusBarField(352)
	AddStatusBarField(318)
	StatusBarText(0, 0, "Lines Read:")
	
	Button_file = ButtonGadget(#PB_Any, 5, 5, 40, 25,"File")
	String_File =  StringGadget(#PB_Any, 45, 5, 870, 25,"")
	
	Tree_0 = TreeGadget(#PB_Any, 0,0,0,0)
	
	Editor_cp = EditorGadget(#PB_Any, 0, 0, 0, 0) ; pos+dimension do not matter witthin a spiltter gadget
	Editor_ncp = EditorGadget(#PB_Any, 0, 0, 0, 0);---------------------------------------------------------------------------------
	
	Splitter_1 = SplitterGadget(#PB_Any, 5, 35, 900, 665, Editor_cp, Editor_ncp)
	Splitter_0 = SplitterGadget(#PB_Any,5,35,1230,665,Splitter_1,Tree_0, #PB_Splitter_Vertical)
	dep_label=TextGadget(#PB_Any, 885, 20, 325, 20,"Dependencies",#PB_Text_Center)
	GadgetToolTip(Tree_0,"Cross Platform Procedure/s (Green)  have dependencies on other Procedures within the Non Cross Platform Procedures (Red).   ") 
	SetGadgetState(Splitter_1, 350)
	SetGadgetState(Splitter_0, 910)
	WindowBounds(Window_0,1240,730,#PB_Ignore,#PB_Ignore)
EndProcedure

Procedure Window_0_Events(event)
	Select event
		Case #PB_Event_CloseWindow
			ProcedureReturn #False
			
		Case #PB_Event_Menu
			Select EventMenu()
			EndSelect
			
		Case #PB_Event_Gadget
			Select EventGadget()
				Case Button_File
					process_file()
			EndSelect
	EndSelect
	ProcedureReturn #True
EndProcedure

Procedure SizeWindowHandler()
	ResizeGadget(Splitter_0,#PB_Ignore, #PB_Ignore, WindowWidth(Window_0)-5,WindowHeight(Window_0)-65)
EndProcedure

OpenWindow_0()

BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler())


Repeat
	event = WaitWindowEvent()
Until Window_0_Events(event) = #False
cleanup()
End
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Tool Procedure sorter & dep finder 4 x platform porting

Post by Tenaja »

Neat tool; thanks for sharing.
Post Reply