File Reading workout

Just starting out? Need help? Post your questions and find answers here.
menschmarkus
Posts: 54
Joined: Thu Apr 10, 2014 3:35 pm

File Reading workout

Post by menschmarkus »

Hi,
for information, usually I do work with PB. Now I try to transfer PB to SB. So far so good.
In the moment I am facing a problem with correct position for action within the program.

Situation:
I open a file and read it line by line Which works fine. Each line will be processed by calling a php script by HTTPRequest().
During this procedure I block other buttons for action by DisableGadget(). I do this right after selecting the file by FileRequester(). It works fine.
The result of the HTTPRequest() should be stored in a structured variable.
At last I have to release buttons after the complete procedure of reading and handling the file content. (Here the problem starts)

I know SB is event driven. Unfortunately I cannot Debug in compiled application mode. I have to do it in compiled App mode because HTTPRequest() does not work in local Debug mode. As soon I run it in Debug Mode Message "HTTPRequest failed" happens and Program stopps.

Following the scheme of the procedures

Code: Select all

structure first
a.s
b.s
endstructure
structure second
c.s
d.s
List result.first()
endstructure

Global MyStructure.second

Procedure HTTPGetEvent(success,result$,userdata)
	if ParseJSON(#json,result$)
		ExtractJSONStructure(JSONValue(#json),@MyStructure.second,second)
		FreeJSON(#json)
	Else
		DisableGadget(#Button_xyz,0)
	endif
endprocedure

Procedure ReadFileCallback(status,filename$,file,size)
	Select status
		While not EOF(#file)
			string$ = ReadString(#file)
			httpquery$ = "http://www..xyz.com?<key>=string$&<key>...."
			HTTPRequest(#PB_HTTP_GET,URL_Encoder(httpquery$),"",@HTTPGetEvent(),0,httpheader())			
		Wend
		;## If I start here with reading out the MyStructure.second structured variable it does not work
		Disable Gadget(#Button_xyz,0)			;## Enabling Button immediately runs without waiting the above loop !??
	endselect
endprocedure

Procedure OpenFileRequesterCallback()
	If NextSelectedFile()
		DisableGadget(#Button_xyz,1)
		OpenFile(#file,SelectedFileID(),@ReadFileCallback(),#PB_LocalFile)
	EndIf
endprocedure

Procedure chooseFileEvent()
	OpenFileRequester,"",@OpenFileRequesterCallback())
endprocedure

Procedure GadgetEvents()
	Select EventGadget()
		Case #Button_start_file_procedure
			chooseFileEvent()
	endselect
endprocedure

Procedure main()
	BindEvent(#PB_Event_Gadget,@GadgetEvents())
endprocedure
main()	
My Question is, Where is the right Code Position to readout the MyStructure variable and to enable the Buttons after it is completely read.
Possibly I did something completely wrong due to the fact it is not PB. Over there it works

Thanks for your assistance
as soon you do it right, it works !
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: File Reading workout

Post by Peter »

I would suggest that the file is read in completely with #PB_File_IgnoreEOL:

Code: Select all

AllLines$ = ReadString(#file, #PB_File_IgnoreEOL)
With CountString(AllLines$, StringToCount$) you can determine the number of lines. (StringToCount$ is the line separator in your file. Depending on the system, this is probably #LF$ or #CRLF$)

With StringField(AllLines$, Index, Separator$) you can run a for-loop over the individual lines. (Separator$: see above)

In HTTPGetEvent() you can increase run a line counter (static or global). If this reaches the number of lines in the file, you can enable your gadget again.

Don't forget to set the line counter back to 0 before you read in another file.
menschmarkus
Posts: 54
Joined: Thu Apr 10, 2014 3:35 pm

Re: File Reading workout

Post by menschmarkus »

Peter

thank you for your reply. Reading file at once and loop it in for next loop could solve the problem.

As further hint to everyone who wants to store results of several HTTPRequest() into a structured variable Like I have done in my sample...it will not work in this way like in my sample.
Now I store each single JSON result (<variable> + result$) in a Global variable until the loop has finished. Finally I add "]}" to the JSON variable to close it properly. It is essential to prepare the php script that results will be output in a proper way !!

By the way, due to the fact the final version on my server is not able to show DEBUG information, I found a way to collect DEBUG information and show it at the end of the loop. It looks like:

Code: Select all

NewList logit.s()						; Create a new log List

ClearList(logit())						; Needs to be called once before loging starts

AddElement(logit()) : logit() = "Your Debug Information"	; Enter this line at any position where you want to get Debug information

Procedure showlog()						; To show Debug result at the end of the loop I call this procedure and use an existing editor Gadget here
	ResetList(logit())
	ForEach logit()
	    SetGadgetText(#editor,GetGadgetText(#editor) + logit() + #CRLF$
	Next
EndProcedure 
I hope this will help the one or other to debug in a productive app during development. Don't forget to disable this before your release ;)

Thank you Peter
as soon you do it right, it works !
Post Reply