473,899 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a struct parser

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
May 10 '06 #1
32 8899

Weiguang Shi wrote:
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[i].a, f[i].b, f[i].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.
~

May 10 '06 #2
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
May 10 '06 #3
Weiguang Shi wrote:
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 ;-)

Who are you talking to? Quote some context when replying.
May 10 '06 #4
Weiguang Shi wrote:
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.

--
Ian Collins.
May 10 '06 #5
Weiguang Shi wrote:
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

May 10 '06 #6
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
May 10 '06 #7


jacob navia wrote On 05/09/06 17:26,:
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?


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.

--
Er*********@sun .com

May 10 '06 #8
In article <4c************ *@individual.ne t>,
Ian Collins <ia******@hotma il.com> wrote:
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?


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

-- Richard

May 10 '06 #9
Richard Tobin wrote:
Ian Collins <ia******@hotma il.com> wrote:
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?


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.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

May 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1469
by: DanielEKFA | last post by:
Hmm... Not sure how to crack this one. I have this code: typedef bool execFunctionType(const commandDataType&); struct commandDataType { SymbolSequence Sequence; string command; execFunctionType* executer; };
3
9555
by: Steven T. Hatton | last post by:
I found the following two statements in the file linked below: struct stat st; stat(fileName.c_str(), &st); http://websvn.kde.org/branches/work/kdevelop4-parser/main.cpp?rev=420247&view=markup This is from /usr/include/sys/stat.h which I'm pretty sure is what #include <sys/stat.h> is pulling in. I plain and simply do not understand what the above two lines of code do. Can someone please explain?
16
3857
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
7
2253
by: Jan Danielsson | last post by:
Hello all, I'm writing an XML parser, and I'd like to know how portable a solution is. When I pass an STag or EmptyElement tag to the application, I (obviously) need to pass the attribute list somehow. I thought about storing the list in something like this: typedef struct _ATTRLIST
3
1870
by: _TR | last post by:
I love C# and I've been porting a lot of my older C++ code, but there is one app that I haven't quite figured out yet. I suppose I could re-engineer the app from the ground up, but I've already accumulated a lot of data files, so the format is set for now. Basically, the data format consists of inlined Structs of different types and lengths. I can decode the first byte to tell what type of struct it is, then point a struct pointer to...
7
1578
by: Hendrik Schober | last post by:
Hi, I am trying to test a command line parser. So I came up with this (simplified): struct test { int argc; const char** argv; template< int N >
1
2508
TMS
by: TMS | last post by:
I have an assignment to write a recursive descent parser. I have a struct that I've defined that has a table after it. I have NEVER done it this way before, and I'm hoping someone can help me access the table. I put the struct inside of main because that is where I've seen it done (surfing the web). If that is wrong, please say so. Here is my code: typedef enum{ repeat, begin, stmt, end, until, cond,
4
2626
by: pallav | last post by:
hello, i'm sorry if this is a bit off topic but perhaps some of you here have experience with c++ with lex/yacc and can help me. i wrote a small lexer/parser using lex/yacc. i'm using c++ as the language to interface with lex/yacc generated code. i have the following user defined struct for YYSTYPE to populate the data that is parsed.
9
3654
by: Jonathan Fine | last post by:
Hello I want to serialise a dictionary, whose keys and values are ordinary strings (i.e. a sequence of bytes). I can of course use pickle, but it has two big faults for me. 1. It should not be used with untrusted data. 2. I want non-Python programs to be able to read and write these dictionaries.
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10866
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5891
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.