<ka*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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....5dbe23fc?hl=en
(e.g., He uses static or hard coded names instead of reading in the names
from a layout...)
Rod Pemberton