readstruct & writestruct

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
John Duchek
User
User
Posts: 83
Joined: Mon May 16, 2005 4:19 pm
Location: St. Louis, MO

readstruct & writestruct

Post 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
John R. Duchek
St. Louis,MO
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post by Thalius »

readstruct & writestruct
actually a nice idea - and even User programmable ;)
might sit behind it if Fred isnt faster *gg*
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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()).
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post 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
"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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 :D
Last edited by Kaeru Gaman on Tue Apr 17, 2007 3:44 pm, edited 1 time in total.
oh... and have a nice day.
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 575
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

I've asked for something similar...

http://www.purebasic.fr/english/viewtopic.php?t=25121

Would be good.
cheers,

bembulak
John Duchek
User
User
Posts: 83
Joined: Mon May 16, 2005 4:19 pm
Location: St. Louis, MO

Post 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
John R. Duchek
St. Louis,MO
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post 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.
Mat
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post 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.
Mat
Post Reply