473,854 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

print struct automaticly

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 :

Oct 9 '06
13 31047
Andrew Poelstra <ap*******@fals e.sitewrote:
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.

C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Oct 9 '06 #11
ka*****@gmail.c om writes:
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 :
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.

Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 9 '06 #12

<ka*****@gmail. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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
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_sta rt,va_arg,va_en d). 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:

(e.g., He uses static or hard coded names instead of reading in the names
from a layout...)
Rod Pemberton
Oct 10 '06 #13
Richard Bos wrote:
Chris Dollin <ch**********@h p.comwrote:
>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.

(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.

Chris "resembling an iron" Dollin
Nit-picking is best done among friends.

Oct 10 '06 #14

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

Similar topics

by: venkatbo | last post by:
Hi folks, Of TurboGers & Django WAF candidates, which one would be easier to use in an environment where the data/content doesn't come an RDBMS, but from other server-side apps... If these are not good candidates, could you suggest appropriate ones... TIA, /venkat
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print struct.calcsize("I") 4
by: nephish | last post by:
hello there, all. i have a difficult app that connects to a server to get information for our database here. this server is our access point to some equipment in the field that we monitor. the messages come in over a socket connection. And according to their (very limited) documentation, are set up in a particular order. like this
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack str size does not match format I thought ip addresses were unsigned 32 bit integers.
by: call_me_anything | last post by:
I have different kind of data structures in different applications. The data structures are big and complex and I would like to print the members of each struct. Can we write a generic piece of code which will automatically find out the type of struct elements and print them (indented manner ?) if they are primitive types like int, float or char* ? else it will recurse into the composite data type.
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return timeout.struct_to_timeout(self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout.struct_size())) The helper timeout module is here:
by: bradawk | last post by:
It has been a long while since I did any C programming. I am trying to read the utmp.h header on RHEL vs 5 but I am getting a little lost with the branching. I wanted to write a simple program that would create a struct of type utmp and then would print out each element name and type. Such as, "the first element is called ut_type and is an integer." I have: #include <stdio.h> # include <utmp.h> int main(int argc, char *argv) { struct...
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char *AccName; int Age; double AccBalance; struct account *Next;
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char AccName; int Age; double AccBalance; struct account *Next;
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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,...
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...
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,...
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...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.