473,231 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Human-readable storage for types (with no pointers)?

I have a big structure tree. All leaves are scalar values (no pointers).
Present are arrays, structures and unions. I want to be able to store/read
the content of the structure in/from a file, and in a readable format.
This is needed to provide manually constructed test input to a function,
which works with this tree. Number of types is huge, and I want to avoid
hand-coding read/write functions for each type.

Ideally, I'm thinking about code which takes .h file with several
typedef-s and produces C code with functions for reading/writing these
structures. For example, for file "types.h", containing definitions like

typedef {...} TypeA;

I'll get file "ReadWriteTypes.c", containing functions like

TypeA* readTypeA(FILE* fp) {...}
void writeTypeA(TypeA* a, FILE* fp) {...}

And the file content will be, for example, similar to what debuggers
show when asked to print a nested data structure. I repeat that the
leaves contain only scalar values, no pointers.

Any hints how this can be done?
I use GNU tools, so sources of gcc and gdb are at my hands. Still
I am in hope that someone has already done this before...

--
Konstantin Shemyak

Remove the obvious 2 parts from the "reply-to"

Nov 13 '05 #1
4 2201

On Tue, 9 Sep 2003, Malcolm wrote:

"Konstantin Shemyak" <sh*****@mail.remove.antispam.ru> wrote...

Ideally, I'm thinking about code which takes .h file with several
typedef-s and produces C code with functions for reading/writing these
structures. For example, for file "types.h", containing definitions like

Any hints how this can be done?

You can write this as a commandline program, called savestruct

savestruct sourcefile.c/.h typename outputfile

the problem is that C source can get very complex, with nested structures
and long chains of typedefs. For instance, you will have to evoke a C
pre-processor over the source if you want to allow the use of defined types.


s/evoke/invoke/

And what #defined types are you thinking of? FILE? I think it'd be
easier to simply ignore any types you weren't sure of. Alternatively,
you could add an option to compile those into calls to similar I/O
functions:

struct foo {
int i;
struct bar j;
};

would yield the function

int ReadStructFoo(struct foo *, FILE *);

which would contain logic for reading the 'int', plus a call to
the user-defined function ReadStructBar() [which would have to
be generated separately and linked together, most likely]. Then
the function WriteFileStuff()

typedef struct
{
int ft;
FILE *fp;
char *fname;
} FileStuff;

could call WriteFILE() to do its dirty work, and the programmer
would be free to say, "Wait, we can't really save a 'FILE *'.
Let's just make WriteFILE() a no-op, or remove the call completely."

Having reduced the structure to a list of basic types,
This is the part that stymies me. Does anyone have some nice
clean source code for parsing simple C declarations (just
single pointers, arrays, structs; nothing fancy necessary)?
I know 'cdecl' must be out there somewhere, but I believe it
uses lex/yacc, which is just ugly.
you then need some
logic to generate the fprintfs and fscanfs to load and save it. This
shouldn't be too difficult to do.


Yes, this part is fairly easy (if you make some standardizing
assumptions).

A really fancy 'savestruct' would probably serialize whole data
structures (which I think is really what the OP wants) -- flatten
trees, and so on. This is also something that would be handy to
have around.

If anyone writes, or has written, anything along these lines, I'd
*really* like to see it.

Thanks, and HTH,
-Arthur
Nov 13 '05 #2

On Tue, 9 Sep 2003, Konstantin Shemyak wrote:

I have a big structure tree. All leaves are scalar values (no pointers).
Present are arrays, structures and unions. I want to be able to store/read
the content of the structure in/from a file, and in a readable format.
This is needed to provide manually constructed test input to a function,
which works with this tree. Number of types is huge, and I want to avoid
hand-coding read/write functions for each type.

Ideally, I'm thinking about code which takes .h file with several
typedef-s and produces C code with functions for reading/writing these
structures. For example, for file "types.h", containing definitions like

typedef {...} TypeA;

I'll get file "ReadWriteTypes.c", containing functions like

TypeA* readTypeA(FILE* fp) {...}
void writeTypeA(TypeA* a, FILE* fp) {...}

I've started trying to work on this project, but I still haven't
gotten very far. It seems like the sort of thing that would be
easy to do in a half-baked way, but hard to make useful.

