www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to read a C++ class from file into memory

reply David Finlayson <david.p.finlayson gmail.com> writes:
I am coming from Python to D, so forgive my limited C/C++ knowledge.

What is the idiomatic way to read a heterogeneous binary structure in D? 

In my C++ book, it shows examples of defining a class or struct with the
appropriate types and then passing a pointer to this class to fread().

However, in Java or Python I could just read the types directly from a binary
stream (including the padding bytes associated with the structure on disk).

How should I do this in D?

I did see this post:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=6071

Note that I ultimately want to store these data back into classes where I can
work with it.

Thanks,

David
Mar 22 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
David Finlayson wrote:
 I am coming from Python to D, so forgive my limited C/C++ knowledge.
Don't worry; I'd be inclined to think it's a good thing :3
 What is the idiomatic way to read a heterogeneous binary structure in D? 
 
 In my C++ book, it shows examples of defining a class or struct with the
appropriate types and then passing a pointer to this class to fread().
For my money, that's a bad idea because the binary representation of a struct or object isn't necessarily the same on different machines or even same machine, different operating system. Quick example: real is a different size on Windows to Linux (IIRC).
 However, in Java or Python I could just read the types directly from a binary
stream (including the padding bytes associated with the structure on disk).
 
 How should I do this in D?
 
 I did see this post:
 
 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=6071
 
 Note that I ultimately want to store these data back into classes where I can
work with it.
 
 Thanks,
 
 David
The nice thing about Python is the pickle protocol. That's what I assume you were using. Since Python can interactively inspect objects to find out what data is attached to them, this is really easy. It's also nice because pickle isn't blind: it will serialise things in a predictable format based on type, not on in-memory layout. So you can dump a bunch of Python objects to a file, send it to another machine, and read them back out again. In D, we're kinda-sorta there. The way I'm solving this is using the .tupleof property of structures. For example: struct Point { double x, y; } { Point pt; foreach( member ; pt.tupleof ) member = 0.0; } At which point, pt.x = pt.y = 0. Combining this with templated functions lets you write out or read in any structure you please. Note: I haven't tried *any* of this with classes, because my feeling is that classes are often much more complex than structures (which are just plain old data, clumped together), plus they're far more likely to have references to other stuff; then what do you do? I thought about including some code from my serialisation library, but it tends to be "all or nothing". Plus, this was written for a research project, and I'm not sure who *actually* owns the code (me or the uni) X_X. Anyway, hope this helps :) -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 22 2007
parent reply David Finlayson <david.p.finlayson gmail.com> writes:
Daniel Keep Wrote:

 For my money, that's a bad idea because the binary representation of a
 struct or object isn't necessarily the same on different machines or
 even same machine, different operating system.
 
Agreed, however, the file is a binary storage format for a sonar system. I have been given enough code snippets from the company to read the file (and I have a working Python version), what I want to do now is convert this code to D. Ultimately, it is my problem to conform to their file format. I don't even know the byte alignment. However, I do have a working Python prototype. (I guess if I thought about it, I could figure the alignment out now).
 The nice thing about Python is the pickle protocol.  That's what I
 assume you were using.  Since Python can interactively inspect objects
 to find out what data is attached to them, this is really easy.  It's
 also nice because pickle isn't blind: it will serialise things in a
 predictable format based on type, not on in-memory layout.
 
In my case, I used Python's struct.unpack module to build a reader for each of the classes, structs and unions (yes, they used all three types). It took me a while, but I was able to identify where the padding bytes were placed to fill out the structures on disk. So, I understand exactly how the file is stored on disk. All I need to do is learn how to read the file efficiently in D. Thanks for answering my post. Do you know how I might use std.stream to read these files? David
Mar 22 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"David Finlayson" <david.p.finlayson gmail.com> wrote in message 
news:etvlr7$1a9p$1 digitalmars.com...

 Thanks for answering my post. Do you know how I might use std.stream to 
 read these files?
