ReadFile oddities - synchronous, open specific local file

Just starting out? Need help? Post your questions and find answers here.
mdp
Posts: 31
Joined: Fri Oct 06, 2017 10:37 am

ReadFile oddities - synchronous, open specific local file

Post by mdp »

I am having problems with ReadFile():
1) I need it to be synchronous: it opens a file for reading, I process it, and the main process does not continue until the operation is done. Instead, I get to use a callback...
2) the manual says « OpenFileRequester()() needs to be called before to have access to local files.» But I open the file as a data repository! How can I need to open a requester before!?
mdp
Posts: 31
Joined: Fri Oct 06, 2017 10:37 am

Re: ReadFile oddities - synchronous, open specific local fil

Post by mdp »

Not even `IncludeBinary` is there...
If the limitation is because of the JS concept of running in a browser from random web pages hence need for security, instead of being code run from a trusted application, well, then JS is not enough. It must be "patched" and augmented with the needed bits of "local OS".
Already the access to SQLite files is not there... How can one load data into the application?
falsam
Posts: 280
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: ReadFile oddities - synchronous, open specific local fil

Post by falsam »

mdp wrote:How can one load data into the application?
With Php :
https://www.spiderbasic.com/documentati ... quest.html

➽ Windows 11 - JDK 1.8 - SB 2.40 - Android 13
http://falsam.com

Sorry for my poor english
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: ReadFile oddities - synchronous, open specific local fil

Post by Peter »

mdp wrote:Already the access to SQLite files is not there... How can one load data into the application?
perhaps SpiderBite fulfils your requirements:

Code: Select all

; either include "SpiderBite.sbi":
; XIncludeFile "[YourPathTo]/SpiderBite.sbi"
;
; ... or copy "SpiderBite.res" to SpiderBasics Residents-Folder

#SpiderBite_Profile = "default"

Procedure GetDataCallback(Success, Result.s)
  
  Debug "Success: " + Success
  Debug "Result: " + Result
  
EndProcedure

EnablePbCgi
  
  UseSQLiteDatabase()
  
  ProcedureDLL.s GetData()
    
    Protected ReturnValue.s
    Protected DB
    
    DB = OpenDatabase(#PB_Any, ":memory:", "", "", #PB_Database_SQLite)
    
    If DB
      
      DatabaseUpdate(DB, "Create Table SuperHeroes (Prename TEXT, Surname TEXT)")
      
      DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Peter', 'Parker')")
      DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Bruce', 'Wayne')")
      DatabaseUpdate(DB, "Insert Into SuperHeroes (Prename, Surname) Values ('Clark', 'Kent')")
      
      If DatabaseQuery(DB, "Select * From SuperHeroes Where Prename = 'Peter'")
        While NextDatabaseRow(DB)
          ReturnValue + GetDatabaseString(DB, 1) + "," + GetDatabaseString(DB, 0) + #CRLF$
        Wend
        FinishDatabaseQuery(DB)
      EndIf
      
      CloseDatabase(DB)
      
    EndIf
    
    ProcedureReturn ReturnValue
    
  EndProcedure
  
DisablePbCgi

GetData()

If 1=2 ; prevent removing the 'unneeded' callback
  GetDataCallback(0, "")
EndIf
Further informations: https://github.com/spiderbytes/SpiderBite

(the documentation is in german, but you can use DeepL for the translation)

Greetings ... Peter
mdp
Posts: 31
Joined: Fri Oct 06, 2017 10:37 am

Re: ReadFile oddities - synchronous, open specific local fil

Post by mdp »

falsam wrote:
mdp wrote:How can one load data into the application?
With Php :
https://www.spiderbasic.com/documentati ... quest.html
Sorry, I don't get it... Do you mean using a network server? It is totally overkill. This approach would restrict SB to client-server applications. To use SB for local application development, possibly offline, we need to access data. This mean custom binary/text (e.g. CSV, "blit" etc.), DB (SQLite) etc.

EDIT: unless SB packs a minimal, maybe transparent server... It's not impossible to implement, and it can be done with minimal libraries! It's a hack, but we need a hack to make full applications out of web-based code.
mdp
Posts: 31
Joined: Fri Oct 06, 2017 10:37 am

Re: ReadFile oddities - synchronous, open specific local fil

Post by mdp »

At the moment, I thought that the best solution seems to embed a JSON literal with IncludeFile.
Am I missing anything? Are there better ideas?

I would love to access the blit of an array of structures, but never tried even in PB...

I would also like to free the memory of source literal objects - just to do things properly.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: ReadFile oddities - synchronous, open specific local fil

Post by Peter »

mdp wrote:Are there better ideas?
perhaps your Json-File with LoadJson()?

btw.: what is a 'blit'?

Greetings ... Peter
mdp
Posts: 31
Joined: Fri Oct 06, 2017 10:37 am

Re: ReadFile oddities - synchronous, open specific local fil

Post by mdp »

Peter wrote:perhaps your Json-File with LoadJson()?


You mean we cannot access random local files but we can if they are JSON formatted (which does not even have a magic header?). I am stunned, it would make no sense
Peter wrote:btw.: what is a 'blit'?
It's when you dump a piece of memory (low level) that you can then load to use it with high level functions. E.g. if you have an array of 10 of a structure of a byte and an int, you could use a dump of 50 bytes as both storage and of source to load into memory. But put in there pointers (e.g. null-terminated strings), and the matter gets messy...

So basically I meant: something that you upload into memory and use directly
mdp
Posts: 31
Joined: Fri Oct 06, 2017 10:37 am

Re: ReadFile oddities - synchronous, open specific local fil

Post by mdp »

mdp wrote:You mean we cannot access random local files but we can if they are JSON formatted (which does not even have a magic header?). I am stunned, it would make no sense
And in fact, I may be wrong, but the manual seems to indicate using a procedure which is part of the Requester library:
Filename$ The name of the file containing the JSON data. It can be an URL or a local file identifier got with SelectedFileID().
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: ReadFile oddities - synchronous, open specific local fil

Post by Peter »

you can use LoadJSON() without Requester:

Code: Select all

Procedure Loaded(Type, Filename$, ObjectId)
	Debug Type
	Debug Filename$
	Debug ObjectId
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
	Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loaded())
BindEvent(#PB_Event_LoadingError, @LoadingError())

LoadJSON(0, "./resources/test.json")
Greetings ... Peter
Post Reply