Here's what I would really like to see, in perceived order of
difficulty:
* Support for all unsigned integral types.
* Support for all signed integral types.
* Support for arrays of all supported types.
* Support for dynamic arrays, given a "size" member;
e.g.,
struct Packet {
int ndata;
unsigned char *data;
};
Be able to specify various ways of handling
dynamic arrays (images might have both height and
width, and data arrays of size (height*width), e.g).
* Support for dynamically allocated character strings
(that end with '\0').
* Intelligent use of typedef names for the structs,
if they're provided; including pointer typedefs.
* Support for recursive data structures: lists and
trees. Requires some knowledge of how the nodes are
organized, I think.
* Support for arbitrarily complicated types, including
function pointers, unions, and bitfields.
Of course, the idea would be that the generated code run
in any hosted environment, and not assume anything about
the representations or sizes of objects.

As I mentioned before, the hard part for me is the parsing
of the struct definition.

Any pointers or advice are welcome.

-Arthur

Nov 13 '05 #3
On Thu, 11 Sep 2003 16:26:07 -0400 (EDT), Arthur J. O'Dwyer <aj*@andrew.cmu.edu> wrote:
[skip]
I've started trying to work on this project, but I still haven't
gotten very far. It seems like the sort of thing that would be
easy to do in a half-baked way, but hard to make useful.

Here's what I would really like to see, in perceived order of
difficulty:
* Support for all unsigned integral types.
* Support for all signed integral types.
* Support for arrays of all supported types.
This plus structures of all supported types is my current need. I guess,
there is a big jump from this to the rest of what you mention.
* Support for dynamic arrays, given a "size" member;
e.g.,
struct Packet {
int ndata;
unsigned char *data;
};
But how would the syntax parser know that ndata refers to the size
of array data?
* Support for arbitrarily complicated types, including
function pointers, unions, and bitfields.


To my understanding, unions are impossible to fit into this scheme,
because again syntax parser would not know how to treat the union
member. Seems that this is a principal obstacle.

Well, if I get some minimal working code, I'll ring the bell.

--
Konstantin Shemyak

Remove the obvious 2 parts from the "reply-to"

Nov 13 '05 #4
Konstantin Shemyak <sh*****@mail.remove.antispam.ru> writes:
Well, if I get some minimal working code, I'll ring the bell.


\a!
Nov 13 '05 #5

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

Similar topics

0
by: Mattis Manzel | last post by:
Hello, About the tecnical aspect of my "Idea for a better use of human intelligence" I was told, that it could be realized in php. www.terrahome.net.tc Is that true? Mattis Manzel
1
by: mthlv12 | last post by:
If you are a minority at IBM check your credit cards carefully, IBM human resources may be stealing from you. I found $370 of charges on my card, it was done by human resources and it took me 5...
9
by: David Schofield | last post by:
It seemed too rude to post this in reply to the post which triggered it but I had to get it off my chest. David
8
by: Enrico Palazo | last post by:
I've asked on every forum that I can find, I'll try here. Why would "smartnavigation" work on some .net servers and not others. "smartnavigation is buggy", will not be accepted as an answer. ...
3
by: HUMAN SOFTWARE DEVELOPMENT NETWORK | last post by:
Hi, Human Software Websphere Development Network, is a community of teleworkers connected to our company. Main goal is to develop software solutions (websites, .Net, VB, Java, Portal, WSE,...
7
by: Jason Reichenbach | last post by:
I've GOT to be missing something painfully obvious, here... I need to programmatically get the full human name of the current user on a local system, the same system upon which the app is...
2
by: Harlin Seritt | last post by:
How can I take a time given in milliseconds (I am doing this for an uptime script) and convert it to human-friendly time i.e. "4 days, 2 hours, 25 minutes, 10 seonds."? Is there a function from the...
2
by: ThunderMusic | last post by:
Hi, I must print a PDF report from a windows service (so without human action). The pdf is located in a specific place on hard drive that is accessible to the application. Until now, I've only...
7
by: Baron Samedi | last post by:
I coded up a hit counter, then extended it to see who was reading my blog, by matching IP. The problem is that I am swamped by crawlers. How can I detect a human, or a crawler? If I can handle...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.