If you exactly how the data is structured, you may be able to define several structures which define the layout of the data, e.g. align(1) struct Header { uint magic; uint version; char[100] comments; } Or something along those lines, and then read it in with readExact: Stream s = ... Header h; s.readExact(&h, Header.sizeof); If the format is more complex, it'll probably take a bit more work, but that's the general idea.
Mar 22 2007
parent reply David Finlayson <david.p.finlayson gmail.com> writes:
Jarrett Billingsley Wrote:

 "David Finlayson" <david.p.finlayson gmail.com> wrote in message 
 news:etvlr7$1a9p$1 digitalmars.com...
 
 Thanks for answering my post. Do you know how I might use std.stream to 
 read these files?
If you exactly how the data is structured, you may be able to define several structures which define the layout of the data, e.g. align(1) struct Header { uint magic; uint version; char[100] comments; } Or something along those lines, and then read it in with readExact: Stream s = ... Header h; s.readExact(&h, Header.sizeof); If the format is more complex, it'll probably take a bit more work, but that's the general idea.
OK, this is what I want. Question: Header h creates a structure of type Header. Is h a pointer? It looks like you dereferenced it with &h in readExact(). I really don't understand how D uses pointers yet.
 
Mar 22 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
David Finlayson wrote:
 Jarrett Billingsley Wrote:
 
 "David Finlayson" <david.p.finlayson gmail.com> wrote in message 
 news:etvlr7$1a9p$1 digitalmars.com...

 Thanks for answering my post. Do you know how I might use std.stream to 
 read these files?
If you exactly how the data is structured, you may be able to define several structures which define the layout of the data, e.g. align(1) struct Header { uint magic; uint version; char[100] comments; } Or something along those lines, and then read it in with readExact: Stream s = ... Header h; s.readExact(&h, Header.sizeof); If the format is more complex, it'll probably take a bit more work, but that's the general idea.
OK, this is what I want. Question: Header h creates a structure of type Header. Is h a pointer? It looks like you dereferenced it with &h in readExact(). I really don't understand how D uses pointers yet.
No, structs in D are POD: Plain Old Data. &h is taking the address of h. What readExact does is it takes a pointer, and a length, and reads exactly that many bytes, and puts them at that pointer. &h works out *where* h is being stored (so readExact can write to it), and Header.sizeof tells it how many bytes to read. -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 22 2007
parent David Finlayson <david.p.finlayson gmail.com> writes:
Thanks Dan and Jerret -

Using readExact() to load a struct worked perfectly.
Mar 23 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"David Finlayson" <david.p.finlayson gmail.com> wrote in message 
news:etuh4g$2ijp$1 digitalmars.com...
I am coming from Python to D, so forgive my limited C/C++ knowledge.

 What is the idiomatic way to read a heterogeneous binary structure in D?

 In my C++ book, it shows examples of defining a class or struct with the 
 appropriate types and then passing a pointer to this class to fread().

 However, in Java or Python I could just read the types directly from a 
 binary stream (including the padding bytes associated with the structure 
 on disk).

 How should I do this in D?

 I did see this post:

 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=6071

 Note that I ultimately want to store these data back into classes where I 
 can work with it.
