473,403 Members | 2,323 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,403 software developers and data experts.

How to detail a struct content (almost) just like adebugger does ?

Hello,

In one hand, I've got a set of more than 130 different structures,

mostly rather simple (a set of simple data members such as int, long, char
[], float ...)
sample :
[h2.h]
#ifndef _H2_H_
#define _H2_H_
#define MEMBER3_LENGTH 100
struct h2 {
int member1 ;
long member2 ;
char member3[MEMBER3_LENGTH] ;
....
} t_h2 ;
#endif

sometimes more complex :
sample :
[h3.h]
#ifndef _H3_H_
#define _H3_H_
struct h3_part {
int member1 ;
long member2 ;
....
} t_h3_part ;

struct h3 {
int memberx ;
t_h3_part innerstruct[3] ;
...
}
#endif
And I need to display the content of any structure, with a function looking
like something like this proto :
void display_content_of (void * data, const char which_structure[])
{
...
if (strcmp (which_structure, "t_h2") == 0)
{
t_h2 *p_h2 = (t_h2 *)data ;
show_all_members_of_h2 (p_h2) ;
}
...
}

I suppose the solution to my quest is something like what does a compiler do
(indeed, my solution is not supposed to work just at run time... It can use
include files, process them, to form a new set of files, compile them and
build the tool I need...)
Maybe yacc & lex would be helpfull, but I need some hints 'cause I don't
know them.
Maye be a solution based on "indexof" and macros exists, too, but I'm not
that confident...

Any help would be much appreciated.

Thank you.

Jerome de Lagausie


Nov 14 '05 #1
3 1516
"Jérôme de Lagausie" wrote:

Hello,

In one hand, I've got a set of more than 130 different structures,

mostly rather simple (a set of simple data members such as int, long, char
[], float ...)
sample :
[h2.h]
#ifndef _H2_H_
#define _H2_H_
#define MEMBER3_LENGTH 100
struct h2 {
int member1 ;
long member2 ;
char member3[MEMBER3_LENGTH] ;
....
} t_h2 ;
#endif

sometimes more complex :
sample :
[h3.h]
#ifndef _H3_H_
#define _H3_H_
struct h3_part {
int member1 ;
long member2 ;
....
} t_h3_part ;

struct h3 {
int memberx ;
t_h3_part innerstruct[3] ;
...
}
#endif

And I need to display the content of any structure, with a function looking
like something like this proto :
void display_content_of (void * data, const char which_structure[])
{
...
if (strcmp (which_structure, "t_h2") == 0)
{
t_h2 *p_h2 = (t_h2 *)data ;
show_all_members_of_h2 (p_h2) ;
}
...
}

I suppose the solution to my quest is something like what does a compiler do
(indeed, my solution is not supposed to work just at run time... It canuse
include files, process them, to form a new set of files, compile them and
build the tool I need...)
Maybe yacc & lex would be helpfull, but I need some hints 'cause I don't
know them.
Maye be a solution based on "indexof" and macros exists, too, but I'm not
that confident...


One method is to build a description for each struct type,
and pass that instead of the which_structure string. The
description gives the offsetof (not indexof) each element
you want to display, plus information about the element type
and possibly the name and display format. Here's a sketch:

typedef struct {
size_t offset;
enum { INT, LONG, ... } type;
const char *name;
const char *format;
} Element;

const Element h2_layout[] = {
{ offsetof(struct h2, member1), INT, "member1", "%d" },
{ offsetof(struct h2, member2), LONG, "member2", "%lo" },
{ offsetof(struct h3, member3), CHAR_ARRAY, "member3", "%s" },
...
{ 0, 0, NULL, NULL } /* end of description */
};

void display_content_of(const void *data, const Element *field) {
const char *ptr;
for ( ; field->name != NULL; ++field) {
printf ("%s = ", field->name);
ptr = (const char*)data + field->offset;
switch (field->type) {
case INT:
printf (field->format, *(const int*)ptr);
break;
case LONG:
printf (field->format, *(const long*)ptr);
break;
case CHAR_ARRAY:
printf (field->format, ptr);
break;
...
}
putchar ('\n');
}
}

This is more or less what a debugger does, although debuggers
usually rely on "private agreements" with the compiler to obtain
the information here encoded in arrays of Element structures.

Building the Element arrays can be tedious, and errors can
result if somebody adds a new element to an existing structure
but forgets to update the corresponding description. In a large
software project, it's probably best to write a small program that
accepts a simple language describing the desired structs, and use
this program to generate C code declaring each struct and defining
its Element array. If you arrange to run this program before
compilation, the struct definitions and their Element descriptions
will remain in agreement.

--
Er*********@sun.com
Nov 14 '05 #2
Thank you for this answer

"Eric Sosman" <Er*********@sun.com> a écrit dans le message de
news:40***************@sun.com...
Building the Element arrays can be tedious, and errors can
result if somebody adds a new element to an existing structure
but forgets to update the corresponding description. In a large
software project, it's probably best to write a small program that
accepts a simple language describing the desired structs, and use
this program to generate C code declaring each struct and defining
its Element array. If you arrange to run this program before
co mpilation, the struct definitions and their Element descriptions
will remain in agreement.
In fact, the solution that I presentlu use is something just like that, but
it's not satisfactory, because my "small program" doesn't accept arrays
(except char[], but these are more "string" type than an array), neither
inner structure.
In fact I even have to deal with a structure containing a tab of structure !

That's why I think that I need to "think different", and have another
approach, since my program needs to "digest" more than 130 structures...
This is more or less what a debugger does, although debuggers
usually rely on "private agreements" with the compiler to obtain
the information here encoded in arrays of Element structures.


I need to sign one of these agreements ;-)
Jerome
Nov 14 '05 #3
"Jérôme de Lagausie" <us****@nombril.net> wrote in message news:<40***********************@news.free.fr>...
Hello,

In one hand, I've got a set of more than 130 different structures,

mostly rather simple (a set of simple data members such as int, long, char
[], float ...)
sample :
[h2.h]
#ifndef _H2_H_
#define _H2_H_
#define MEMBER3_LENGTH 100
struct h2 {
int member1 ;
long member2 ;
char member3[MEMBER3_LENGTH] ;
....
} t_h2 ;
#endif

sometimes more complex :
sample :
[h3.h]
#ifndef _H3_H_
#define _H3_H_
struct h3_part {
int member1 ;
long member2 ;
....
} t_h3_part ;

struct h3 {
int memberx ;
t_h3_part innerstruct[3] ;
...
}
#endif
And I need to display the content of any structure, with a function looking
like something like this proto :
void display_content_of (void * data, const char which_structure[])
{
...
if (strcmp (which_structure, "t_h2") == 0)
{
t_h2 *p_h2 = (t_h2 *)data ;
show_all_members_of_h2 (p_h2) ;
}
...
}

I suppose the solution to my quest is something like what does a compiler do
(indeed, my solution is not supposed to work just at run time... It can use
include files, process them, to form a new set of files, compile them and
build the tool I need...)
Maybe yacc & lex would be helpfull, but I need some hints 'cause I don't
know them.
Maye be a solution based on "indexof" and macros exists, too, but I'm not
that confident...

Any help would be much appreciated.

Thank you.

Jerome de Lagausie

Consider using hash-tables rather than structs. It would make it a
lot easier to write code to print them out.

Also consider creating some sort of variant type. For instance a
struct where one member stores the type of the data stored in the
other member.

The alternative is to write some sort of language that you can then
process, as Eric suggests.
Nov 14 '05 #4

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

Similar topics

15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
3
by: sqlboy2000 | last post by:
Hi, I've just started playing around with master / detail pages and I'm trying to simply change the detail page displayed in the contentplaceholder control from code behind. I can't seem to find...
5
by: dis_is_eagle | last post by:
Hi.I would like to know the difference between a C struct and a C++ struct.I think in a C++ struct member variables and functions are by default private whether in a C struct they are public by...
21
by: Zytan | last post by:
Is it possible, as in C? I don't think it is. Just checking. Zytan
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
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,...

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.