a struct parser

W

Weiguang Shi

Hi,

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?

Thank you.
Wei
 
B

Bill Pursell

Weiguang said:
Hi,

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?

I think it's called a compiler. I really don't mean that
sarcastically,
but I'm not sure you're explaining your question thoroughly. For
reading, it sounds like you want something like:

#include <stdio.h>
#define NUM_FOO 50

struct foo {
int a;
char b;
unsigned int c;
};

int
main(void)
{
struct foo f[NUM_FOO];

while ( fread (f, sizeof *f, NUM_FOO, stdin) == NUM_FOO) {
int i;
for (i=0; i< NUM_FOO; i++)
printf("a = %d, b = %c, c = %u\n",
f.a, f.b, f.c);
}
return ferror(stdin);
}

You'll need to be careful if your input stream was created
by a different compiler, which may have aligned the fields of the
structure differently.
~
 
W

Weiguang Shi

Thanks for the feedback and a program to put things in perspective.

What I want is to do manually only
struct foo {
int a;
char b;
unsigned int c;
};

And I want a command to ``create'' binary foo's out of this, too.

I'm just hopelessly lazy ;-)

Wei
 
V

void * clvrmnky()

Weiguang said:
Thanks for the feedback and a program to put things in perspective.

What I want is to do manually only


And I want a command to ``create'' binary foo's out of this, too.

I'm just hopelessly lazy ;-)
Who are you talking to? Quote some context when replying.
 
I

Ian Collins

Weiguang said:
Hi,

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?
You are looking for something to serialise your struct, which isn't
something built in to C. You will have to roll your own.
 
R

Robert Gamble

Weiguang said:
Hi,

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?

Since the exact representation of types in C is implementation defined,
as opposed some certain other languages, you cannot just write out the
binary representation and expect to be able to read it back in on
another machine (or even with another compiler). You can either use a
textual representaion as your storage medium, which isn't completely
portable either, or you can convert the data to a predefined binary
format that is not dependent on the actual machine representation of
the data.

Robert Gamble
 
J

jacob navia

Weiguang Shi a écrit :
Hi,

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?

Thank you.
Wei

Hi Mr Wei.

I can give you that... for a price. It will read a structure definition
in c and output a read/write program for it.

How much are you ready to pay for it?

jacob
 
E

Eric Sosman

jacob navia wrote On 05/09/06 17:26,:
Weiguang Shi a écrit :



Hi Mr Wei.

I can give you that... for a price. It will read a structure definition
in c and output a read/write program for it.

How much are you ready to pay for it?

Warning to Mr. Wei:

If you hire Mr. Navia, be sure to specify that the
implementation language (both of the tool itself and of
the code it generates) must be "ISO Standard C." Accept
no substitutes.
 
R

Richard Tobin

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?
[/QUOTE]

Unless someone else has already done so, which is what the OP was asking
about.

-- Richard
 
W

websnarf

Richard said:
Unless someone else has already done so, which is what the OP was asking
about.

No, in C you can't have a well defined way of serializing structs in
general.

The problem, is most clearly illustrated with any char * entry. If
some header parser/serializer sees that there is a char * field in a
struct, what should it assume that it means? Is that a pointer to one
character, or is it a pointer to a NUL terminated string? Is the
underlying buffer assumed to be of some certain length (which cannot
even be hinted at in the declaration, except by a comment or something
lame like that)?

In fact that the underlying buffer for the char * may be characterized
by another field in the struct (like it is for a bstring for example.)
I.e., some other field may in fact describe the length of the usable
buffer in which a NUL terminated string is placed -- this information
need not be stored in the serialized form, however, how would a
serializing tool know to do this?

And of course none of this speaks to how you are supposed to fill in a
struct which contains a union as a sub-field. Usually *which* entry of
the union is used is indicated by a state derivable from the other
fields (if its not just one of the fields itself.)

This is why things like CORBA, COM, and IDLs in general exist.
 
H

Haude Daniel

Hi,

Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?

I've written a program that, if given a struct definition (in a simple, non-C
format) will create the corresponding C struct definition as well as functions
to parse and write textual representations of the values of the struct members
as key/value pairs. Essentially I wanted a 1:1 correspondence between a
configuration/paramater text file and a struct inside the program.

This is actually a pretty nifty code generator that I want to publish some
day. However, at the moment it is not in a publishable condition and I have
no time to work on it at the moment. Since I know that many fans of C code
generators are hanging out in this group I'll post a notice when it is
finished. Of course I don't have to mention that both the code generator and
the generated code are pure, unblemished ISO C90.

--Daniel
 
R

Richard Tobin

No, in C you can't have a well defined way of serializing structs in
general.

The problem, is most clearly illustrated with any char * entry. If
some header parser/serializer sees that there is a char * field in a
struct, what should it assume that it means? Is that a pointer to one
character, or is it a pointer to a NUL terminated string? Is the
underlying buffer assumed to be of some certain length (which cannot
even be hinted at in the declaration, except by a comment or something
lame like that)?

