Get struct definition


 
Thread Tools Search this Thread
Top Forums Programming Get struct definition
# 1  
Old 02-04-2015
Get struct definition

I have many headers with huge amount of structures in them, typical one looks like this:

Code:
$ cat a.h
struct Rec1 {
        int     f1;
        int     f2;
};
 struct Rec2 {
        char    r1;
        char    r2;
};
 struct Rec3 {
        int     f1;
        float   k1;
        float   k2;
};

To quickly look at a given definition I do this:
Code:
$ sed -n "/Rec2/,/^$/p" a.h
struct Rec2 {
        char    r1;
        char    r2;
};

I use an alias for the command and it is easy and quick, works in all systems where I need it.

Now I inherited headers with this type of definition:
Code:
$ cat b.h
typedef struct {
        int     f1;
        int     f2;
}Rec1;
typedef struct {
        char    r1;
        char    r2;
}Rec2;
typedef struct {
        int     f1;
        float   k1;
        float   k2;
}Rec3;

so my simple sed does not work anymore. Any idea how to deal with this format?

I thought of awk:
Code:
awk 'BEGIN{ n = 1; }
{
        if($0 ~ "typedef") {
                n=1;
        }
        arr[n]=$0;
        n++;
        if($0 ~ "Rec2") {
                for(i=1;i<=n;i++)
                        print arr[i];
        }
}' b.h

but I don't like it - too complex for a simple alias I can carry with me in different environments I deal with. And I am not sure awk is available everywhere. Any bash/sed based ideas?
# 2  
Old 02-04-2015
This sed might help:
Code:
sed -n ' /Rec2/ {H;g;p;d}; /typedef/ {h;d}; H' file
typedef struct {
        char    r1;
        char    r2;
}Rec2;

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-04-2015
Well, I am not doing it right somehow:
Code:
 
 $ sed -n '/Rec2/ {H;g;p;d}; /typedef/ {h;d}; H' b.h 
sed: Command garbled: /Rec2/ {H;g;p;d}; /typedef/ {h;d}; H

RudiC, can you check my syntax, please?
# 4  
Old 02-05-2015
Worked on my linux; on (Free)BSD I tried
Code:
sed -n '/Rec2/ {H;g;p;d; }; /typedef/ {h;d; }; H' file

Watch the ; } in both cases; it needs both. Some seds need a newline char in front of the }. PLay around with it...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-05-2015
If you are on Linux, another potential solution:
Code:
$ tac b.h | sed -n '/Rec2/,/typedef struct/p' | tac
typedef struct {
        char    r1;
        char    r2;
}Rec2;

# 6  
Old 02-05-2015
I love tac, but it is not available on my Unix systems. Thanks for the idea!
# 7  
Old 02-05-2015
To search all header files in the current directory in either of your two specified formats, the following will work with either ed or ex with any shell that accepts basic Bourne shell syntax (including any POSIX conforming shell such as bash and ksh):
Code:
#!/bin/ksh
for i in *.h
do	ed -s "$i" <<-EOF
		g/^[ 	]*struct[ 	][ 	]*$1[ 	]*{/ .,/^};/p
		g/^}[ 	]*$1[ 	]*;/ ?typedef[ 	][ 	]*struct[ 	]*{?,.p
	EOF
done

Note that there is a single <tab> character before the EOF on the next to the last line. It will not work correctly if this tab is changed to spaces. Note also that in the here-document there is a single <space> character and a single <tab> character between each pair of square brackets ([ and ]).

With the sample a.h and b.h headers given in post #1 in this thread, if the above script is saved in a file named tester and it is made executable:
Code:
chmod +x tester

then the command:
Code:
./tester Rec3

produces the output:
Code:
 struct Rec3 {
        int     f1;
        float   k1;
        float   k2;
};
typedef struct {
        int     f1;
        float   k1;
        float   k2;
}Rec3;

These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Variable definition

Hi all, I'm bit new to the advanced bash shell scripting. When I'm looking at some of the existing code in my organization, got confused with a few variable definings. For ex: var1={1:-30} var2="abc def ghi" var3={xyz:-$var2} In above, 1st and last lines are confusing me.... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

2. What is on Your Mind?

Definition of Bytes

A byte is the smallest unit of storage which can be accessed in a computer's memory- either in RAM or ROM.It also holds exactly 8 bits.But its old view one byte was sufficient to hold one 8 bit character.Modern days especially on .NET or international versions of Win 32, 16 bits is needed. ... (2 Replies)
Discussion started by: stoudtLion
2 Replies

3. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

4. Programming

help with struct command in C

in C i am using this code to get the c time or a time or m time struct dirent *dir; struct stat my; stat(what, &my); thetime = my.st_ctime; How can i check if i have permission to check the c time of the file? (1 Reply)
Discussion started by: omega666
1 Replies

5. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

6. UNIX for Dummies Questions & Answers

Definition of $-

Could someone please direct me to a link that gives the definitions for each of the letters from the results of the $- environment variable? It would be nice to know what shell options each of the letters represents, but I am specifically looking for the shell option for 'c' (lowercase c). Thank... (12 Replies)
Discussion started by: sszd
12 Replies

7. Shell Programming and Scripting

daemons definition

hi there, can somebody give me a definition for daemons, or example what are they !! and what the use for? i've done some research and all what i found is /etc/... or /usr/bin/... and i haven't quietly got the concept. any ideas !! Thanks. (5 Replies)
Discussion started by: new2Linux
5 Replies

8. Programming

Struct Array

in my .c file i have a struct atop of the program defined as follows: #define MAX 10 int curtab; static struct tab { int count; int use; } tab; with the initial function following it like so: int tab_create(int init_count) { int i; for(i=0; i < MAX; i++) {... (1 Reply)
Discussion started by: micmac700
1 Replies

9. Programming

save a struct

hi all , can i save a structure in c in a file? how ? help me , thx. :) (2 Replies)
Discussion started by: kall_ANSI
2 Replies

10. Programming

Struct Initialization

Hi We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams. Following is a code sample: #include <stdlib.h> #include <stdio.h> typedef struct TEST3 {... (4 Replies)
Discussion started by: amatsaka
4 Replies
Login or Register to Ask a Question