File :: Readstructure (), WriteStructure () functions

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

File :: Readstructure (), WriteStructure () functions

Post by Lunasole »

The VB6 had nice thing that PB still misses for some reason - writing whole structure to a file.

For example here is structure with dynamical string.

Code: Select all

Structure test 
	s.s
EndStructure
Define X.test
	X\s = "1234"
	
CreateFile(1, "D:\1.txt")
	WriteData(1, @X, SizeOf(X))
CloseFile(1)
It saves to file only pointer value of a string, which is useless.
If using same code with VB6 it saves and reads the whole string itself, not a pointer. Which is very useful and you don't have to write your own bloated module to pack every structure manually.

Code: Select all

Option Explicit
Private Type Test
    s As String
End Type


Private Sub Command1_Click()
    Dim X As Test
        X.s = "1234"

    Open "D:\1.txt" For Binary As 1
        Put 1, 1, X
    Close 1
    
    Dim Y As Test
    
    Open "D:\1.txt" For Binary As 1
        Get 1, 1, Y     ; not even size to read specified. Y is not exactly as X
    Close 1
    
    Debug.Print Y.s
End Sub
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: File :: Readstructure (), WriteStructure () functions

Post by STARGÅTE »

In case of Strings (Ascii, UTF8, Unicode), Integers (4Byte or 8Byte) and List/Map/Array-Headers, WriteStructure have to write also some headers, otherwise you can't read it again with ReadStructure. So some one have to define a format.

But than you can use the JSON Library with InsertJSONStructure().
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: File :: Readstructure (), WriteStructure () functions

Post by Demivec »

Perhaps using JSON. Example Link.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: File :: Readstructure (), WriteStructure () functions

Post by Keya »

+1! Saving to/from JSON is really quite handy and minimal code, but well... it's JSON text structure not binary, so it comes with both performance and filesize penalties (it even saves the variable names along with values) that i'm usually not interested in. I think WriteData(@structure, SizeOf(structure)) works only if there are no nested structures, so im not sure if its even possible for us users to implement an alternative in a practical sense... it seems something only the compiler could do?
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: File :: Readstructure (), WriteStructure () functions

Post by Lunasole »

JSON might be used, but it is poor replacement of binary, slow and useless web-format as for me, even worst than classic .ini. So no, I wouldn't use it for such things (nor for store anything ^^), especially when need to save a lot of data.

STARGÅTE wrote:In case of Strings (Ascii, UTF8, Unicode), Integers (4Byte or 8Byte) and List/Map/Array-Headers, WriteStructure have to write also some headers, otherwise you can't read it again with ReadStructure. So some one have to define a format.
The string encoding and int sizes are not a problem, may be passed as params to a ReadStructure() / write.
The maps/list/arrays, yes, more complicated, they can be nested, recursive and so on, it might be harder to do. But anyway format to dump to file must be close to how PB stores them internally to avoid doing too much operations. Need to know enough how it handles maps/lists to tell something about format and how complicated could be to realize such stuff
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply