HTML5 Filesystem in Smart Mobile Studio
Yesterday I presented a new QTX feature, namely a virtual filesystem which runs in your browser. In short it allows you to create files through an in-memory filesystem, which can then be stored in its totality in local-storage, global-storage or in a browser database.
Well today I can show you an example of how you use this. Here is a short teaser. Notice the use of anonymous records, which is really a fantastic feature of Smart Pascal.
mFileSystem:=TQTXFileSystem.Create(NIL); try mFileSystem.mkDir('first') .mkDir('second') .mkFile('text.txt',null); mFile:=TQTXFileSystemFile(mFileSystem.FindFileObject ('/first/second/text.txt')); if mFile<>NIL then begin mFile.WriteData ( record id:= 12; text:= "this is awesome"; cost:= 12.95 end); writeln(mFile.ReadData.text); end else writeln('invalid path'); finally mFilesystem.free; end;
Im not going to divulge to much about this code. Most Smart Pascal developers will understand what’s going on I believe. But in short:
- mkdir() creates a folder, and returns the folder object
- mkfile() creates a file and returns the file object
- FindFileObject() takes a path and recursively drills down to locate the filesystem-object
- WriteData() stores a variant value in a file
- ReadData() returns the variant file-content
Since mkDir() and mkFile() returns the object, we can do fancy stuff like chaining make operands together:
mFileSystem.mkDir('first') .mkDir('second') .mkFile('text.txt',null);
But what about speed I hear you say? Well I had to re-code this twice, because like my friend Eric Grange pointed out the other day – JSON.Stringify() is extremely costly in terms of CPU and time. Which means that if every file object supports ISerializable it would take ages before the procedure returns (and then the browser drops the call).
So what I do now is that – when you call SaveTo() to store the full content of the filesystem, it generates a multi-layered array of records. It then calls JSON.Stringify and Deserialize the whole array in a single pass, which is blistering fast.
Note: Localstorage has a limit of some 15 megabytes or so (or was that 10?) so dont expect much. But if you want to store some data, perhaps a document or preferences, then qtx.storage.filesystem is exactly what you need.
Storage galore
Let’s see what happens when we generate 1000 files with a short string as the content. We use TW3LocalStorage as the target medium. Here is a snapshot from Webkit that verifies that it’s been properly stored:
And here is the code which generates the folders and files (pretty neat stuff!):
w3Button1.OnClick:=Procedure (sender:TObject) var x: Integer; mName: String; mFileSystem: TQTXFileSystem; mStore: TW3LocalStorage; begin mFileSystem:=TQTXFileSystem.Create(NIL); try mFileSystem.mkDir('first') .mkDir('second') .mkFile('text.txt',null); for x:=1 to 1000 do begin mName:="file" + getNewID().toString() + ".txt"; mFileSystem.mkfile(mName,"this is fantastic"); end; mStore:=TW3LocalStorage.Create; try mStore.Open("filesystem"); try mFileSystem.SaveTo("files",mStore); finally mStore.Close; end; finally mStore.free; end; showmessage("Data stored!"); finally mFilesystem.free; end; end;
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
You must be logged in to post a comment.