Non Sucking YAML parser

Dirk Koopman djk at tobit.co.uk
Wed Sep 13 18:32:56 BST 2006


On Wed, 2006-09-13 at 17:26 +0100, David Cantrell wrote:

> One of my own projects has been stalled for about a year because of
> frustration that I need to feed a struct to a socket and read a struct
> back.  And that's just with one machine and one compiler.  I *really*
> don't want to hard-code whatever rules gcc uses for building x86 structs
> into my module, because someone else might use Sun's compiler on Sparc,
> or gcc on MIPS.
> 
> Thou shalt abjure etc the vile heresy that all the world's a VAX^W
> C program.
> 

I do this sort of thing rather a lot. The exact solution depends on what
sort of speed/portability/wrist-ache trade-off you want to make. But,
even though gcc packed structure definitions seem to have settled down
it still do it like this (ish E&OE)

typedef struct {
	UC a SH, 	/* a short #define SH [2] */
	b LG,		/* a long #define LG [4] */
	c FL,		/* a float #define FL [4] */
	d DB,		/* a double #define DB [8] */
	e [some define or other+1], 	/* string */  
	f BY;		/* a byte  #define BY [1] */ 
} foo_t;

with a set of conversion routines 

US getsh(UC *)
UL getlg(UC *)
double getdb(UC *)
etc

void putsh(US, UC *)
void putdb(double, UC *)

etc

The get and put routines standardise on which way around one stores the
numbers in the arrays of bytes, I happen to use little endian, because
that is what I deal with most. Obviously if you are not using IEEE
floating point format then all bets are off. 

Bit tedious packing and unpacking arrays but I a have print/scanf
equivalent which helps and it always just works...

Dirk

 





More information about the london.pm mailing list