Page 1 of 1
readstruct & writestruct
Posted: Tue Apr 17, 2007 1:51 pm
by John Duchek
I am new so maybe I missed it. I have 2 items on my wishlist. One is a rehash of many suggestions before, but a multiline comment would be nice.
The other one I couldn't find in this forum is a way to read and write structures as unit to a file. I find readbyte, readword etc, but don't see an easy way to write a whole structure to a file as a unit. Am I missing it?
Thanks
John
Posted: Tue Apr 17, 2007 2:07 pm
by Kaeru Gaman
> a multiline comment would be nice.
to outcommend several lines of code:
mark the lines and press CTRL-B to outcommend them,
press Alt-B to remove the commending again.
> readstruct & writestruct
yap, thats a good idea. I'll second that request.
(I don't remember there is something like it, or is there?)
since a struct is like a Dataset/Recordset, it should be possible to read/write one in whole.
additional an adopted fileseek maybe nice,
but this can easily be solved via normal fileseek and SizeOf(struct) as multiplicator.
Posted: Tue Apr 17, 2007 2:34 pm
by Thalius
readstruct & writestruct
actually a nice idea - and even User programmable

might sit behind it if Fred isnt faster *gg*
Posted: Tue Apr 17, 2007 3:12 pm
by Trond
Code: Select all
Macro WriteStruct(File, Variable)
WriteData(File, @Variable, SizeOf(Variable))
EndMacro
Macro ReadStruct(File, Variable)
ReadData(File, @Variable, SizeOf(Variable))
EndMacro
Limitations:
- No strings
- If the name of the declared structured variable is also a structure name, then it will not work UNLESS the variable is of type structure name (because types have precedence over variables in SizeOf()).
Posted: Tue Apr 17, 2007 3:24 pm
by Thalius
Ok simple, i actually had an idea how to implement strings in there also. Would require a base header or at least some encapsulation method to ensure stringspace.
Thalius
Posted: Tue Apr 17, 2007 3:33 pm
by Kaeru Gaman
to be used as a recordset, a string should have fixed length.
and fixed strings are within the structure, not in the pool.
PS:
@trond
nice macro, should fulfill it's purpose

Posted: Tue Apr 17, 2007 3:39 pm
by bembulak
Posted: Thu Apr 19, 2007 2:16 pm
by John Duchek
The Cntrl B/Alt B works great. I was unaware of that. Thank you. All my structs have strings embedded and they get pretty unwieldy to handle when doing file access.
Thanks,
John
Posted: Thu Apr 19, 2007 5:18 pm
by MrMat
Each string structure has associated s_ data (basically a list of offsets to strings and other structures in a structure). Hopefully one day there will be a native command to access this data because it is useful for lots of things, e.g. clearing, comparing, copying and freeing structured memory containing strings - all of which is very useful when dealing when tree structures and skiplists and is laborious and error prone to code manually.
So this s_ data could also be used to code something that iterates through any structure and saves/loads string and non-string data if someone wants to attempt it but it would be a hack since the s_ data isn't natively available or documented.
Posted: Thu Apr 19, 2007 6:25 pm
by Kaeru Gaman
the problem is, that structures with dynamic strings are quite useless for building recordsets.
the main point of a recordset is a unique length for each set of the same structure.
this is the only point that makes fast recordset access in a file possible.
this kind of file is the grandfather of relational databases.
the dynamic strings are a pretty idea to save space in memory,
but it's even more difficult to write that to a file,
because then in the file each recordset will need previous/next-pointers like a linked-list has,
and you will have to browse the file from beginning setwise to find your desired recordset.
in the end I would say: decide!
if you want a convenient and small solution for memory, you'll have to write or read the complete list at once and handle it within memory.
if you want a tabellaric file, use fixlenth strings in your structure.
Posted: Thu Apr 19, 2007 6:54 pm
by MrMat
The original post just asks for a way to dump and restore structured memory. Iteration can be performed via pointers to next/previous element as mentioned or if fast random access is required then an index can be built (for example CHM uses this where each record cannot be the same size) but i think this is overcomplicating it.