If you haven't got too many classes/structures to serialize, I've attached a module with a simple serialization/deserialization mechanism that works with std.stream. It will automatically serialize out all primitive and array types, as well as structures which have no unions. You can specify custom serialization and deserialization methods for classes and structures, and you can make structures behave as though they were an opaque chunk of data (for performance when reading/writing). It's very easy to use; to serialize anything, you just write: Serialize(stream, data); And to deserialize it again: Deserialize(stream, data); Defining the custom methods for classes and structs is easy. The serialize function should just be declared as "void serialize(Stream s)" and the deserialize function as "static T deserialize(Stream s)", where T is the type for which you're defining the deserialize function. I extracted this code from a larger module, and I think it has everything it needs to work, if it doesn't let me know! begin 666 cereal.d M;6]D=6QE(&-E<F5A;#L- M=71F(#T <W1D+G5T9CL-" T*+R\ 4V]M92!T>7!E('1R86ET<R!S='5F9BX- M"G1E;7!L871E(&ES4W1R:6YG5'EP92A4*0T*>PT*"6-O;G-T(&)O;VP :7-3 M=')I;F=4>7!E(#T :7,H5" Z(&-H87);72D ?'P :7,H5" Z('=C:&%R6UTI M"G1E;7!L871E(&ES26YT5'EP92A4*0T*>PT*"6-O;G-T(&)O;VP :7-);G14 M>7!E(#T :7,H5" ]/2!I;G0I('Q\(&ES*%0 /3T =6EN="D ?'P :7,H5" ] M/2!L;VYG*2!\?"!I<RA4(#T]('5L;VYG*2!\? T*"0D)"0D)"6ES*%0 /3T M<VAO<G0I('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!)9' *R Q*3L-" D)"65L<V4-" D)"0EC;VYS="!B M>F54>7!E(#T :7-0;VEN=&5R5'EP92$H5"D ?'P :7,H5" ]/2!F=6YC=&EO M;BD ?'P :7,H5" ]/2!D96QE9V%T92D ?'P-" D):7,H5" ]/2!I;G1E<F9A M8V4I('Q\(&ES*%0 /3T =6YI;VXI('Q\("AI<RAT>7!E;V8H5"YK97ES*2D M96YT('=A>7, 9&%T82!C86X 8F4 <V5R:6%L:7IE9"!A;F0 9&5S97)I86QI M>F5D+ T*96YU;2!397)I86QI>F5-971H;V0-"GL-" E);G9A;&ED+ T*"59E M"GT-" T*+R\ 1VEV96X 82!T>7!E+"!D971E<FUI;F4 :&]W('1O('-E<FEA M;&EZ92!O<B!D97-E<FEA;&EZ92!A('9A;'5E(&]F('1H870 ='EP92X-"G1E M;7!L871E(%1Y<&5397)I86QI>F5-971H;V0H5"D-"GL-" ES=&%T:6, :68H M86QI>F5-971H;V0 /2!397)I86QI>F5-971H;V0N26YV86QI9#L-" EE;'-E M5'EP95-E<FEA;&EZ94UE=&AO9"$H='EP96]F*%1;,%TI*2 ]/2!397)I86QI M>F5-971H;V0N26YV86QI9"D-" D)"6-O;G-T(%1Y<&5397)I86QI>F5-971H M;V0 /2!397)I86QI>F5-971H;V0N26YV86QI9#L-" D)96QS92!S=&%T:6, M:68H5'EP95-E<FEA;&EZ94UE=&AO9"$H='EP96]F*%1;,%TI*2 ]/2!397)I M:&]D(#T 4V5R:6%L:7IE365T:&]D+E9E8W1O<CL-" D)96QS90T*"0D)8V]N M<W0 5'EP95-E<FEA;&EZ94UE=&AO9" ](%-E<FEA;&EZ94UE=&AO9"Y397%U M"6-O;G-T(%1Y<&5397)I86QI>F5-971H;V0 /2!397)I86QI>F5-971H;V0N M0W5S=&]M.PT*"65L<V4 <W1A=&EC(&EF*&ES*%0 /3T <W1R=6-T*2D-" E[ M;V8H5"YS97)I86QI>F4I*2D-" D)"7L-" D)"0ES=&%T:6, :68H:7,H='EP M96]F*%0N9&5S97)I86QI>F4I*2D-" D)"0D)8V]N<W0 5'EP95-E<FEA;&EZ M"0D)"0EC;VYS="!4>7!E4V5R:6%L:7IE365T:&]D(#T 4V5R:6%L:7IE365T M;V8H5"YD97-E<FEA;&EZ92DI*0T*"0D)"6-O;G-T(%1Y<&5397)I86QI>F5- M971H;V0 /2!397)I86QI>F5-971H;V0N26YV86QI9#L-" D)"65L<V4 <W1A M94UE=&AO9" ](%-E<FEA;&EZ94UE=&AO9"Y);G9A;&ED.PT*"0D)96QS90T* M"0D)"6-O;G-T(%1Y<&5397)I86QI>F5-971H;V0 /2!397)I86QI>F5-971H M:7IE365T:&]D(#T M"E=R:71E(&]U="!A('9A;'5E('1O(&$ <W1R96%M+B 5&AI<R!W:6QL(&%U M=&]M871I8V%L;'D =W)I=&4 ;W5T(&YE<W1E9"!A<G)A>7, 86YD(&5N=&ER M92!S=')U8W1U<F5S+ T*4&]I;G1E<G,L(&9U;F-T:6]N<RP 9&5L96=A=&5S M+"!I;G1E<F9A8V5S+"!U;FEO;G,L(&%N9"!!07, 8V%N)W0 8F4 <V5R:6%L M:7IE9"X-" T*1F]R(&%R<F%Y<RP :70 =VEL;"!T<GD =&\ =W)I=&4 =&AE M(&)I9V=E<W0 8VAU;FMS(&%T(&$ =&EM92!P;W-S:6)L92X (%-O(&EF('EO M('-T<G5C="!T>7!E(&UA<FME9"!A<R!397)I86QI>F5!<T-H=6YK+"!I="!W M;F-E+B 3W1H97)W:7-E+"!I="=L;"!W<FET92!O=70 =&AE(&%R<F%Y(&5L M96UE;G0M8GDM96QE;65N="X-" T*1F]R('-T<G5C=',L('1H92!F;VQL;W=I M8F]T:" B=F]I9"!S97)I86QI>F4H4W1R96%M(',I(B!A;F0 (G-T871I8R!4 M(&1E<V5R:6%L:7IE*%-T<F5A;2!S*2( ;65T:&]D<RP-" D ("!397)I86QI M>F4O1&5S97)I86QI>F4 =VEL;"!C86QL('1H;W-E+ T*"3(I($EF('1H92!S M=64B(&1E8VQA<F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!397)I86QI>F5-971H;V0N26YV86QI9"D-" E[ M?B B)R!C86YN;W0 8F4 <V5R:6%L:7IE9"XB*3L-" D)24Y604Q)1%]465!% M971H;V0N5F5C=&]R*0T*"7L-" D)<W1A=&EC(&EF*&ES*%0 /3T 9&-H87); M72D ?'P :7,H5" ]/2!W8VAA<EM=*2D-" D)>PT*"0D)8VAA<EM=('-T<B ] M('5T9BYT;U541C H=F%L=64I.PT*"0D)<RYW<FET92AS='(N;&5N9W1H*3L- M" D)"7,N=W)I=&5%>&%C="AS='(N<'1R+"!C:&%R+G-I>F5O9B J('-T<BYL M96YG=& I.PT*"0D)<RYW<FET945X86-T*'9A;'5E+G!T<BP ='EP96]F*%1; M=&%T:6, :68H;65T:&]D(#T](%-E<FEA;&EZ94UE=&AO9"Y397%U96YC92D- M:6, :68H;65T:&]D(#T M*&UE;6)E<CL =F%L=64N='5P;&5O9BD-" D)"5-E<FEA;&EZ92AS+"!M96UB M/2!397)I86QI>F5-971H;V0N0VAU;FLL(")397)I86QI>F4B*3L-" D)<RYW M<FET945X86-T*"9V86QU92P 5"YS:7IE;V8I.PT*"7T-"GT-" T*+R\O(%1H M92!O<'!O<VET92!O9B!397)I86QI>F4H*2X (%1H92!S86UE(')U;&5S(&%P M<&QY(&AE<F4 87, =VET:"!397)I86QI>F4H*2X-"G9O:60 1&5S97)I86QI M(#T M:&]D(#T](%-E<FEA;&EZ94UE=&AO9"Y);G9A;&ED*0T*"7L-" D)<')A9VUA M*&US9RP (D5R<F]R.B!4>7!E("<B('X 5"YS=')I;F=O9B!^("(G(&-A;FYO M" EE;'-E('-T871I8R!I9BAM971H;V0 /3T 4V5R:6%L:7IE365T:&]D+E9E M" D)<W1A=&EC(&EF*&ES*%0 /3T 9&-H87);72D ?'P :7,H5" ]/2!W8VAA M"0D-" D)"7-T871I8R!I9BAI<RA4(#T](&1C:&%R6UTI*0T*"0D)"61E<W0 M/2!U=&8N=&]55$8S,BAS='(I.PT*"0D)96QS90T*"0D)"61E<W0 /2!U=&8N M,%TI+G-I>F5O9B J(&1E<W0N;&5N9W1H*3L-" D)?0T*"7T-" EE;'-E('-T M871I8R!I9BAM971H;V0 /3T 4V5R:6%L:7IE365T:&]D+E-E<75E;F-E*0T* M9W1H(#T M<V5R:6%L:7IE*',L('8I.PT*"7T-" EE;'-E('-T871I8R!I9BAM971H;V0 M<V5R:6%L:7IE*',I.PT*"7T-" EE;'-E('-T871I8R!I9BAM971H;V0 /3T M4V5R:6%L:7IE365T:&]D+E1U<&QE*0T*"7L-" D)9F]R96%C:"AM96UB97([ ` end
Mar 22 2007
parent reply David Finlayson <david.p.finlayson gmail.com> writes:
For the moment, I just want to understand the key part of your Deserialize
function.

The secret sauce is in this line (and others like it): 

s.readExact(strptr, char.sizeof * len)

where s is a file stream. It looks like with this method, it is only possible
to read a single variable or an array of the same type (as you are doing here).
Is it possible to send a pointer to a struct and read THAT in from the stream?
If so, how does it handle padding bytes? I know there is an align() attribute
for structs that might apply here.
Mar 22 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"David Finlayson" <david.p.finlayson gmail.com> wrote in message 
news:etvmlg$1auh$1 digitalmars.com...
 For the moment, I just want to understand the key part of your Deserialize 
 function.

 The secret sauce is in this line (and others like it):

 s.readExact(strptr, char.sizeof * len)

 where s is a file stream. It looks like with this method, it is only 
 possible to read a single variable or an array of the same type (as you 
 are doing here). Is it possible to send a pointer to a struct and read 
 THAT in from the stream? If so, how does it handle padding bytes? I know 
 there is an align() attribute for structs that might apply here.
Yes, as you've seen in the other post, you can do that. As for alignment issues, that's up to your structure to know the layout of your data. Say you know the format is something like: Header structure: 0x0000 4 bytes: Magic number 0x0004 2 bytes: Version 0x0005 1 byte: Flags 0x0006 1 byte: (padding) 0x0008 24 bytes: Comments 0x0020 4 bytes: offset in file to some table 0x0023 4 bytes: (reserved) -------------------------- Total: 40 bytes So you could write your structure like this. Notice we use "align(1)" on the structure to signal to the compiler that the members should be packed in as tightly as possible. This way we have complete control over how the data is laid out. align(1) struct Header { uint magic; ushort version; ubyte flags; ubyte _padding1; char[24] comments; uint tableOffset; uint _reserved; } // For good measure static assert(Header.sizeof == 40); That static assert is there to make sure that the structure's size matches the calculated size of the header, and to make sure that we don't inadvertently change the header struct and mess things up. Of course, if the header is a variable length, that assert probably wouldn't be there.
Mar 23 2007