print struct automaticly

K

katysei

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.
 
R

Richard Bos

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring

No. There is no such thing in ISO C, and if you want to write one
yourself you'll have a nice task on your hands, too. For starters, there
is no way in ISO C to let your program get at the names (rather than the
values) of a random struct member - except for remembering it from the
start, which rather puts a dent in the generality of the tool.

Richard
 
C

Chris Dollin

Richard said:
No. There is no such thing in ISO C, and if you want to write one
yourself you'll have a nice task on your hands, too. For starters, there
is no way in ISO C to let your program get at the names (rather than the
values) of a random struct member - except for remembering it from the
start, which rather puts a dent in the generality of the tool.

A sufficiently cunning tool -- or sufficiently transparent markup in
the C code -- can generate the printing code from the struct declaration,
yes? General enough?

Of course there are all sorts of issues about /how/ the items in the
struct(s) should be printed. Generally speaking, I'd write the
struct-printer function as part of the cluster of functions that
belong with the struct [1]; since large structs from smaller structs
grow, I wouldn't be faced with the "oh /frelling/ tedium, I have
to write a print function for a 117-element struct /now/!". (Also
I wouldn't write a flat 117-element struct anyway.)

[1] EG print one, initialise one, malloc one, free (a pointer to) one,
test two of them for equality/similarity, construct one from a
simple string description, send one to bed with a cup of cocoa
and a good book.
 
M

mark_bluemel

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

You may like to look at some platform-specifics (like the STABS and
DWARF debugging formats) to see if they are appropriate, but that's not
part of the standard C language.

For general purposes, Chris Dollin's suggestion seems most appropriate.
 
C

Cong Wang

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;

}A a;

it will print it's contents :
i=0
s="somestring
etc;
Thanx.
Kat.

Try C++ instead.
 
R

Richard Bos

Chris Dollin said:
A sufficiently cunning tool -- or sufficiently transparent markup in
the C code -- can generate the printing code from the struct declaration,
yes? General enough?

Yes, but it can't be part of the program itself. You can certainly write
a tool that reads C code, and outputs more C code that prints the
contents of a struct defined in the first bit of code. You can also link
the second bit of code into the first bit, and call it from there. But
that's a two-stage process at compile-time.
What you can't (portably, or even sanely) do is link the tool into the
first bit of code, and make it a one-stage process, generating the
printing code on the fly, at run-time.
Of course there are all sorts of issues about /how/ the items in the
struct(s) should be printed. Generally speaking, I'd write the
struct-printer function as part of the cluster of functions that
belong with the struct [1];

There is that; and in a normal program, you wouldn't usually want a
simple dump like this in the first place. In fact, you might well want
more than one, for different parts of the program.

Richard
 
A

Ancient_Hacker

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

not built into C. Y

You could write a 33-line Perl program that might do a passable job for
simple structs.



And IIRC old IBM EXTENDED FORTRAN IV (1966) had a NAMELIST feature
that pretty much does exactly that. And not only for output, you could
use it for INPUT.

But we digress.
 
A

Andrew Poelstra

Just to satisfy my curiosity:
How would you do that in C++?

I believe that you would make a class for your object, and make it a
friend of the iostream object (so that it would work with cout/cin).

I can't for the life of me remember how to do that, though.
 
J

jacob navia

Andrew said:
I believe that you would make a class for your object, and make it a
friend of the iostream object (so that it would work with cout/cin).

I can't for the life of me remember how to do that, though.

Anyway in C++ structs are classes...

The iostream classes will automatically figure out how to print each
member of your structure?

That would be real news to me.
 
C

Christopher Benson-Manica

Andrew Poelstra said:
I believe that you would make a class for your object, and make it a
friend of the iostream object (so that it would work with cout/cin).

I don't believe that's what OP wants; I think OP is looking for
something much like the reflective/introspective capabilities of Java,
which of course are no more available in C++ than they are in C.
I can't for the life of me remember how to do that, though.

It's in the C++ FAQ, if you're so inclined.
 
K

Keith Thompson

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

struct A

{
int i;
char * s;
}

A a;

it will print it's contents :
i=0
s="somestring
etc;

The easiest approach is probably going to be to write an output
routine for each of your structs. The drawback, of course, is that it
becomes harder in proportion to how many structs you have, and how
many members each one has.

There's nothing built into the language that will do this for you.

