module stub generator for testing?

M

Marco

I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

thanks

Marco
 
U

usenet

I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

Not aware of anything that does precisely this, but it'd be trivial to
write in either C or Perl. Even in less then 2 days.
 
C

Chuck F.

Marco said:
>
I am looking for an open-source or free tool that parses a C
header file (.h) for a module and creates a .c file with the
functions stubbed out ( ideally with dummy returns for
non-void). This will be used for testing purposes.

I have googled with little success. ( for a C++ class example
google "stubgen").

C++ is off-topic here. The tool required is your editor. Simply
eliminate the macros, typedefs, etc. and append a variant of:

{
return 0;
}

to each function header. Rename the resultant file from .h to .c.
Add a line "#include <original.h>" at the head.
 
M

Malcolm

Marco said:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.
Unfortunately it is not possible to write a general-case "stub generator".
C functions are allowed to modify values passed to them by pointers, and the
structures pointed to can be arbitrarily complex.
Also, it is permitted to call functions by indirection.
 
M

Michael Tiomkin

Marco said:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

I think you can cannibalize this stubgen using a couple of scripts:

1. The first script adds a line
class TemporaryUglyClass {
before the .h file, and the line
};
after the .h file. Don't change the file, just send all the stuff to
to the standard output. It's just 3 lines of awk or shell script.
2. The 2nd script reads the result of 'stubgen', changes any
"Method: TemporaryUglyClass::" string to :Function: " string and
removes all other "TemporaryUglyClass::" strings. It's also 3 lines of
awk.
3. The 3rd script runs the first script on your .h file, runs 'stubgen'
on the result of the 1st script, and runs the 2nd script on the result
of 'stubgen'. This can be one line of a shell script.
1st_scr Input_file_name | stubgen | 2nd_scr > stubs_file_name.c

Michael
 
R

Rahul Chandok

Hi,

You can try the following logic :

1) Open your header file in Read mode.
2) Create the c stub file. (Open in write mode)
3) Read the header file line by line and search for character "(" or
")" in the line.
4) If you don't find this character then ignore the line, otherwise
write the whole line to the C file, Don't write the last character ";"
to the c file.
5) Then search for the void in the beggining of the line, if it is not
there then
write the following in the c file
"{
return 0;
}"

else
just write the following in the .c file
"{
return;
}"

HTH
Rahul
 
F

Francis Glassborow

Rahul Chandok said:
5) Then search for the void in the beggining of the line, if it is not
there then
write the following in the c file
"{
return 0;

struct X {
int i;
int j;
} myfunc();

I think fails under your method.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top