Obviously there can't be a completely general tool for generating
serializer and deserializers from struct declarations unaided. But
that doesn't mean that there can't be useful tools, even if you
consider some of the obvious annotation mechanisms "lame".

-- Richard
 
W

Weiguang Shi

No, in C you can't have a well defined way of serializing structs in
general.

The problem, is most clearly illustrated with any char * entry. If
some header parser/serializer sees that there is a char * field in a
struct, what should it assume that it means? Is that a pointer to one
character, or is it a pointer to a NUL terminated string? Is the
underlying buffer assumed to be of some certain length (which cannot
even be hinted at in the declaration, except by a comment or something
lame like that)?
I can accept some comments. Actually, it might be useful to hint to
the parser what option it should generate for each entry.
 
W

Weiguang Shi

Warning to Mr. Wei:

If you hire Mr. Navia, be sure to specify that the
implementation language (both of the tool itself and of
the code it generates) must be "ISO Standard C." Accept
no substitutes.
Thanks. But how did you magically intercepted his email to me? Did you
get my reply to him, too?

Wei
 
W

Weiguang Shi

I've written a program that, if given a struct definition (in a simple, non-C
format) will create the corresponding C struct definition as well as functions
to parse and write textual representations of the values of the struct members
as key/value pairs. Essentially I wanted a 1:1 correspondence between a
configuration/paramater text file and a struct inside the program.
I think it makes sense to enforce rules by using a "spec" language.
This is actually a pretty nifty code generator that I want to publish some
day. However, at the moment it is not in a publishable condition and I have
no time to work on it at the moment. Since I know that many fans of C code
generators are hanging out in this group I'll post a notice when it is
finished. Of course I don't have to mention that both the code generator and
the generated code are pure, unblemished ISO C90.

--Daniel
When will the tool go public? Will you be willing to share the code
now?

Wei
 
J

jacob navia

Weiguang Shi a écrit :
Thanks. But how did you magically intercepted his email to me? Did you
get my reply to him, too?

Wei

I did a mistake and posted "answer" to the group instead of "answer to
the sender only". I retired (cancelled ) the message one second after
it appeared, but Mr Sosman, that is very busy following my messages
intercepted it, and immediately started a polemic, since he is not very
busy (or apparently has nothing better to do).

I think there is no point in going further with the polemic.

I had started an adaptation of the lcc-win32 compiler for generating
code that would write and read a structure in a compiler independent way
from/to disk.

A problem were the embedded pointers within the structures, and whether
to follow them or not. I vaguely remember talking in this group about
this, and that a first version that followed the inner pointers in a
recursive way was criticized here. I started modifying it, but then I
was engaged by other customers in other stuff and that stayed unfinished
till now.

So, when I saw Mr Wei's message I remembered that and tried to sell him
that development.

That is all.
 
F

Flash Gordon

jacob said:
Weiguang Shi a écrit :

I did a mistake and posted "answer" to the group instead of "answer to
the sender only". I retired (cancelled ) the message one second after
it appeared, but Mr Sosman, that is very busy following my messages
intercepted it,

I'm sure he is busy. I'm also sure that you know that cancels rarely
work because most servers ignore them. So it is not very surprising that
Eric saw your message. For the record, I also saw it and my system polls
for messages hourly.
> and immediately started a polemic, since he is not very
busy (or apparently has nothing better to do).

I think there is no point in going further with the polemic.

So, when I saw Mr Wei's message I remembered that and tried to sell him
that development.

That is all.

Eric's point was completely valid. Unless Weiguang is prepared to be
tied to the compiler and system of *your* choice he should specify that
it be implemented in standard C. As a SW developer you should have no
problem with specifications being complete, in fact you should encourage
this!
 
W

Walter Roberson

jacob navia said:
I did a mistake and posted "answer" to the group instead of "answer to
the sender only". I retired (cancelled ) the message one second after
it appeared, but Mr Sosman, that is very busy following my messages
intercepted it, and immediately started a polemic, since he is not very
busy (or apparently has nothing better to do).

Many systems do not accept 'cancel' messages, or only accept
cancel messages digitally signed by a limited number of trusted
spam-fighting organizations.
 
M

Martin Ambuhl

Weiguang said:
Thanks. But how did you magically intercepted his email to me? Did you
get my reply to him, too?

No one intercepted an email, and it required no magic.
His message <[email protected]>
was posted to comp.lang.c at 09 May 2006 23:26:46 CEST.
It is openly available to all.
 
C

CBFalconer

Martin said:
No one intercepted an email, and it required no magic.
His message <[email protected]>
was posted to comp.lang.c at 09 May 2006 23:26:46 CEST.
It is openly available to all.

Most of us would strongly echo Eric Sosmans warning. M. Navia is
well known for using non-standard constructs and creating
non-portable systems. As Richard Heathfield has pointed out, he
has seriously damaged his reputation here.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top