If you're *really* ambitious, you can write a program that parses your
C source, finds structure declarations, and generates C source for
routines to print them. This is a lot of work, but once it's done
it's no more difficult to add more structs.

I've done something similar in the past (I don't have the code).
Parsing C is difficult; typedefs make it a very complicated job.

Another approach would be to use a data definition language of some
sort, and generate both your struct definitions *and* the printing
routines from that. This saves you the effort of building a full C
parser, but it means that some of your declarations will be
automatically generated, which can have drawbacks.

There may be existing tools to do some of this.
 
R

Rod Pemberton

I have to print several large structs to print the screen.
I was wondering if thers a tool to do so automaticly

Sorry, no. And, it definately isn't a task for someone new to C. This was
considered to be an expert task in PL/1 and it is much harder to completely
implement in C. If you're still interested, I've provided more information.

It is possible to partially automate the process. I'd recommend manually
feeding the just the code for the C structs into additional programs, the
output of which could be used by your main program to display the data.

The key to doing this easily, instead of completely, is that your C
structures would need to be free of typedef's: no typedef'd structs or
typedef'd data types. If your structs have data which use typedef's, the
task will be very difficult since you need to "undo" or "unroll" the
typedef's which could be anywhere within a "mountain" of code. It would
require almost a full C parser and preprocessor instead of a simple grammar
or text processing.

I previously posted this here (I used flex but might be able to use AWK for
typedef free structs...):

"Your question reminds me of a record editor for a database. I programmed
one of those a number of years ago in PL/1. I ended up using a number of
tools to do the job. The final output had a single line for each piece of
data in the structure which contained the name of the data, type of data,
offset from the start of the structure, etc. I used a flex grammar to parse
the PL/1 data structure into a clean format. I then used PL/1 program which
read the clean format and structure definition to output the size and
offsets of the data. This created a generic "map" for the record. This
generic map could then be loaded into the record editing program.

You should be able to do something similar in C with the help of a simple
grammar. I would start by looking sizeof() and offsetof()."

Come to think of it, I think I also output the fully qualified name. For
PL/1, I think I was using the size for the length of strings which are
fundamental type in that language. Anyway, you said you needed to transform
this:
struct A

{
int i;
char * s;
}

A a;

For C you'd probably want something with a field layout like this:

fully qualified name, some indicator of variable type, offset of
element, sizeof element (maybe unneeded)

And, the transformed or output data for each field filled in for your struct
would look something like this:

a.i, int, 0, 4
a.s, char *, 4, 4

You'd then need a routine which reads in the layout and another which a)
prints the name, b) uses the type information to select an appropriate
intermediate variable, c) casts the variable to the current structure
address + offset, and d) then displays the variable.

C also has variable arguments functionality with varargs
(va_list,va_start,va_arg,va_end). If you can figure out how to use these
for this situation, they might simplify your implementation. If you want to
see how varargs are used, some *printf() routines use them.


Although it is slightly different from what you want, this post by David
Tribble might give you some ideas as how to progress:
http://groups.google.com/group/comp.std.c/msg/2ffa5a5e5dbe23fc?hl=en

(e.g., He uses static or hard coded names instead of reading in the names
from a layout...)


Rod Pemberton
 
C

Chris Dollin

Richard said:
Yes, but it can't be part of the program itself.
Concur.

You can certainly write
a tool that reads C code, and outputs more C code that prints the
contents of a struct defined in the first bit of code. You can also link
the second bit of code into the first bit, and call it from there. But
that's a two-stage process at compile-time.
Yes.

What you can't (portably, or even sanely) do is link the tool into the
first bit of code, and make it a one-stage process, generating the
printing code on the fly, at run-time.

Indeed.

(Oh, agreement, it's so /boring/!)
Of course there are all sorts of issues about /how/ the items in the
struct(s) should be printed. Generally speaking, I'd write the
struct-printer function as part of the cluster of functions that
belong with the struct [1];

There is that; and in a normal program, you wouldn't usually want a
simple dump like this in the first place. In fact, you might well want
more than one, for different parts of the program.

I've found that it's useful to have at least two lying around: the
one that prints the Whole Thing, warts and all, and the one that
prints a Pretty View. (Of course that might end up being a difference
in a prettiness parameter.)

Oh /bother/, I agreed again. Must take less sleep.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top